blob: 2bf56c737a2f46dbd5f44a2dba70e9c9d2c4b3f1 [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) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000422 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000423
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000424 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000425 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000426 Args.push_back(ArgExp);
427 continue;
428 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000429
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000430 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000431 if (StrLit->getLength() == 0 ||
432 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000433 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000434 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000435 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000437 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000438
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000439 // We allow constant strings to be used as a placeholder for expressions
440 // that are not valid C++ syntax, but warn that they are ignored.
441 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
442 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000443 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000444 continue;
445 }
446
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000448
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000449 // A pointer to member expression of the form &MyClass::mu is treated
450 // specially -- we need to look at the type of the member.
451 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
452 if (UOp->getOpcode() == UO_AddrOf)
453 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
454 if (DRE->getDecl()->isCXXInstanceMember())
455 ArgTy = DRE->getDecl()->getType();
456
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000457 // First see if we can just cast to record type, or point to record type.
458 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000460 // Now check if we index into a record type function param.
461 if(!RT && ParamIdxOk) {
462 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000463 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
464 if(FD && IL) {
465 unsigned int NumParams = FD->getNumParams();
466 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
468 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
469 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
471 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000472 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000473 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000474 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000475 }
476 }
477
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000478 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000479
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000480 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000481 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482}
483
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000484//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000485// Attribute Implementations
486//===----------------------------------------------------------------------===//
487
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000488// FIXME: All this manual attribute parsing code is gross. At the
489// least add some helper functions to check most argument patterns (#
490// and types of args).
491
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000492enum ThreadAttributeDeclKind {
493 ThreadExpectedFieldOrGlobalVar,
494 ThreadExpectedFunctionOrMethod,
495 ThreadExpectedClassOrStruct
496};
497
Michael Hanf1aae3b2012-08-03 17:40:43 +0000498static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000499 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000501 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000502
503 // D must be either a member field or global (potentially shared) variable.
504 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000505 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
506 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000507 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000508 }
509
Michael Handc691572012-07-23 18:48:41 +0000510 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000511}
512
Michael Handc691572012-07-23 18:48:41 +0000513static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
514 if (!checkGuardedVarAttrCommon(S, D, Attr))
515 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000516
Michael Han51d8c522013-01-24 16:46:58 +0000517 D->addAttr(::new (S.Context)
518 GuardedVarAttr(Attr.getRange(), S.Context,
519 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000520}
521
Michael Hanf1aae3b2012-08-03 17:40:43 +0000522static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000523 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000524 if (!checkGuardedVarAttrCommon(S, D, Attr))
525 return;
526
527 if (!threadSafetyCheckIsPointer(S, D, Attr))
528 return;
529
Michael Han51d8c522013-01-24 16:46:58 +0000530 D->addAttr(::new (S.Context)
531 PtGuardedVarAttr(Attr.getRange(), S.Context,
532 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000533}
534
Michael Hanf1aae3b2012-08-03 17:40:43 +0000535static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
536 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000537 Expr* &Arg) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000538 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000539 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540
541 // D must be either a member field or global (potentially shared) variable.
542 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000543 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
544 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000545 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000546 }
547
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000548 SmallVector<Expr*, 1> Args;
549 // check that all arguments are lockable objects
550 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
551 unsigned Size = Args.size();
552 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000553 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000554
Michael Handc691572012-07-23 18:48:41 +0000555 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000556
Michael Handc691572012-07-23 18:48:41 +0000557 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000558}
559
Michael Handc691572012-07-23 18:48:41 +0000560static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
561 Expr *Arg = 0;
562 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
563 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000564
Michael Handc691572012-07-23 18:48:41 +0000565 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
570 Expr *Arg = 0;
571 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
572 return;
573
574 if (!threadSafetyCheckIsPointer(S, D, Attr))
575 return;
576
577 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
578 S.Context, Arg));
579}
580
Michael Hanf1aae3b2012-08-03 17:40:43 +0000581static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000582 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000583 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000584 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000585
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000586 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000587 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000588 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
589 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000591 }
592
Michael Handc691572012-07-23 18:48:41 +0000593 return true;
594}
595
596static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
597 if (!checkLockableAttrCommon(S, D, Attr))
598 return;
599
600 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
601}
602
Michael Hanf1aae3b2012-08-03 17:40:43 +0000603static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000604 const AttributeList &Attr) {
605 if (!checkLockableAttrCommon(S, D, Attr))
606 return;
607
Michael Han51d8c522013-01-24 16:46:58 +0000608 D->addAttr(::new (S.Context)
609 ScopedLockableAttr(Attr.getRange(), S.Context,
610 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000611}
612
Kostya Serebryany85aee962013-02-26 06:58:27 +0000613static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
614 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000615 if (!checkAttributeNumArgs(S, Attr, 0))
616 return;
617
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000618 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000619 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
620 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000621 return;
622 }
623
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000624 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000625 S.Context));
626}
627
Kostya Serebryany85aee962013-02-26 06:58:27 +0000628static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000629 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000630 if (!checkAttributeNumArgs(S, Attr, 0))
631 return;
632
633 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000635 << Attr.getName() << ExpectedFunctionOrMethod;
636 return;
637 }
638
Michael Han51d8c522013-01-24 16:46:58 +0000639 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000640 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
641 Attr.getAttributeSpellingListIndex()));
642}
643
644static void handleNoSanitizeMemory(Sema &S, Decl *D,
645 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000646 if (!checkAttributeNumArgs(S, Attr, 0))
647 return;
648
649 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
651 << Attr.getName() << ExpectedFunctionOrMethod;
652 return;
653 }
654
655 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
656 S.Context));
657}
658
659static void handleNoSanitizeThread(Sema &S, Decl *D,
660 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000661 if (!checkAttributeNumArgs(S, Attr, 0))
662 return;
663
664 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
665 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
666 << Attr.getName() << ExpectedFunctionOrMethod;
667 return;
668 }
669
670 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
671 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000672}
673
Michael Hanf1aae3b2012-08-03 17:40:43 +0000674static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
675 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000676 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000677 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000678 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000679
680 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000681 ValueDecl *VD = dyn_cast<ValueDecl>(D);
682 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000683 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
684 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000685 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000686 }
687
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000688 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000689 QualType QT = VD->getType();
690 if (!QT->isDependentType()) {
691 const RecordType *RT = getRecordType(QT);
692 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000693 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000694 << Attr.getName();
695 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000696 }
697 }
698
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000699 // Check that all arguments are lockable objects.
700 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000701 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000703
Michael Handc691572012-07-23 18:48:41 +0000704 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000705}
706
Michael Hanf1aae3b2012-08-03 17:40:43 +0000707static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000708 const AttributeList &Attr) {
709 SmallVector<Expr*, 1> Args;
710 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
711 return;
712
713 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000714 D->addAttr(::new (S.Context)
715 AcquiredAfterAttr(Attr.getRange(), S.Context,
716 StartArg, Args.size(),
717 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000718}
719
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000721 const AttributeList &Attr) {
722 SmallVector<Expr*, 1> Args;
723 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
724 return;
725
726 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000727 D->addAttr(::new (S.Context)
728 AcquiredBeforeAttr(Attr.getRange(), S.Context,
729 StartArg, Args.size(),
730 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000731}
732
Michael Hanf1aae3b2012-08-03 17:40:43 +0000733static bool checkLockFunAttrCommon(Sema &S, Decl *D,
734 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000735 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000736 // zero or more arguments ok
737
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000738 // check that the attribute is applied to a function
739 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000740 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
741 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000742 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000743 }
744
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000745 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000746 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000747
Michael Handc691572012-07-23 18:48:41 +0000748 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000749}
750
Michael Hanf1aae3b2012-08-03 17:40:43 +0000751static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000752 const AttributeList &Attr) {
753 SmallVector<Expr*, 1> Args;
754 if (!checkLockFunAttrCommon(S, D, Attr, Args))
755 return;
756
757 unsigned Size = Args.size();
758 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000759 D->addAttr(::new (S.Context)
760 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
761 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000762}
763
Michael Hanf1aae3b2012-08-03 17:40:43 +0000764static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000765 const AttributeList &Attr) {
766 SmallVector<Expr*, 1> Args;
767 if (!checkLockFunAttrCommon(S, D, Attr, Args))
768 return;
769
770 unsigned Size = Args.size();
771 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000772 D->addAttr(::new (S.Context)
773 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
774 StartArg, Size,
775 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000776}
777
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000778static void handleAssertSharedLockAttr(Sema &S, Decl *D,
779 const AttributeList &Attr) {
780 SmallVector<Expr*, 1> Args;
781 if (!checkLockFunAttrCommon(S, D, Attr, Args))
782 return;
783
784 unsigned Size = Args.size();
785 Expr **StartArg = Size == 0 ? 0 : &Args[0];
786 D->addAttr(::new (S.Context)
787 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
789}
790
791static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
792 const AttributeList &Attr) {
793 SmallVector<Expr*, 1> Args;
794 if (!checkLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
799 D->addAttr(::new (S.Context)
800 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
801 StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
803}
804
805
Michael Hanf1aae3b2012-08-03 17:40:43 +0000806static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
807 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000808 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000809 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000810 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000811
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000812 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000813 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
814 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000815 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000816 }
817
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000818 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000819 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000820 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000821 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000822 }
823
824 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000825 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000826
Michael Handc691572012-07-23 18:48:41 +0000827 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000828}
829
Michael Hanf1aae3b2012-08-03 17:40:43 +0000830static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000831 const AttributeList &Attr) {
832 SmallVector<Expr*, 2> Args;
833 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
834 return;
835
836 unsigned Size = Args.size();
837 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000838 D->addAttr(::new (S.Context)
839 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
840 Attr.getArg(0), StartArg, Size,
841 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000842}
843
Michael Hanf1aae3b2012-08-03 17:40:43 +0000844static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000845 const AttributeList &Attr) {
846 SmallVector<Expr*, 2> Args;
847 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
848 return;
849
850 unsigned Size = Args.size();
851 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000852 D->addAttr(::new (S.Context)
853 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
854 Attr.getArg(0), StartArg, Size,
855 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000856}
857
Michael Hanf1aae3b2012-08-03 17:40:43 +0000858static bool checkLocksRequiredCommon(Sema &S, Decl *D,
859 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000860 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000861 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000862 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000863
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000864 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000865 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
866 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000867 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000868 }
869
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000870 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000871 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000872 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000873 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000874
Michael Handc691572012-07-23 18:48:41 +0000875 return true;
876}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000877
Michael Hanf1aae3b2012-08-03 17:40:43 +0000878static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000879 const AttributeList &Attr) {
880 SmallVector<Expr*, 1> Args;
881 if (!checkLocksRequiredCommon(S, D, Attr, Args))
882 return;
883
884 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000885 D->addAttr(::new (S.Context)
886 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
887 StartArg, Args.size(),
888 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000889}
890
Michael Hanf1aae3b2012-08-03 17:40:43 +0000891static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000892 const AttributeList &Attr) {
893 SmallVector<Expr*, 1> Args;
894 if (!checkLocksRequiredCommon(S, D, Attr, Args))
895 return;
896
897 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000898 D->addAttr(::new (S.Context)
899 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
900 StartArg, Args.size(),
901 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000902}
903
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000904static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000906 // zero or more arguments ok
907
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000908 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000909 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
910 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000911 return;
912 }
913
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000914 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000915 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000916 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000917 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000918 Expr **StartArg = Size == 0 ? 0 : &Args[0];
919
Michael Han51d8c522013-01-24 16:46:58 +0000920 D->addAttr(::new (S.Context)
921 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
922 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000923}
924
925static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000926 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000927 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000928 return;
929
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000930 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000931 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
932 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000933 return;
934 }
935
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000936 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000937 SmallVector<Expr*, 1> Args;
938 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
939 unsigned Size = Args.size();
940 if (Size == 0)
941 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000942
Michael Han51d8c522013-01-24 16:46:58 +0000943 D->addAttr(::new (S.Context)
944 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
945 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000946}
947
948static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000949 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000950 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000951 return;
952
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000953 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000954 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
955 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000956 return;
957 }
958
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000959 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000960 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000961 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000962 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000963 if (Size == 0)
964 return;
965 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000966
Michael Han51d8c522013-01-24 16:46:58 +0000967 D->addAttr(::new (S.Context)
968 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
969 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000970}
971
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000972static void handleConsumesAttr(Sema &S, Decl *D,
973 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000974 if (!checkAttributeNumArgs(S, Attr, 0)) return;
975
976 if (!(isa<CXXMethodDecl>(D) || isa<CXXConstructorDecl>(D))) {
Aaron Ballman34776d42013-08-21 22:07:20 +0000977 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
978 << Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000979 return;
980 }
981
982 D->addAttr(::new (S.Context)
983 ConsumesAttr(Attr.getRange(), S.Context,
984 Attr.getAttributeSpellingListIndex()));
985}
986
987static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
988 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000989 if (!checkAttributeNumArgs(S, Attr, 0)) return;
990
991 if (!isa<CXXMethodDecl>(D)) {
Aaron Ballman34776d42013-08-21 22:07:20 +0000992 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
993 << Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000994 return;
995 }
996
997 D->addAttr(::new (S.Context)
998 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
999 Attr.getAttributeSpellingListIndex()));
1000}
1001
1002static void handleTestsConsumedAttr(Sema &S, Decl *D,
1003 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001004 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1005
1006 if (!isa<CXXMethodDecl>(D)) {
Aaron Ballman34776d42013-08-21 22:07:20 +00001007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1008 << Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001009 return;
1010 }
1011
1012 D->addAttr(::new (S.Context)
1013 TestsConsumedAttr(Attr.getRange(), S.Context,
1014 Attr.getAttributeSpellingListIndex()));
1015}
1016
1017static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1018 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001019 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1020
1021 if (!isa<CXXMethodDecl>(D)) {
Aaron Ballman34776d42013-08-21 22:07:20 +00001022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1023 << Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001024 return;
1025 }
1026
1027 D->addAttr(::new (S.Context)
1028 TestsUnconsumedAttr(Attr.getRange(), S.Context,
1029 Attr.getAttributeSpellingListIndex()));
1030}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001031
Chandler Carruth1b03c872011-07-02 00:01:44 +00001032static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1033 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001034 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1035 if (TD == 0) {
1036 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1037 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001038 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001039 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001040 }
Mike Stumpbf916502009-07-24 19:02:52 +00001041
Richard Smitha4fa9002013-01-13 02:11:23 +00001042 // Remember this typedef decl, we will need it later for diagnostics.
1043 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001044}
1045
Chandler Carruth1b03c872011-07-02 00:01:44 +00001046static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001047 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001048 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001049 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001050
Chandler Carruth87c44602011-07-01 23:49:12 +00001051 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001052 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001053 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001054 // If the alignment is less than or equal to 8 bits, the packed attribute
1055 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001056 if (!FD->getType()->isDependentType() &&
1057 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001058 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001060 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001061 else
Michael Han51d8c522013-01-24 16:46:58 +00001062 FD->addAttr(::new (S.Context)
1063 PackedAttr(Attr.getRange(), S.Context,
1064 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001065 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001066 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001067}
1068
Chandler Carruth1b03c872011-07-02 00:01:44 +00001069static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001070 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001071 RD->addAttr(::new (S.Context)
1072 MsStructAttr(Attr.getRange(), S.Context,
1073 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001074 else
1075 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1076}
1077
Chandler Carruth1b03c872011-07-02 00:01:44 +00001078static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001079 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001080 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001081 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001083 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001084 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001085 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001086 D->addAttr(::new (S.Context)
1087 IBActionAttr(Attr.getRange(), S.Context,
1088 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001089 return;
1090 }
1091
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001092 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001093}
1094
Ted Kremenek2f041d02011-09-29 07:02:25 +00001095static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1096 // The IBOutlet/IBOutletCollection attributes only apply to instance
1097 // variables or properties of Objective-C classes. The outlet must also
1098 // have an object reference type.
1099 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1100 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001101 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001102 << Attr.getName() << VD->getType() << 0;
1103 return false;
1104 }
1105 }
1106 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1107 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001108 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001109 << Attr.getName() << PD->getType() << 1;
1110 return false;
1111 }
1112 }
1113 else {
1114 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1115 return false;
1116 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001117
Ted Kremenek2f041d02011-09-29 07:02:25 +00001118 return true;
1119}
1120
Chandler Carruth1b03c872011-07-02 00:01:44 +00001121static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001122 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001123 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001124 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001125
1126 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001127 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001128
Michael Han51d8c522013-01-24 16:46:58 +00001129 D->addAttr(::new (S.Context)
1130 IBOutletAttr(Attr.getRange(), S.Context,
1131 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001132}
1133
Chandler Carruth1b03c872011-07-02 00:01:44 +00001134static void handleIBOutletCollection(Sema &S, Decl *D,
1135 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001136
1137 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001138 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001139 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1140 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001141 return;
1142 }
1143
Ted Kremenek2f041d02011-09-29 07:02:25 +00001144 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001145 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001146
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001147 IdentifierInfo *II = Attr.getParameterName();
1148 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001149 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001150
John McCallb3d87482010-08-24 05:47:05 +00001151 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001152 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001153 if (!TypeRep) {
1154 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1155 return;
1156 }
John McCallb3d87482010-08-24 05:47:05 +00001157 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001158 // Diagnose use of non-object type in iboutletcollection attribute.
1159 // FIXME. Gnu attribute extension ignores use of builtin types in
1160 // attributes. So, __attribute__((iboutletcollection(char))) will be
1161 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001162 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001163 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1164 return;
1165 }
Michael Han51d8c522013-01-24 16:46:58 +00001166 D->addAttr(::new (S.Context)
1167 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1168 QT, Attr.getParameterLoc(),
1169 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001170}
1171
Chandler Carruthd309c812011-07-01 23:49:16 +00001172static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001173 if (const RecordType *UT = T->getAsUnionType())
1174 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1175 RecordDecl *UD = UT->getDecl();
1176 for (RecordDecl::field_iterator it = UD->field_begin(),
1177 itend = UD->field_end(); it != itend; ++it) {
1178 QualType QT = it->getType();
1179 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1180 T = QT;
1181 return;
1182 }
1183 }
1184 }
1185}
1186
Nuno Lopes587de5b2012-05-24 00:22:00 +00001187static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001188 if (!isFunctionOrMethod(D)) {
1189 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001190 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001191 return;
1192 }
1193
Nuno Lopes587de5b2012-05-24 00:22:00 +00001194 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1195 return;
1196
Nuno Lopes587de5b2012-05-24 00:22:00 +00001197 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001198 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1199 Expr *Ex = Attr.getArg(i);
1200 uint64_t Idx;
1201 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1202 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001203 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001204
1205 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001206 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001207 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1209 << Attr.getName() << AANT_ArgumentIntegerConstant
1210 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001211 return;
1212 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001213 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001214 }
1215
1216 // check if the function returns a pointer
1217 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1218 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001219 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001220 }
1221
Michael Han51d8c522013-01-24 16:46:58 +00001222 D->addAttr(::new (S.Context)
1223 AllocSizeAttr(Attr.getRange(), S.Context,
1224 SizeArgs.data(), SizeArgs.size(),
1225 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001226}
1227
Chandler Carruth1b03c872011-07-02 00:01:44 +00001228static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001229 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1230 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001231 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001232 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001233 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001234 return;
1235 }
Mike Stumpbf916502009-07-24 19:02:52 +00001236
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001237 SmallVector<unsigned, 8> NonNullArgs;
1238 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1239 Expr *Ex = Attr.getArg(i);
1240 uint64_t Idx;
1241 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1242 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001243 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001244
1245 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001246 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001247 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001248
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001249 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001250 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001251 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001252 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001253 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001254 }
Mike Stumpbf916502009-07-24 19:02:52 +00001255
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001256 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001257 }
Mike Stumpbf916502009-07-24 19:02:52 +00001258
1259 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1260 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001261 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001262 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1263 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001264 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001265 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001266 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001267 }
Mike Stumpbf916502009-07-24 19:02:52 +00001268
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001269 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001270 if (NonNullArgs.empty()) {
1271 // Warn the trivial case only if attribute is not coming from a
1272 // macro instantiation.
1273 if (Attr.getLoc().isFileID())
1274 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001275 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001276 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001277 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001278
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001279 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001280 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001281 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001282 D->addAttr(::new (S.Context)
1283 NonNullAttr(Attr.getRange(), S.Context, start, size,
1284 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001285}
1286
Chandler Carruth1b03c872011-07-02 00:01:44 +00001287static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001288 // This attribute must be applied to a function declaration.
1289 // The first argument to the attribute must be a string,
1290 // the name of the resource, for example "malloc".
1291 // The following arguments must be argument indexes, the arguments must be
1292 // of integer type for Returns, otherwise of pointer type.
1293 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001294 // after being held. free() should be __attribute((ownership_takes)), whereas
1295 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001296
1297 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001298 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001299 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001300 return;
1301 }
1302 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001303 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001304 switch (AL.getKind()) {
1305 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001306 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001307 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001308 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1309 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001310 return;
1311 }
Jordy Rose2a479922010-08-12 08:54:03 +00001312 break;
1313 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001314 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001315 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001316 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1317 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001318 return;
1319 }
Jordy Rose2a479922010-08-12 08:54:03 +00001320 break;
1321 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001322 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001323 if (AL.getNumArgs() > 1) {
1324 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001325 << AL.getName() << AL.getNumArgs() + 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001326 return;
1327 }
Jordy Rose2a479922010-08-12 08:54:03 +00001328 break;
1329 default:
1330 // This should never happen given how we are called.
1331 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001332 }
1333
Chandler Carruth87c44602011-07-01 23:49:12 +00001334 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001335 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1336 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001337 return;
1338 }
1339
Chris Lattner5f9e2722011-07-23 10:55:15 +00001340 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341
1342 // Normalize the argument, __foo__ becomes foo.
1343 if (Module.startswith("__") && Module.endswith("__"))
1344 Module = Module.substr(2, Module.size() - 4);
1345
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001346
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001347 SmallVector<unsigned, 8> OwnershipArgs;
1348 for (unsigned i = 0; i < AL.getNumArgs(); ++i) {
1349 Expr *Ex = AL.getArg(i);
1350 uint64_t Idx;
1351 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
1352 AL.getLoc(), i + 1, Ex, Idx))
1353 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001354
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001356 case OwnershipAttr::Takes:
1357 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001359 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001360 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1361 // FIXME: Should also highlight argument in decl.
1362 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001363 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001364 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001365 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001366 continue;
1367 }
1368 break;
1369 }
Sean Huntcf807c42010-08-18 23:23:40 +00001370 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001371 if (AL.getNumArgs() > 1) {
1372 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001373 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001374 llvm::APSInt ArgNum(32);
1375 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1376 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1377 S.Diag(AL.getLoc(), diag::err_ownership_type)
1378 << "ownership_returns" << "integer"
1379 << IdxExpr->getSourceRange();
1380 return;
1381 }
1382 }
1383 break;
1384 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001385 } // switch
1386
1387 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001388 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001389 i = D->specific_attr_begin<OwnershipAttr>(),
1390 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001391 i != e; ++i) {
1392 if ((*i)->getOwnKind() != K) {
1393 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1394 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001395 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001396 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1397 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398 }
1399 }
1400 }
1401 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001402 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 }
1404
1405 unsigned* start = OwnershipArgs.data();
1406 unsigned size = OwnershipArgs.size();
1407 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001408
1409 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001410 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1411 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001412 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001413 }
Sean Huntcf807c42010-08-18 23:23:40 +00001414
Michael Han51d8c522013-01-24 16:46:58 +00001415 D->addAttr(::new (S.Context)
1416 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1417 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001418}
1419
Chandler Carruth1b03c872011-07-02 00:01:44 +00001420static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001421 // Check the attribute arguments.
1422 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1424 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001425 return;
1426 }
1427
Chandler Carruth87c44602011-07-01 23:49:12 +00001428 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001430 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001431 return;
1432 }
1433
Chandler Carruth87c44602011-07-01 23:49:12 +00001434 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001435
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001436 // gcc rejects
1437 // class c {
1438 // static int a __attribute__((weakref ("v2")));
1439 // static int b() __attribute__((weakref ("f3")));
1440 // };
1441 // and ignores the attributes of
1442 // void f(void) {
1443 // static int a __attribute__((weakref ("v2")));
1444 // }
1445 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001446 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001447 if (!Ctx->isFileContext()) {
1448 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001449 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001450 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001451 }
1452
1453 // The GCC manual says
1454 //
1455 // At present, a declaration to which `weakref' is attached can only
1456 // be `static'.
1457 //
1458 // It also says
1459 //
1460 // Without a TARGET,
1461 // given as an argument to `weakref' or to `alias', `weakref' is
1462 // equivalent to `weak'.
1463 //
1464 // gcc 4.4.1 will accept
1465 // int a7 __attribute__((weakref));
1466 // as
1467 // int a7 __attribute__((weak));
1468 // This looks like a bug in gcc. We reject that for now. We should revisit
1469 // it if this behaviour is actually used.
1470
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001471 // GCC rejects
1472 // static ((alias ("y"), weakref)).
1473 // Should we? How to check that weakref is before or after alias?
1474
1475 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001476 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001477 Arg = Arg->IgnoreParenCasts();
1478 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1479
Douglas Gregor5cee1192011-07-27 05:40:30 +00001480 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001482 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001483 return;
1484 }
1485 // GCC will accept anything as the argument of weakref. Should we
1486 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001487 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001488 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001489 }
1490
Michael Han51d8c522013-01-24 16:46:58 +00001491 D->addAttr(::new (S.Context)
1492 WeakRefAttr(Attr.getRange(), S.Context,
1493 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001494}
1495
Chandler Carruth1b03c872011-07-02 00:01:44 +00001496static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001497 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001498 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001499 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001500
Peter Collingbourne7a730022010-11-23 20:45:58 +00001501 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001502 Arg = Arg->IgnoreParenCasts();
1503 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001504
Douglas Gregor5cee1192011-07-27 05:40:30 +00001505 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001506 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1507 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001508 return;
1509 }
Mike Stumpbf916502009-07-24 19:02:52 +00001510
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001511 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001512 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1513 return;
1514 }
1515
Chris Lattner6b6b5372008-06-26 18:38:35 +00001516 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001517
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001518 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001519 Str->getString(),
1520 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001521}
1522
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001523static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1524 // Check the attribute arguments.
1525 if (!checkAttributeNumArgs(S, Attr, 0))
1526 return;
1527
1528 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1529 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1530 << Attr.getName() << ExpectedFunctionOrMethod;
1531 return;
1532 }
1533
Michael Han51d8c522013-01-24 16:46:58 +00001534 D->addAttr(::new (S.Context)
1535 MinSizeAttr(Attr.getRange(), S.Context,
1536 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001537}
1538
Benjamin Krameree409a92012-05-12 21:10:52 +00001539static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1540 // Check the attribute arguments.
1541 if (!checkAttributeNumArgs(S, Attr, 0))
1542 return;
1543
1544 if (!isa<FunctionDecl>(D)) {
1545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1546 << Attr.getName() << ExpectedFunction;
1547 return;
1548 }
1549
1550 if (D->hasAttr<HotAttr>()) {
1551 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1552 << Attr.getName() << "hot";
1553 return;
1554 }
1555
Michael Han51d8c522013-01-24 16:46:58 +00001556 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1557 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001558}
1559
1560static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1561 // Check the attribute arguments.
1562 if (!checkAttributeNumArgs(S, Attr, 0))
1563 return;
1564
1565 if (!isa<FunctionDecl>(D)) {
1566 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1567 << Attr.getName() << ExpectedFunction;
1568 return;
1569 }
1570
1571 if (D->hasAttr<ColdAttr>()) {
1572 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1573 << Attr.getName() << "cold";
1574 return;
1575 }
1576
Michael Han51d8c522013-01-24 16:46:58 +00001577 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1578 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001579}
1580
Chandler Carruth1b03c872011-07-02 00:01:44 +00001581static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001582 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001583 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001584 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001585
Chandler Carruth87c44602011-07-01 23:49:12 +00001586 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001588 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001589 return;
1590 }
1591
Michael Han51d8c522013-01-24 16:46:58 +00001592 D->addAttr(::new (S.Context)
1593 NakedAttr(Attr.getRange(), S.Context,
1594 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001595}
1596
Chandler Carruth1b03c872011-07-02 00:01:44 +00001597static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001599 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001600 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1602 << Attr.getName() << 0;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001603 return;
1604 }
1605
Chandler Carruth87c44602011-07-01 23:49:12 +00001606 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001608 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001609 return;
1610 }
Mike Stumpbf916502009-07-24 19:02:52 +00001611
Michael Han51d8c522013-01-24 16:46:58 +00001612 D->addAttr(::new (S.Context)
1613 AlwaysInlineAttr(Attr.getRange(), S.Context,
1614 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001615}
1616
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001617static void handleTLSModelAttr(Sema &S, Decl *D,
1618 const AttributeList &Attr) {
1619 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001620 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001621 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001622
1623 Expr *Arg = Attr.getArg(0);
1624 Arg = Arg->IgnoreParenCasts();
1625 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1626
1627 // Check that it is a string.
1628 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1630 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001631 return;
1632 }
1633
Richard Smith38afbc72013-04-13 02:43:54 +00001634 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1636 << Attr.getName() << ExpectedTLSVar;
1637 return;
1638 }
1639
1640 // Check that the value.
1641 StringRef Model = Str->getString();
1642 if (Model != "global-dynamic" && Model != "local-dynamic"
1643 && Model != "initial-exec" && Model != "local-exec") {
1644 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1645 return;
1646 }
1647
Michael Han51d8c522013-01-24 16:46:58 +00001648 D->addAttr(::new (S.Context)
1649 TLSModelAttr(Attr.getRange(), S.Context, Model,
1650 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001651}
1652
Chandler Carruth1b03c872011-07-02 00:01:44 +00001653static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001654 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001655 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1657 << Attr.getName() << 0;
Ryan Flynn76168e22009-08-09 20:07:29 +00001658 return;
1659 }
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Chandler Carruth87c44602011-07-01 23:49:12 +00001661 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001662 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001663 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001664 D->addAttr(::new (S.Context)
1665 MallocAttr(Attr.getRange(), S.Context,
1666 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001667 return;
1668 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001669 }
1670
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001671 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001672}
1673
Chandler Carruth1b03c872011-07-02 00:01:44 +00001674static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001675 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001676 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001677 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001678
Michael Han51d8c522013-01-24 16:46:58 +00001679 D->addAttr(::new (S.Context)
1680 MayAliasAttr(Attr.getRange(), S.Context,
1681 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001682}
1683
Chandler Carruth1b03c872011-07-02 00:01:44 +00001684static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001685 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001686 D->addAttr(::new (S.Context)
1687 NoCommonAttr(Attr.getRange(), S.Context,
1688 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001689 else
1690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001692}
1693
Chandler Carruth1b03c872011-07-02 00:01:44 +00001694static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001695 if (S.LangOpts.CPlusPlus) {
1696 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1697 return;
1698 }
1699
Chandler Carruth87c44602011-07-01 23:49:12 +00001700 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 CommonAttr(Attr.getRange(), S.Context,
1703 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001704 else
1705 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001706 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001707}
1708
Chandler Carruth1b03c872011-07-02 00:01:44 +00001709static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001710 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001711
1712 if (S.CheckNoReturnAttr(attr)) return;
1713
Chandler Carruth87c44602011-07-01 23:49:12 +00001714 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001715 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001716 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001717 return;
1718 }
1719
Michael Han51d8c522013-01-24 16:46:58 +00001720 D->addAttr(::new (S.Context)
1721 NoReturnAttr(attr.getRange(), S.Context,
1722 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001723}
1724
1725bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001726 if (attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001727 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1728 << attr.getName() << 0;
John McCall711c52b2011-01-05 12:14:39 +00001729 attr.setInvalid();
1730 return true;
1731 }
1732
1733 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1737 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001738
1739 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1740 // because 'analyzer_noreturn' does not impact the type.
1741
Chandler Carruth1731e202011-07-11 23:30:35 +00001742 if(!checkAttributeNumArgs(S, Attr, 0))
1743 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001744
Chandler Carruth87c44602011-07-01 23:49:12 +00001745 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1746 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001747 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1748 && !VD->getType()->isFunctionPointerType())) {
1749 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001750 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001751 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001752 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001753 return;
1754 }
1755 }
1756
Michael Han51d8c522013-01-24 16:46:58 +00001757 D->addAttr(::new (S.Context)
1758 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1759 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001760}
1761
Richard Smithcd8ab512013-01-17 01:30:42 +00001762static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1763 const AttributeList &Attr) {
1764 // C++11 [dcl.attr.noreturn]p1:
1765 // The attribute may be applied to the declarator-id in a function
1766 // declaration.
1767 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1768 if (!FD) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1770 << Attr.getName() << ExpectedFunctionOrMethod;
1771 return;
1772 }
1773
Michael Han51d8c522013-01-24 16:46:58 +00001774 D->addAttr(::new (S.Context)
1775 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1776 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001777}
1778
John Thompson35cc9622010-08-09 21:53:52 +00001779// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001780static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001781/*
1782 Returning a Vector Class in Registers
1783
Eric Christopherf48f3672010-12-01 22:13:54 +00001784 According to the PPU ABI specifications, a class with a single member of
1785 vector type is returned in memory when used as the return value of a function.
1786 This results in inefficient code when implementing vector classes. To return
1787 the value in a single vector register, add the vecreturn attribute to the
1788 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001789
1790 Example:
1791
1792 struct Vector
1793 {
1794 __vector float xyzw;
1795 } __attribute__((vecreturn));
1796
1797 Vector Add(Vector lhs, Vector rhs)
1798 {
1799 Vector result;
1800 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1801 return result; // This will be returned in a register
1802 }
1803*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001804 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001806 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001807 return;
1808 }
1809
Chandler Carruth87c44602011-07-01 23:49:12 +00001810 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001811 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1812 return;
1813 }
1814
Chandler Carruth87c44602011-07-01 23:49:12 +00001815 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001816 int count = 0;
1817
1818 if (!isa<CXXRecordDecl>(record)) {
1819 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1820 return;
1821 }
1822
1823 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1824 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1825 return;
1826 }
1827
Eric Christopherf48f3672010-12-01 22:13:54 +00001828 for (RecordDecl::field_iterator iter = record->field_begin();
1829 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001830 if ((count == 1) || !iter->getType()->isVectorType()) {
1831 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1832 return;
1833 }
1834 count++;
1835 }
1836
Michael Han51d8c522013-01-24 16:46:58 +00001837 D->addAttr(::new (S.Context)
1838 VecReturnAttr(Attr.getRange(), S.Context,
1839 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001840}
1841
Richard Smith3a2b7a12013-01-28 22:42:45 +00001842static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1843 const AttributeList &Attr) {
1844 if (isa<ParmVarDecl>(D)) {
1845 // [[carries_dependency]] can only be applied to a parameter if it is a
1846 // parameter of a function declaration or lambda.
1847 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1848 S.Diag(Attr.getLoc(),
1849 diag::err_carries_dependency_param_not_function_decl);
1850 return;
1851 }
1852 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001854 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001855 return;
1856 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001857
1858 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1859 Attr.getRange(), S.Context,
1860 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001861}
1862
Chandler Carruth1b03c872011-07-02 00:01:44 +00001863static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001864 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001865 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1867 << Attr.getName() << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001868 return;
1869 }
Mike Stumpbf916502009-07-24 19:02:52 +00001870
Chandler Carruth87c44602011-07-01 23:49:12 +00001871 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001872 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001874 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001875 return;
1876 }
Mike Stumpbf916502009-07-24 19:02:52 +00001877
Michael Han51d8c522013-01-24 16:46:58 +00001878 D->addAttr(::new (S.Context)
1879 UnusedAttr(Attr.getRange(), S.Context,
1880 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001881}
1882
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001883static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1884 const AttributeList &Attr) {
1885 // check the attribute arguments.
1886 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1888 << Attr.getName() << 0;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001889 return;
1890 }
1891
1892 if (!isa<FunctionDecl>(D)) {
1893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1894 << Attr.getName() << ExpectedFunction;
1895 return;
1896 }
1897
Michael Han51d8c522013-01-24 16:46:58 +00001898 D->addAttr(::new (S.Context)
1899 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1900 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001901}
1902
Chandler Carruth1b03c872011-07-02 00:01:44 +00001903static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001904 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001905 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1907 << Attr.getName() << 0;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001908 return;
1909 }
Mike Stumpbf916502009-07-24 19:02:52 +00001910
Chandler Carruth87c44602011-07-01 23:49:12 +00001911 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001912 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001913 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1914 return;
1915 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001916 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001917 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001918 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001919 return;
1920 }
Mike Stumpbf916502009-07-24 19:02:52 +00001921
Michael Han51d8c522013-01-24 16:46:58 +00001922 D->addAttr(::new (S.Context)
1923 UsedAttr(Attr.getRange(), S.Context,
1924 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001925}
1926
Chandler Carruth1b03c872011-07-02 00:01:44 +00001927static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001928 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001929 if (Attr.getNumArgs() > 1) {
1930 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001931 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001932 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001933
1934 int priority = 65535; // FIXME: Do not hardcode such constants.
1935 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001936 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001937 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001938 if (E->isTypeDependent() || E->isValueDependent() ||
1939 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001940 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001941 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001942 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001943 return;
1944 }
1945 priority = Idx.getZExtValue();
1946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Chandler Carruth87c44602011-07-01 23:49:12 +00001948 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001949 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001950 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001951 return;
1952 }
1953
Michael Han51d8c522013-01-24 16:46:58 +00001954 D->addAttr(::new (S.Context)
1955 ConstructorAttr(Attr.getRange(), S.Context, priority,
1956 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001957}
1958
Chandler Carruth1b03c872011-07-02 00:01:44 +00001959static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001960 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001961 if (Attr.getNumArgs() > 1) {
1962 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001963 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001964 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001965
1966 int priority = 65535; // FIXME: Do not hardcode such constants.
1967 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001968 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001969 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001970 if (E->isTypeDependent() || E->isValueDependent() ||
1971 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001972 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001973 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001974 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001975 return;
1976 }
1977 priority = Idx.getZExtValue();
1978 }
Mike Stumpbf916502009-07-24 19:02:52 +00001979
Chandler Carruth87c44602011-07-01 23:49:12 +00001980 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001981 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001982 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001983 return;
1984 }
1985
Michael Han51d8c522013-01-24 16:46:58 +00001986 D->addAttr(::new (S.Context)
1987 DestructorAttr(Attr.getRange(), S.Context, priority,
1988 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001989}
1990
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001991template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00001992static void handleAttrWithMessage(Sema &S, Decl *D,
1993 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001994 unsigned NumArgs = Attr.getNumArgs();
1995 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001996 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001997 return;
1998 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001999
2000 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002001 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002002 if (NumArgs == 1) {
2003 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002004 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002005 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type)
2006 << Attr.getName() << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002007 return;
2008 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002009 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002010 }
Mike Stumpbf916502009-07-24 19:02:52 +00002011
Michael Han51d8c522013-01-24 16:46:58 +00002012 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2013 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002014}
2015
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002016static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2017 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002018 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002019 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002020
Michael Han51d8c522013-01-24 16:46:58 +00002021 D->addAttr(::new (S.Context)
2022 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2023 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002024}
2025
Patrick Beardb2f68202012-04-06 18:12:22 +00002026static void handleObjCRootClassAttr(Sema &S, Decl *D,
2027 const AttributeList &Attr) {
2028 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002029 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2030 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002031 return;
2032 }
2033
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002034 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002035 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002036
Michael Han51d8c522013-01-24 16:46:58 +00002037 D->addAttr(::new (S.Context)
2038 ObjCRootClassAttr(Attr.getRange(), S.Context,
2039 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002040}
2041
Michael Han51d8c522013-01-24 16:46:58 +00002042static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2043 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002044 if (!isa<ObjCInterfaceDecl>(D)) {
2045 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2046 return;
2047 }
2048
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002049 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002050 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002051
Michael Han51d8c522013-01-24 16:46:58 +00002052 D->addAttr(::new (S.Context)
2053 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2054 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002055}
2056
Jordy Rosefad5de92012-05-08 03:27:22 +00002057static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2058 IdentifierInfo *Platform,
2059 VersionTuple Introduced,
2060 VersionTuple Deprecated,
2061 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002062 StringRef PlatformName
2063 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2064 if (PlatformName.empty())
2065 PlatformName = Platform->getName();
2066
2067 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2068 // of these steps are needed).
2069 if (!Introduced.empty() && !Deprecated.empty() &&
2070 !(Introduced <= Deprecated)) {
2071 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2072 << 1 << PlatformName << Deprecated.getAsString()
2073 << 0 << Introduced.getAsString();
2074 return true;
2075 }
2076
2077 if (!Introduced.empty() && !Obsoleted.empty() &&
2078 !(Introduced <= Obsoleted)) {
2079 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2080 << 2 << PlatformName << Obsoleted.getAsString()
2081 << 0 << Introduced.getAsString();
2082 return true;
2083 }
2084
2085 if (!Deprecated.empty() && !Obsoleted.empty() &&
2086 !(Deprecated <= Obsoleted)) {
2087 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2088 << 2 << PlatformName << Obsoleted.getAsString()
2089 << 1 << Deprecated.getAsString();
2090 return true;
2091 }
2092
2093 return false;
2094}
2095
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002096/// \brief Check whether the two versions match.
2097///
2098/// If either version tuple is empty, then they are assumed to match. If
2099/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2100static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2101 bool BeforeIsOkay) {
2102 if (X.empty() || Y.empty())
2103 return true;
2104
2105 if (X == Y)
2106 return true;
2107
2108 if (BeforeIsOkay && X < Y)
2109 return true;
2110
2111 return false;
2112}
2113
Rafael Espindola51be6e32013-01-08 22:04:34 +00002114AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002115 IdentifierInfo *Platform,
2116 VersionTuple Introduced,
2117 VersionTuple Deprecated,
2118 VersionTuple Obsoleted,
2119 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002120 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002121 bool Override,
2122 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002123 VersionTuple MergedIntroduced = Introduced;
2124 VersionTuple MergedDeprecated = Deprecated;
2125 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002126 bool FoundAny = false;
2127
Rafael Espindola98ae8342012-05-10 02:50:16 +00002128 if (D->hasAttrs()) {
2129 AttrVec &Attrs = D->getAttrs();
2130 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2131 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2132 if (!OldAA) {
2133 ++i;
2134 continue;
2135 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002136
Rafael Espindola98ae8342012-05-10 02:50:16 +00002137 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2138 if (OldPlatform != Platform) {
2139 ++i;
2140 continue;
2141 }
2142
2143 FoundAny = true;
2144 VersionTuple OldIntroduced = OldAA->getIntroduced();
2145 VersionTuple OldDeprecated = OldAA->getDeprecated();
2146 VersionTuple OldObsoleted = OldAA->getObsoleted();
2147 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002148
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002149 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2150 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2151 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2152 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002153 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002154 if (Override) {
2155 int Which = -1;
2156 VersionTuple FirstVersion;
2157 VersionTuple SecondVersion;
2158 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2159 Which = 0;
2160 FirstVersion = OldIntroduced;
2161 SecondVersion = Introduced;
2162 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2163 Which = 1;
2164 FirstVersion = Deprecated;
2165 SecondVersion = OldDeprecated;
2166 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2167 Which = 2;
2168 FirstVersion = Obsoleted;
2169 SecondVersion = OldObsoleted;
2170 }
2171
2172 if (Which == -1) {
2173 Diag(OldAA->getLocation(),
2174 diag::warn_mismatched_availability_override_unavail)
2175 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2176 } else {
2177 Diag(OldAA->getLocation(),
2178 diag::warn_mismatched_availability_override)
2179 << Which
2180 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2181 << FirstVersion.getAsString() << SecondVersion.getAsString();
2182 }
2183 Diag(Range.getBegin(), diag::note_overridden_method);
2184 } else {
2185 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2186 Diag(Range.getBegin(), diag::note_previous_attribute);
2187 }
2188
Rafael Espindola98ae8342012-05-10 02:50:16 +00002189 Attrs.erase(Attrs.begin() + i);
2190 --e;
2191 continue;
2192 }
2193
2194 VersionTuple MergedIntroduced2 = MergedIntroduced;
2195 VersionTuple MergedDeprecated2 = MergedDeprecated;
2196 VersionTuple MergedObsoleted2 = MergedObsoleted;
2197
2198 if (MergedIntroduced2.empty())
2199 MergedIntroduced2 = OldIntroduced;
2200 if (MergedDeprecated2.empty())
2201 MergedDeprecated2 = OldDeprecated;
2202 if (MergedObsoleted2.empty())
2203 MergedObsoleted2 = OldObsoleted;
2204
2205 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2206 MergedIntroduced2, MergedDeprecated2,
2207 MergedObsoleted2)) {
2208 Attrs.erase(Attrs.begin() + i);
2209 --e;
2210 continue;
2211 }
2212
2213 MergedIntroduced = MergedIntroduced2;
2214 MergedDeprecated = MergedDeprecated2;
2215 MergedObsoleted = MergedObsoleted2;
2216 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002217 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002218 }
2219
2220 if (FoundAny &&
2221 MergedIntroduced == Introduced &&
2222 MergedDeprecated == Deprecated &&
2223 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002224 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002225
Ted Kremenekcb344392013-04-06 00:34:27 +00002226 // Only create a new attribute if !Override, but we want to do
2227 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002228 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002229 MergedDeprecated, MergedObsoleted) &&
2230 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002231 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2232 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002233 Obsoleted, IsUnavailable, Message,
2234 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002235 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002236 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002237}
2238
Chandler Carruth1b03c872011-07-02 00:01:44 +00002239static void handleAvailabilityAttr(Sema &S, Decl *D,
2240 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002241 IdentifierInfo *Platform = Attr.getParameterName();
2242 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002243 unsigned Index = Attr.getAttributeSpellingListIndex();
2244
Rafael Espindola3b294362012-05-06 19:56:25 +00002245 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002246 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2247 << Platform;
2248
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002249 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2250 if (!ND) {
2251 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2252 return;
2253 }
2254
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002255 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2256 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2257 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002258 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002259 StringRef Str;
2260 const StringLiteral *SE =
2261 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2262 if (SE)
2263 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002264
Rafael Espindola51be6e32013-01-08 22:04:34 +00002265 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002266 Platform,
2267 Introduced.Version,
2268 Deprecated.Version,
2269 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002270 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002271 /*Override=*/false,
2272 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002273 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002274 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002275}
2276
John McCalld4c3d662013-02-20 01:54:26 +00002277template <class T>
2278static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2279 typename T::VisibilityType value,
2280 unsigned attrSpellingListIndex) {
2281 T *existingAttr = D->getAttr<T>();
2282 if (existingAttr) {
2283 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2284 if (existingValue == value)
2285 return NULL;
2286 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2287 S.Diag(range.getBegin(), diag::note_previous_attribute);
2288 D->dropAttr<T>();
2289 }
2290 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2291}
2292
Rafael Espindola599f1b72012-05-13 03:25:18 +00002293VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002294 VisibilityAttr::VisibilityType Vis,
2295 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002296 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2297 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002298}
2299
John McCalld4c3d662013-02-20 01:54:26 +00002300TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2301 TypeVisibilityAttr::VisibilityType Vis,
2302 unsigned AttrSpellingListIndex) {
2303 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2304 AttrSpellingListIndex);
2305}
2306
2307static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2308 bool isTypeVisibility) {
2309 // Visibility attributes don't mean anything on a typedef.
2310 if (isa<TypedefNameDecl>(D)) {
2311 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2312 << Attr.getName();
2313 return;
2314 }
2315
2316 // 'type_visibility' can only go on a type or namespace.
2317 if (isTypeVisibility &&
2318 !(isa<TagDecl>(D) ||
2319 isa<ObjCInterfaceDecl>(D) ||
2320 isa<NamespaceDecl>(D))) {
2321 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2322 << Attr.getName() << ExpectedTypeOrNamespace;
2323 return;
2324 }
2325
Chris Lattner6b6b5372008-06-26 18:38:35 +00002326 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002327 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002328 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002329
Peter Collingbourne7a730022010-11-23 20:45:58 +00002330 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002331 Arg = Arg->IgnoreParenCasts();
2332 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002333
Douglas Gregor5cee1192011-07-27 05:40:30 +00002334 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002335 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2336 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002337 return;
2338 }
Mike Stumpbf916502009-07-24 19:02:52 +00002339
Chris Lattner5f9e2722011-07-23 10:55:15 +00002340 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002341 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002342
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002343 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002344 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002345 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002346 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002347 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002348 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002349 else if (TypeStr == "protected") {
2350 // Complain about attempts to use protected visibility on targets
2351 // (like Darwin) that don't support it.
2352 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2353 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2354 type = VisibilityAttr::Default;
2355 } else {
2356 type = VisibilityAttr::Protected;
2357 }
2358 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002359 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002360 return;
2361 }
Mike Stumpbf916502009-07-24 19:02:52 +00002362
Michael Han51d8c522013-01-24 16:46:58 +00002363 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002364 clang::Attr *newAttr;
2365 if (isTypeVisibility) {
2366 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2367 (TypeVisibilityAttr::VisibilityType) type,
2368 Index);
2369 } else {
2370 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2371 }
2372 if (newAttr)
2373 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002374}
2375
Chandler Carruth1b03c872011-07-02 00:01:44 +00002376static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2377 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002378 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2379 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002381 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002382 return;
2383 }
2384
Chandler Carruth87c44602011-07-01 23:49:12 +00002385 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2386 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002387 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002388 << Attr.getName() << 1 << AANT_ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002389 } else {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002390 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2391 << Attr.getName() << 0;
John McCalld5313b02011-03-02 11:33:24 +00002392 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002393 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002394 return;
2395 }
2396
Chris Lattner5f9e2722011-07-23 10:55:15 +00002397 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002398 ObjCMethodFamilyAttr::FamilyKind family;
2399 if (param == "none")
2400 family = ObjCMethodFamilyAttr::OMF_None;
2401 else if (param == "alloc")
2402 family = ObjCMethodFamilyAttr::OMF_alloc;
2403 else if (param == "copy")
2404 family = ObjCMethodFamilyAttr::OMF_copy;
2405 else if (param == "init")
2406 family = ObjCMethodFamilyAttr::OMF_init;
2407 else if (param == "mutableCopy")
2408 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2409 else if (param == "new")
2410 family = ObjCMethodFamilyAttr::OMF_new;
2411 else {
2412 // Just warn and ignore it. This is future-proof against new
2413 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002414 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002415 return;
2416 }
2417
John McCallf85e1932011-06-15 23:02:42 +00002418 if (family == ObjCMethodFamilyAttr::OMF_init &&
2419 !method->getResultType()->isObjCObjectPointerType()) {
2420 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2421 << method->getResultType();
2422 // Ignore the attribute.
2423 return;
2424 }
2425
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002426 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002427 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002428}
2429
Chandler Carruth1b03c872011-07-02 00:01:44 +00002430static void handleObjCExceptionAttr(Sema &S, Decl *D,
2431 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002432 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002433 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002434
Chris Lattner0db29ec2009-02-14 08:09:34 +00002435 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2436 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2438 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002439 return;
2440 }
Mike Stumpbf916502009-07-24 19:02:52 +00002441
Michael Han51d8c522013-01-24 16:46:58 +00002442 D->addAttr(::new (S.Context)
2443 ObjCExceptionAttr(Attr.getRange(), S.Context,
2444 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002445}
2446
Chandler Carruth1b03c872011-07-02 00:01:44 +00002447static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002448 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002449 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002450 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002451 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002452 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002453 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2454 return;
2455 }
2456 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002457 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2458 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002459 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002460 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2461 return;
2462 }
2463 }
2464 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002465 // It is okay to include this attribute on properties, e.g.:
2466 //
2467 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2468 //
2469 // In this case it follows tradition and suppresses an error in the above
2470 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002471 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002472 }
Michael Han51d8c522013-01-24 16:46:58 +00002473 D->addAttr(::new (S.Context)
2474 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2475 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002476}
2477
Mike Stumpbf916502009-07-24 19:02:52 +00002478static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002479handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002480 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002481 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002482
2483 if (!isa<FunctionDecl>(D)) {
2484 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2485 return;
2486 }
2487
Michael Han51d8c522013-01-24 16:46:58 +00002488 D->addAttr(::new (S.Context)
2489 OverloadableAttr(Attr.getRange(), S.Context,
2490 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002491}
2492
Chandler Carruth1b03c872011-07-02 00:01:44 +00002493static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002494 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002495 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002496 << Attr.getName() << 1 << AANT_ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002497 return;
2498 }
Mike Stumpbf916502009-07-24 19:02:52 +00002499
Steve Naroff9eae5762008-09-18 16:44:58 +00002500 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002501 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2502 << Attr.getName() << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002503 return;
2504 }
Mike Stumpbf916502009-07-24 19:02:52 +00002505
Sean Huntcf807c42010-08-18 23:23:40 +00002506 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002507 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002508 type = BlocksAttr::ByRef;
2509 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002510 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002511 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002512 return;
2513 }
Mike Stumpbf916502009-07-24 19:02:52 +00002514
Michael Han51d8c522013-01-24 16:46:58 +00002515 D->addAttr(::new (S.Context)
2516 BlocksAttr(Attr.getRange(), S.Context, type,
2517 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002518}
2519
Chandler Carruth1b03c872011-07-02 00:01:44 +00002520static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002521 // check the attribute arguments.
2522 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002523 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002524 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002525 }
2526
John McCall3323fad2011-09-09 07:56:05 +00002527 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002528 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002529 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002530 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002531 if (E->isTypeDependent() || E->isValueDependent() ||
2532 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002534 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002535 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002536 return;
2537 }
Mike Stumpbf916502009-07-24 19:02:52 +00002538
John McCall3323fad2011-09-09 07:56:05 +00002539 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002540 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2541 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002542 return;
2543 }
John McCall3323fad2011-09-09 07:56:05 +00002544
2545 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002546 }
2547
John McCall3323fad2011-09-09 07:56:05 +00002548 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002549 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002550 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002551 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002552 if (E->isTypeDependent() || E->isValueDependent() ||
2553 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002555 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002556 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002557 return;
2558 }
2559 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002560
John McCall3323fad2011-09-09 07:56:05 +00002561 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002562 // FIXME: This error message could be improved, it would be nice
2563 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002564 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2565 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002566 return;
2567 }
2568 }
2569
Chandler Carruth87c44602011-07-01 23:49:12 +00002570 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002571 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002572 if (isa<FunctionNoProtoType>(FT)) {
2573 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2574 return;
2575 }
Mike Stumpbf916502009-07-24 19:02:52 +00002576
Chris Lattner897cd902009-03-17 23:03:47 +00002577 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002578 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002579 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002580 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002581 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002582 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002583 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002584 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002585 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002586 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2587 if (!BD->isVariadic()) {
2588 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2589 return;
2590 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002591 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002592 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002593 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002594 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002595 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002596 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002597 int m = Ty->isFunctionPointerType() ? 0 : 1;
2598 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002599 return;
2600 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002601 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002602 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002603 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002604 return;
2605 }
Anders Carlsson77091822008-10-05 18:05:59 +00002606 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002608 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002609 return;
2610 }
Michael Han51d8c522013-01-24 16:46:58 +00002611 D->addAttr(::new (S.Context)
2612 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2613 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002614}
2615
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002616static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2617 // Check the attribute arguments.
2618 if (!checkAttributeNumArgs(S, Attr, 0))
2619 return;
2620
2621 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2622 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2623 else
2624 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2625}
2626
Chandler Carruth1b03c872011-07-02 00:01:44 +00002627static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002628 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002629 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002630 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002631
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002632 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002633 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002634 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002635 return;
2636 }
Mike Stumpbf916502009-07-24 19:02:52 +00002637
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002638 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2639 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2640 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002641 return;
2642 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002643 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2644 if (MD->getResultType()->isVoidType()) {
2645 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2646 << Attr.getName() << 1;
2647 return;
2648 }
2649
Michael Han51d8c522013-01-24 16:46:58 +00002650 D->addAttr(::new (S.Context)
2651 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2652 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002653}
2654
Chandler Carruth1b03c872011-07-02 00:01:44 +00002655static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002656 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002657 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2659 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002660 return;
2661 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002662
Chandler Carruth87c44602011-07-01 23:49:12 +00002663 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002664 if (isa<CXXRecordDecl>(D)) {
2665 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2666 return;
2667 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2669 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002670 return;
2671 }
2672
Chandler Carruth87c44602011-07-01 23:49:12 +00002673 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002674
Michael Han51d8c522013-01-24 16:46:58 +00002675 nd->addAttr(::new (S.Context)
2676 WeakAttr(Attr.getRange(), S.Context,
2677 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002678}
2679
Chandler Carruth1b03c872011-07-02 00:01:44 +00002680static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002681 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002682 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002683 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002684
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002685
2686 // weak_import only applies to variable & function declarations.
2687 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002688 if (!D->canBeWeakImported(isDef)) {
2689 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002690 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2691 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002692 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002693 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002694 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002695 // Nothing to warn about here.
2696 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002698 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002699
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002700 return;
2701 }
2702
Michael Han51d8c522013-01-24 16:46:58 +00002703 D->addAttr(::new (S.Context)
2704 WeakImportAttr(Attr.getRange(), S.Context,
2705 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002706}
2707
Tanya Lattner0df579e2012-07-09 22:06:01 +00002708// Handles reqd_work_group_size and work_group_size_hint.
2709static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002710 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002711 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2712 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2713
Nate Begeman6f3d8382009-06-26 06:32:41 +00002714 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002715 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002716
2717 unsigned WGSize[3];
2718 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002719 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002720 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002721 if (E->isTypeDependent() || E->isValueDependent() ||
2722 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002723 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2724 << Attr.getName() << AANT_ArgumentIntegerConstant
2725 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002726 return;
2727 }
2728 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2729 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002730
2731 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2732 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2733 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2734 if (!(A->getXDim() == WGSize[0] &&
2735 A->getYDim() == WGSize[1] &&
2736 A->getZDim() == WGSize[2])) {
2737 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2738 Attr.getName();
2739 }
2740 }
2741
2742 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2743 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2744 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2745 if (!(A->getXDim() == WGSize[0] &&
2746 A->getYDim() == WGSize[1] &&
2747 A->getZDim() == WGSize[2])) {
2748 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2749 Attr.getName();
2750 }
2751 }
2752
2753 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2754 D->addAttr(::new (S.Context)
2755 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002756 WGSize[0], WGSize[1], WGSize[2],
2757 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002758 else
2759 D->addAttr(::new (S.Context)
2760 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002761 WGSize[0], WGSize[1], WGSize[2],
2762 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002763}
2764
Joey Gouly37453b92013-03-08 09:42:32 +00002765static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2766 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2767
2768 // Attribute has 1 argument.
2769 if (!checkAttributeNumArgs(S, Attr, 1))
2770 return;
2771
2772 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2773
2774 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2775 (ParmType->isBooleanType() ||
2776 !ParmType->isIntegralType(S.getASTContext()))) {
2777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2778 << ParmType;
2779 return;
2780 }
2781
2782 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2783 D->hasAttr<VecTypeHintAttr>()) {
2784 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2785 if (A->getTypeHint() != ParmType) {
2786 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2787 return;
2788 }
2789 }
2790
2791 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2792 ParmType, Attr.getLoc()));
2793}
2794
Joey Gouly96cead52013-03-14 09:54:43 +00002795static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2796 if (!dyn_cast<VarDecl>(D))
2797 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2798 << 9;
2799 StringRef EndianType = Attr.getParameterName()->getName();
2800 if (EndianType != "host" && EndianType != "device")
2801 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2802}
2803
Rafael Espindola599f1b72012-05-13 03:25:18 +00002804SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002805 StringRef Name,
2806 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002807 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2808 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002809 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002810 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2811 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002812 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002813 }
Michael Han51d8c522013-01-24 16:46:58 +00002814 return ::new (Context) SectionAttr(Range, Context, Name,
2815 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002816}
2817
Chandler Carruth1b03c872011-07-02 00:01:44 +00002818static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002819 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002820 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002821 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002822
2823 // Make sure that there is a string literal as the sections's single
2824 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002825 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002826 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002827 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002828 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2829 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002830 return;
2831 }
Mike Stump1eb44332009-09-09 15:08:12 +00002832
Chris Lattner797c3c42009-08-10 19:03:04 +00002833 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002834 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002835 if (!Error.empty()) {
2836 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2837 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002838 return;
2839 }
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002841 // This attribute cannot be applied to local variables.
2842 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2843 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2844 return;
2845 }
Michael Han51d8c522013-01-24 16:46:58 +00002846
2847 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002848 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002849 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002850 if (NewAttr)
2851 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002852}
2853
Chris Lattner6b6b5372008-06-26 18:38:35 +00002854
Chandler Carruth1b03c872011-07-02 00:01:44 +00002855static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002856 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002857 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002858 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2859 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002860 return;
2861 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002862
Chandler Carruth87c44602011-07-01 23:49:12 +00002863 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002864 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002865 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002866 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002867 D->addAttr(::new (S.Context)
2868 NoThrowAttr(Attr.getRange(), S.Context,
2869 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002870 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002871}
2872
Chandler Carruth1b03c872011-07-02 00:01:44 +00002873static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002874 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002875 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002876 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2877 << Attr.getName() << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002878 return;
2879 }
Mike Stumpbf916502009-07-24 19:02:52 +00002880
Chandler Carruth87c44602011-07-01 23:49:12 +00002881 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002882 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002883 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002884 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002885 D->addAttr(::new (S.Context)
2886 ConstAttr(Attr.getRange(), S.Context,
2887 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002888 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002889}
2890
Chandler Carruth1b03c872011-07-02 00:01:44 +00002891static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002892 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002893 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002894 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002895
Michael Han51d8c522013-01-24 16:46:58 +00002896 D->addAttr(::new (S.Context)
2897 PureAttr(Attr.getRange(), S.Context,
2898 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002899}
2900
Chandler Carruth1b03c872011-07-02 00:01:44 +00002901static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002902 if (!Attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002903 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2904 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002905 return;
2906 }
Mike Stumpbf916502009-07-24 19:02:52 +00002907
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002908 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2910 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002911 return;
2912 }
Mike Stumpbf916502009-07-24 19:02:52 +00002913
Chandler Carruth87c44602011-07-01 23:49:12 +00002914 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002915
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002916 if (!VD || !VD->hasLocalStorage()) {
2917 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2918 return;
2919 }
Mike Stumpbf916502009-07-24 19:02:52 +00002920
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002921 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002922 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002923 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002924 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2925 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002926 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002927 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002928 Attr.getParameterName();
2929 return;
2930 }
Mike Stumpbf916502009-07-24 19:02:52 +00002931
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002932 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2933 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002934 S.Diag(Attr.getParameterLoc(),
2935 diag::err_attribute_cleanup_arg_not_function)
2936 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002937 return;
2938 }
2939
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002940 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002941 S.Diag(Attr.getParameterLoc(),
2942 diag::err_attribute_cleanup_func_must_take_one_arg)
2943 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002944 return;
2945 }
Mike Stumpbf916502009-07-24 19:02:52 +00002946
Anders Carlsson89941c12009-02-07 23:16:50 +00002947 // We're currently more strict than GCC about what function types we accept.
2948 // If this ever proves to be a problem it should be easy to fix.
2949 QualType Ty = S.Context.getPointerType(VD->getType());
2950 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002951 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2952 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002953 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002954 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2955 Attr.getParameterName() << ParamTy << Ty;
2956 return;
2957 }
Mike Stumpbf916502009-07-24 19:02:52 +00002958
Michael Han51d8c522013-01-24 16:46:58 +00002959 D->addAttr(::new (S.Context)
2960 CleanupAttr(Attr.getRange(), S.Context, FD,
2961 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002962 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00002963 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002964}
2965
Mike Stumpbf916502009-07-24 19:02:52 +00002966/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002967/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002968static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002969 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002970 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002971
Chandler Carruth87c44602011-07-01 23:49:12 +00002972 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002973 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002974 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002975 return;
2976 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002977
Peter Collingbourne7a730022010-11-23 20:45:58 +00002978 Expr *IdxExpr = Attr.getArg(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002979 uint64_t ArgIdx;
2980 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2981 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002982 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002983
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002984 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002985 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002986
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002987 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2988 if (not_nsstring_type &&
2989 !isCFStringType(Ty, S.Context) &&
2990 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002991 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002992 // FIXME: Should highlight the actual expression that has the wrong type.
2993 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002994 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002995 << IdxExpr->getSourceRange();
2996 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002997 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002998 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002999 if (!isNSStringType(Ty, S.Context) &&
3000 !isCFStringType(Ty, S.Context) &&
3001 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003002 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003003 // FIXME: Should highlight the actual expression that has the wrong type.
3004 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003005 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003006 << IdxExpr->getSourceRange();
3007 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003008 }
3009
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003010 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3011 // because that has corrected for the implicit this parameter, and is zero-
3012 // based. The attribute expects what the user wrote explicitly.
3013 llvm::APSInt Val;
3014 IdxExpr->EvaluateAsInt(Val, S.Context);
3015
Michael Han51d8c522013-01-24 16:46:58 +00003016 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003017 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003018 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003019}
3020
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003021enum FormatAttrKind {
3022 CFStringFormat,
3023 NSStringFormat,
3024 StrftimeFormat,
3025 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003026 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003027 InvalidFormat
3028};
3029
3030/// getFormatAttrKind - Map from format attribute names to supported format
3031/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003032static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003033 return llvm::StringSwitch<FormatAttrKind>(Format)
3034 // Check for formats that get handled specially.
3035 .Case("NSString", NSStringFormat)
3036 .Case("CFString", CFStringFormat)
3037 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003038
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003039 // Otherwise, check for supported formats.
3040 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3041 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3042 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003043
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003044 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3045 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003046}
3047
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003048/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003049/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003050static void handleInitPriorityAttr(Sema &S, Decl *D,
3051 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003052 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003053 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3054 return;
3055 }
3056
Chandler Carruth87c44602011-07-01 23:49:12 +00003057 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003058 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3059 Attr.setInvalid();
3060 return;
3061 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003062 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003063 if (S.Context.getAsArrayType(T))
3064 T = S.Context.getBaseElementType(T);
3065 if (!T->getAs<RecordType>()) {
3066 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3067 Attr.setInvalid();
3068 return;
3069 }
3070
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003071 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003072 Attr.setInvalid();
3073 return;
3074 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003075 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003076
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003077 llvm::APSInt priority(32);
3078 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3079 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003080 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3081 << Attr.getName() << AANT_ArgumentIntegerConstant
3082 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003083 Attr.setInvalid();
3084 return;
3085 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003086 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003087 if (prioritynum < 101 || prioritynum > 65535) {
3088 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3089 << priorityExpr->getSourceRange();
3090 Attr.setInvalid();
3091 return;
3092 }
Michael Han51d8c522013-01-24 16:46:58 +00003093 D->addAttr(::new (S.Context)
3094 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3095 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003096}
3097
Rafael Espindola599f1b72012-05-13 03:25:18 +00003098FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003099 int FormatIdx, int FirstArg,
3100 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003101 // Check whether we already have an equivalent format attribute.
3102 for (specific_attr_iterator<FormatAttr>
3103 i = D->specific_attr_begin<FormatAttr>(),
3104 e = D->specific_attr_end<FormatAttr>();
3105 i != e ; ++i) {
3106 FormatAttr *f = *i;
3107 if (f->getType() == Format &&
3108 f->getFormatIdx() == FormatIdx &&
3109 f->getFirstArg() == FirstArg) {
3110 // If we don't have a valid location for this attribute, adopt the
3111 // location.
3112 if (f->getLocation().isInvalid())
3113 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003114 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003115 }
3116 }
3117
Michael Han51d8c522013-01-24 16:46:58 +00003118 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3119 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003120}
3121
Mike Stumpbf916502009-07-24 19:02:52 +00003122/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003123/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003124static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003125
Chris Lattner545dd342008-06-28 23:36:30 +00003126 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003127 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003128 << Attr.getName() << 1 << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003129 return;
3130 }
3131
Chris Lattner545dd342008-06-28 23:36:30 +00003132 if (Attr.getNumArgs() != 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3134 << Attr.getName() << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003135 return;
3136 }
3137
Chandler Carruth87c44602011-07-01 23:49:12 +00003138 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003139 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003140 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141 return;
3142 }
3143
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003144 // In C++ the implicit 'this' function parameter also counts, and they are
3145 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003146 bool HasImplicitThisParam = isInstanceMethod(D);
3147 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003148 unsigned FirstIdx = 1;
3149
Chris Lattner5f9e2722011-07-23 10:55:15 +00003150 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003151
3152 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003153 if (Format.startswith("__") && Format.endswith("__"))
3154 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003155
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003156 // Check for supported formats.
3157 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003158
3159 if (Kind == IgnoredFormat)
3160 return;
3161
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003162 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003163 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003164 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003165 return;
3166 }
3167
3168 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003169 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003170 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003171 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3172 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003173 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003174 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003175 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003176 return;
3177 }
3178
3179 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003181 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003182 return;
3183 }
3184
3185 // FIXME: Do we need to bounds check?
3186 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003187
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003188 if (HasImplicitThisParam) {
3189 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003190 S.Diag(Attr.getLoc(),
3191 diag::err_format_attribute_implicit_this_format_string)
3192 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003193 return;
3194 }
3195 ArgIdx--;
3196 }
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Chris Lattner6b6b5372008-06-26 18:38:35 +00003198 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003199 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003200
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003201 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003202 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003203 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3204 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003205 return;
3206 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003207 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003208 // FIXME: do we need to check if the type is NSString*? What are the
3209 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003210 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003211 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003212 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3213 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003214 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003215 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003216 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003217 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003218 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003219 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3220 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003221 return;
3222 }
3223
3224 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003225 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003226 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003227 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3228 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003230 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003231 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003232 return;
3233 }
3234
3235 // check if the function is variadic if the 3rd argument non-zero
3236 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003237 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003238 ++NumArgs; // +1 for ...
3239 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003240 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003241 return;
3242 }
3243 }
3244
Chris Lattner3c73c412008-11-19 08:23:25 +00003245 // strftime requires FirstArg to be 0 because it doesn't read from any
3246 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003247 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003248 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003249 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3250 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003251 return;
3252 }
3253 // if 0 it disables parameter checking (to use with e.g. va_list)
3254 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003255 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003256 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003257 return;
3258 }
3259
Rafael Espindola599f1b72012-05-13 03:25:18 +00003260 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3261 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003262 FirstArg.getZExtValue(),
3263 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003264 if (NewAttr)
3265 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003266}
3267
Chandler Carruth1b03c872011-07-02 00:01:44 +00003268static void handleTransparentUnionAttr(Sema &S, Decl *D,
3269 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003270 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003271 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003272 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003273
Chris Lattner6b6b5372008-06-26 18:38:35 +00003274
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003275 // Try to find the underlying union declaration.
3276 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003277 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003278 if (TD && TD->getUnderlyingType()->isUnionType())
3279 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3280 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003281 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003282
3283 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003284 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003285 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003286 return;
3287 }
3288
John McCall5e1cdac2011-10-07 06:10:15 +00003289 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003290 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003291 diag::warn_transparent_union_attribute_not_definition);
3292 return;
3293 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003294
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003295 RecordDecl::field_iterator Field = RD->field_begin(),
3296 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003297 if (Field == FieldEnd) {
3298 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3299 return;
3300 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003301
David Blaikie581deb32012-06-06 20:45:41 +00003302 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003303 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003304 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003305 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003306 diag::warn_transparent_union_attribute_floating)
3307 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003308 return;
3309 }
3310
3311 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3312 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3313 for (; Field != FieldEnd; ++Field) {
3314 QualType FieldType = Field->getType();
3315 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3316 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3317 // Warn if we drop the attribute.
3318 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003319 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003320 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003321 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003322 diag::warn_transparent_union_attribute_field_size_align)
3323 << isSize << Field->getDeclName() << FieldBits;
3324 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003325 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003326 diag::note_transparent_union_first_field_size_align)
3327 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003328 return;
3329 }
3330 }
3331
Michael Han51d8c522013-01-24 16:46:58 +00003332 RD->addAttr(::new (S.Context)
3333 TransparentUnionAttr(Attr.getRange(), S.Context,
3334 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003335}
3336
Chandler Carruth1b03c872011-07-02 00:01:44 +00003337static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003338 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003339 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003340 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003341
Peter Collingbourne7a730022010-11-23 20:45:58 +00003342 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003343 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003344
Chris Lattner6b6b5372008-06-26 18:38:35 +00003345 // Make sure that there is a string literal as the annotation's single
3346 // argument.
3347 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003348 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3349 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003350 return;
3351 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003352
3353 // Don't duplicate annotations that are already set.
3354 for (specific_attr_iterator<AnnotateAttr>
3355 i = D->specific_attr_begin<AnnotateAttr>(),
3356 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3357 if ((*i)->getAnnotation() == SE->getString())
3358 return;
3359 }
Michael Han51d8c522013-01-24 16:46:58 +00003360
3361 D->addAttr(::new (S.Context)
3362 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3363 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003364}
3365
Chandler Carruth1b03c872011-07-02 00:01:44 +00003366static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003367 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003368 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3370 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003371 return;
3372 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003373
Richard Smithbe507b62013-02-01 08:12:08 +00003374 if (Attr.getNumArgs() == 0) {
3375 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3376 true, 0, Attr.getAttributeSpellingListIndex()));
3377 return;
3378 }
3379
Richard Smithf6565a92013-02-22 08:32:16 +00003380 Expr *E = Attr.getArg(0);
3381 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3382 S.Diag(Attr.getEllipsisLoc(),
3383 diag::err_pack_expansion_without_parameter_packs);
3384 return;
3385 }
3386
3387 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3388 return;
3389
3390 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3391 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003392}
3393
3394void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003395 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003396 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3397 SourceLocation AttrLoc = AttrRange.getBegin();
3398
Richard Smith4cd81c52013-01-29 09:02:09 +00003399 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003400 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003401 // C++11 [dcl.align]p1:
3402 // An alignment-specifier may be applied to a variable or to a class
3403 // data member, but it shall not be applied to a bit-field, a function
3404 // parameter, the formal parameter of a catch clause, or a variable
3405 // declared with the register storage class specifier. An
3406 // alignment-specifier may also be applied to the declaration of a class
3407 // or enumeration type.
3408 // C11 6.7.5/2:
3409 // An alignment attribute shall not be specified in a declaration of
3410 // a typedef, or a bit-field, or a function, or a parameter, or an
3411 // object declared with the register storage-class specifier.
3412 int DiagKind = -1;
3413 if (isa<ParmVarDecl>(D)) {
3414 DiagKind = 0;
3415 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3416 if (VD->getStorageClass() == SC_Register)
3417 DiagKind = 1;
3418 if (VD->isExceptionVariable())
3419 DiagKind = 2;
3420 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3421 if (FD->isBitField())
3422 DiagKind = 3;
3423 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003424 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3425 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003426 << (TmpAttr.isC11() ? ExpectedVariableOrField
3427 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003428 return;
3429 }
3430 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003431 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003432 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003433 return;
3434 }
3435 }
3436
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003437 if (E->isTypeDependent() || E->isValueDependent()) {
3438 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003439 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3440 AA->setPackExpansion(IsPackExpansion);
3441 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003442 return;
3443 }
Michael Hana31f65b2013-02-01 01:19:17 +00003444
Sean Huntcf807c42010-08-18 23:23:40 +00003445 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003446 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003447 ExprResult ICE
3448 = VerifyIntegerConstantExpression(E, &Alignment,
3449 diag::err_aligned_attribute_argument_not_int,
3450 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003451 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003452 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003453
3454 // C++11 [dcl.align]p2:
3455 // -- if the constant expression evaluates to zero, the alignment
3456 // specifier shall have no effect
3457 // C11 6.7.5p6:
3458 // An alignment specification of zero has no effect.
3459 if (!(TmpAttr.isAlignas() && !Alignment) &&
3460 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003461 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3462 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003463 return;
3464 }
Michael Hana31f65b2013-02-01 01:19:17 +00003465
Richard Smithbe507b62013-02-01 08:12:08 +00003466 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003467 // We've already verified it's a power of 2, now let's make sure it's
3468 // 8192 or less.
3469 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003470 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003471 << E->getSourceRange();
3472 return;
3473 }
3474 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003475
Richard Smithf6565a92013-02-22 08:32:16 +00003476 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3477 ICE.take(), SpellingListIndex);
3478 AA->setPackExpansion(IsPackExpansion);
3479 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003480}
3481
Michael Hana31f65b2013-02-01 01:19:17 +00003482void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003483 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003484 // FIXME: Cache the number on the Attr object if non-dependent?
3485 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003486 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3487 SpellingListIndex);
3488 AA->setPackExpansion(IsPackExpansion);
3489 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003490}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003491
Richard Smithbe507b62013-02-01 08:12:08 +00003492void Sema::CheckAlignasUnderalignment(Decl *D) {
3493 assert(D->hasAttrs() && "no attributes on decl");
3494
3495 QualType Ty;
3496 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3497 Ty = VD->getType();
3498 else
3499 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003500 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003501 return;
3502
3503 // C++11 [dcl.align]p5, C11 6.7.5/4:
3504 // The combined effect of all alignment attributes in a declaration shall
3505 // not specify an alignment that is less strict than the alignment that
3506 // would otherwise be required for the entity being declared.
3507 AlignedAttr *AlignasAttr = 0;
3508 unsigned Align = 0;
3509 for (specific_attr_iterator<AlignedAttr>
3510 I = D->specific_attr_begin<AlignedAttr>(),
3511 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3512 if (I->isAlignmentDependent())
3513 return;
3514 if (I->isAlignas())
3515 AlignasAttr = *I;
3516 Align = std::max(Align, I->getAlignment(Context));
3517 }
3518
3519 if (AlignasAttr && Align) {
3520 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3521 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3522 if (NaturalAlign > RequestedAlign)
3523 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3524 << Ty << (unsigned)NaturalAlign.getQuantity();
3525 }
3526}
3527
Chandler Carruthd309c812011-07-01 23:49:16 +00003528/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003529/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003530///
Mike Stumpbf916502009-07-24 19:02:52 +00003531/// Despite what would be logical, the mode attribute is a decl attribute, not a
3532/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3533/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003534static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003535 // This attribute isn't documented, but glibc uses it. It changes
3536 // the width of an int or unsigned int to the specified size.
3537
3538 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003539 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003540 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003541
Chris Lattnerfbf13472008-06-27 22:18:37 +00003542
3543 IdentifierInfo *Name = Attr.getParameterName();
3544 if (!Name) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003545 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3546 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003547 return;
3548 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003549
Chris Lattner5f9e2722011-07-23 10:55:15 +00003550 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003551
3552 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003553 if (Str.startswith("__") && Str.endswith("__"))
3554 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003555
3556 unsigned DestWidth = 0;
3557 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003558 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003559 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003560 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003561 switch (Str[0]) {
3562 case 'Q': DestWidth = 8; break;
3563 case 'H': DestWidth = 16; break;
3564 case 'S': DestWidth = 32; break;
3565 case 'D': DestWidth = 64; break;
3566 case 'X': DestWidth = 96; break;
3567 case 'T': DestWidth = 128; break;
3568 }
3569 if (Str[1] == 'F') {
3570 IntegerMode = false;
3571 } else if (Str[1] == 'C') {
3572 IntegerMode = false;
3573 ComplexMode = true;
3574 } else if (Str[1] != 'I') {
3575 DestWidth = 0;
3576 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003577 break;
3578 case 4:
3579 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3580 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003581 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003582 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003583 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003584 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003585 break;
3586 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003587 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003588 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003589 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003590 case 11:
3591 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003592 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003593 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003594 }
3595
3596 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003597 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003598 OldTy = TD->getUnderlyingType();
3599 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3600 OldTy = VD->getType();
3601 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003602 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003603 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003604 return;
3605 }
Eli Friedman73397492009-03-03 06:41:03 +00003606
John McCall183700f2009-09-21 23:43:11 +00003607 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003608 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3609 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003610 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003611 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3612 } else if (ComplexMode) {
3613 if (!OldTy->isComplexType())
3614 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3615 } else {
3616 if (!OldTy->isFloatingType())
3617 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3618 }
3619
Mike Stump390b4cc2009-05-16 07:39:55 +00003620 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3621 // and friends, at least with glibc.
3622 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3623 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003624 // FIXME: Make sure floating-point mappings are accurate
3625 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003626 QualType NewTy;
3627 switch (DestWidth) {
3628 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003629 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003630 return;
3631 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003632 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003633 return;
3634 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003635 if (!IntegerMode) {
3636 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3637 return;
3638 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003639 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003640 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003641 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003642 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003643 break;
3644 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003645 if (!IntegerMode) {
3646 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3647 return;
3648 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003649 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003650 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003651 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003652 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003653 break;
3654 case 32:
3655 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003656 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003657 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003658 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003659 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003660 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003661 break;
3662 case 64:
3663 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003664 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003665 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003666 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003667 NewTy = S.Context.LongTy;
3668 else
3669 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003670 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003671 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003672 NewTy = S.Context.UnsignedLongTy;
3673 else
3674 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003675 break;
Eli Friedman73397492009-03-03 06:41:03 +00003676 case 96:
3677 NewTy = S.Context.LongDoubleTy;
3678 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003679 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003680 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3681 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003682 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3683 return;
3684 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003685 if (IntegerMode) {
3686 if (OldTy->isSignedIntegerType())
3687 NewTy = S.Context.Int128Ty;
3688 else
3689 NewTy = S.Context.UnsignedInt128Ty;
3690 } else
3691 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003692 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003693 }
3694
Eli Friedman73397492009-03-03 06:41:03 +00003695 if (ComplexMode) {
3696 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003697 }
3698
3699 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003700 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3701 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3702 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003703 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003704
3705 D->addAttr(::new (S.Context)
3706 ModeAttr(Attr.getRange(), S.Context, Name,
3707 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003708}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003709
Chandler Carruth1b03c872011-07-02 00:01:44 +00003710static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003711 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003712 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003713 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003714
Nick Lewycky78d1a102012-07-24 01:40:49 +00003715 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3716 if (!VD->hasGlobalStorage())
3717 S.Diag(Attr.getLoc(),
3718 diag::warn_attribute_requires_functions_or_static_globals)
3719 << Attr.getName();
3720 } else if (!isFunctionOrMethod(D)) {
3721 S.Diag(Attr.getLoc(),
3722 diag::warn_attribute_requires_functions_or_static_globals)
3723 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003724 return;
3725 }
Mike Stumpbf916502009-07-24 19:02:52 +00003726
Michael Han51d8c522013-01-24 16:46:58 +00003727 D->addAttr(::new (S.Context)
3728 NoDebugAttr(Attr.getRange(), S.Context,
3729 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003730}
3731
Chandler Carruth1b03c872011-07-02 00:01:44 +00003732static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003733 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003734 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003735 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003736
Mike Stumpbf916502009-07-24 19:02:52 +00003737
Chandler Carruth87c44602011-07-01 23:49:12 +00003738 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003740 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003741 return;
3742 }
Mike Stumpbf916502009-07-24 19:02:52 +00003743
Michael Han51d8c522013-01-24 16:46:58 +00003744 D->addAttr(::new (S.Context)
3745 NoInlineAttr(Attr.getRange(), S.Context,
3746 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003747}
3748
Chandler Carruth1b03c872011-07-02 00:01:44 +00003749static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3750 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003751 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003752 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003753 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003754
Chris Lattner7255a2d2010-06-22 00:03:40 +00003755
Chandler Carruth87c44602011-07-01 23:49:12 +00003756 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003758 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003759 return;
3760 }
3761
Michael Han51d8c522013-01-24 16:46:58 +00003762 D->addAttr(::new (S.Context)
3763 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3764 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003765}
3766
Chandler Carruth1b03c872011-07-02 00:01:44 +00003767static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003768 if (S.LangOpts.CUDA) {
3769 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003770 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003771 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3772 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003773 return;
3774 }
3775
Chandler Carruth87c44602011-07-01 23:49:12 +00003776 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003777 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003778 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003779 return;
3780 }
3781
Michael Han51d8c522013-01-24 16:46:58 +00003782 D->addAttr(::new (S.Context)
3783 CUDAConstantAttr(Attr.getRange(), S.Context,
3784 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003785 } else {
3786 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3787 }
3788}
3789
Chandler Carruth1b03c872011-07-02 00:01:44 +00003790static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003791 if (S.LangOpts.CUDA) {
3792 // check the attribute arguments.
3793 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003794 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3795 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003796 return;
3797 }
3798
Chandler Carruth87c44602011-07-01 23:49:12 +00003799 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003800 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003801 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003802 return;
3803 }
3804
Michael Han51d8c522013-01-24 16:46:58 +00003805 D->addAttr(::new (S.Context)
3806 CUDADeviceAttr(Attr.getRange(), S.Context,
3807 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003808 } else {
3809 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3810 }
3811}
3812
Chandler Carruth1b03c872011-07-02 00:01:44 +00003813static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003814 if (S.LangOpts.CUDA) {
3815 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003816 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003817 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003818
Chandler Carruth87c44602011-07-01 23:49:12 +00003819 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003821 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003822 return;
3823 }
3824
Chandler Carruth87c44602011-07-01 23:49:12 +00003825 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003826 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003827 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003828 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003829 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3830 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003831 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003832 "void");
3833 } else {
3834 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3835 << FD->getType();
3836 }
3837 return;
3838 }
3839
Michael Han51d8c522013-01-24 16:46:58 +00003840 D->addAttr(::new (S.Context)
3841 CUDAGlobalAttr(Attr.getRange(), S.Context,
3842 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003843 } else {
3844 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3845 }
3846}
3847
Chandler Carruth1b03c872011-07-02 00:01:44 +00003848static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003849 if (S.LangOpts.CUDA) {
3850 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003851 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003852 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003853
Peter Collingbourneced76712010-12-01 03:15:31 +00003854
Chandler Carruth87c44602011-07-01 23:49:12 +00003855 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003857 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003858 return;
3859 }
3860
Michael Han51d8c522013-01-24 16:46:58 +00003861 D->addAttr(::new (S.Context)
3862 CUDAHostAttr(Attr.getRange(), S.Context,
3863 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003864 } else {
3865 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3866 }
3867}
3868
Chandler Carruth1b03c872011-07-02 00:01:44 +00003869static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003870 if (S.LangOpts.CUDA) {
3871 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003872 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003873 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003874
Chandler Carruth87c44602011-07-01 23:49:12 +00003875 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003877 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 return;
3879 }
3880
Michael Han51d8c522013-01-24 16:46:58 +00003881 D->addAttr(::new (S.Context)
3882 CUDASharedAttr(Attr.getRange(), S.Context,
3883 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 } else {
3885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3886 }
3887}
3888
Chandler Carruth1b03c872011-07-02 00:01:44 +00003889static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003890 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003891 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003892 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003893
Chandler Carruth87c44602011-07-01 23:49:12 +00003894 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003895 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003897 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003898 return;
3899 }
Mike Stumpbf916502009-07-24 19:02:52 +00003900
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003901 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003902 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003903 return;
3904 }
Mike Stumpbf916502009-07-24 19:02:52 +00003905
Michael Han51d8c522013-01-24 16:46:58 +00003906 D->addAttr(::new (S.Context)
3907 GNUInlineAttr(Attr.getRange(), S.Context,
3908 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003909}
3910
Chandler Carruth1b03c872011-07-02 00:01:44 +00003911static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003912 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003913
Aaron Ballmanfff32482012-12-09 17:45:41 +00003914 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003915 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003916 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3917 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003918 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003919 return;
3920
Chandler Carruth87c44602011-07-01 23:49:12 +00003921 if (!isa<ObjCMethodDecl>(D)) {
3922 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3923 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003924 return;
3925 }
3926
Chandler Carruth87c44602011-07-01 23:49:12 +00003927 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003928 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003929 D->addAttr(::new (S.Context)
3930 FastCallAttr(Attr.getRange(), S.Context,
3931 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003932 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003933 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003934 D->addAttr(::new (S.Context)
3935 StdCallAttr(Attr.getRange(), S.Context,
3936 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003937 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003938 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003939 D->addAttr(::new (S.Context)
3940 ThisCallAttr(Attr.getRange(), S.Context,
3941 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003942 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003943 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003944 D->addAttr(::new (S.Context)
3945 CDeclAttr(Attr.getRange(), S.Context,
3946 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003947 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003948 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003949 D->addAttr(::new (S.Context)
3950 PascalAttr(Attr.getRange(), S.Context,
3951 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003952 return;
Charles Davise8519c32013-08-30 04:39:01 +00003953 case AttributeList::AT_MSABI:
3954 D->addAttr(::new (S.Context)
3955 MSABIAttr(Attr.getRange(), S.Context,
3956 Attr.getAttributeSpellingListIndex()));
3957 return;
3958 case AttributeList::AT_SysVABI:
3959 D->addAttr(::new (S.Context)
3960 SysVABIAttr(Attr.getRange(), S.Context,
3961 Attr.getAttributeSpellingListIndex()));
3962 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003963 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003964 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003965 switch (CC) {
3966 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003967 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003968 break;
3969 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003970 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003971 break;
3972 default:
3973 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003974 }
3975
Michael Han51d8c522013-01-24 16:46:58 +00003976 D->addAttr(::new (S.Context)
3977 PcsAttr(Attr.getRange(), S.Context, PCS,
3978 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003979 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003980 }
Derek Schuff263366f2012-10-16 22:30:41 +00003981 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003982 D->addAttr(::new (S.Context)
3983 PnaclCallAttr(Attr.getRange(), S.Context,
3984 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003985 return;
Guy Benyei38980082012-12-25 08:53:55 +00003986 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003987 D->addAttr(::new (S.Context)
3988 IntelOclBiccAttr(Attr.getRange(), S.Context,
3989 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003990 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003991
Abramo Bagnarae215f722010-04-30 13:10:51 +00003992 default:
3993 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003994 }
3995}
3996
Chandler Carruth1b03c872011-07-02 00:01:44 +00003997static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003998 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003999}
4000
Guy Benyei1db70402013-03-24 13:58:12 +00004001static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Guy Benyei1db70402013-03-24 13:58:12 +00004002 Expr *E = Attr.getArg(0);
4003 llvm::APSInt ArgNum(32);
4004 if (E->isTypeDependent() || E->isValueDependent() ||
4005 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4007 << Attr.getName() << AANT_ArgumentIntegerConstant
4008 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004009 return;
4010 }
4011
4012 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4013 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4014}
4015
Aaron Ballmanfff32482012-12-09 17:45:41 +00004016bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4017 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004018 if (attr.isInvalid())
4019 return true;
4020
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004021 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4022 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004023 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4024 << attr.getName() << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004025 attr.setInvalid();
4026 return true;
4027 }
4028
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004029 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4030 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004031 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004032 case AttributeList::AT_CDecl: CC = CC_C; break;
4033 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4034 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4035 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4036 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00004037 case AttributeList::AT_MSABI:
4038 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
4039 CC_X86_64Win64;
4040 break;
4041 case AttributeList::AT_SysVABI:
4042 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4043 CC_C;
4044 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004045 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004046 Expr *Arg = attr.getArg(0);
4047 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004048 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004049 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4050 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004051 attr.setInvalid();
4052 return true;
4053 }
4054
Chris Lattner5f9e2722011-07-23 10:55:15 +00004055 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004056 if (StrRef == "aapcs") {
4057 CC = CC_AAPCS;
4058 break;
4059 } else if (StrRef == "aapcs-vfp") {
4060 CC = CC_AAPCS_VFP;
4061 break;
4062 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004063
4064 attr.setInvalid();
4065 Diag(attr.getLoc(), diag::err_invalid_pcs);
4066 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004067 }
Derek Schuff263366f2012-10-16 22:30:41 +00004068 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004069 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004070 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004071 }
4072
Aaron Ballman82bfa192012-10-02 14:26:08 +00004073 const TargetInfo &TI = Context.getTargetInfo();
4074 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4075 if (A == TargetInfo::CCCR_Warning) {
4076 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004077
4078 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4079 if (FD)
4080 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4081 TargetInfo::CCMT_NonMember;
4082 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004083 }
4084
John McCall711c52b2011-01-05 12:14:39 +00004085 return false;
4086}
4087
Chandler Carruth1b03c872011-07-02 00:01:44 +00004088static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004089 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004090
4091 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004092 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004093 return;
4094
Chandler Carruth87c44602011-07-01 23:49:12 +00004095 if (!isa<ObjCMethodDecl>(D)) {
4096 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4097 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004098 return;
4099 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004100
Michael Han51d8c522013-01-24 16:46:58 +00004101 D->addAttr(::new (S.Context)
4102 RegparmAttr(Attr.getRange(), S.Context, numParams,
4103 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004104}
4105
4106/// Checks a regparm attribute, returning true if it is ill-formed and
4107/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004108bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4109 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004110 return true;
4111
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004112 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004113 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004114 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004115 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004116
Chandler Carruth87c44602011-07-01 23:49:12 +00004117 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004118 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004119 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004120 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004121 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4122 << Attr.getName() << AANT_ArgumentIntegerConstant
4123 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004124 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004125 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004126 }
4127
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004128 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004129 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004130 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004131 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004132 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004133 }
4134
John McCall711c52b2011-01-05 12:14:39 +00004135 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004136 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004137 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004138 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004139 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004140 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004141 }
4142
John McCall711c52b2011-01-05 12:14:39 +00004143 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004144}
4145
Chandler Carruth1b03c872011-07-02 00:01:44 +00004146static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004147 if (S.LangOpts.CUDA) {
4148 // check the attribute arguments.
4149 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004150 // FIXME: 0 is not okay.
4151 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004152 return;
4153 }
4154
Chandler Carruth87c44602011-07-01 23:49:12 +00004155 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004156 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004157 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004158 return;
4159 }
4160
4161 Expr *MaxThreadsExpr = Attr.getArg(0);
4162 llvm::APSInt MaxThreads(32);
4163 if (MaxThreadsExpr->isTypeDependent() ||
4164 MaxThreadsExpr->isValueDependent() ||
4165 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004166 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004167 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004168 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004169 return;
4170 }
4171
4172 llvm::APSInt MinBlocks(32);
4173 if (Attr.getNumArgs() > 1) {
4174 Expr *MinBlocksExpr = Attr.getArg(1);
4175 if (MinBlocksExpr->isTypeDependent() ||
4176 MinBlocksExpr->isValueDependent() ||
4177 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004178 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004179 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004180 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004181 return;
4182 }
4183 }
4184
Michael Han51d8c522013-01-24 16:46:58 +00004185 D->addAttr(::new (S.Context)
4186 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4187 MaxThreads.getZExtValue(),
4188 MinBlocks.getZExtValue(),
4189 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004190 } else {
4191 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4192 }
4193}
4194
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004195static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4196 const AttributeList &Attr) {
4197 StringRef AttrName = Attr.getName()->getName();
4198 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004199 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004200 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004201 return;
4202 }
4203
4204 if (Attr.getNumArgs() != 2) {
4205 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004206 << Attr.getName() << /* required args = */ 3;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004207 return;
4208 }
4209
4210 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4211
4212 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4214 << Attr.getName() << ExpectedFunctionOrMethod;
4215 return;
4216 }
4217
4218 uint64_t ArgumentIdx;
4219 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4220 Attr.getLoc(), 2,
4221 Attr.getArg(0), ArgumentIdx))
4222 return;
4223
4224 uint64_t TypeTagIdx;
4225 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4226 Attr.getLoc(), 3,
4227 Attr.getArg(1), TypeTagIdx))
4228 return;
4229
4230 bool IsPointer = (AttrName == "pointer_with_type_tag");
4231 if (IsPointer) {
4232 // Ensure that buffer has a pointer type.
4233 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4234 if (!BufferTy->isPointerType()) {
4235 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004236 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004237 }
4238 }
4239
Michael Han51d8c522013-01-24 16:46:58 +00004240 D->addAttr(::new (S.Context)
4241 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4242 ArgumentIdx, TypeTagIdx, IsPointer,
4243 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004244}
4245
4246static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4247 const AttributeList &Attr) {
4248 IdentifierInfo *PointerKind = Attr.getParameterName();
4249 if (!PointerKind) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004251 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004252 return;
4253 }
4254
4255 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4256
Michael Han51d8c522013-01-24 16:46:58 +00004257 D->addAttr(::new (S.Context)
4258 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4259 MatchingCType,
4260 Attr.getLayoutCompatible(),
4261 Attr.getMustBeNull(),
4262 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004263}
4264
Chris Lattner0744e5f2008-06-29 00:23:49 +00004265//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004266// Checker-specific attribute handlers.
4267//===----------------------------------------------------------------------===//
4268
John McCallc7ad3812011-01-25 03:31:58 +00004269static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004270 return type->isDependentType() ||
4271 type->isObjCObjectPointerType() ||
4272 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004273}
4274static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004275 return type->isDependentType() ||
4276 type->isPointerType() ||
4277 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004278}
4279
Chandler Carruth1b03c872011-07-02 00:01:44 +00004280static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004281 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004282 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004283 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004284 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004285 return;
4286 }
4287
4288 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004289 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004290 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4291 cf = false;
4292 } else {
4293 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4294 cf = true;
4295 }
4296
4297 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004298 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004299 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004300 return;
4301 }
4302
4303 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004304 param->addAttr(::new (S.Context)
4305 CFConsumedAttr(Attr.getRange(), S.Context,
4306 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004307 else
Michael Han51d8c522013-01-24 16:46:58 +00004308 param->addAttr(::new (S.Context)
4309 NSConsumedAttr(Attr.getRange(), S.Context,
4310 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004311}
4312
Chandler Carruth1b03c872011-07-02 00:01:44 +00004313static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4314 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004315 if (!isa<ObjCMethodDecl>(D)) {
4316 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004317 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004318 return;
4319 }
4320
Michael Han51d8c522013-01-24 16:46:58 +00004321 D->addAttr(::new (S.Context)
4322 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4323 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004324}
4325
Chandler Carruth1b03c872011-07-02 00:01:44 +00004326static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4327 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004328
John McCallc7ad3812011-01-25 03:31:58 +00004329 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004330
Chandler Carruth87c44602011-07-01 23:49:12 +00004331 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004332 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004333 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004334 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004335 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004336 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4337 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004338 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004339 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004340 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004341 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004342 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004343 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004344 return;
4345 }
Mike Stumpbf916502009-07-24 19:02:52 +00004346
John McCallc7ad3812011-01-25 03:31:58 +00004347 bool typeOK;
4348 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004349 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004350 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004351 case AttributeList::AT_NSReturnsAutoreleased:
4352 case AttributeList::AT_NSReturnsRetained:
4353 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004354 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4355 cf = false;
4356 break;
4357
Sean Hunt8e083e72012-06-19 23:57:03 +00004358 case AttributeList::AT_CFReturnsRetained:
4359 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004360 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4361 cf = true;
4362 break;
4363 }
4364
4365 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004366 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004367 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004368 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004369 }
Mike Stumpbf916502009-07-24 19:02:52 +00004370
Chandler Carruth87c44602011-07-01 23:49:12 +00004371 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004372 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004373 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004374 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004375 D->addAttr(::new (S.Context)
4376 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4377 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004378 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004379 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004380 D->addAttr(::new (S.Context)
4381 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4382 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004383 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004384 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004385 D->addAttr(::new (S.Context)
4386 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4387 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004388 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004389 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004390 D->addAttr(::new (S.Context)
4391 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4392 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004393 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004395 D->addAttr(::new (S.Context)
4396 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4397 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004398 return;
4399 };
4400}
4401
John McCalldc7c5ad2011-07-22 08:53:00 +00004402static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4403 const AttributeList &attr) {
4404 SourceLocation loc = attr.getLoc();
4405
4406 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4407
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004408 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004409 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004410 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004411 return;
4412 }
4413
4414 // Check that the method returns a normal pointer.
4415 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004416
4417 if (!resultType->isReferenceType() &&
4418 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004419 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4420 << SourceRange(loc)
4421 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4422
4423 // Drop the attribute.
4424 return;
4425 }
4426
Michael Han51d8c522013-01-24 16:46:58 +00004427 method->addAttr(::new (S.Context)
4428 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4429 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004430}
4431
Fariborz Jahanian84101132012-09-07 23:46:23 +00004432static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4433 const AttributeList &attr) {
4434 SourceLocation loc = attr.getLoc();
4435 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4436
4437 if (!method) {
4438 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4439 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4440 return;
4441 }
4442 DeclContext *DC = method->getDeclContext();
4443 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4444 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4445 << attr.getName() << 0;
4446 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4447 return;
4448 }
4449 if (method->getMethodFamily() == OMF_dealloc) {
4450 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4451 << attr.getName() << 1;
4452 return;
4453 }
4454
Michael Han51d8c522013-01-24 16:46:58 +00004455 method->addAttr(::new (S.Context)
4456 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4457 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004458}
4459
John McCall8dfac0b2011-09-30 05:12:12 +00004460/// Handle cf_audited_transfer and cf_unknown_transfer.
4461static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4462 if (!isa<FunctionDecl>(D)) {
4463 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004464 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004465 return;
4466 }
4467
Sean Hunt8e083e72012-06-19 23:57:03 +00004468 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004469
4470 // Check whether there's a conflicting attribute already present.
4471 Attr *Existing;
4472 if (IsAudited) {
4473 Existing = D->getAttr<CFUnknownTransferAttr>();
4474 } else {
4475 Existing = D->getAttr<CFAuditedTransferAttr>();
4476 }
4477 if (Existing) {
4478 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4479 << A.getName()
4480 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4481 << A.getRange() << Existing->getRange();
4482 return;
4483 }
4484
4485 // All clear; add the attribute.
4486 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004487 D->addAttr(::new (S.Context)
4488 CFAuditedTransferAttr(A.getRange(), S.Context,
4489 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004490 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004491 D->addAttr(::new (S.Context)
4492 CFUnknownTransferAttr(A.getRange(), S.Context,
4493 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004494 }
4495}
4496
John McCallfe98da02011-09-29 07:17:38 +00004497static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4498 const AttributeList &Attr) {
4499 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4500 if (!RD || RD->isUnion()) {
4501 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004502 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004503 }
4504
4505 IdentifierInfo *ParmName = Attr.getParameterName();
4506
4507 // In Objective-C, verify that the type names an Objective-C type.
4508 // We don't want to check this outside of ObjC because people sometimes
4509 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004510 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004511 // Check for an existing type with this name.
4512 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4513 Sema::LookupOrdinaryName);
4514 if (S.LookupName(R, Sc)) {
4515 NamedDecl *Target = R.getFoundDecl();
4516 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4517 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4518 S.Diag(Target->getLocStart(), diag::note_declared_at);
4519 }
4520 }
4521 }
4522
Michael Han51d8c522013-01-24 16:46:58 +00004523 D->addAttr(::new (S.Context)
4524 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4525 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004526}
4527
Chandler Carruth1b03c872011-07-02 00:01:44 +00004528static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4529 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004530 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004531
Chandler Carruth87c44602011-07-01 23:49:12 +00004532 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004533 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004534}
4535
Chandler Carruth1b03c872011-07-02 00:01:44 +00004536static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4537 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004538 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004539 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004540 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004541 return;
4542 }
4543
Chandler Carruth87c44602011-07-01 23:49:12 +00004544 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004545 QualType type = vd->getType();
4546
4547 if (!type->isDependentType() &&
4548 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004549 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004550 << type;
4551 return;
4552 }
4553
4554 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4555
4556 // If we have no lifetime yet, check the lifetime we're presumably
4557 // going to infer.
4558 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4559 lifetime = type->getObjCARCImplicitLifetime();
4560
4561 switch (lifetime) {
4562 case Qualifiers::OCL_None:
4563 assert(type->isDependentType() &&
4564 "didn't infer lifetime for non-dependent type?");
4565 break;
4566
4567 case Qualifiers::OCL_Weak: // meaningful
4568 case Qualifiers::OCL_Strong: // meaningful
4569 break;
4570
4571 case Qualifiers::OCL_ExplicitNone:
4572 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004573 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004574 << (lifetime == Qualifiers::OCL_Autoreleasing);
4575 break;
4576 }
4577
Chandler Carruth87c44602011-07-01 23:49:12 +00004578 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004579 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4580 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004581}
4582
Francois Pichet11542142010-12-19 06:50:37 +00004583//===----------------------------------------------------------------------===//
4584// Microsoft specific attribute handlers.
4585//===----------------------------------------------------------------------===//
4586
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004587// Check if MS extensions or some other language extensions are enabled. If
4588// not, issue a diagnostic that the given attribute is unused.
4589static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4590 bool OtherExtension = false) {
4591 if (S.LangOpts.MicrosoftExt || OtherExtension)
4592 return true;
4593 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4594 return false;
4595}
4596
Chandler Carruth1b03c872011-07-02 00:01:44 +00004597static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004598 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4599 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004600
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004601 // check the attribute arguments.
4602 if (!checkAttributeNumArgs(S, Attr, 1))
4603 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004604
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004605 Expr *Arg = Attr.getArg(0);
4606 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4607 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004608 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4609 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004610 return;
4611 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004612
David Majnemerbb0ed292013-08-09 08:56:20 +00004613 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4614 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004615 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004616 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4617 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004618
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004619 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004620 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004621 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4622 return;
4623 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004624
David Majnemerbb0ed292013-08-09 08:56:20 +00004625 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004626 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004627 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004628 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4629 return;
4630 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004631 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004632 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4633 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004634 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004635 }
Francois Pichet11542142010-12-19 06:50:37 +00004636
David Majnemerbb0ed292013-08-09 08:56:20 +00004637 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4638 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004639}
4640
John McCallc052dbb2012-05-22 21:28:12 +00004641static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004642 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004643 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004644
4645 AttributeList::Kind Kind = Attr.getKind();
4646 if (Kind == AttributeList::AT_SingleInheritance)
4647 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004648 ::new (S.Context)
4649 SingleInheritanceAttr(Attr.getRange(), S.Context,
4650 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004651 else if (Kind == AttributeList::AT_MultipleInheritance)
4652 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004653 ::new (S.Context)
4654 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4655 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004656 else if (Kind == AttributeList::AT_VirtualInheritance)
4657 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004658 ::new (S.Context)
4659 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4660 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004661}
4662
4663static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004664 if (!checkMicrosoftExt(S, Attr))
4665 return;
4666
4667 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004668 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004669 D->addAttr(
4670 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4671 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004672}
4673
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004674static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004675 if (!checkMicrosoftExt(S, Attr))
4676 return;
4677 D->addAttr(::new (S.Context)
4678 ForceInlineAttr(Attr.getRange(), S.Context,
4679 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004680}
4681
Reid Klecknera7225342013-05-20 14:02:37 +00004682static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4683 if (!checkMicrosoftExt(S, Attr))
4684 return;
4685 // Check linkage after possibly merging declaratinos. See
4686 // checkAttributesAfterMerging().
4687 D->addAttr(::new (S.Context)
4688 SelectAnyAttr(Attr.getRange(), S.Context,
4689 Attr.getAttributeSpellingListIndex()));
4690}
4691
Ted Kremenekb71368d2009-05-09 02:44:38 +00004692//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004693// Top Level Sema Entry Points
4694//===----------------------------------------------------------------------===//
4695
Richard Smith4a97b8e2013-08-29 00:47:48 +00004696/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4697/// the attribute applies to decls. If the attribute is a type attribute, just
4698/// silently ignore it if a GNU attribute.
4699static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4700 const AttributeList &Attr,
4701 bool IncludeCXX11Attributes) {
4702 if (Attr.isInvalid())
4703 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004704
Richard Smith4a97b8e2013-08-29 00:47:48 +00004705 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4706 // instead.
4707 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4708 return;
4709
Chris Lattner803d0802008-06-29 00:43:07 +00004710 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004711 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4712 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4713 case AttributeList::AT_IBOutletCollection:
4714 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004715 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004716 case AttributeList::AT_ObjCGC:
4717 case AttributeList::AT_VectorSize:
4718 case AttributeList::AT_NeonVectorType:
4719 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004720 case AttributeList::AT_Ptr32:
4721 case AttributeList::AT_Ptr64:
4722 case AttributeList::AT_SPtr:
4723 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004724 // Ignore these, these are type attributes, handled by
4725 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004726 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004727 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4728 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4729 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4730 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004731 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004732 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004733 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004734 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004735 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4736 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4737 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004738 handleDependencyAttr(S, scope, D, Attr);
4739 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004740 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4741 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4742 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004743 case AttributeList::AT_CXX11NoReturn:
4744 handleCXX11NoReturnAttr(S, D, Attr);
4745 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004746 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004747 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004748 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004749 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4750 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004751 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004752 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004753 case AttributeList::AT_MinSize:
4754 handleMinSizeAttr(S, D, Attr);
4755 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4757 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4758 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004759 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4760 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004761 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4762 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004763 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004764 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004765 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4766 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004767 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004768 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4769 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004770 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004771 case AttributeList::AT_ownership_returns:
4772 case AttributeList::AT_ownership_takes:
4773 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004774 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004775 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4776 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4777 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4778 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4779 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4780 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4781 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004782
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004784 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004785 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004786 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004787
Sean Hunt8e083e72012-06-19 23:57:03 +00004788 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004789 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4790
Fariborz Jahanian84101132012-09-07 23:46:23 +00004791 case AttributeList::AT_ObjCRequiresSuper:
4792 handleObjCRequiresSuperAttr(S, D, Attr); break;
4793
Sean Hunt8e083e72012-06-19 23:57:03 +00004794 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004795 handleNSBridgedAttr(S, scope, D, Attr); break;
4796
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_CFAuditedTransfer:
4798 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004799 handleCFTransferAttr(S, D, Attr); break;
4800
Ted Kremenekb71368d2009-05-09 02:44:38 +00004801 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_CFConsumed:
4803 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4804 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004805 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004806
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_NSReturnsAutoreleased:
4808 case AttributeList::AT_NSReturnsNotRetained:
4809 case AttributeList::AT_CFReturnsNotRetained:
4810 case AttributeList::AT_NSReturnsRetained:
4811 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004812 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004813
Tanya Lattner0df579e2012-07-09 22:06:01 +00004814 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004815 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004816 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004817
Joey Gouly37453b92013-03-08 09:42:32 +00004818 case AttributeList::AT_VecTypeHint:
4819 handleVecTypeHint(S, D, Attr); break;
4820
Joey Gouly96cead52013-03-14 09:54:43 +00004821 case AttributeList::AT_Endian:
4822 handleEndianAttr(S, D, Attr);
4823 break;
4824
Sean Hunt8e083e72012-06-19 23:57:03 +00004825 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004826 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004827
Sean Hunt8e083e72012-06-19 23:57:03 +00004828 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4829 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4830 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004831 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004832 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004833 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004834 handleArcWeakrefUnavailableAttr (S, D, Attr);
4835 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004836 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004837 handleObjCRootClassAttr(S, D, Attr);
4838 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004840 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004841 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4843 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004844 handleReturnsTwiceAttr(S, D, Attr);
4845 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004846 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004847 case AttributeList::AT_Visibility:
4848 handleVisibilityAttr(S, D, Attr, false);
4849 break;
4850 case AttributeList::AT_TypeVisibility:
4851 handleVisibilityAttr(S, D, Attr, true);
4852 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004853 case AttributeList::AT_WarnUnused:
4854 handleWarnUnusedAttr(S, D, Attr);
4855 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004857 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004858 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4859 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4860 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4861 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004862 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004863 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004864 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004865 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004866 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004867 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004868 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004869 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004870 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4871 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4872 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4873 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4874 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4875 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4876 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4877 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4878 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004879 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004880 // Just ignore
4881 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004882 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004883 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004884 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004885 case AttributeList::AT_StdCall:
4886 case AttributeList::AT_CDecl:
4887 case AttributeList::AT_FastCall:
4888 case AttributeList::AT_ThisCall:
4889 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004890 case AttributeList::AT_MSABI:
4891 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004892 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004893 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004894 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004895 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004896 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004897 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004898 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004899 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004900 case AttributeList::AT_OpenCLImageAccess:
4901 handleOpenCLImageAccessAttr(S, D, Attr);
4902 break;
John McCallc052dbb2012-05-22 21:28:12 +00004903
4904 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004905 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004906 handleMsStructAttr(S, D, Attr);
4907 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004908 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004909 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004910 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004911 case AttributeList::AT_SingleInheritance:
4912 case AttributeList::AT_MultipleInheritance:
4913 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004914 handleInheritanceAttr(S, D, Attr);
4915 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004916 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004917 handlePortabilityAttr(S, D, Attr);
4918 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004919 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004920 handleForceInlineAttr(S, D, Attr);
4921 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004922 case AttributeList::AT_SelectAny:
4923 handleSelectAnyAttr(S, D, Attr);
4924 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004925
4926 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004927 case AttributeList::AT_AssertExclusiveLock:
4928 handleAssertExclusiveLockAttr(S, D, Attr);
4929 break;
4930 case AttributeList::AT_AssertSharedLock:
4931 handleAssertSharedLockAttr(S, D, Attr);
4932 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004933 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004934 handleGuardedVarAttr(S, D, Attr);
4935 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004936 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004937 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004938 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004939 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004940 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004941 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004942 case AttributeList::AT_NoSanitizeAddress:
4943 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004944 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004945 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004946 handleNoThreadSafetyAnalysis(S, D, Attr);
4947 break;
4948 case AttributeList::AT_NoSanitizeThread:
4949 handleNoSanitizeThread(S, D, Attr);
4950 break;
4951 case AttributeList::AT_NoSanitizeMemory:
4952 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004953 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004954 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004955 handleLockableAttr(S, D, Attr);
4956 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004957 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004958 handleGuardedByAttr(S, D, Attr);
4959 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004960 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004961 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004962 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004963 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004964 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004965 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004966 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004967 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004968 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004969 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004970 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004971 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004972 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004973 handleLockReturnedAttr(S, D, Attr);
4974 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004975 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004976 handleLocksExcludedAttr(S, D, Attr);
4977 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004978 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004979 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004980 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004981 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004982 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004983 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004984 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004985 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004986 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004987 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004988 handleUnlockFunAttr(S, D, Attr);
4989 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004990 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004991 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004992 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004993 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004994 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004995 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004996
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004997 // Uniqueness analysis attributes.
4998 case AttributeList::AT_Consumes:
4999 handleConsumesAttr(S, D, Attr);
5000 break;
5001 case AttributeList::AT_CallableWhenUnconsumed:
5002 handleCallableWhenUnconsumedAttr(S, D, Attr);
5003 break;
5004 case AttributeList::AT_TestsConsumed:
5005 handleTestsConsumedAttr(S, D, Attr);
5006 break;
5007 case AttributeList::AT_TestsUnconsumed:
5008 handleTestsUnconsumedAttr(S, D, Attr);
5009 break;
5010
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005011 // Type safety attributes.
5012 case AttributeList::AT_ArgumentWithTypeTag:
5013 handleArgumentWithTypeTagAttr(S, D, Attr);
5014 break;
5015 case AttributeList::AT_TypeTagForDatatype:
5016 handleTypeTagForDatatypeAttr(S, D, Attr);
5017 break;
5018
Chris Lattner803d0802008-06-29 00:43:07 +00005019 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005020 // Ask target about the attribute.
5021 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5022 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005023 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5024 diag::warn_unhandled_ms_attribute_ignored :
5025 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005026 break;
5027 }
5028}
5029
5030/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5031/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005032void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005033 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005034 bool IncludeCXX11Attributes) {
5035 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005036 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005037
5038 // GCC accepts
5039 // static int a9 __attribute__((weakref));
5040 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005041 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005042 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005043 cast<NamedDecl>(D)->getNameAsString();
5044 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005045 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005046 }
5047}
5048
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005049// Annotation attributes are the only attributes allowed after an access
5050// specifier.
5051bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5052 const AttributeList *AttrList) {
5053 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005054 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005055 handleAnnotateAttr(*this, ASDecl, *l);
5056 } else {
5057 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5058 return true;
5059 }
5060 }
5061
5062 return false;
5063}
5064
John McCalle82247a2011-10-01 05:17:03 +00005065/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5066/// contains any decl attributes that we should warn about.
5067static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5068 for ( ; A; A = A->getNext()) {
5069 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005070 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005071 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5072
5073 if (A->getKind() == AttributeList::UnknownAttribute) {
5074 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5075 << A->getName() << A->getRange();
5076 } else {
5077 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5078 << A->getName() << A->getRange();
5079 }
5080 }
5081}
5082
5083/// checkUnusedDeclAttributes - Given a declarator which is not being
5084/// used to build a declaration, complain about any decl attributes
5085/// which might be lying around on it.
5086void Sema::checkUnusedDeclAttributes(Declarator &D) {
5087 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5088 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5089 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5090 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5091}
5092
Ryan Flynne25ff832009-07-30 03:15:39 +00005093/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005094/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005095NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5096 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005097 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005098 NamedDecl *NewD = 0;
5099 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005100 FunctionDecl *NewFD;
5101 // FIXME: Missing call to CheckFunctionDeclaration().
5102 // FIXME: Mangling?
5103 // FIXME: Is the qualifier info correct?
5104 // FIXME: Is the DeclContext correct?
5105 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5106 Loc, Loc, DeclarationName(II),
5107 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005108 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005109 FD->hasPrototype(),
5110 false/*isConstexprSpecified*/);
5111 NewD = NewFD;
5112
5113 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005114 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005115
5116 // Fake up parameter variables; they are declared as if this were
5117 // a typedef.
5118 QualType FDTy = FD->getType();
5119 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5120 SmallVector<ParmVarDecl*, 16> Params;
5121 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5122 AE = FT->arg_type_end(); AI != AE; ++AI) {
5123 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5124 Param->setScopeInfo(0, Params.size());
5125 Params.push_back(Param);
5126 }
David Blaikie4278c652011-09-21 18:16:56 +00005127 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005128 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005129 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5130 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005131 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005132 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005133 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005134 if (VD->getQualifier()) {
5135 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005136 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005137 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005138 }
5139 return NewD;
5140}
5141
James Dennett1dfbd922012-06-14 21:40:34 +00005142/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005143/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005144void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005145 if (W.getUsed()) return; // only do this once
5146 W.setUsed(true);
5147 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5148 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005149 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005150 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5151 NDId->getName()));
5152 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005153 WeakTopLevelDecl.push_back(NewD);
5154 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5155 // to insert Decl at TU scope, sorry.
5156 DeclContext *SavedContext = CurContext;
5157 CurContext = Context.getTranslationUnitDecl();
5158 PushOnScopeChains(NewD, S);
5159 CurContext = SavedContext;
5160 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005161 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005162 }
5163}
5164
Rafael Espindola65611bf2013-03-02 21:41:48 +00005165void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5166 // It's valid to "forward-declare" #pragma weak, in which case we
5167 // have to do this.
5168 LoadExternalWeakUndeclaredIdentifiers();
5169 if (!WeakUndeclaredIdentifiers.empty()) {
5170 NamedDecl *ND = NULL;
5171 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5172 if (VD->isExternC())
5173 ND = VD;
5174 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5175 if (FD->isExternC())
5176 ND = FD;
5177 if (ND) {
5178 if (IdentifierInfo *Id = ND->getIdentifier()) {
5179 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5180 = WeakUndeclaredIdentifiers.find(Id);
5181 if (I != WeakUndeclaredIdentifiers.end()) {
5182 WeakInfo W = I->second;
5183 DeclApplyPragmaWeak(S, ND, W);
5184 WeakUndeclaredIdentifiers[Id] = W;
5185 }
5186 }
5187 }
5188 }
5189}
5190
Chris Lattner0744e5f2008-06-29 00:23:49 +00005191/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5192/// it, apply them to D. This is a bit tricky because PD can have attributes
5193/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005194void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005195 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005196 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005197 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005198
Chris Lattner0744e5f2008-06-29 00:23:49 +00005199 // Walk the declarator structure, applying decl attributes that were in a type
5200 // position to the decl itself. This handles cases like:
5201 // int *__attr__(x)** D;
5202 // when X is a decl attribute.
5203 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5204 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005205 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005206
Chris Lattner0744e5f2008-06-29 00:23:49 +00005207 // Finally, apply any attributes on the decl itself.
5208 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005209 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005210}
John McCall54abf7d2009-11-04 02:18:39 +00005211
John McCallf85e1932011-06-15 23:02:42 +00005212/// Is the given declaration allowed to use a forbidden type?
5213static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5214 // Private ivars are always okay. Unfortunately, people don't
5215 // always properly make their ivars private, even in system headers.
5216 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005217 // Function declarations in sys headers will be marked unavailable.
5218 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5219 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005220 return false;
5221
5222 // Require it to be declared in a system header.
5223 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5224}
5225
5226/// Handle a delayed forbidden-type diagnostic.
5227static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5228 Decl *decl) {
5229 if (decl && isForbiddenTypeAllowed(S, decl)) {
5230 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5231 "this system declaration uses an unsupported type"));
5232 return;
5233 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005234 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005235 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005236 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005237 // kind of forbidden type messages on unavailable functions.
5238 if (FD->hasAttr<UnavailableAttr>() &&
5239 diag.getForbiddenTypeDiagnostic() ==
5240 diag::err_arc_array_param_no_ownership) {
5241 diag.Triggered = true;
5242 return;
5243 }
5244 }
John McCallf85e1932011-06-15 23:02:42 +00005245
5246 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5247 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5248 diag.Triggered = true;
5249}
5250
John McCall92576642012-05-07 06:16:41 +00005251void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5252 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005253 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005254 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005255
John McCall92576642012-05-07 06:16:41 +00005256 // When delaying diagnostics to run in the context of a parsed
5257 // declaration, we only want to actually emit anything if parsing
5258 // succeeds.
5259 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005260
John McCall92576642012-05-07 06:16:41 +00005261 // We emit all the active diagnostics in this pool or any of its
5262 // parents. In general, we'll get one pool for the decl spec
5263 // and a child pool for each declarator; in a decl group like:
5264 // deprecated_typedef foo, *bar, baz();
5265 // only the declarator pops will be passed decls. This is correct;
5266 // we really do need to consider delayed diagnostics from the decl spec
5267 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005268 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005269 do {
John McCall13489672012-05-07 06:16:58 +00005270 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005271 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5272 // This const_cast is a bit lame. Really, Triggered should be mutable.
5273 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005274 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005275 continue;
5276
John McCalleee1d542011-02-14 07:13:47 +00005277 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005278 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005279 // Don't bother giving deprecation diagnostics if the decl is invalid.
5280 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005281 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005282 break;
5283
5284 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005285 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005286 break;
John McCallf85e1932011-06-15 23:02:42 +00005287
5288 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005289 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005290 break;
John McCall2f514482010-01-27 03:50:35 +00005291 }
5292 }
John McCall92576642012-05-07 06:16:41 +00005293 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005294}
5295
John McCall13489672012-05-07 06:16:58 +00005296/// Given a set of delayed diagnostics, re-emit them as if they had
5297/// been delayed in the current context instead of in the given pool.
5298/// Essentially, this just moves them to the current pool.
5299void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5300 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5301 assert(curPool && "re-emitting in undelayed context not supported");
5302 curPool->steal(pool);
5303}
5304
John McCall54abf7d2009-11-04 02:18:39 +00005305static bool isDeclDeprecated(Decl *D) {
5306 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005307 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005308 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005309 // A category implicitly has the availability of the interface.
5310 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5311 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005312 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5313 return false;
5314}
5315
Eli Friedmanc3b23082012-08-08 21:52:41 +00005316static void
5317DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5318 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005319 const ObjCInterfaceDecl *UnknownObjCClass,
5320 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005321 DeclarationName Name = D->getDeclName();
5322 if (!Message.empty()) {
5323 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5324 S.Diag(D->getLocation(),
5325 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5326 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005327 if (ObjCPropery)
5328 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5329 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005330 } else if (!UnknownObjCClass) {
5331 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5332 S.Diag(D->getLocation(),
5333 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5334 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005335 if (ObjCPropery)
5336 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5337 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005338 } else {
5339 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5340 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5341 }
5342}
5343
John McCall9c3087b2010-08-26 02:13:20 +00005344void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005345 Decl *Ctx) {
5346 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005347 return;
5348
John McCall2f514482010-01-27 03:50:35 +00005349 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005350 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5351 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005352 DD.getUnknownObjCClass(),
5353 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005354}
5355
Chris Lattner5f9e2722011-07-23 10:55:15 +00005356void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005357 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005358 const ObjCInterfaceDecl *UnknownObjCClass,
5359 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005360 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005361 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005362 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5363 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005364 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005365 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005366 return;
5367 }
5368
5369 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005370 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005371 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005372 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005373}