blob: 47b130888f97d5f09a6c3ac38cd2e6ebfe669102 [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)) {
950 StringRef Param = Attr.getArgAsIdent(0)->Ident->getName();
951
952 if (Param == "unknown")
953 DefaultState = ConsumableAttr::Unknown;
954 else if (Param == "consumed")
955 DefaultState = ConsumableAttr::Consumed;
956 else if (Param == "unconsumed")
957 DefaultState = ConsumableAttr::Unconsumed;
958 else {
959 S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param;
960 return;
961 }
962
963 } else {
964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
965 << Attr.getName() << AANT_ArgumentIdentifier;
966 return;
967 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000968
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000969 if (!isa<CXXRecordDecl>(D)) {
970 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
971 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000972 return;
973 }
974
975 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +0000976 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000977 Attr.getAttributeSpellingListIndex()));
978}
979
980static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
981 const AttributeList &Attr) {
982 ASTContext &CurrContext = S.getASTContext();
983 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
984
985 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
986 if (!RD->hasAttr<ConsumableAttr>()) {
987 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
988 RD->getNameAsString();
989
990 return false;
991 }
992 }
993
994 return true;
995}
996
997static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000998 if (!isa<CXXMethodDecl>(D)) {
999 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1000 Attr.getName() << ExpectedMethod;
1001 return;
1002 }
1003
1004 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1005 return;
1006
1007 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001008 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001009 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001010}
1011
1012static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1013 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001014 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1016 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001017 return;
1018 }
1019
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001020 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1021 return;
1022
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001023 D->addAttr(::new (S.Context)
1024 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001025 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001026}
1027
1028static void handleTestsConsumedAttr(Sema &S, Decl *D,
1029 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001030 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001031 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1032 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001033 return;
1034 }
1035
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001036 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1037 return;
1038
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001039 D->addAttr(::new (S.Context)
1040 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001041 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001042}
1043
1044static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1045 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001046 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001047 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1048 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001049 return;
1050 }
1051
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001052 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1053 return;
1054
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001055 D->addAttr(::new (S.Context)
1056 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001057 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001058}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001059
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001060static void handleReturnTypestateAttr(Sema &S, Decl *D,
1061 const AttributeList &Attr) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001062 ReturnTypestateAttr::ConsumedState ReturnState;
1063
1064 if (Attr.isArgIdent(0)) {
1065 StringRef Param = Attr.getArgAsIdent(0)->Ident->getName();
1066
1067 if (Param == "unknown") {
1068 ReturnState = ReturnTypestateAttr::Unknown;
1069 } else if (Param == "consumed") {
1070 ReturnState = ReturnTypestateAttr::Consumed;
1071 } else if (Param == "unconsumed") {
1072 ReturnState = ReturnTypestateAttr::Unconsumed;
1073 } else {
1074 S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param;
1075 return;
1076 }
1077
1078 } else {
1079 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1080 Attr.getName() << AANT_ArgumentIdentifier;
1081 return;
1082 }
1083
1084 if (!isa<FunctionDecl>(D)) {
1085 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1086 Attr.getName() << ExpectedFunction;
1087 return;
1088 }
1089
1090 // FIXME: This check is currently being done in the analysis. It can be
1091 // enabled here only after the parser propagates attributes at
1092 // template specialization definition, not declaration.
1093 //QualType ReturnType;
1094 //
1095 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1096 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1097 //
1098 //} else {
1099 //
1100 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1101 //}
1102 //
1103 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1104 //
1105 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1106 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1107 // ReturnType.getAsString();
1108 // return;
1109 //}
1110
1111 D->addAttr(::new (S.Context)
1112 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1113 Attr.getAttributeSpellingListIndex()));
1114}
1115
Chandler Carruth1b03c872011-07-02 00:01:44 +00001116static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1117 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001118 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1119 if (TD == 0) {
1120 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1121 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001122 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001123 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001124 }
Mike Stumpbf916502009-07-24 19:02:52 +00001125
Richard Smitha4fa9002013-01-13 02:11:23 +00001126 // Remember this typedef decl, we will need it later for diagnostics.
1127 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001128}
1129
Chandler Carruth1b03c872011-07-02 00:01:44 +00001130static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001131 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001132 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001133 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001134 // If the alignment is less than or equal to 8 bits, the packed attribute
1135 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001136 if (!FD->getType()->isDependentType() &&
1137 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001138 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001139 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001140 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001141 else
Michael Han51d8c522013-01-24 16:46:58 +00001142 FD->addAttr(::new (S.Context)
1143 PackedAttr(Attr.getRange(), S.Context,
1144 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001145 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001146 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001147}
1148
Chandler Carruth1b03c872011-07-02 00:01:44 +00001149static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001150 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001151 RD->addAttr(::new (S.Context)
1152 MsStructAttr(Attr.getRange(), S.Context,
1153 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001154 else
1155 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1156}
1157
Chandler Carruth1b03c872011-07-02 00:01:44 +00001158static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001159 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001160 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001161 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001162 D->addAttr(::new (S.Context)
1163 IBActionAttr(Attr.getRange(), S.Context,
1164 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001165 return;
1166 }
1167
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001168 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001169}
1170
Ted Kremenek2f041d02011-09-29 07:02:25 +00001171static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1172 // The IBOutlet/IBOutletCollection attributes only apply to instance
1173 // variables or properties of Objective-C classes. The outlet must also
1174 // have an object reference type.
1175 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1176 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001177 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001178 << Attr.getName() << VD->getType() << 0;
1179 return false;
1180 }
1181 }
1182 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1183 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001184 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001185 << Attr.getName() << PD->getType() << 1;
1186 return false;
1187 }
1188 }
1189 else {
1190 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1191 return false;
1192 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001193
Ted Kremenek2f041d02011-09-29 07:02:25 +00001194 return true;
1195}
1196
Chandler Carruth1b03c872011-07-02 00:01:44 +00001197static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001198 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001199 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001200
Michael Han51d8c522013-01-24 16:46:58 +00001201 D->addAttr(::new (S.Context)
1202 IBOutletAttr(Attr.getRange(), S.Context,
1203 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001204}
1205
Chandler Carruth1b03c872011-07-02 00:01:44 +00001206static void handleIBOutletCollection(Sema &S, Decl *D,
1207 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001208
1209 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001210 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001211 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1212 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001213 return;
1214 }
1215
Ted Kremenek2f041d02011-09-29 07:02:25 +00001216 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001217 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001218
Aaron Ballman624421f2013-08-31 01:11:41 +00001219 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1220 IdentifierInfo *II;
1221 SourceLocation ILS;
1222 if (IL) {
1223 II = IL->Ident;
1224 ILS = IL->Loc;
1225 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001226 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001227 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001228
John McCallb3d87482010-08-24 05:47:05 +00001229 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001230 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001231 if (!TypeRep) {
1232 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1233 return;
1234 }
John McCallb3d87482010-08-24 05:47:05 +00001235 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001236 // Diagnose use of non-object type in iboutletcollection attribute.
1237 // FIXME. Gnu attribute extension ignores use of builtin types in
1238 // attributes. So, __attribute__((iboutletcollection(char))) will be
1239 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001240 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001241 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1242 return;
1243 }
Michael Han51d8c522013-01-24 16:46:58 +00001244 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001245 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001246 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001247}
1248
Chandler Carruthd309c812011-07-01 23:49:16 +00001249static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001250 if (const RecordType *UT = T->getAsUnionType())
1251 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1252 RecordDecl *UD = UT->getDecl();
1253 for (RecordDecl::field_iterator it = UD->field_begin(),
1254 itend = UD->field_end(); it != itend; ++it) {
1255 QualType QT = it->getType();
1256 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1257 T = QT;
1258 return;
1259 }
1260 }
1261 }
1262}
1263
Nuno Lopes587de5b2012-05-24 00:22:00 +00001264static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001265 if (!isFunctionOrMethod(D)) {
1266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001267 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001268 return;
1269 }
1270
Nuno Lopes587de5b2012-05-24 00:22:00 +00001271 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1272 return;
1273
Nuno Lopes587de5b2012-05-24 00:22:00 +00001274 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001275 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001276 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001277 uint64_t Idx;
1278 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1279 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001280 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001281
1282 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001283 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001284 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001285 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1286 << Attr.getName() << AANT_ArgumentIntegerConstant
1287 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001288 return;
1289 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001290 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001291 }
1292
1293 // check if the function returns a pointer
1294 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1295 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001296 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001297 }
1298
Michael Han51d8c522013-01-24 16:46:58 +00001299 D->addAttr(::new (S.Context)
1300 AllocSizeAttr(Attr.getRange(), S.Context,
1301 SizeArgs.data(), SizeArgs.size(),
1302 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001303}
1304
Chandler Carruth1b03c872011-07-02 00:01:44 +00001305static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001306 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1307 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001308 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001309 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001310 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001311 return;
1312 }
Mike Stumpbf916502009-07-24 19:02:52 +00001313
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001314 SmallVector<unsigned, 8> NonNullArgs;
1315 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001316 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001317 uint64_t Idx;
1318 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1319 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001320 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001321
1322 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001323 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001324 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001325
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001326 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001327 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001328 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001329 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001330 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001331 }
Mike Stumpbf916502009-07-24 19:02:52 +00001332
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001333 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001334 }
Mike Stumpbf916502009-07-24 19:02:52 +00001335
1336 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1337 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001338 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001339 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1340 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001341 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001342 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001343 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001344 }
Mike Stumpbf916502009-07-24 19:02:52 +00001345
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001346 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001347 if (NonNullArgs.empty()) {
1348 // Warn the trivial case only if attribute is not coming from a
1349 // macro instantiation.
1350 if (Attr.getLoc().isFileID())
1351 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001352 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001353 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001354 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001355
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001356 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001357 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001359 D->addAttr(::new (S.Context)
1360 NonNullAttr(Attr.getRange(), S.Context, start, size,
1361 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001362}
1363
Chandler Carruth1b03c872011-07-02 00:01:44 +00001364static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001365 // This attribute must be applied to a function declaration.
1366 // The first argument to the attribute must be a string,
1367 // the name of the resource, for example "malloc".
1368 // The following arguments must be argument indexes, the arguments must be
1369 // of integer type for Returns, otherwise of pointer type.
1370 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001371 // after being held. free() should be __attribute((ownership_takes)), whereas
1372 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001373
Aaron Ballman624421f2013-08-31 01:11:41 +00001374 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001375 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001376 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001377 return;
1378 }
1379 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001380 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001381 switch (AL.getKind()) {
1382 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001383 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001384 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001385 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1386 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001387 return;
1388 }
Jordy Rose2a479922010-08-12 08:54:03 +00001389 break;
1390 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001391 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001392 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001393 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1394 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001395 return;
1396 }
Jordy Rose2a479922010-08-12 08:54:03 +00001397 break;
1398 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001399 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001400 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001401 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001402 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 return;
1404 }
Jordy Rose2a479922010-08-12 08:54:03 +00001405 break;
1406 default:
1407 // This should never happen given how we are called.
1408 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001409 }
1410
Chandler Carruth87c44602011-07-01 23:49:12 +00001411 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001412 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1413 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001414 return;
1415 }
1416
Aaron Ballman624421f2013-08-31 01:11:41 +00001417 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001418
1419 // Normalize the argument, __foo__ becomes foo.
1420 if (Module.startswith("__") && Module.endswith("__"))
1421 Module = Module.substr(2, Module.size() - 4);
1422
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001423
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001424 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001425 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1426 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001427 uint64_t Idx;
1428 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001429 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001430 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001431
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001432 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001433 case OwnershipAttr::Takes:
1434 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001435 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001436 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001437 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1438 // FIXME: Should also highlight argument in decl.
1439 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001440 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001441 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001442 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001443 continue;
1444 }
1445 break;
1446 }
Sean Huntcf807c42010-08-18 23:23:40 +00001447 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001448 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001449 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001450 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001451 llvm::APSInt ArgNum(32);
1452 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1453 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1454 S.Diag(AL.getLoc(), diag::err_ownership_type)
1455 << "ownership_returns" << "integer"
1456 << IdxExpr->getSourceRange();
1457 return;
1458 }
1459 }
1460 break;
1461 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001462 } // switch
1463
1464 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001465 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001466 i = D->specific_attr_begin<OwnershipAttr>(),
1467 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001468 i != e; ++i) {
1469 if ((*i)->getOwnKind() != K) {
1470 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1471 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001472 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001473 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1474 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001475 }
1476 }
1477 }
1478 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001479 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001480 }
1481
1482 unsigned* start = OwnershipArgs.data();
1483 unsigned size = OwnershipArgs.size();
1484 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001485
1486 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001487 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1488 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001489 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001490 }
Sean Huntcf807c42010-08-18 23:23:40 +00001491
Michael Han51d8c522013-01-24 16:46:58 +00001492 D->addAttr(::new (S.Context)
1493 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1494 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001495}
1496
Chandler Carruth1b03c872011-07-02 00:01:44 +00001497static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001498 // Check the attribute arguments.
1499 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001500 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1501 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001502 return;
1503 }
1504
Chandler Carruth87c44602011-07-01 23:49:12 +00001505 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001506 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001507 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001508 return;
1509 }
1510
Chandler Carruth87c44602011-07-01 23:49:12 +00001511 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001512
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001513 // gcc rejects
1514 // class c {
1515 // static int a __attribute__((weakref ("v2")));
1516 // static int b() __attribute__((weakref ("f3")));
1517 // };
1518 // and ignores the attributes of
1519 // void f(void) {
1520 // static int a __attribute__((weakref ("v2")));
1521 // }
1522 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001523 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001524 if (!Ctx->isFileContext()) {
1525 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001526 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001527 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001528 }
1529
1530 // The GCC manual says
1531 //
1532 // At present, a declaration to which `weakref' is attached can only
1533 // be `static'.
1534 //
1535 // It also says
1536 //
1537 // Without a TARGET,
1538 // given as an argument to `weakref' or to `alias', `weakref' is
1539 // equivalent to `weak'.
1540 //
1541 // gcc 4.4.1 will accept
1542 // int a7 __attribute__((weakref));
1543 // as
1544 // int a7 __attribute__((weak));
1545 // This looks like a bug in gcc. We reject that for now. We should revisit
1546 // it if this behaviour is actually used.
1547
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001548 // GCC rejects
1549 // static ((alias ("y"), weakref)).
1550 // Should we? How to check that weakref is before or after alias?
1551
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001552 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1553 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1554 // StringRef parameter it was given anyway.
Aaron Ballman624421f2013-08-31 01:11:41 +00001555 if (Attr.isArgExpr(0)) {
1556 Expr *Arg = Attr.getArgAsExpr(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001557 Arg = Arg->IgnoreParenCasts();
1558 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1559
Douglas Gregor5cee1192011-07-27 05:40:30 +00001560 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001561 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001562 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001563 return;
1564 }
1565 // GCC will accept anything as the argument of weakref. Should we
1566 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001567 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001568 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001569 }
1570
Michael Han51d8c522013-01-24 16:46:58 +00001571 D->addAttr(::new (S.Context)
1572 WeakRefAttr(Attr.getRange(), S.Context,
1573 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001574}
1575
Chandler Carruth1b03c872011-07-02 00:01:44 +00001576static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001577 StringLiteral *Str = 0;
1578 if (Attr.isArgExpr(0))
1579 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00001580
Douglas Gregor5cee1192011-07-27 05:40:30 +00001581 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001582 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1583 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001584 return;
1585 }
Mike Stumpbf916502009-07-24 19:02:52 +00001586
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001587 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001588 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1589 return;
1590 }
1591
Chris Lattner6b6b5372008-06-26 18:38:35 +00001592 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001593
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001594 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001595 Str->getString(),
1596 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001597}
1598
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001599static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001600 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1602 << Attr.getName() << ExpectedFunctionOrMethod;
1603 return;
1604 }
1605
Michael Han51d8c522013-01-24 16:46:58 +00001606 D->addAttr(::new (S.Context)
1607 MinSizeAttr(Attr.getRange(), S.Context,
1608 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001609}
1610
Benjamin Krameree409a92012-05-12 21:10:52 +00001611static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001612 if (!isa<FunctionDecl>(D)) {
1613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1614 << Attr.getName() << ExpectedFunction;
1615 return;
1616 }
1617
1618 if (D->hasAttr<HotAttr>()) {
1619 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1620 << Attr.getName() << "hot";
1621 return;
1622 }
1623
Michael Han51d8c522013-01-24 16:46:58 +00001624 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1625 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001626}
1627
1628static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001629 if (!isa<FunctionDecl>(D)) {
1630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1631 << Attr.getName() << ExpectedFunction;
1632 return;
1633 }
1634
1635 if (D->hasAttr<ColdAttr>()) {
1636 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1637 << Attr.getName() << "cold";
1638 return;
1639 }
1640
Michael Han51d8c522013-01-24 16:46:58 +00001641 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1642 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001643}
1644
Chandler Carruth1b03c872011-07-02 00:01:44 +00001645static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001646 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001648 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001649 return;
1650 }
1651
Michael Han51d8c522013-01-24 16:46:58 +00001652 D->addAttr(::new (S.Context)
1653 NakedAttr(Attr.getRange(), S.Context,
1654 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001655}
1656
Chandler Carruth1b03c872011-07-02 00:01:44 +00001657static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1658 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001659 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001661 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001662 return;
1663 }
Mike Stumpbf916502009-07-24 19:02:52 +00001664
Michael Han51d8c522013-01-24 16:46:58 +00001665 D->addAttr(::new (S.Context)
1666 AlwaysInlineAttr(Attr.getRange(), S.Context,
1667 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001668}
1669
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001670static void handleTLSModelAttr(Sema &S, Decl *D,
1671 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001672 Expr *Arg = Attr.getArgAsExpr(0);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001673 Arg = Arg->IgnoreParenCasts();
1674 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1675
1676 // Check that it is a string.
1677 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001678 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1679 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001680 return;
1681 }
1682
Richard Smith38afbc72013-04-13 02:43:54 +00001683 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1685 << Attr.getName() << ExpectedTLSVar;
1686 return;
1687 }
1688
1689 // Check that the value.
1690 StringRef Model = Str->getString();
1691 if (Model != "global-dynamic" && Model != "local-dynamic"
1692 && Model != "initial-exec" && Model != "local-exec") {
1693 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1694 return;
1695 }
1696
Michael Han51d8c522013-01-24 16:46:58 +00001697 D->addAttr(::new (S.Context)
1698 TLSModelAttr(Attr.getRange(), S.Context, Model,
1699 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001700}
1701
Chandler Carruth1b03c872011-07-02 00:01:44 +00001702static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001703 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001704 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001705 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001706 D->addAttr(::new (S.Context)
1707 MallocAttr(Attr.getRange(), S.Context,
1708 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001709 return;
1710 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001711 }
1712
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001713 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001714}
1715
Chandler Carruth1b03c872011-07-02 00:01:44 +00001716static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001717 D->addAttr(::new (S.Context)
1718 MayAliasAttr(Attr.getRange(), S.Context,
1719 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001720}
1721
Chandler Carruth1b03c872011-07-02 00:01:44 +00001722static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001723 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001724 D->addAttr(::new (S.Context)
1725 NoCommonAttr(Attr.getRange(), S.Context,
1726 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001727 else
1728 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001729 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001730}
1731
Chandler Carruth1b03c872011-07-02 00:01:44 +00001732static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001733 if (S.LangOpts.CPlusPlus) {
1734 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1735 return;
1736 }
1737
Chandler Carruth87c44602011-07-01 23:49:12 +00001738 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001739 D->addAttr(::new (S.Context)
1740 CommonAttr(Attr.getRange(), S.Context,
1741 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001742 else
1743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001744 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001745}
1746
Chandler Carruth1b03c872011-07-02 00:01:44 +00001747static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001748 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001749
1750 if (S.CheckNoReturnAttr(attr)) return;
1751
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001753 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001754 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001755 return;
1756 }
1757
Michael Han51d8c522013-01-24 16:46:58 +00001758 D->addAttr(::new (S.Context)
1759 NoReturnAttr(attr.getRange(), S.Context,
1760 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001761}
1762
1763bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001764 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001765 attr.setInvalid();
1766 return true;
1767 }
1768
1769 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001770}
1771
Chandler Carruth1b03c872011-07-02 00:01:44 +00001772static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1773 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001774
1775 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1776 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001777 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1778 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001779 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1780 && !VD->getType()->isFunctionPointerType())) {
1781 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001782 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001783 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001784 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001785 return;
1786 }
1787 }
1788
Michael Han51d8c522013-01-24 16:46:58 +00001789 D->addAttr(::new (S.Context)
1790 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1791 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001792}
1793
Richard Smithcd8ab512013-01-17 01:30:42 +00001794static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1795 const AttributeList &Attr) {
1796 // C++11 [dcl.attr.noreturn]p1:
1797 // The attribute may be applied to the declarator-id in a function
1798 // declaration.
1799 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1800 if (!FD) {
1801 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1802 << Attr.getName() << ExpectedFunctionOrMethod;
1803 return;
1804 }
1805
Michael Han51d8c522013-01-24 16:46:58 +00001806 D->addAttr(::new (S.Context)
1807 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1808 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001809}
1810
John Thompson35cc9622010-08-09 21:53:52 +00001811// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001812static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001813/*
1814 Returning a Vector Class in Registers
1815
Eric Christopherf48f3672010-12-01 22:13:54 +00001816 According to the PPU ABI specifications, a class with a single member of
1817 vector type is returned in memory when used as the return value of a function.
1818 This results in inefficient code when implementing vector classes. To return
1819 the value in a single vector register, add the vecreturn attribute to the
1820 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001821
1822 Example:
1823
1824 struct Vector
1825 {
1826 __vector float xyzw;
1827 } __attribute__((vecreturn));
1828
1829 Vector Add(Vector lhs, Vector rhs)
1830 {
1831 Vector result;
1832 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1833 return result; // This will be returned in a register
1834 }
1835*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001836 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001837 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001838 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001839 return;
1840 }
1841
Chandler Carruth87c44602011-07-01 23:49:12 +00001842 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001843 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1844 return;
1845 }
1846
Chandler Carruth87c44602011-07-01 23:49:12 +00001847 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001848 int count = 0;
1849
1850 if (!isa<CXXRecordDecl>(record)) {
1851 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1852 return;
1853 }
1854
1855 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1856 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1857 return;
1858 }
1859
Eric Christopherf48f3672010-12-01 22:13:54 +00001860 for (RecordDecl::field_iterator iter = record->field_begin();
1861 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001862 if ((count == 1) || !iter->getType()->isVectorType()) {
1863 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1864 return;
1865 }
1866 count++;
1867 }
1868
Michael Han51d8c522013-01-24 16:46:58 +00001869 D->addAttr(::new (S.Context)
1870 VecReturnAttr(Attr.getRange(), S.Context,
1871 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001872}
1873
Richard Smith3a2b7a12013-01-28 22:42:45 +00001874static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1875 const AttributeList &Attr) {
1876 if (isa<ParmVarDecl>(D)) {
1877 // [[carries_dependency]] can only be applied to a parameter if it is a
1878 // parameter of a function declaration or lambda.
1879 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1880 S.Diag(Attr.getLoc(),
1881 diag::err_carries_dependency_param_not_function_decl);
1882 return;
1883 }
1884 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001885 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001886 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001887 return;
1888 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001889
1890 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1891 Attr.getRange(), S.Context,
1892 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001893}
1894
Chandler Carruth1b03c872011-07-02 00:01:44 +00001895static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001896 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001897 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001898 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001899 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001900 return;
1901 }
Mike Stumpbf916502009-07-24 19:02:52 +00001902
Michael Han51d8c522013-01-24 16:46:58 +00001903 D->addAttr(::new (S.Context)
1904 UnusedAttr(Attr.getRange(), S.Context,
1905 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001906}
1907
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001908static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1909 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001910 if (!isa<FunctionDecl>(D)) {
1911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1912 << Attr.getName() << ExpectedFunction;
1913 return;
1914 }
1915
Michael Han51d8c522013-01-24 16:46:58 +00001916 D->addAttr(::new (S.Context)
1917 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1918 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001919}
1920
Chandler Carruth1b03c872011-07-02 00:01:44 +00001921static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001922 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001923 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001924 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1925 return;
1926 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001927 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001928 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001929 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001930 return;
1931 }
Mike Stumpbf916502009-07-24 19:02:52 +00001932
Michael Han51d8c522013-01-24 16:46:58 +00001933 D->addAttr(::new (S.Context)
1934 UsedAttr(Attr.getRange(), S.Context,
1935 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001936}
1937
Chandler Carruth1b03c872011-07-02 00:01:44 +00001938static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001939 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001940 if (Attr.getNumArgs() > 1) {
1941 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001942 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001943 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001944
1945 int priority = 65535; // FIXME: Do not hardcode such constants.
1946 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001947 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001948 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001949 if (E->isTypeDependent() || E->isValueDependent() ||
1950 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001951 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001952 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001953 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001954 return;
1955 }
1956 priority = Idx.getZExtValue();
1957 }
Mike Stumpbf916502009-07-24 19:02:52 +00001958
Chandler Carruth87c44602011-07-01 23:49:12 +00001959 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001960 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001961 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001962 return;
1963 }
1964
Michael Han51d8c522013-01-24 16:46:58 +00001965 D->addAttr(::new (S.Context)
1966 ConstructorAttr(Attr.getRange(), S.Context, priority,
1967 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001968}
1969
Chandler Carruth1b03c872011-07-02 00:01:44 +00001970static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001971 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001972 if (Attr.getNumArgs() > 1) {
1973 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001974 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001975 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001976
1977 int priority = 65535; // FIXME: Do not hardcode such constants.
1978 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001979 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001980 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001981 if (E->isTypeDependent() || E->isValueDependent() ||
1982 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001983 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001984 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001985 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001986 return;
1987 }
1988 priority = Idx.getZExtValue();
1989 }
Mike Stumpbf916502009-07-24 19:02:52 +00001990
Chandler Carruth87c44602011-07-01 23:49:12 +00001991 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001992 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001993 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001994 return;
1995 }
1996
Michael Han51d8c522013-01-24 16:46:58 +00001997 D->addAttr(::new (S.Context)
1998 DestructorAttr(Attr.getRange(), S.Context, priority,
1999 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002000}
2001
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002002template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002003static void handleAttrWithMessage(Sema &S, Decl *D,
2004 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002005 unsigned NumArgs = Attr.getNumArgs();
2006 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002007 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002008 return;
2009 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002010
2011 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002012 StringRef Str;
Aaron Ballman624421f2013-08-31 01:11:41 +00002013 if (Attr.isArgExpr(0)) {
2014 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002015 if (!SE) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002016 S.Diag(Attr.getArgAsExpr(0)->getLocStart(),
2017 diag::err_attribute_argument_type) << Attr.getName()
2018 << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002019 return;
2020 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002021 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002022 }
Mike Stumpbf916502009-07-24 19:02:52 +00002023
Michael Han51d8c522013-01-24 16:46:58 +00002024 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2025 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002026}
2027
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002028static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2029 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002030 D->addAttr(::new (S.Context)
2031 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2032 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002033}
2034
Patrick Beardb2f68202012-04-06 18:12:22 +00002035static void handleObjCRootClassAttr(Sema &S, Decl *D,
2036 const AttributeList &Attr) {
2037 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2039 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002040 return;
2041 }
2042
Michael Han51d8c522013-01-24 16:46:58 +00002043 D->addAttr(::new (S.Context)
2044 ObjCRootClassAttr(Attr.getRange(), S.Context,
2045 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002046}
2047
Michael Han51d8c522013-01-24 16:46:58 +00002048static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2049 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002050 if (!isa<ObjCInterfaceDecl>(D)) {
2051 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2052 return;
2053 }
2054
Michael Han51d8c522013-01-24 16:46:58 +00002055 D->addAttr(::new (S.Context)
2056 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2057 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002058}
2059
Jordy Rosefad5de92012-05-08 03:27:22 +00002060static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2061 IdentifierInfo *Platform,
2062 VersionTuple Introduced,
2063 VersionTuple Deprecated,
2064 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002065 StringRef PlatformName
2066 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2067 if (PlatformName.empty())
2068 PlatformName = Platform->getName();
2069
2070 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2071 // of these steps are needed).
2072 if (!Introduced.empty() && !Deprecated.empty() &&
2073 !(Introduced <= Deprecated)) {
2074 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2075 << 1 << PlatformName << Deprecated.getAsString()
2076 << 0 << Introduced.getAsString();
2077 return true;
2078 }
2079
2080 if (!Introduced.empty() && !Obsoleted.empty() &&
2081 !(Introduced <= Obsoleted)) {
2082 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2083 << 2 << PlatformName << Obsoleted.getAsString()
2084 << 0 << Introduced.getAsString();
2085 return true;
2086 }
2087
2088 if (!Deprecated.empty() && !Obsoleted.empty() &&
2089 !(Deprecated <= Obsoleted)) {
2090 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2091 << 2 << PlatformName << Obsoleted.getAsString()
2092 << 1 << Deprecated.getAsString();
2093 return true;
2094 }
2095
2096 return false;
2097}
2098
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002099/// \brief Check whether the two versions match.
2100///
2101/// If either version tuple is empty, then they are assumed to match. If
2102/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2103static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2104 bool BeforeIsOkay) {
2105 if (X.empty() || Y.empty())
2106 return true;
2107
2108 if (X == Y)
2109 return true;
2110
2111 if (BeforeIsOkay && X < Y)
2112 return true;
2113
2114 return false;
2115}
2116
Rafael Espindola51be6e32013-01-08 22:04:34 +00002117AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002118 IdentifierInfo *Platform,
2119 VersionTuple Introduced,
2120 VersionTuple Deprecated,
2121 VersionTuple Obsoleted,
2122 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002123 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002124 bool Override,
2125 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002126 VersionTuple MergedIntroduced = Introduced;
2127 VersionTuple MergedDeprecated = Deprecated;
2128 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002129 bool FoundAny = false;
2130
Rafael Espindola98ae8342012-05-10 02:50:16 +00002131 if (D->hasAttrs()) {
2132 AttrVec &Attrs = D->getAttrs();
2133 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2134 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2135 if (!OldAA) {
2136 ++i;
2137 continue;
2138 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002139
Rafael Espindola98ae8342012-05-10 02:50:16 +00002140 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2141 if (OldPlatform != Platform) {
2142 ++i;
2143 continue;
2144 }
2145
2146 FoundAny = true;
2147 VersionTuple OldIntroduced = OldAA->getIntroduced();
2148 VersionTuple OldDeprecated = OldAA->getDeprecated();
2149 VersionTuple OldObsoleted = OldAA->getObsoleted();
2150 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002151
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002152 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2153 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2154 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2155 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002156 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002157 if (Override) {
2158 int Which = -1;
2159 VersionTuple FirstVersion;
2160 VersionTuple SecondVersion;
2161 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2162 Which = 0;
2163 FirstVersion = OldIntroduced;
2164 SecondVersion = Introduced;
2165 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2166 Which = 1;
2167 FirstVersion = Deprecated;
2168 SecondVersion = OldDeprecated;
2169 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2170 Which = 2;
2171 FirstVersion = Obsoleted;
2172 SecondVersion = OldObsoleted;
2173 }
2174
2175 if (Which == -1) {
2176 Diag(OldAA->getLocation(),
2177 diag::warn_mismatched_availability_override_unavail)
2178 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2179 } else {
2180 Diag(OldAA->getLocation(),
2181 diag::warn_mismatched_availability_override)
2182 << Which
2183 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2184 << FirstVersion.getAsString() << SecondVersion.getAsString();
2185 }
2186 Diag(Range.getBegin(), diag::note_overridden_method);
2187 } else {
2188 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2189 Diag(Range.getBegin(), diag::note_previous_attribute);
2190 }
2191
Rafael Espindola98ae8342012-05-10 02:50:16 +00002192 Attrs.erase(Attrs.begin() + i);
2193 --e;
2194 continue;
2195 }
2196
2197 VersionTuple MergedIntroduced2 = MergedIntroduced;
2198 VersionTuple MergedDeprecated2 = MergedDeprecated;
2199 VersionTuple MergedObsoleted2 = MergedObsoleted;
2200
2201 if (MergedIntroduced2.empty())
2202 MergedIntroduced2 = OldIntroduced;
2203 if (MergedDeprecated2.empty())
2204 MergedDeprecated2 = OldDeprecated;
2205 if (MergedObsoleted2.empty())
2206 MergedObsoleted2 = OldObsoleted;
2207
2208 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2209 MergedIntroduced2, MergedDeprecated2,
2210 MergedObsoleted2)) {
2211 Attrs.erase(Attrs.begin() + i);
2212 --e;
2213 continue;
2214 }
2215
2216 MergedIntroduced = MergedIntroduced2;
2217 MergedDeprecated = MergedDeprecated2;
2218 MergedObsoleted = MergedObsoleted2;
2219 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002220 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002221 }
2222
2223 if (FoundAny &&
2224 MergedIntroduced == Introduced &&
2225 MergedDeprecated == Deprecated &&
2226 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002227 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002228
Ted Kremenekcb344392013-04-06 00:34:27 +00002229 // Only create a new attribute if !Override, but we want to do
2230 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002231 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002232 MergedDeprecated, MergedObsoleted) &&
2233 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002234 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2235 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002236 Obsoleted, IsUnavailable, Message,
2237 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002238 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002239 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002240}
2241
Chandler Carruth1b03c872011-07-02 00:01:44 +00002242static void handleAvailabilityAttr(Sema &S, Decl *D,
2243 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002244 if (!checkAttributeNumArgs(S, Attr, 1))
2245 return;
2246 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002247 unsigned Index = Attr.getAttributeSpellingListIndex();
2248
Aaron Ballman624421f2013-08-31 01:11:41 +00002249 IdentifierInfo *II = Platform->Ident;
2250 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2251 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2252 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002253
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002254 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2255 if (!ND) {
2256 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2257 return;
2258 }
2259
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002260 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2261 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2262 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002263 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002264 StringRef Str;
2265 const StringLiteral *SE =
2266 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2267 if (SE)
2268 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002269
Aaron Ballman624421f2013-08-31 01:11:41 +00002270 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002271 Introduced.Version,
2272 Deprecated.Version,
2273 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002274 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002275 /*Override=*/false,
2276 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002277 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002278 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002279}
2280
John McCalld4c3d662013-02-20 01:54:26 +00002281template <class T>
2282static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2283 typename T::VisibilityType value,
2284 unsigned attrSpellingListIndex) {
2285 T *existingAttr = D->getAttr<T>();
2286 if (existingAttr) {
2287 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2288 if (existingValue == value)
2289 return NULL;
2290 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2291 S.Diag(range.getBegin(), diag::note_previous_attribute);
2292 D->dropAttr<T>();
2293 }
2294 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2295}
2296
Rafael Espindola599f1b72012-05-13 03:25:18 +00002297VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002298 VisibilityAttr::VisibilityType Vis,
2299 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002300 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2301 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002302}
2303
John McCalld4c3d662013-02-20 01:54:26 +00002304TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2305 TypeVisibilityAttr::VisibilityType Vis,
2306 unsigned AttrSpellingListIndex) {
2307 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2308 AttrSpellingListIndex);
2309}
2310
2311static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2312 bool isTypeVisibility) {
2313 // Visibility attributes don't mean anything on a typedef.
2314 if (isa<TypedefNameDecl>(D)) {
2315 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2316 << Attr.getName();
2317 return;
2318 }
2319
2320 // 'type_visibility' can only go on a type or namespace.
2321 if (isTypeVisibility &&
2322 !(isa<TagDecl>(D) ||
2323 isa<ObjCInterfaceDecl>(D) ||
2324 isa<NamespaceDecl>(D))) {
2325 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2326 << Attr.getName() << ExpectedTypeOrNamespace;
2327 return;
2328 }
2329
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002330 // Check that the argument is a string literal.
2331 StringLiteral *Str = 0;
2332 if (Attr.isArgExpr(0))
2333 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00002334
Douglas Gregor5cee1192011-07-27 05:40:30 +00002335 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2337 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002338 return;
2339 }
Mike Stumpbf916502009-07-24 19:02:52 +00002340
Chris Lattner5f9e2722011-07-23 10:55:15 +00002341 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002342 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002343
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002344 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002345 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002346 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002347 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002348 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002349 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002350 else if (TypeStr == "protected") {
2351 // Complain about attempts to use protected visibility on targets
2352 // (like Darwin) that don't support it.
2353 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2354 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2355 type = VisibilityAttr::Default;
2356 } else {
2357 type = VisibilityAttr::Protected;
2358 }
2359 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002360 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002361 return;
2362 }
Mike Stumpbf916502009-07-24 19:02:52 +00002363
Michael Han51d8c522013-01-24 16:46:58 +00002364 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002365 clang::Attr *newAttr;
2366 if (isTypeVisibility) {
2367 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2368 (TypeVisibilityAttr::VisibilityType) type,
2369 Index);
2370 } else {
2371 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2372 }
2373 if (newAttr)
2374 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002375}
2376
Chandler Carruth1b03c872011-07-02 00:01:44 +00002377static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2378 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002379 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2380 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002381 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002382 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002383 return;
2384 }
2385
Aaron Ballman624421f2013-08-31 01:11:41 +00002386 if (!Attr.isArgIdent(0)) {
2387 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2388 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002389 return;
2390 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002391
Aaron Ballman624421f2013-08-31 01:11:41 +00002392 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2393
2394 StringRef param = IL->Ident->getName();
John McCalld5313b02011-03-02 11:33:24 +00002395 ObjCMethodFamilyAttr::FamilyKind family;
2396 if (param == "none")
2397 family = ObjCMethodFamilyAttr::OMF_None;
2398 else if (param == "alloc")
2399 family = ObjCMethodFamilyAttr::OMF_alloc;
2400 else if (param == "copy")
2401 family = ObjCMethodFamilyAttr::OMF_copy;
2402 else if (param == "init")
2403 family = ObjCMethodFamilyAttr::OMF_init;
2404 else if (param == "mutableCopy")
2405 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2406 else if (param == "new")
2407 family = ObjCMethodFamilyAttr::OMF_new;
2408 else {
2409 // Just warn and ignore it. This is future-proof against new
2410 // families being used in system headers.
Aaron Ballman624421f2013-08-31 01:11:41 +00002411 S.Diag(IL->Loc, diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002412 return;
2413 }
2414
John McCallf85e1932011-06-15 23:02:42 +00002415 if (family == ObjCMethodFamilyAttr::OMF_init &&
2416 !method->getResultType()->isObjCObjectPointerType()) {
2417 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2418 << method->getResultType();
2419 // Ignore the attribute.
2420 return;
2421 }
2422
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002423 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002424 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002425}
2426
Chandler Carruth1b03c872011-07-02 00:01:44 +00002427static void handleObjCExceptionAttr(Sema &S, Decl *D,
2428 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002429 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2430 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2432 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002433 return;
2434 }
Mike Stumpbf916502009-07-24 19:02:52 +00002435
Michael Han51d8c522013-01-24 16:46:58 +00002436 D->addAttr(::new (S.Context)
2437 ObjCExceptionAttr(Attr.getRange(), S.Context,
2438 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002439}
2440
Chandler Carruth1b03c872011-07-02 00:01:44 +00002441static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002442 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002443 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002444 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002445 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2446 return;
2447 }
2448 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002449 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2450 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002451 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002452 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2453 return;
2454 }
2455 }
2456 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002457 // It is okay to include this attribute on properties, e.g.:
2458 //
2459 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2460 //
2461 // In this case it follows tradition and suppresses an error in the above
2462 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002463 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002464 }
Michael Han51d8c522013-01-24 16:46:58 +00002465 D->addAttr(::new (S.Context)
2466 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2467 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002468}
2469
Mike Stumpbf916502009-07-24 19:02:52 +00002470static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002471handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002472 if (!isa<FunctionDecl>(D)) {
2473 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2474 return;
2475 }
2476
Michael Han51d8c522013-01-24 16:46:58 +00002477 D->addAttr(::new (S.Context)
2478 OverloadableAttr(Attr.getRange(), S.Context,
2479 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002480}
2481
Chandler Carruth1b03c872011-07-02 00:01:44 +00002482static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002483 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002484 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002485 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002486 return;
2487 }
Mike Stumpbf916502009-07-24 19:02:52 +00002488
Aaron Ballman624421f2013-08-31 01:11:41 +00002489 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002490 BlocksAttr::BlockType type;
Aaron Ballman624421f2013-08-31 01:11:41 +00002491 if (II->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002492 type = BlocksAttr::ByRef;
2493 else {
Aaron Ballman624421f2013-08-31 01:11:41 +00002494 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) << "blocks"
2495 << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002496 return;
2497 }
Mike Stumpbf916502009-07-24 19:02:52 +00002498
Michael Han51d8c522013-01-24 16:46:58 +00002499 D->addAttr(::new (S.Context)
2500 BlocksAttr(Attr.getRange(), S.Context, type,
2501 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002502}
2503
Chandler Carruth1b03c872011-07-02 00:01:44 +00002504static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002505 // check the attribute arguments.
2506 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002507 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002508 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002509 }
2510
John McCall3323fad2011-09-09 07:56:05 +00002511 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002512 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002513 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002514 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002515 if (E->isTypeDependent() || E->isValueDependent() ||
2516 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002517 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002518 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002519 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002520 return;
2521 }
Mike Stumpbf916502009-07-24 19:02:52 +00002522
John McCall3323fad2011-09-09 07:56:05 +00002523 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002524 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2525 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002526 return;
2527 }
John McCall3323fad2011-09-09 07:56:05 +00002528
2529 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002530 }
2531
John McCall3323fad2011-09-09 07:56:05 +00002532 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002533 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002534 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002535 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002536 if (E->isTypeDependent() || E->isValueDependent() ||
2537 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002539 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002540 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002541 return;
2542 }
2543 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002544
John McCall3323fad2011-09-09 07:56:05 +00002545 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002546 // FIXME: This error message could be improved, it would be nice
2547 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002548 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2549 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002550 return;
2551 }
2552 }
2553
Chandler Carruth87c44602011-07-01 23:49:12 +00002554 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002555 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002556 if (isa<FunctionNoProtoType>(FT)) {
2557 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2558 return;
2559 }
Mike Stumpbf916502009-07-24 19:02:52 +00002560
Chris Lattner897cd902009-03-17 23:03:47 +00002561 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002562 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002563 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002564 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002565 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002566 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002568 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002569 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002570 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2571 if (!BD->isVariadic()) {
2572 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2573 return;
2574 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002575 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002576 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002577 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002578 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002579 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002580 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002581 int m = Ty->isFunctionPointerType() ? 0 : 1;
2582 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002583 return;
2584 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002585 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002587 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002588 return;
2589 }
Anders Carlsson77091822008-10-05 18:05:59 +00002590 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002591 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002592 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002593 return;
2594 }
Michael Han51d8c522013-01-24 16:46:58 +00002595 D->addAttr(::new (S.Context)
2596 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2597 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002598}
2599
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002600static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002601 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2602 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2603 else
2604 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2605}
2606
Chandler Carruth1b03c872011-07-02 00:01:44 +00002607static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002608 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002609 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002610 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002611 return;
2612 }
Mike Stumpbf916502009-07-24 19:02:52 +00002613
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002614 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2616 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002617 return;
2618 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002619 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2620 if (MD->getResultType()->isVoidType()) {
2621 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2622 << Attr.getName() << 1;
2623 return;
2624 }
2625
Michael Han51d8c522013-01-24 16:46:58 +00002626 D->addAttr(::new (S.Context)
2627 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2628 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002629}
2630
Chandler Carruth1b03c872011-07-02 00:01:44 +00002631static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002632 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002633 if (isa<CXXRecordDecl>(D)) {
2634 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2635 return;
2636 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2638 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002639 return;
2640 }
2641
Chandler Carruth87c44602011-07-01 23:49:12 +00002642 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002643
Michael Han51d8c522013-01-24 16:46:58 +00002644 nd->addAttr(::new (S.Context)
2645 WeakAttr(Attr.getRange(), S.Context,
2646 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002647}
2648
Chandler Carruth1b03c872011-07-02 00:01:44 +00002649static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002650 // weak_import only applies to variable & function declarations.
2651 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002652 if (!D->canBeWeakImported(isDef)) {
2653 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002654 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2655 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002656 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002657 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002658 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002659 // Nothing to warn about here.
2660 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002662 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002663
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002664 return;
2665 }
2666
Michael Han51d8c522013-01-24 16:46:58 +00002667 D->addAttr(::new (S.Context)
2668 WeakImportAttr(Attr.getRange(), S.Context,
2669 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002670}
2671
Tanya Lattner0df579e2012-07-09 22:06:01 +00002672// Handles reqd_work_group_size and work_group_size_hint.
2673static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002674 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002675 unsigned WGSize[3];
2676 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002677 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002678 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002679 if (E->isTypeDependent() || E->isValueDependent() ||
2680 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002681 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2682 << Attr.getName() << AANT_ArgumentIntegerConstant
2683 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002684 return;
2685 }
2686 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2687 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002688
2689 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2690 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2691 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2692 if (!(A->getXDim() == WGSize[0] &&
2693 A->getYDim() == WGSize[1] &&
2694 A->getZDim() == WGSize[2])) {
2695 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2696 Attr.getName();
2697 }
2698 }
2699
2700 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2701 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2702 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2703 if (!(A->getXDim() == WGSize[0] &&
2704 A->getYDim() == WGSize[1] &&
2705 A->getZDim() == WGSize[2])) {
2706 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2707 Attr.getName();
2708 }
2709 }
2710
2711 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2712 D->addAttr(::new (S.Context)
2713 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002714 WGSize[0], WGSize[1], WGSize[2],
2715 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002716 else
2717 D->addAttr(::new (S.Context)
2718 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002719 WGSize[0], WGSize[1], WGSize[2],
2720 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002721}
2722
Joey Gouly37453b92013-03-08 09:42:32 +00002723static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2724 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2725
Aaron Ballman624421f2013-08-31 01:11:41 +00002726 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002727 return;
2728
Aaron Ballman624421f2013-08-31 01:11:41 +00002729 if (!Attr.hasParsedType()) {
2730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2731 << Attr.getName() << 1;
2732 return;
2733 }
2734
Joey Gouly37453b92013-03-08 09:42:32 +00002735 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2736
2737 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2738 (ParmType->isBooleanType() ||
2739 !ParmType->isIntegralType(S.getASTContext()))) {
2740 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2741 << ParmType;
2742 return;
2743 }
2744
2745 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2746 D->hasAttr<VecTypeHintAttr>()) {
2747 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2748 if (A->getTypeHint() != ParmType) {
2749 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2750 return;
2751 }
2752 }
2753
2754 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2755 ParmType, Attr.getLoc()));
2756}
2757
Rafael Espindola599f1b72012-05-13 03:25:18 +00002758SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002759 StringRef Name,
2760 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002761 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2762 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002763 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002764 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2765 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002766 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002767 }
Michael Han51d8c522013-01-24 16:46:58 +00002768 return ::new (Context) SectionAttr(Range, Context, Name,
2769 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002770}
2771
Chandler Carruth1b03c872011-07-02 00:01:44 +00002772static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002773 // Make sure that there is a string literal as the sections's single
2774 // argument.
Aaron Ballman624421f2013-08-31 01:11:41 +00002775 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002776 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002777 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002778 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2779 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002780 return;
2781 }
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Chris Lattner797c3c42009-08-10 19:03:04 +00002783 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002784 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002785 if (!Error.empty()) {
2786 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2787 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002788 return;
2789 }
Mike Stump1eb44332009-09-09 15:08:12 +00002790
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002791 // This attribute cannot be applied to local variables.
2792 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2793 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2794 return;
2795 }
Michael Han51d8c522013-01-24 16:46:58 +00002796
2797 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002798 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002799 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002800 if (NewAttr)
2801 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002802}
2803
Chris Lattner6b6b5372008-06-26 18:38:35 +00002804
Chandler Carruth1b03c872011-07-02 00:01:44 +00002805static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002806 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002807 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002808 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002809 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002810 D->addAttr(::new (S.Context)
2811 NoThrowAttr(Attr.getRange(), S.Context,
2812 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002813 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002814}
2815
Chandler Carruth1b03c872011-07-02 00:01:44 +00002816static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002817 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002818 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002819 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002820 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002821 D->addAttr(::new (S.Context)
2822 ConstAttr(Attr.getRange(), S.Context,
2823 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002824 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002825}
2826
Chandler Carruth1b03c872011-07-02 00:01:44 +00002827static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002828 D->addAttr(::new (S.Context)
2829 PureAttr(Attr.getRange(), S.Context,
2830 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002831}
2832
Chandler Carruth1b03c872011-07-02 00:01:44 +00002833static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002834 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002835 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002836 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002837 return;
2838 }
Mike Stumpbf916502009-07-24 19:02:52 +00002839
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002840 Expr *E = Attr.getArgAsExpr(0);
2841 SourceLocation Loc = E->getExprLoc();
2842 FunctionDecl *FD = 0;
2843 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002844
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002845 // gcc only allows for simple identifiers. Since we support more than gcc, we
2846 // will warn the user.
2847 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2848 if (DRE->hasQualifier())
2849 S.Diag(Loc, diag::warn_cleanup_ext);
2850 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2851 NI = DRE->getNameInfo();
2852 if (!FD) {
2853 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2854 << NI.getName();
2855 return;
2856 }
2857 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2858 if (ULE->hasExplicitTemplateArgs())
2859 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002860
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002861 // This will diagnose the case where the function cannot be found.
2862 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2863 NI = ULE->getNameInfo();
2864 } else {
2865 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002866 return;
2867 }
2868
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002869 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002870 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2871 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002872 return;
2873 }
Mike Stumpbf916502009-07-24 19:02:52 +00002874
Anders Carlsson89941c12009-02-07 23:16:50 +00002875 // We're currently more strict than GCC about what function types we accept.
2876 // If this ever proves to be a problem it should be easy to fix.
2877 QualType Ty = S.Context.getPointerType(VD->getType());
2878 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002879 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2880 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002881 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2882 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002883 return;
2884 }
Mike Stumpbf916502009-07-24 19:02:52 +00002885
Michael Han51d8c522013-01-24 16:46:58 +00002886 D->addAttr(::new (S.Context)
2887 CleanupAttr(Attr.getRange(), S.Context, FD,
2888 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002889}
2890
Mike Stumpbf916502009-07-24 19:02:52 +00002891/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002892/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002893static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002894 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002895 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002896 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002897 return;
2898 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002899
Aaron Ballman624421f2013-08-31 01:11:41 +00002900 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002901 uint64_t ArgIdx;
2902 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2903 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002904 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002905
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002906 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002907 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002908
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002909 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2910 if (not_nsstring_type &&
2911 !isCFStringType(Ty, S.Context) &&
2912 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002913 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002914 // FIXME: Should highlight the actual expression that has the wrong type.
2915 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002916 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002917 << IdxExpr->getSourceRange();
2918 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002919 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002920 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002921 if (!isNSStringType(Ty, S.Context) &&
2922 !isCFStringType(Ty, S.Context) &&
2923 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002924 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002925 // FIXME: Should highlight the actual expression that has the wrong type.
2926 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002927 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002928 << IdxExpr->getSourceRange();
2929 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002930 }
2931
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002932 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2933 // because that has corrected for the implicit this parameter, and is zero-
2934 // based. The attribute expects what the user wrote explicitly.
2935 llvm::APSInt Val;
2936 IdxExpr->EvaluateAsInt(Val, S.Context);
2937
Michael Han51d8c522013-01-24 16:46:58 +00002938 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002939 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002940 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002941}
2942
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002943enum FormatAttrKind {
2944 CFStringFormat,
2945 NSStringFormat,
2946 StrftimeFormat,
2947 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002948 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002949 InvalidFormat
2950};
2951
2952/// getFormatAttrKind - Map from format attribute names to supported format
2953/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002954static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002955 return llvm::StringSwitch<FormatAttrKind>(Format)
2956 // Check for formats that get handled specially.
2957 .Case("NSString", NSStringFormat)
2958 .Case("CFString", CFStringFormat)
2959 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002960
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002961 // Otherwise, check for supported formats.
2962 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2963 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2964 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002965
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002966 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2967 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002968}
2969
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002970/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002971/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002972static void handleInitPriorityAttr(Sema &S, Decl *D,
2973 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002974 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002975 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2976 return;
2977 }
2978
Chandler Carruth87c44602011-07-01 23:49:12 +00002979 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002980 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2981 Attr.setInvalid();
2982 return;
2983 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002984 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002985 if (S.Context.getAsArrayType(T))
2986 T = S.Context.getBaseElementType(T);
2987 if (!T->getAs<RecordType>()) {
2988 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2989 Attr.setInvalid();
2990 return;
2991 }
2992
Aaron Ballman624421f2013-08-31 01:11:41 +00002993 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002994
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002995 llvm::APSInt priority(32);
2996 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2997 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2999 << Attr.getName() << AANT_ArgumentIntegerConstant
3000 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003001 Attr.setInvalid();
3002 return;
3003 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003004 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003005 if (prioritynum < 101 || prioritynum > 65535) {
3006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3007 << priorityExpr->getSourceRange();
3008 Attr.setInvalid();
3009 return;
3010 }
Michael Han51d8c522013-01-24 16:46:58 +00003011 D->addAttr(::new (S.Context)
3012 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3013 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003014}
3015
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003016FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3017 IdentifierInfo *Format, int FormatIdx,
3018 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003019 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003020 // Check whether we already have an equivalent format attribute.
3021 for (specific_attr_iterator<FormatAttr>
3022 i = D->specific_attr_begin<FormatAttr>(),
3023 e = D->specific_attr_end<FormatAttr>();
3024 i != e ; ++i) {
3025 FormatAttr *f = *i;
3026 if (f->getType() == Format &&
3027 f->getFormatIdx() == FormatIdx &&
3028 f->getFirstArg() == FirstArg) {
3029 // If we don't have a valid location for this attribute, adopt the
3030 // location.
3031 if (f->getLocation().isInvalid())
3032 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003033 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003034 }
3035 }
3036
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003037 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3038 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003039}
3040
Mike Stumpbf916502009-07-24 19:02:52 +00003041/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003042/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003043static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003044 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003045 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003046 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003047 return;
3048 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003049
Chandler Carruth87c44602011-07-01 23:49:12 +00003050 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003052 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003053 return;
3054 }
3055
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003056 // In C++ the implicit 'this' function parameter also counts, and they are
3057 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003058 bool HasImplicitThisParam = isInstanceMethod(D);
3059 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003060 unsigned FirstIdx = 1;
3061
Aaron Ballman624421f2013-08-31 01:11:41 +00003062 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3063 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003064
3065 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003066 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003067 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003068 // If we've modified the string name, we need a new identifier for it.
3069 II = &S.Context.Idents.get(Format);
3070 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003071
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003072 // Check for supported formats.
3073 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003074
3075 if (Kind == IgnoredFormat)
3076 return;
3077
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003078 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003079 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003080 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003081 return;
3082 }
3083
3084 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003085 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003086 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003087 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3088 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003089 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003090 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003091 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003092 return;
3093 }
3094
3095 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003096 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003097 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003098 return;
3099 }
3100
3101 // FIXME: Do we need to bounds check?
3102 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003103
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003104 if (HasImplicitThisParam) {
3105 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003106 S.Diag(Attr.getLoc(),
3107 diag::err_format_attribute_implicit_this_format_string)
3108 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003109 return;
3110 }
3111 ArgIdx--;
3112 }
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Chris Lattner6b6b5372008-06-26 18:38:35 +00003114 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003115 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003116
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003117 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003118 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003119 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3120 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003121 return;
3122 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003123 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003124 // FIXME: do we need to check if the type is NSString*? What are the
3125 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003126 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003127 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003128 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3129 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003130 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003131 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003132 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003133 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003134 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003135 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3136 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003137 return;
3138 }
3139
3140 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003141 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003142 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003143 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3144 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003146 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003147 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003148 return;
3149 }
3150
3151 // check if the function is variadic if the 3rd argument non-zero
3152 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003153 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003154 ++NumArgs; // +1 for ...
3155 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003156 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003157 return;
3158 }
3159 }
3160
Chris Lattner3c73c412008-11-19 08:23:25 +00003161 // strftime requires FirstArg to be 0 because it doesn't read from any
3162 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003163 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003164 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003165 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3166 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003167 return;
3168 }
3169 // if 0 it disables parameter checking (to use with e.g. va_list)
3170 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003171 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003172 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003173 return;
3174 }
3175
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003176 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003177 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003178 FirstArg.getZExtValue(),
3179 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003180 if (NewAttr)
3181 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003182}
3183
Chandler Carruth1b03c872011-07-02 00:01:44 +00003184static void handleTransparentUnionAttr(Sema &S, Decl *D,
3185 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003186 // Try to find the underlying union declaration.
3187 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003188 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003189 if (TD && TD->getUnderlyingType()->isUnionType())
3190 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3191 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003192 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003193
3194 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003196 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197 return;
3198 }
3199
John McCall5e1cdac2011-10-07 06:10:15 +00003200 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003201 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003202 diag::warn_transparent_union_attribute_not_definition);
3203 return;
3204 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003205
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003206 RecordDecl::field_iterator Field = RD->field_begin(),
3207 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003208 if (Field == FieldEnd) {
3209 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3210 return;
3211 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003212
David Blaikie581deb32012-06-06 20:45:41 +00003213 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003214 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003215 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003216 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003217 diag::warn_transparent_union_attribute_floating)
3218 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003219 return;
3220 }
3221
3222 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3223 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3224 for (; Field != FieldEnd; ++Field) {
3225 QualType FieldType = Field->getType();
3226 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3227 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3228 // Warn if we drop the attribute.
3229 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003230 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003231 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003232 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003233 diag::warn_transparent_union_attribute_field_size_align)
3234 << isSize << Field->getDeclName() << FieldBits;
3235 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003236 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003237 diag::note_transparent_union_first_field_size_align)
3238 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003239 return;
3240 }
3241 }
3242
Michael Han51d8c522013-01-24 16:46:58 +00003243 RD->addAttr(::new (S.Context)
3244 TransparentUnionAttr(Attr.getRange(), S.Context,
3245 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003246}
3247
Chandler Carruth1b03c872011-07-02 00:01:44 +00003248static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003249 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003250 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003251
Chris Lattner6b6b5372008-06-26 18:38:35 +00003252 // Make sure that there is a string literal as the annotation's single
3253 // argument.
3254 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003255 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3256 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003257 return;
3258 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003259
3260 // Don't duplicate annotations that are already set.
3261 for (specific_attr_iterator<AnnotateAttr>
3262 i = D->specific_attr_begin<AnnotateAttr>(),
3263 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3264 if ((*i)->getAnnotation() == SE->getString())
3265 return;
3266 }
Michael Han51d8c522013-01-24 16:46:58 +00003267
3268 D->addAttr(::new (S.Context)
3269 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3270 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003271}
3272
Chandler Carruth1b03c872011-07-02 00:01:44 +00003273static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003274 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003275 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003276 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3277 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003278 return;
3279 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003280
Richard Smithbe507b62013-02-01 08:12:08 +00003281 if (Attr.getNumArgs() == 0) {
3282 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3283 true, 0, Attr.getAttributeSpellingListIndex()));
3284 return;
3285 }
3286
Aaron Ballman624421f2013-08-31 01:11:41 +00003287 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003288 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3289 S.Diag(Attr.getEllipsisLoc(),
3290 diag::err_pack_expansion_without_parameter_packs);
3291 return;
3292 }
3293
3294 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3295 return;
3296
3297 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3298 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003299}
3300
3301void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003302 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003303 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3304 SourceLocation AttrLoc = AttrRange.getBegin();
3305
Richard Smith4cd81c52013-01-29 09:02:09 +00003306 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003307 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003308 // C++11 [dcl.align]p1:
3309 // An alignment-specifier may be applied to a variable or to a class
3310 // data member, but it shall not be applied to a bit-field, a function
3311 // parameter, the formal parameter of a catch clause, or a variable
3312 // declared with the register storage class specifier. An
3313 // alignment-specifier may also be applied to the declaration of a class
3314 // or enumeration type.
3315 // C11 6.7.5/2:
3316 // An alignment attribute shall not be specified in a declaration of
3317 // a typedef, or a bit-field, or a function, or a parameter, or an
3318 // object declared with the register storage-class specifier.
3319 int DiagKind = -1;
3320 if (isa<ParmVarDecl>(D)) {
3321 DiagKind = 0;
3322 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3323 if (VD->getStorageClass() == SC_Register)
3324 DiagKind = 1;
3325 if (VD->isExceptionVariable())
3326 DiagKind = 2;
3327 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3328 if (FD->isBitField())
3329 DiagKind = 3;
3330 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003331 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3332 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003333 << (TmpAttr.isC11() ? ExpectedVariableOrField
3334 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003335 return;
3336 }
3337 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003338 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003339 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003340 return;
3341 }
3342 }
3343
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003344 if (E->isTypeDependent() || E->isValueDependent()) {
3345 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003346 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3347 AA->setPackExpansion(IsPackExpansion);
3348 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003349 return;
3350 }
Michael Hana31f65b2013-02-01 01:19:17 +00003351
Sean Huntcf807c42010-08-18 23:23:40 +00003352 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003353 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003354 ExprResult ICE
3355 = VerifyIntegerConstantExpression(E, &Alignment,
3356 diag::err_aligned_attribute_argument_not_int,
3357 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003358 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003359 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003360
3361 // C++11 [dcl.align]p2:
3362 // -- if the constant expression evaluates to zero, the alignment
3363 // specifier shall have no effect
3364 // C11 6.7.5p6:
3365 // An alignment specification of zero has no effect.
3366 if (!(TmpAttr.isAlignas() && !Alignment) &&
3367 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003368 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3369 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003370 return;
3371 }
Michael Hana31f65b2013-02-01 01:19:17 +00003372
Richard Smithbe507b62013-02-01 08:12:08 +00003373 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003374 // We've already verified it's a power of 2, now let's make sure it's
3375 // 8192 or less.
3376 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003377 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003378 << E->getSourceRange();
3379 return;
3380 }
3381 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003382
Richard Smithf6565a92013-02-22 08:32:16 +00003383 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3384 ICE.take(), SpellingListIndex);
3385 AA->setPackExpansion(IsPackExpansion);
3386 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003387}
3388
Michael Hana31f65b2013-02-01 01:19:17 +00003389void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003390 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003391 // FIXME: Cache the number on the Attr object if non-dependent?
3392 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003393 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3394 SpellingListIndex);
3395 AA->setPackExpansion(IsPackExpansion);
3396 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003397}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003398
Richard Smithbe507b62013-02-01 08:12:08 +00003399void Sema::CheckAlignasUnderalignment(Decl *D) {
3400 assert(D->hasAttrs() && "no attributes on decl");
3401
3402 QualType Ty;
3403 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3404 Ty = VD->getType();
3405 else
3406 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003407 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003408 return;
3409
3410 // C++11 [dcl.align]p5, C11 6.7.5/4:
3411 // The combined effect of all alignment attributes in a declaration shall
3412 // not specify an alignment that is less strict than the alignment that
3413 // would otherwise be required for the entity being declared.
3414 AlignedAttr *AlignasAttr = 0;
3415 unsigned Align = 0;
3416 for (specific_attr_iterator<AlignedAttr>
3417 I = D->specific_attr_begin<AlignedAttr>(),
3418 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3419 if (I->isAlignmentDependent())
3420 return;
3421 if (I->isAlignas())
3422 AlignasAttr = *I;
3423 Align = std::max(Align, I->getAlignment(Context));
3424 }
3425
3426 if (AlignasAttr && Align) {
3427 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3428 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3429 if (NaturalAlign > RequestedAlign)
3430 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3431 << Ty << (unsigned)NaturalAlign.getQuantity();
3432 }
3433}
3434
Chandler Carruthd309c812011-07-01 23:49:16 +00003435/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003436/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003437///
Mike Stumpbf916502009-07-24 19:02:52 +00003438/// Despite what would be logical, the mode attribute is a decl attribute, not a
3439/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3440/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003441static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003442 // This attribute isn't documented, but glibc uses it. It changes
3443 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003444 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003445 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3446 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003447 return;
3448 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003449
Aaron Ballman624421f2013-08-31 01:11:41 +00003450 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3451 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003452
3453 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003454 if (Str.startswith("__") && Str.endswith("__"))
3455 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003456
3457 unsigned DestWidth = 0;
3458 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003459 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003460 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003461 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003462 switch (Str[0]) {
3463 case 'Q': DestWidth = 8; break;
3464 case 'H': DestWidth = 16; break;
3465 case 'S': DestWidth = 32; break;
3466 case 'D': DestWidth = 64; break;
3467 case 'X': DestWidth = 96; break;
3468 case 'T': DestWidth = 128; break;
3469 }
3470 if (Str[1] == 'F') {
3471 IntegerMode = false;
3472 } else if (Str[1] == 'C') {
3473 IntegerMode = false;
3474 ComplexMode = true;
3475 } else if (Str[1] != 'I') {
3476 DestWidth = 0;
3477 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003478 break;
3479 case 4:
3480 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3481 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003482 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003483 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003484 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003485 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003486 break;
3487 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003488 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003489 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003490 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003491 case 11:
3492 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003493 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003494 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003495 }
3496
3497 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003498 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003499 OldTy = TD->getUnderlyingType();
3500 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3501 OldTy = VD->getType();
3502 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003503 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003504 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003505 return;
3506 }
Eli Friedman73397492009-03-03 06:41:03 +00003507
John McCall183700f2009-09-21 23:43:11 +00003508 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003509 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3510 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003511 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003512 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3513 } else if (ComplexMode) {
3514 if (!OldTy->isComplexType())
3515 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3516 } else {
3517 if (!OldTy->isFloatingType())
3518 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3519 }
3520
Mike Stump390b4cc2009-05-16 07:39:55 +00003521 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3522 // and friends, at least with glibc.
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003523 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3524 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003525 // FIXME: Make sure floating-point mappings are accurate
3526 // FIXME: Support XF and TF types
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003527 QualType NewTy;
3528 switch (DestWidth) {
3529 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003530 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003531 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003532 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003533 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003534 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003535 case 8:
3536 if (!IntegerMode) {
3537 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3538 return;
3539 }
3540 if (OldTy->isSignedIntegerType())
3541 NewTy = S.Context.SignedCharTy;
3542 else
3543 NewTy = S.Context.UnsignedCharTy;
3544 break;
3545 case 16:
3546 if (!IntegerMode) {
3547 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3548 return;
3549 }
3550 if (OldTy->isSignedIntegerType())
3551 NewTy = S.Context.ShortTy;
3552 else
3553 NewTy = S.Context.UnsignedShortTy;
3554 break;
3555 case 32:
3556 if (!IntegerMode)
3557 NewTy = S.Context.FloatTy;
3558 else if (OldTy->isSignedIntegerType())
3559 NewTy = S.Context.IntTy;
3560 else
3561 NewTy = S.Context.UnsignedIntTy;
3562 break;
3563 case 64:
3564 if (!IntegerMode)
3565 NewTy = S.Context.DoubleTy;
3566 else if (OldTy->isSignedIntegerType())
3567 if (S.Context.getTargetInfo().getLongWidth() == 64)
3568 NewTy = S.Context.LongTy;
3569 else
3570 NewTy = S.Context.LongLongTy;
3571 else
3572 if (S.Context.getTargetInfo().getLongWidth() == 64)
3573 NewTy = S.Context.UnsignedLongTy;
3574 else
3575 NewTy = S.Context.UnsignedLongLongTy;
3576 break;
3577 case 96:
3578 NewTy = S.Context.LongDoubleTy;
3579 break;
3580 case 128:
3581 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3582 &llvm::APFloat::PPCDoubleDouble) {
3583 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3584 return;
3585 }
3586 if (IntegerMode) {
3587 if (OldTy->isSignedIntegerType())
3588 NewTy = S.Context.Int128Ty;
3589 else
3590 NewTy = S.Context.UnsignedInt128Ty;
3591 } else
3592 NewTy = S.Context.LongDoubleTy;
3593 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003594 }
3595
Eli Friedman73397492009-03-03 06:41:03 +00003596 if (ComplexMode) {
3597 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003598 }
3599
3600 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003601 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3602 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3603 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003604 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003605
3606 D->addAttr(::new (S.Context)
3607 ModeAttr(Attr.getRange(), S.Context, Name,
3608 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003609}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003610
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003612 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3613 if (!VD->hasGlobalStorage())
3614 S.Diag(Attr.getLoc(),
3615 diag::warn_attribute_requires_functions_or_static_globals)
3616 << Attr.getName();
3617 } else if (!isFunctionOrMethod(D)) {
3618 S.Diag(Attr.getLoc(),
3619 diag::warn_attribute_requires_functions_or_static_globals)
3620 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003621 return;
3622 }
Mike Stumpbf916502009-07-24 19:02:52 +00003623
Michael Han51d8c522013-01-24 16:46:58 +00003624 D->addAttr(::new (S.Context)
3625 NoDebugAttr(Attr.getRange(), S.Context,
3626 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003627}
3628
Chandler Carruth1b03c872011-07-02 00:01:44 +00003629static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003630 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003632 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003633 return;
3634 }
Mike Stumpbf916502009-07-24 19:02:52 +00003635
Michael Han51d8c522013-01-24 16:46:58 +00003636 D->addAttr(::new (S.Context)
3637 NoInlineAttr(Attr.getRange(), S.Context,
3638 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003639}
3640
Chandler Carruth1b03c872011-07-02 00:01:44 +00003641static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3642 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003643 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003644 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003645 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003646 return;
3647 }
3648
Michael Han51d8c522013-01-24 16:46:58 +00003649 D->addAttr(::new (S.Context)
3650 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3651 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003652}
3653
Chandler Carruth1b03c872011-07-02 00:01:44 +00003654static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003655 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003656 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003658 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003659 return;
3660 }
3661
Michael Han51d8c522013-01-24 16:46:58 +00003662 D->addAttr(::new (S.Context)
3663 CUDAConstantAttr(Attr.getRange(), S.Context,
3664 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003665 } else {
3666 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3667 }
3668}
3669
Chandler Carruth1b03c872011-07-02 00:01:44 +00003670static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003671 if (S.LangOpts.CUDA) {
3672 // check the attribute arguments.
3673 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3675 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003676 return;
3677 }
3678
Chandler Carruth87c44602011-07-01 23:49:12 +00003679 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003680 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003681 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003682 return;
3683 }
3684
Michael Han51d8c522013-01-24 16:46:58 +00003685 D->addAttr(::new (S.Context)
3686 CUDADeviceAttr(Attr.getRange(), S.Context,
3687 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003688 } else {
3689 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3690 }
3691}
3692
Chandler Carruth1b03c872011-07-02 00:01:44 +00003693static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003694 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003695 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003696 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003697 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003698 return;
3699 }
3700
Chandler Carruth87c44602011-07-01 23:49:12 +00003701 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003702 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003703 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003704 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003705 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3706 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003707 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003708 "void");
3709 } else {
3710 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3711 << FD->getType();
3712 }
3713 return;
3714 }
3715
Michael Han51d8c522013-01-24 16:46:58 +00003716 D->addAttr(::new (S.Context)
3717 CUDAGlobalAttr(Attr.getRange(), S.Context,
3718 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003719 } else {
3720 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3721 }
3722}
3723
Chandler Carruth1b03c872011-07-02 00:01:44 +00003724static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003725 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003726 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003727 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003728 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003729 return;
3730 }
3731
Michael Han51d8c522013-01-24 16:46:58 +00003732 D->addAttr(::new (S.Context)
3733 CUDAHostAttr(Attr.getRange(), S.Context,
3734 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003735 } else {
3736 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3737 }
3738}
3739
Chandler Carruth1b03c872011-07-02 00:01:44 +00003740static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003741 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003742 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003744 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003745 return;
3746 }
3747
Michael Han51d8c522013-01-24 16:46:58 +00003748 D->addAttr(::new (S.Context)
3749 CUDASharedAttr(Attr.getRange(), S.Context,
3750 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003751 } else {
3752 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3753 }
3754}
3755
Chandler Carruth1b03c872011-07-02 00:01:44 +00003756static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003757 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003758 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003760 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003761 return;
3762 }
Mike Stumpbf916502009-07-24 19:02:52 +00003763
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003764 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003765 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003766 return;
3767 }
Mike Stumpbf916502009-07-24 19:02:52 +00003768
Michael Han51d8c522013-01-24 16:46:58 +00003769 D->addAttr(::new (S.Context)
3770 GNUInlineAttr(Attr.getRange(), S.Context,
3771 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003772}
3773
Chandler Carruth1b03c872011-07-02 00:01:44 +00003774static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003775 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003776
Aaron Ballmanfff32482012-12-09 17:45:41 +00003777 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003778 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003779 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3780 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003781 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003782 return;
3783
Chandler Carruth87c44602011-07-01 23:49:12 +00003784 if (!isa<ObjCMethodDecl>(D)) {
3785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3786 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003787 return;
3788 }
3789
Chandler Carruth87c44602011-07-01 23:49:12 +00003790 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003791 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003792 D->addAttr(::new (S.Context)
3793 FastCallAttr(Attr.getRange(), S.Context,
3794 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003795 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003796 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003797 D->addAttr(::new (S.Context)
3798 StdCallAttr(Attr.getRange(), S.Context,
3799 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003800 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003801 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003802 D->addAttr(::new (S.Context)
3803 ThisCallAttr(Attr.getRange(), S.Context,
3804 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003805 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003806 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003807 D->addAttr(::new (S.Context)
3808 CDeclAttr(Attr.getRange(), S.Context,
3809 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003810 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003811 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003812 D->addAttr(::new (S.Context)
3813 PascalAttr(Attr.getRange(), S.Context,
3814 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003815 return;
Charles Davise8519c32013-08-30 04:39:01 +00003816 case AttributeList::AT_MSABI:
3817 D->addAttr(::new (S.Context)
3818 MSABIAttr(Attr.getRange(), S.Context,
3819 Attr.getAttributeSpellingListIndex()));
3820 return;
3821 case AttributeList::AT_SysVABI:
3822 D->addAttr(::new (S.Context)
3823 SysVABIAttr(Attr.getRange(), S.Context,
3824 Attr.getAttributeSpellingListIndex()));
3825 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003826 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003827 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003828 switch (CC) {
3829 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003830 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003831 break;
3832 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003833 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003834 break;
3835 default:
3836 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003837 }
3838
Michael Han51d8c522013-01-24 16:46:58 +00003839 D->addAttr(::new (S.Context)
3840 PcsAttr(Attr.getRange(), S.Context, PCS,
3841 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003842 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003843 }
Derek Schuff263366f2012-10-16 22:30:41 +00003844 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003845 D->addAttr(::new (S.Context)
3846 PnaclCallAttr(Attr.getRange(), S.Context,
3847 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003848 return;
Guy Benyei38980082012-12-25 08:53:55 +00003849 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003850 D->addAttr(::new (S.Context)
3851 IntelOclBiccAttr(Attr.getRange(), S.Context,
3852 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003853 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003854
Abramo Bagnarae215f722010-04-30 13:10:51 +00003855 default:
3856 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003857 }
3858}
3859
Chandler Carruth1b03c872011-07-02 00:01:44 +00003860static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003861 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003862}
3863
Guy Benyei1db70402013-03-24 13:58:12 +00003864static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003865 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003866 llvm::APSInt ArgNum(32);
3867 if (E->isTypeDependent() || E->isValueDependent() ||
3868 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003869 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3870 << Attr.getName() << AANT_ArgumentIntegerConstant
3871 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003872 return;
3873 }
3874
3875 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3876 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3877}
3878
Aaron Ballmanfff32482012-12-09 17:45:41 +00003879bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3880 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003881 if (attr.isInvalid())
3882 return true;
3883
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003884 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003885 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003886 attr.setInvalid();
3887 return true;
3888 }
3889
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003890 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3891 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003892 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003893 case AttributeList::AT_CDecl: CC = CC_C; break;
3894 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3895 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3896 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3897 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003898 case AttributeList::AT_MSABI:
3899 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3900 CC_X86_64Win64;
3901 break;
3902 case AttributeList::AT_SysVABI:
3903 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3904 CC_C;
3905 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003906 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00003907 StringLiteral *Str = 0;
3908 if (attr.isArgExpr(0))
3909 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
3910
Douglas Gregor5cee1192011-07-27 05:40:30 +00003911 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003912 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
3913 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003914 attr.setInvalid();
3915 return true;
3916 }
3917
Chris Lattner5f9e2722011-07-23 10:55:15 +00003918 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003919 if (StrRef == "aapcs") {
3920 CC = CC_AAPCS;
3921 break;
3922 } else if (StrRef == "aapcs-vfp") {
3923 CC = CC_AAPCS_VFP;
3924 break;
3925 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003926
3927 attr.setInvalid();
3928 Diag(attr.getLoc(), diag::err_invalid_pcs);
3929 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003930 }
Derek Schuff263366f2012-10-16 22:30:41 +00003931 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003932 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003933 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003934 }
3935
Aaron Ballman82bfa192012-10-02 14:26:08 +00003936 const TargetInfo &TI = Context.getTargetInfo();
3937 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3938 if (A == TargetInfo::CCCR_Warning) {
3939 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003940
3941 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3942 if (FD)
3943 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3944 TargetInfo::CCMT_NonMember;
3945 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003946 }
3947
John McCall711c52b2011-01-05 12:14:39 +00003948 return false;
3949}
3950
Chandler Carruth1b03c872011-07-02 00:01:44 +00003951static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003952 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003953
3954 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003955 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003956 return;
3957
Chandler Carruth87c44602011-07-01 23:49:12 +00003958 if (!isa<ObjCMethodDecl>(D)) {
3959 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3960 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003961 return;
3962 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003963
Michael Han51d8c522013-01-24 16:46:58 +00003964 D->addAttr(::new (S.Context)
3965 RegparmAttr(Attr.getRange(), S.Context, numParams,
3966 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003967}
3968
3969/// Checks a regparm attribute, returning true if it is ill-formed and
3970/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003971bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3972 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003973 return true;
3974
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003975 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003976 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003977 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003978 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003979
Aaron Ballman624421f2013-08-31 01:11:41 +00003980 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003981 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003982 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003983 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003984 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3985 << Attr.getName() << AANT_ArgumentIntegerConstant
3986 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003987 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003988 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003989 }
3990
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003991 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003992 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003993 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003994 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003995 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003996 }
3997
John McCall711c52b2011-01-05 12:14:39 +00003998 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003999 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004000 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004001 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004002 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004003 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004004 }
4005
John McCall711c52b2011-01-05 12:14:39 +00004006 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004007}
4008
Chandler Carruth1b03c872011-07-02 00:01:44 +00004009static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004010 if (S.LangOpts.CUDA) {
4011 // check the attribute arguments.
4012 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004013 // FIXME: 0 is not okay.
4014 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004015 return;
4016 }
4017
Chandler Carruth87c44602011-07-01 23:49:12 +00004018 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004019 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004020 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004021 return;
4022 }
4023
Aaron Ballman624421f2013-08-31 01:11:41 +00004024 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004025 llvm::APSInt MaxThreads(32);
4026 if (MaxThreadsExpr->isTypeDependent() ||
4027 MaxThreadsExpr->isValueDependent() ||
4028 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004029 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004030 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004031 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004032 return;
4033 }
4034
4035 llvm::APSInt MinBlocks(32);
4036 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004037 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004038 if (MinBlocksExpr->isTypeDependent() ||
4039 MinBlocksExpr->isValueDependent() ||
4040 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004041 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004042 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004043 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004044 return;
4045 }
4046 }
4047
Michael Han51d8c522013-01-24 16:46:58 +00004048 D->addAttr(::new (S.Context)
4049 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4050 MaxThreads.getZExtValue(),
4051 MinBlocks.getZExtValue(),
4052 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004053 } else {
4054 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4055 }
4056}
4057
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004058static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4059 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004060 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004061 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004062 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004063 return;
4064 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004065
4066 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004067 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004068
Aaron Ballman624421f2013-08-31 01:11:41 +00004069 StringRef AttrName = Attr.getName()->getName();
4070 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004071
4072 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4073 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4074 << Attr.getName() << ExpectedFunctionOrMethod;
4075 return;
4076 }
4077
4078 uint64_t ArgumentIdx;
4079 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4080 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004081 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004082 return;
4083
4084 uint64_t TypeTagIdx;
4085 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4086 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004087 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004088 return;
4089
4090 bool IsPointer = (AttrName == "pointer_with_type_tag");
4091 if (IsPointer) {
4092 // Ensure that buffer has a pointer type.
4093 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4094 if (!BufferTy->isPointerType()) {
4095 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004096 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004097 }
4098 }
4099
Michael Han51d8c522013-01-24 16:46:58 +00004100 D->addAttr(::new (S.Context)
4101 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4102 ArgumentIdx, TypeTagIdx, IsPointer,
4103 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004104}
4105
4106static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4107 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004108 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004110 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004111 return;
4112 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004113
4114 if (!checkAttributeNumArgs(S, Attr, 1))
4115 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004116
Aaron Ballman624421f2013-08-31 01:11:41 +00004117 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004118 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4119
Michael Han51d8c522013-01-24 16:46:58 +00004120 D->addAttr(::new (S.Context)
4121 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4122 MatchingCType,
4123 Attr.getLayoutCompatible(),
4124 Attr.getMustBeNull(),
4125 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004126}
4127
Chris Lattner0744e5f2008-06-29 00:23:49 +00004128//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004129// Checker-specific attribute handlers.
4130//===----------------------------------------------------------------------===//
4131
John McCallc7ad3812011-01-25 03:31:58 +00004132static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004133 return type->isDependentType() ||
4134 type->isObjCObjectPointerType() ||
4135 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004136}
4137static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004138 return type->isDependentType() ||
4139 type->isPointerType() ||
4140 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004141}
4142
Chandler Carruth1b03c872011-07-02 00:01:44 +00004143static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004144 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004145 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004146 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004147 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004148 return;
4149 }
4150
4151 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004152 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004153 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4154 cf = false;
4155 } else {
4156 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4157 cf = true;
4158 }
4159
4160 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004161 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004162 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004163 return;
4164 }
4165
4166 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004167 param->addAttr(::new (S.Context)
4168 CFConsumedAttr(Attr.getRange(), S.Context,
4169 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004170 else
Michael Han51d8c522013-01-24 16:46:58 +00004171 param->addAttr(::new (S.Context)
4172 NSConsumedAttr(Attr.getRange(), S.Context,
4173 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004174}
4175
Chandler Carruth1b03c872011-07-02 00:01:44 +00004176static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4177 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004178 if (!isa<ObjCMethodDecl>(D)) {
4179 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004180 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004181 return;
4182 }
4183
Michael Han51d8c522013-01-24 16:46:58 +00004184 D->addAttr(::new (S.Context)
4185 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4186 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004187}
4188
Chandler Carruth1b03c872011-07-02 00:01:44 +00004189static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4190 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004191
John McCallc7ad3812011-01-25 03:31:58 +00004192 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004193
Chandler Carruth87c44602011-07-01 23:49:12 +00004194 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004195 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004196 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004197 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004198 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004199 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4200 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004201 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004202 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004203 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004204 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004205 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004206 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004207 return;
4208 }
Mike Stumpbf916502009-07-24 19:02:52 +00004209
John McCallc7ad3812011-01-25 03:31:58 +00004210 bool typeOK;
4211 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004212 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004213 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004214 case AttributeList::AT_NSReturnsAutoreleased:
4215 case AttributeList::AT_NSReturnsRetained:
4216 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004217 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4218 cf = false;
4219 break;
4220
Sean Hunt8e083e72012-06-19 23:57:03 +00004221 case AttributeList::AT_CFReturnsRetained:
4222 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004223 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4224 cf = true;
4225 break;
4226 }
4227
4228 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004229 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004230 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004231 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004232 }
Mike Stumpbf916502009-07-24 19:02:52 +00004233
Chandler Carruth87c44602011-07-01 23:49:12 +00004234 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004235 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004236 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004237 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004238 D->addAttr(::new (S.Context)
4239 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4240 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004241 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004242 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004243 D->addAttr(::new (S.Context)
4244 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4245 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004246 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004247 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004248 D->addAttr(::new (S.Context)
4249 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4250 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004251 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004252 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004253 D->addAttr(::new (S.Context)
4254 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4255 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004256 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004257 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004258 D->addAttr(::new (S.Context)
4259 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4260 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004261 return;
4262 };
4263}
4264
John McCalldc7c5ad2011-07-22 08:53:00 +00004265static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4266 const AttributeList &attr) {
4267 SourceLocation loc = attr.getLoc();
4268
4269 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4270
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004271 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004272 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004273 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004274 return;
4275 }
4276
4277 // Check that the method returns a normal pointer.
4278 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004279
4280 if (!resultType->isReferenceType() &&
4281 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004282 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4283 << SourceRange(loc)
4284 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4285
4286 // Drop the attribute.
4287 return;
4288 }
4289
Michael Han51d8c522013-01-24 16:46:58 +00004290 method->addAttr(::new (S.Context)
4291 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4292 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004293}
4294
Fariborz Jahanian84101132012-09-07 23:46:23 +00004295static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4296 const AttributeList &attr) {
4297 SourceLocation loc = attr.getLoc();
4298 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4299
4300 if (!method) {
4301 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4302 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4303 return;
4304 }
4305 DeclContext *DC = method->getDeclContext();
4306 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4307 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4308 << attr.getName() << 0;
4309 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4310 return;
4311 }
4312 if (method->getMethodFamily() == OMF_dealloc) {
4313 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4314 << attr.getName() << 1;
4315 return;
4316 }
4317
Michael Han51d8c522013-01-24 16:46:58 +00004318 method->addAttr(::new (S.Context)
4319 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4320 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004321}
4322
John McCall8dfac0b2011-09-30 05:12:12 +00004323/// Handle cf_audited_transfer and cf_unknown_transfer.
4324static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4325 if (!isa<FunctionDecl>(D)) {
4326 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004327 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004328 return;
4329 }
4330
Sean Hunt8e083e72012-06-19 23:57:03 +00004331 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004332
4333 // Check whether there's a conflicting attribute already present.
4334 Attr *Existing;
4335 if (IsAudited) {
4336 Existing = D->getAttr<CFUnknownTransferAttr>();
4337 } else {
4338 Existing = D->getAttr<CFAuditedTransferAttr>();
4339 }
4340 if (Existing) {
4341 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4342 << A.getName()
4343 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4344 << A.getRange() << Existing->getRange();
4345 return;
4346 }
4347
4348 // All clear; add the attribute.
4349 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004350 D->addAttr(::new (S.Context)
4351 CFAuditedTransferAttr(A.getRange(), S.Context,
4352 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004353 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004354 D->addAttr(::new (S.Context)
4355 CFUnknownTransferAttr(A.getRange(), S.Context,
4356 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004357 }
4358}
4359
John McCallfe98da02011-09-29 07:17:38 +00004360static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4361 const AttributeList &Attr) {
4362 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4363 if (!RD || RD->isUnion()) {
4364 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004365 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004366 }
4367
Aaron Ballman624421f2013-08-31 01:11:41 +00004368 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004369
4370 // In Objective-C, verify that the type names an Objective-C type.
4371 // We don't want to check this outside of ObjC because people sometimes
4372 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004373 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004374 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004375 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004376 Sema::LookupOrdinaryName);
4377 if (S.LookupName(R, Sc)) {
4378 NamedDecl *Target = R.getFoundDecl();
4379 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4380 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4381 S.Diag(Target->getLocStart(), diag::note_declared_at);
4382 }
4383 }
4384 }
4385
Michael Han51d8c522013-01-24 16:46:58 +00004386 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004387 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004388 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004389}
4390
Chandler Carruth1b03c872011-07-02 00:01:44 +00004391static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4392 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004393 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004394
Chandler Carruth87c44602011-07-01 23:49:12 +00004395 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004396 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004397}
4398
Chandler Carruth1b03c872011-07-02 00:01:44 +00004399static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4400 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004401 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004402 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004403 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004404 return;
4405 }
4406
Chandler Carruth87c44602011-07-01 23:49:12 +00004407 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004408 QualType type = vd->getType();
4409
4410 if (!type->isDependentType() &&
4411 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004412 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004413 << type;
4414 return;
4415 }
4416
4417 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4418
4419 // If we have no lifetime yet, check the lifetime we're presumably
4420 // going to infer.
4421 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4422 lifetime = type->getObjCARCImplicitLifetime();
4423
4424 switch (lifetime) {
4425 case Qualifiers::OCL_None:
4426 assert(type->isDependentType() &&
4427 "didn't infer lifetime for non-dependent type?");
4428 break;
4429
4430 case Qualifiers::OCL_Weak: // meaningful
4431 case Qualifiers::OCL_Strong: // meaningful
4432 break;
4433
4434 case Qualifiers::OCL_ExplicitNone:
4435 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004436 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004437 << (lifetime == Qualifiers::OCL_Autoreleasing);
4438 break;
4439 }
4440
Chandler Carruth87c44602011-07-01 23:49:12 +00004441 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004442 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4443 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004444}
4445
Francois Pichet11542142010-12-19 06:50:37 +00004446//===----------------------------------------------------------------------===//
4447// Microsoft specific attribute handlers.
4448//===----------------------------------------------------------------------===//
4449
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004450// Check if MS extensions or some other language extensions are enabled. If
4451// not, issue a diagnostic that the given attribute is unused.
4452static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4453 bool OtherExtension = false) {
4454 if (S.LangOpts.MicrosoftExt || OtherExtension)
4455 return true;
4456 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4457 return false;
4458}
4459
Chandler Carruth1b03c872011-07-02 00:01:44 +00004460static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004461 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4462 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004463
Aaron Ballman624421f2013-08-31 01:11:41 +00004464 Expr *Arg = Attr.getArgAsExpr(0);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004465 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4466 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4468 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004469 return;
4470 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004471
David Majnemerbb0ed292013-08-09 08:56:20 +00004472 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4473 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004474 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004475 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4476 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004477
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004478 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004479 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004480 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4481 return;
4482 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004483
David Majnemerbb0ed292013-08-09 08:56:20 +00004484 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004485 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004486 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004487 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4488 return;
4489 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004490 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004491 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4492 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004493 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004494 }
Francois Pichet11542142010-12-19 06:50:37 +00004495
David Majnemerbb0ed292013-08-09 08:56:20 +00004496 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4497 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004498}
4499
John McCallc052dbb2012-05-22 21:28:12 +00004500static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004501 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004502 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004503
4504 AttributeList::Kind Kind = Attr.getKind();
4505 if (Kind == AttributeList::AT_SingleInheritance)
4506 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004507 ::new (S.Context)
4508 SingleInheritanceAttr(Attr.getRange(), S.Context,
4509 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004510 else if (Kind == AttributeList::AT_MultipleInheritance)
4511 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004512 ::new (S.Context)
4513 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4514 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004515 else if (Kind == AttributeList::AT_VirtualInheritance)
4516 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004517 ::new (S.Context)
4518 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4519 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004520}
4521
4522static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004523 if (!checkMicrosoftExt(S, Attr))
4524 return;
4525
4526 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004527 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004528 D->addAttr(
4529 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4530 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004531}
4532
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004533static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004534 if (!checkMicrosoftExt(S, Attr))
4535 return;
4536 D->addAttr(::new (S.Context)
4537 ForceInlineAttr(Attr.getRange(), S.Context,
4538 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004539}
4540
Reid Klecknera7225342013-05-20 14:02:37 +00004541static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4542 if (!checkMicrosoftExt(S, Attr))
4543 return;
4544 // Check linkage after possibly merging declaratinos. See
4545 // checkAttributesAfterMerging().
4546 D->addAttr(::new (S.Context)
4547 SelectAnyAttr(Attr.getRange(), S.Context,
4548 Attr.getAttributeSpellingListIndex()));
4549}
4550
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004551/// Handles semantic checking for features that are common to all attributes,
4552/// such as checking whether a parameter was properly specified, or the correct
4553/// number of arguments were passed, etc.
4554static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4555 const AttributeList &Attr) {
4556 // Several attributes carry different semantics than the parsing requires, so
4557 // those are opted out of the common handling.
4558 //
4559 // We also bail on unknown and ignored attributes because those are handled
4560 // as part of the target-specific handling logic.
4561 if (Attr.hasCustomParsing() ||
4562 Attr.getKind() == AttributeList::UnknownAttribute ||
4563 Attr.getKind() == AttributeList::IgnoredAttribute)
4564 return false;
4565
4566 // If there are no optional arguments, then checking for the argument count
4567 // is trivial.
4568 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4569 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4570 return true;
4571 return false;
4572}
4573
Ted Kremenekb71368d2009-05-09 02:44:38 +00004574//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004575// Top Level Sema Entry Points
4576//===----------------------------------------------------------------------===//
4577
Richard Smith4a97b8e2013-08-29 00:47:48 +00004578/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4579/// the attribute applies to decls. If the attribute is a type attribute, just
4580/// silently ignore it if a GNU attribute.
4581static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4582 const AttributeList &Attr,
4583 bool IncludeCXX11Attributes) {
4584 if (Attr.isInvalid())
4585 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004586
Richard Smith4a97b8e2013-08-29 00:47:48 +00004587 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4588 // instead.
4589 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4590 return;
4591
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004592 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4593 return;
4594
Chris Lattner803d0802008-06-29 00:43:07 +00004595 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004596 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4597 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4598 case AttributeList::AT_IBOutletCollection:
4599 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004600 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004601 case AttributeList::AT_ObjCGC:
4602 case AttributeList::AT_VectorSize:
4603 case AttributeList::AT_NeonVectorType:
4604 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004605 case AttributeList::AT_Ptr32:
4606 case AttributeList::AT_Ptr64:
4607 case AttributeList::AT_SPtr:
4608 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004609 // Ignore these, these are type attributes, handled by
4610 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004611 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004612 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4613 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4614 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4615 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004616 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004617 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004618 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004619 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004620 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4621 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4622 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004623 handleDependencyAttr(S, scope, D, Attr);
4624 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004625 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4626 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4627 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004628 case AttributeList::AT_CXX11NoReturn:
4629 handleCXX11NoReturnAttr(S, D, Attr);
4630 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004631 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004632 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004633 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004634 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4635 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004636 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004637 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004638 case AttributeList::AT_MinSize:
4639 handleMinSizeAttr(S, D, Attr);
4640 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004641 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4642 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4643 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004644 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4645 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004646 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4647 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004648 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004649 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004650 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4651 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004652 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004653 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4654 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004655 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004656 case AttributeList::AT_ownership_returns:
4657 case AttributeList::AT_ownership_takes:
4658 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004659 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004660 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4661 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4662 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4663 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4664 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4665 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4666 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004667
Sean Hunt8e083e72012-06-19 23:57:03 +00004668 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004669 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004670 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004671 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004672
Sean Hunt8e083e72012-06-19 23:57:03 +00004673 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004674 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4675
Fariborz Jahanian84101132012-09-07 23:46:23 +00004676 case AttributeList::AT_ObjCRequiresSuper:
4677 handleObjCRequiresSuperAttr(S, D, Attr); break;
4678
Sean Hunt8e083e72012-06-19 23:57:03 +00004679 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004680 handleNSBridgedAttr(S, scope, D, Attr); break;
4681
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_CFAuditedTransfer:
4683 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004684 handleCFTransferAttr(S, D, Attr); break;
4685
Ted Kremenekb71368d2009-05-09 02:44:38 +00004686 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004687 case AttributeList::AT_CFConsumed:
4688 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4689 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004690 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004691
Sean Hunt8e083e72012-06-19 23:57:03 +00004692 case AttributeList::AT_NSReturnsAutoreleased:
4693 case AttributeList::AT_NSReturnsNotRetained:
4694 case AttributeList::AT_CFReturnsNotRetained:
4695 case AttributeList::AT_NSReturnsRetained:
4696 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004697 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004698
Tanya Lattner0df579e2012-07-09 22:06:01 +00004699 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004700 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004701 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004702
Joey Gouly37453b92013-03-08 09:42:32 +00004703 case AttributeList::AT_VecTypeHint:
4704 handleVecTypeHint(S, D, Attr); break;
4705
Sean Hunt8e083e72012-06-19 23:57:03 +00004706 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004707 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004708
Sean Hunt8e083e72012-06-19 23:57:03 +00004709 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4710 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4711 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004712 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004713 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004714 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004715 handleArcWeakrefUnavailableAttr (S, D, Attr);
4716 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004717 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004718 handleObjCRootClassAttr(S, D, Attr);
4719 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004720 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004721 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004722 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004723 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4724 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004725 handleReturnsTwiceAttr(S, D, Attr);
4726 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004727 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004728 case AttributeList::AT_Visibility:
4729 handleVisibilityAttr(S, D, Attr, false);
4730 break;
4731 case AttributeList::AT_TypeVisibility:
4732 handleVisibilityAttr(S, D, Attr, true);
4733 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004734 case AttributeList::AT_WarnUnused:
4735 handleWarnUnusedAttr(S, D, Attr);
4736 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004737 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004738 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004739 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4740 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4741 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4742 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004743 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004744 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004746 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004747 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004748 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004749 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004750 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004751 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4752 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4753 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4754 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4755 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4756 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4757 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4758 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4759 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004760 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004761 // Just ignore
4762 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004763 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004764 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004765 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004766 case AttributeList::AT_StdCall:
4767 case AttributeList::AT_CDecl:
4768 case AttributeList::AT_FastCall:
4769 case AttributeList::AT_ThisCall:
4770 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004771 case AttributeList::AT_MSABI:
4772 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004773 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004774 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004775 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004776 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004777 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004778 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004779 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004780 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004781 case AttributeList::AT_OpenCLImageAccess:
4782 handleOpenCLImageAccessAttr(S, D, Attr);
4783 break;
John McCallc052dbb2012-05-22 21:28:12 +00004784
4785 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004786 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004787 handleMsStructAttr(S, D, Attr);
4788 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004790 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004791 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004792 case AttributeList::AT_SingleInheritance:
4793 case AttributeList::AT_MultipleInheritance:
4794 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004795 handleInheritanceAttr(S, D, Attr);
4796 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004798 handlePortabilityAttr(S, D, Attr);
4799 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004800 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004801 handleForceInlineAttr(S, D, Attr);
4802 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004803 case AttributeList::AT_SelectAny:
4804 handleSelectAnyAttr(S, D, Attr);
4805 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004806
4807 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004808 case AttributeList::AT_AssertExclusiveLock:
4809 handleAssertExclusiveLockAttr(S, D, Attr);
4810 break;
4811 case AttributeList::AT_AssertSharedLock:
4812 handleAssertSharedLockAttr(S, D, Attr);
4813 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004815 handleGuardedVarAttr(S, D, Attr);
4816 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004818 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004819 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004820 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004821 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004822 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004823 case AttributeList::AT_NoSanitizeAddress:
4824 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004827 handleNoThreadSafetyAnalysis(S, D, Attr);
4828 break;
4829 case AttributeList::AT_NoSanitizeThread:
4830 handleNoSanitizeThread(S, D, Attr);
4831 break;
4832 case AttributeList::AT_NoSanitizeMemory:
4833 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004834 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004835 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004836 handleLockableAttr(S, D, Attr);
4837 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004838 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004839 handleGuardedByAttr(S, D, Attr);
4840 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004841 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004842 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004843 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004845 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004846 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004848 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004849 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004851 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004852 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004853 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004854 handleLockReturnedAttr(S, D, Attr);
4855 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004857 handleLocksExcludedAttr(S, D, Attr);
4858 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004859 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004860 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004861 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004862 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004863 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004864 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004865 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004866 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004867 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004869 handleUnlockFunAttr(S, D, Attr);
4870 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004871 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004872 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004873 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004875 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004876 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004877
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004878 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004879 case AttributeList::AT_Consumable:
4880 handleConsumableAttr(S, D, Attr);
4881 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004882 case AttributeList::AT_Consumes:
4883 handleConsumesAttr(S, D, Attr);
4884 break;
4885 case AttributeList::AT_CallableWhenUnconsumed:
4886 handleCallableWhenUnconsumedAttr(S, D, Attr);
4887 break;
4888 case AttributeList::AT_TestsConsumed:
4889 handleTestsConsumedAttr(S, D, Attr);
4890 break;
4891 case AttributeList::AT_TestsUnconsumed:
4892 handleTestsUnconsumedAttr(S, D, Attr);
4893 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004894 case AttributeList::AT_ReturnTypestate:
4895 handleReturnTypestateAttr(S, D, Attr);
4896 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004897
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004898 // Type safety attributes.
4899 case AttributeList::AT_ArgumentWithTypeTag:
4900 handleArgumentWithTypeTagAttr(S, D, Attr);
4901 break;
4902 case AttributeList::AT_TypeTagForDatatype:
4903 handleTypeTagForDatatypeAttr(S, D, Attr);
4904 break;
4905
Chris Lattner803d0802008-06-29 00:43:07 +00004906 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004907 // Ask target about the attribute.
4908 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4909 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004910 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4911 diag::warn_unhandled_ms_attribute_ignored :
4912 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004913 break;
4914 }
4915}
4916
4917/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4918/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004919void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004920 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004921 bool IncludeCXX11Attributes) {
4922 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004923 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004924
4925 // GCC accepts
4926 // static int a9 __attribute__((weakref));
4927 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004928 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004929 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004930 cast<NamedDecl>(D)->getNameAsString();
4931 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004932 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004933 }
4934}
4935
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004936// Annotation attributes are the only attributes allowed after an access
4937// specifier.
4938bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4939 const AttributeList *AttrList) {
4940 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004941 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004942 handleAnnotateAttr(*this, ASDecl, *l);
4943 } else {
4944 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4945 return true;
4946 }
4947 }
4948
4949 return false;
4950}
4951
John McCalle82247a2011-10-01 05:17:03 +00004952/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4953/// contains any decl attributes that we should warn about.
4954static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4955 for ( ; A; A = A->getNext()) {
4956 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004957 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004958 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4959
4960 if (A->getKind() == AttributeList::UnknownAttribute) {
4961 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4962 << A->getName() << A->getRange();
4963 } else {
4964 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4965 << A->getName() << A->getRange();
4966 }
4967 }
4968}
4969
4970/// checkUnusedDeclAttributes - Given a declarator which is not being
4971/// used to build a declaration, complain about any decl attributes
4972/// which might be lying around on it.
4973void Sema::checkUnusedDeclAttributes(Declarator &D) {
4974 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4975 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4976 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4977 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4978}
4979
Ryan Flynne25ff832009-07-30 03:15:39 +00004980/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004981/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004982NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4983 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004984 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004985 NamedDecl *NewD = 0;
4986 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004987 FunctionDecl *NewFD;
4988 // FIXME: Missing call to CheckFunctionDeclaration().
4989 // FIXME: Mangling?
4990 // FIXME: Is the qualifier info correct?
4991 // FIXME: Is the DeclContext correct?
4992 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4993 Loc, Loc, DeclarationName(II),
4994 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004995 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004996 FD->hasPrototype(),
4997 false/*isConstexprSpecified*/);
4998 NewD = NewFD;
4999
5000 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005001 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005002
5003 // Fake up parameter variables; they are declared as if this were
5004 // a typedef.
5005 QualType FDTy = FD->getType();
5006 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5007 SmallVector<ParmVarDecl*, 16> Params;
5008 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5009 AE = FT->arg_type_end(); AI != AE; ++AI) {
5010 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5011 Param->setScopeInfo(0, Params.size());
5012 Params.push_back(Param);
5013 }
David Blaikie4278c652011-09-21 18:16:56 +00005014 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005015 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005016 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5017 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005018 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005019 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005020 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005021 if (VD->getQualifier()) {
5022 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005023 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005024 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005025 }
5026 return NewD;
5027}
5028
James Dennett1dfbd922012-06-14 21:40:34 +00005029/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005030/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005031void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005032 if (W.getUsed()) return; // only do this once
5033 W.setUsed(true);
5034 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5035 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005036 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005037 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5038 NDId->getName()));
5039 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005040 WeakTopLevelDecl.push_back(NewD);
5041 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5042 // to insert Decl at TU scope, sorry.
5043 DeclContext *SavedContext = CurContext;
5044 CurContext = Context.getTranslationUnitDecl();
5045 PushOnScopeChains(NewD, S);
5046 CurContext = SavedContext;
5047 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005048 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005049 }
5050}
5051
Rafael Espindola65611bf2013-03-02 21:41:48 +00005052void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5053 // It's valid to "forward-declare" #pragma weak, in which case we
5054 // have to do this.
5055 LoadExternalWeakUndeclaredIdentifiers();
5056 if (!WeakUndeclaredIdentifiers.empty()) {
5057 NamedDecl *ND = NULL;
5058 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5059 if (VD->isExternC())
5060 ND = VD;
5061 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5062 if (FD->isExternC())
5063 ND = FD;
5064 if (ND) {
5065 if (IdentifierInfo *Id = ND->getIdentifier()) {
5066 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5067 = WeakUndeclaredIdentifiers.find(Id);
5068 if (I != WeakUndeclaredIdentifiers.end()) {
5069 WeakInfo W = I->second;
5070 DeclApplyPragmaWeak(S, ND, W);
5071 WeakUndeclaredIdentifiers[Id] = W;
5072 }
5073 }
5074 }
5075 }
5076}
5077
Chris Lattner0744e5f2008-06-29 00:23:49 +00005078/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5079/// it, apply them to D. This is a bit tricky because PD can have attributes
5080/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005081void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005082 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005083 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005084 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005085
Chris Lattner0744e5f2008-06-29 00:23:49 +00005086 // Walk the declarator structure, applying decl attributes that were in a type
5087 // position to the decl itself. This handles cases like:
5088 // int *__attr__(x)** D;
5089 // when X is a decl attribute.
5090 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5091 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005092 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005093
Chris Lattner0744e5f2008-06-29 00:23:49 +00005094 // Finally, apply any attributes on the decl itself.
5095 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005096 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005097}
John McCall54abf7d2009-11-04 02:18:39 +00005098
John McCallf85e1932011-06-15 23:02:42 +00005099/// Is the given declaration allowed to use a forbidden type?
5100static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5101 // Private ivars are always okay. Unfortunately, people don't
5102 // always properly make their ivars private, even in system headers.
5103 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005104 // Function declarations in sys headers will be marked unavailable.
5105 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5106 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005107 return false;
5108
5109 // Require it to be declared in a system header.
5110 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5111}
5112
5113/// Handle a delayed forbidden-type diagnostic.
5114static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5115 Decl *decl) {
5116 if (decl && isForbiddenTypeAllowed(S, decl)) {
5117 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5118 "this system declaration uses an unsupported type"));
5119 return;
5120 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005121 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005122 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005123 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005124 // kind of forbidden type messages on unavailable functions.
5125 if (FD->hasAttr<UnavailableAttr>() &&
5126 diag.getForbiddenTypeDiagnostic() ==
5127 diag::err_arc_array_param_no_ownership) {
5128 diag.Triggered = true;
5129 return;
5130 }
5131 }
John McCallf85e1932011-06-15 23:02:42 +00005132
5133 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5134 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5135 diag.Triggered = true;
5136}
5137
John McCall92576642012-05-07 06:16:41 +00005138void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5139 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005140 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005141 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005142
John McCall92576642012-05-07 06:16:41 +00005143 // When delaying diagnostics to run in the context of a parsed
5144 // declaration, we only want to actually emit anything if parsing
5145 // succeeds.
5146 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005147
John McCall92576642012-05-07 06:16:41 +00005148 // We emit all the active diagnostics in this pool or any of its
5149 // parents. In general, we'll get one pool for the decl spec
5150 // and a child pool for each declarator; in a decl group like:
5151 // deprecated_typedef foo, *bar, baz();
5152 // only the declarator pops will be passed decls. This is correct;
5153 // we really do need to consider delayed diagnostics from the decl spec
5154 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005155 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005156 do {
John McCall13489672012-05-07 06:16:58 +00005157 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005158 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5159 // This const_cast is a bit lame. Really, Triggered should be mutable.
5160 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005161 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005162 continue;
5163
John McCalleee1d542011-02-14 07:13:47 +00005164 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005165 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005166 // Don't bother giving deprecation diagnostics if the decl is invalid.
5167 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005168 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005169 break;
5170
5171 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005172 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005173 break;
John McCallf85e1932011-06-15 23:02:42 +00005174
5175 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005176 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005177 break;
John McCall2f514482010-01-27 03:50:35 +00005178 }
5179 }
John McCall92576642012-05-07 06:16:41 +00005180 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005181}
5182
John McCall13489672012-05-07 06:16:58 +00005183/// Given a set of delayed diagnostics, re-emit them as if they had
5184/// been delayed in the current context instead of in the given pool.
5185/// Essentially, this just moves them to the current pool.
5186void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5187 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5188 assert(curPool && "re-emitting in undelayed context not supported");
5189 curPool->steal(pool);
5190}
5191
John McCall54abf7d2009-11-04 02:18:39 +00005192static bool isDeclDeprecated(Decl *D) {
5193 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005194 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005195 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005196 // A category implicitly has the availability of the interface.
5197 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5198 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005199 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5200 return false;
5201}
5202
Eli Friedmanc3b23082012-08-08 21:52:41 +00005203static void
5204DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5205 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005206 const ObjCInterfaceDecl *UnknownObjCClass,
5207 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005208 DeclarationName Name = D->getDeclName();
5209 if (!Message.empty()) {
5210 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5211 S.Diag(D->getLocation(),
5212 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5213 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005214 if (ObjCPropery)
5215 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5216 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005217 } else if (!UnknownObjCClass) {
5218 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5219 S.Diag(D->getLocation(),
5220 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5221 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005222 if (ObjCPropery)
5223 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5224 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005225 } else {
5226 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5227 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5228 }
5229}
5230
John McCall9c3087b2010-08-26 02:13:20 +00005231void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005232 Decl *Ctx) {
5233 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005234 return;
5235
John McCall2f514482010-01-27 03:50:35 +00005236 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005237 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5238 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005239 DD.getUnknownObjCClass(),
5240 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005241}
5242
Chris Lattner5f9e2722011-07-23 10:55:15 +00005243void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005244 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005245 const ObjCInterfaceDecl *UnknownObjCClass,
5246 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005247 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005248 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005249 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5250 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005251 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005252 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005253 return;
5254 }
5255
5256 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005257 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005258 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005259 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005260}