blob: a82b3969075d92cac548dd6c5df7246e742f1a33 [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 assert(!Attr.isInvalid());
501
502 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000503 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000504
505 // D must be either a member field or global (potentially shared) variable.
506 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000507 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
508 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000509 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000510 }
511
Michael Handc691572012-07-23 18:48:41 +0000512 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000513}
514
Michael Handc691572012-07-23 18:48:41 +0000515static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
516 if (!checkGuardedVarAttrCommon(S, D, Attr))
517 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000518
Michael Han51d8c522013-01-24 16:46:58 +0000519 D->addAttr(::new (S.Context)
520 GuardedVarAttr(Attr.getRange(), S.Context,
521 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000522}
523
Michael Hanf1aae3b2012-08-03 17:40:43 +0000524static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000525 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000526 if (!checkGuardedVarAttrCommon(S, D, Attr))
527 return;
528
529 if (!threadSafetyCheckIsPointer(S, D, Attr))
530 return;
531
Michael Han51d8c522013-01-24 16:46:58 +0000532 D->addAttr(::new (S.Context)
533 PtGuardedVarAttr(Attr.getRange(), S.Context,
534 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000535}
536
Michael Hanf1aae3b2012-08-03 17:40:43 +0000537static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
538 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000539 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540 assert(!Attr.isInvalid());
541
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000542 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000543 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000544
545 // D must be either a member field or global (potentially shared) variable.
546 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000547 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
548 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000549 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000550 }
551
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000552 SmallVector<Expr*, 1> Args;
553 // check that all arguments are lockable objects
554 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
555 unsigned Size = Args.size();
556 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000557 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000558
Michael Handc691572012-07-23 18:48:41 +0000559 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000560
Michael Handc691572012-07-23 18:48:41 +0000561 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000562}
563
Michael Handc691572012-07-23 18:48:41 +0000564static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
565 Expr *Arg = 0;
566 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
567 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000568
Michael Handc691572012-07-23 18:48:41 +0000569 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
570}
571
Michael Hanf1aae3b2012-08-03 17:40:43 +0000572static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000573 const AttributeList &Attr) {
574 Expr *Arg = 0;
575 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
576 return;
577
578 if (!threadSafetyCheckIsPointer(S, D, Attr))
579 return;
580
581 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
582 S.Context, Arg));
583}
584
Michael Hanf1aae3b2012-08-03 17:40:43 +0000585static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000586 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000587 assert(!Attr.isInvalid());
588
589 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000591
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000592 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000593 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000594 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
595 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000596 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000597 }
598
Michael Handc691572012-07-23 18:48:41 +0000599 return true;
600}
601
602static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
603 if (!checkLockableAttrCommon(S, D, Attr))
604 return;
605
606 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
607}
608
Michael Hanf1aae3b2012-08-03 17:40:43 +0000609static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
611 if (!checkLockableAttrCommon(S, D, Attr))
612 return;
613
Michael Han51d8c522013-01-24 16:46:58 +0000614 D->addAttr(::new (S.Context)
615 ScopedLockableAttr(Attr.getRange(), S.Context,
616 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000617}
618
Kostya Serebryany85aee962013-02-26 06:58:27 +0000619static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
620 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000621 assert(!Attr.isInvalid());
622
623 if (!checkAttributeNumArgs(S, Attr, 0))
624 return;
625
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000626 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000627 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
628 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000629 return;
630 }
631
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000632 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000633 S.Context));
634}
635
Kostya Serebryany85aee962013-02-26 06:58:27 +0000636static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000637 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000638 assert(!Attr.isInvalid());
639
640 if (!checkAttributeNumArgs(S, Attr, 0))
641 return;
642
643 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000645 << Attr.getName() << ExpectedFunctionOrMethod;
646 return;
647 }
648
Michael Han51d8c522013-01-24 16:46:58 +0000649 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000650 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
651 Attr.getAttributeSpellingListIndex()));
652}
653
654static void handleNoSanitizeMemory(Sema &S, Decl *D,
655 const AttributeList &Attr) {
656 assert(!Attr.isInvalid());
657
658 if (!checkAttributeNumArgs(S, Attr, 0))
659 return;
660
661 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
663 << Attr.getName() << ExpectedFunctionOrMethod;
664 return;
665 }
666
667 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
668 S.Context));
669}
670
671static void handleNoSanitizeThread(Sema &S, Decl *D,
672 const AttributeList &Attr) {
673 assert(!Attr.isInvalid());
674
675 if (!checkAttributeNumArgs(S, Attr, 0))
676 return;
677
678 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
680 << Attr.getName() << ExpectedFunctionOrMethod;
681 return;
682 }
683
684 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
685 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000686}
687
Michael Hanf1aae3b2012-08-03 17:40:43 +0000688static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
689 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000690 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000691 assert(!Attr.isInvalid());
692
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000693 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000694 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000695
696 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000697 ValueDecl *VD = dyn_cast<ValueDecl>(D);
698 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000699 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
700 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000701 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000702 }
703
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000704 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 QualType QT = VD->getType();
706 if (!QT->isDependentType()) {
707 const RecordType *RT = getRecordType(QT);
708 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000709 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000710 << Attr.getName();
711 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000712 }
713 }
714
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000715 // Check that all arguments are lockable objects.
716 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000717 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000718 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000719
Michael Handc691572012-07-23 18:48:41 +0000720 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000721}
722
Michael Hanf1aae3b2012-08-03 17:40:43 +0000723static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000724 const AttributeList &Attr) {
725 SmallVector<Expr*, 1> Args;
726 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
727 return;
728
729 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000730 D->addAttr(::new (S.Context)
731 AcquiredAfterAttr(Attr.getRange(), S.Context,
732 StartArg, Args.size(),
733 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000734}
735
Michael Hanf1aae3b2012-08-03 17:40:43 +0000736static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000737 const AttributeList &Attr) {
738 SmallVector<Expr*, 1> Args;
739 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
740 return;
741
742 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000743 D->addAttr(::new (S.Context)
744 AcquiredBeforeAttr(Attr.getRange(), S.Context,
745 StartArg, Args.size(),
746 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000747}
748
Michael Hanf1aae3b2012-08-03 17:40:43 +0000749static bool checkLockFunAttrCommon(Sema &S, Decl *D,
750 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000751 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000752 assert(!Attr.isInvalid());
753
754 // zero or more arguments ok
755
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000756 // check that the attribute is applied to a function
757 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000758 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
759 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000760 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761 }
762
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000763 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000764 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000765
Michael Handc691572012-07-23 18:48:41 +0000766 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000767}
768
Michael Hanf1aae3b2012-08-03 17:40:43 +0000769static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000770 const AttributeList &Attr) {
771 SmallVector<Expr*, 1> Args;
772 if (!checkLockFunAttrCommon(S, D, Attr, Args))
773 return;
774
775 unsigned Size = Args.size();
776 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000777 D->addAttr(::new (S.Context)
778 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
779 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000780}
781
Michael Hanf1aae3b2012-08-03 17:40:43 +0000782static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000783 const AttributeList &Attr) {
784 SmallVector<Expr*, 1> Args;
785 if (!checkLockFunAttrCommon(S, D, Attr, Args))
786 return;
787
788 unsigned Size = Args.size();
789 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000790 D->addAttr(::new (S.Context)
791 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
792 StartArg, Size,
793 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000794}
795
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000796static void handleAssertSharedLockAttr(Sema &S, Decl *D,
797 const AttributeList &Attr) {
798 SmallVector<Expr*, 1> Args;
799 if (!checkLockFunAttrCommon(S, D, Attr, Args))
800 return;
801
802 unsigned Size = Args.size();
803 Expr **StartArg = Size == 0 ? 0 : &Args[0];
804 D->addAttr(::new (S.Context)
805 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
806 Attr.getAttributeSpellingListIndex()));
807}
808
809static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
810 const AttributeList &Attr) {
811 SmallVector<Expr*, 1> Args;
812 if (!checkLockFunAttrCommon(S, D, Attr, Args))
813 return;
814
815 unsigned Size = Args.size();
816 Expr **StartArg = Size == 0 ? 0 : &Args[0];
817 D->addAttr(::new (S.Context)
818 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
819 StartArg, Size,
820 Attr.getAttributeSpellingListIndex()));
821}
822
823
Michael Hanf1aae3b2012-08-03 17:40:43 +0000824static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
825 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000826 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000827 assert(!Attr.isInvalid());
828
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000829 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000830 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000831
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000832 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000833 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
834 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000835 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000836 }
837
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000839 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000840 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000841 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842 }
843
844 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000845 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000846
Michael Handc691572012-07-23 18:48:41 +0000847 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000848}
849
Michael Hanf1aae3b2012-08-03 17:40:43 +0000850static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000851 const AttributeList &Attr) {
852 SmallVector<Expr*, 2> Args;
853 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
854 return;
855
856 unsigned Size = Args.size();
857 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000858 D->addAttr(::new (S.Context)
859 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
860 Attr.getArg(0), StartArg, Size,
861 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000862}
863
Michael Hanf1aae3b2012-08-03 17:40:43 +0000864static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000865 const AttributeList &Attr) {
866 SmallVector<Expr*, 2> Args;
867 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
868 return;
869
870 unsigned Size = Args.size();
871 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000872 D->addAttr(::new (S.Context)
873 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
874 Attr.getArg(0), StartArg, Size,
875 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000876}
877
Michael Hanf1aae3b2012-08-03 17:40:43 +0000878static bool checkLocksRequiredCommon(Sema &S, Decl *D,
879 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000880 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000881 assert(!Attr.isInvalid());
882
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000883 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000884 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000885
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000887 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
888 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000889 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000890 }
891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000893 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000894 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000895 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000896
Michael Handc691572012-07-23 18:48:41 +0000897 return true;
898}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000899
Michael Hanf1aae3b2012-08-03 17:40:43 +0000900static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000901 const AttributeList &Attr) {
902 SmallVector<Expr*, 1> Args;
903 if (!checkLocksRequiredCommon(S, D, Attr, Args))
904 return;
905
906 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000907 D->addAttr(::new (S.Context)
908 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
909 StartArg, Args.size(),
910 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000911}
912
Michael Hanf1aae3b2012-08-03 17:40:43 +0000913static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000914 const AttributeList &Attr) {
915 SmallVector<Expr*, 1> Args;
916 if (!checkLocksRequiredCommon(S, D, Attr, Args))
917 return;
918
919 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000920 D->addAttr(::new (S.Context)
921 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
922 StartArg, Args.size(),
923 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000924}
925
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000927 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000928 assert(!Attr.isInvalid());
929
930 // zero or more arguments ok
931
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000932 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000933 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
934 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000935 return;
936 }
937
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000938 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000939 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000940 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000941 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000942 Expr **StartArg = Size == 0 ? 0 : &Args[0];
943
Michael Han51d8c522013-01-24 16:46:58 +0000944 D->addAttr(::new (S.Context)
945 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
946 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000947}
948
949static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000950 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000951 assert(!Attr.isInvalid());
952
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000953 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000954 return;
955
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000956 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000957 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
958 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000959 return;
960 }
961
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000962 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000963 SmallVector<Expr*, 1> Args;
964 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
965 unsigned Size = Args.size();
966 if (Size == 0)
967 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000968
Michael Han51d8c522013-01-24 16:46:58 +0000969 D->addAttr(::new (S.Context)
970 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
971 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000972}
973
974static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000975 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000976 assert(!Attr.isInvalid());
977
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000978 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000979 return;
980
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000981 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000982 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
983 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000984 return;
985 }
986
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000987 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000988 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000989 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000990 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000991 if (Size == 0)
992 return;
993 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000994
Michael Han51d8c522013-01-24 16:46:58 +0000995 D->addAttr(::new (S.Context)
996 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
997 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000998}
999
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001000static void handleConsumesAttr(Sema &S, Decl *D,
1001 const AttributeList &Attr) {
1002 assert(!Attr.isInvalid());
1003 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1004
1005 if (!(isa<CXXMethodDecl>(D) || isa<CXXConstructorDecl>(D))) {
1006 S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) <<
1007 Attr.getName();
1008 return;
1009 }
1010
1011 D->addAttr(::new (S.Context)
1012 ConsumesAttr(Attr.getRange(), S.Context,
1013 Attr.getAttributeSpellingListIndex()));
1014}
1015
1016static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1017 const AttributeList &Attr) {
1018 assert(!Attr.isInvalid());
1019 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1020
1021 if (!isa<CXXMethodDecl>(D)) {
1022 S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) <<
1023 Attr.getName();
1024 return;
1025 }
1026
1027 D->addAttr(::new (S.Context)
1028 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
1029 Attr.getAttributeSpellingListIndex()));
1030}
1031
1032static void handleTestsConsumedAttr(Sema &S, Decl *D,
1033 const AttributeList &Attr) {
1034 assert(!Attr.isInvalid());
1035 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1036
1037 if (!isa<CXXMethodDecl>(D)) {
1038 S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) <<
1039 Attr.getName();
1040 return;
1041 }
1042
1043 D->addAttr(::new (S.Context)
1044 TestsConsumedAttr(Attr.getRange(), S.Context,
1045 Attr.getAttributeSpellingListIndex()));
1046}
1047
1048static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1049 const AttributeList &Attr) {
1050 assert(!Attr.isInvalid());
1051 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1052
1053 if (!isa<CXXMethodDecl>(D)) {
1054 S.Diag(Attr.getLoc(), diag::warn_uniqueness_attribute_wrong_decl_type) <<
1055 Attr.getName();
1056 return;
1057 }
1058
1059 D->addAttr(::new (S.Context)
1060 TestsUnconsumedAttr(Attr.getRange(), S.Context,
1061 Attr.getAttributeSpellingListIndex()));
1062}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001063
Chandler Carruth1b03c872011-07-02 00:01:44 +00001064static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1065 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001066 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1067 if (TD == 0) {
1068 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1069 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001070 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001071 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001072 }
Mike Stumpbf916502009-07-24 19:02:52 +00001073
Richard Smitha4fa9002013-01-13 02:11:23 +00001074 // Remember this typedef decl, we will need it later for diagnostics.
1075 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001076}
1077
Chandler Carruth1b03c872011-07-02 00:01:44 +00001078static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001079 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001080 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001081 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Chandler Carruth87c44602011-07-01 23:49:12 +00001083 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001084 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001085 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001086 // If the alignment is less than or equal to 8 bits, the packed attribute
1087 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001088 if (!FD->getType()->isDependentType() &&
1089 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001090 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001091 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001092 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001093 else
Michael Han51d8c522013-01-24 16:46:58 +00001094 FD->addAttr(::new (S.Context)
1095 PackedAttr(Attr.getRange(), S.Context,
1096 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001097 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001098 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001099}
1100
Chandler Carruth1b03c872011-07-02 00:01:44 +00001101static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001102 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001103 RD->addAttr(::new (S.Context)
1104 MsStructAttr(Attr.getRange(), S.Context,
1105 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001106 else
1107 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1108}
1109
Chandler Carruth1b03c872011-07-02 00:01:44 +00001110static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001111 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001112 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001113 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001114
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001115 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001116 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001117 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001118 D->addAttr(::new (S.Context)
1119 IBActionAttr(Attr.getRange(), S.Context,
1120 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001121 return;
1122 }
1123
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001124 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001125}
1126
Ted Kremenek2f041d02011-09-29 07:02:25 +00001127static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1128 // The IBOutlet/IBOutletCollection attributes only apply to instance
1129 // variables or properties of Objective-C classes. The outlet must also
1130 // have an object reference type.
1131 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1132 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001133 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001134 << Attr.getName() << VD->getType() << 0;
1135 return false;
1136 }
1137 }
1138 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1139 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001140 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001141 << Attr.getName() << PD->getType() << 1;
1142 return false;
1143 }
1144 }
1145 else {
1146 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1147 return false;
1148 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001149
Ted Kremenek2f041d02011-09-29 07:02:25 +00001150 return true;
1151}
1152
Chandler Carruth1b03c872011-07-02 00:01:44 +00001153static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001154 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001155 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001156 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001157
1158 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001159 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001160
Michael Han51d8c522013-01-24 16:46:58 +00001161 D->addAttr(::new (S.Context)
1162 IBOutletAttr(Attr.getRange(), S.Context,
1163 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001164}
1165
Chandler Carruth1b03c872011-07-02 00:01:44 +00001166static void handleIBOutletCollection(Sema &S, Decl *D,
1167 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001168
1169 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001170 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001171 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1172 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001173 return;
1174 }
1175
Ted Kremenek2f041d02011-09-29 07:02:25 +00001176 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001177 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001178
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001179 IdentifierInfo *II = Attr.getParameterName();
1180 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001181 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001182
John McCallb3d87482010-08-24 05:47:05 +00001183 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001184 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001185 if (!TypeRep) {
1186 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1187 return;
1188 }
John McCallb3d87482010-08-24 05:47:05 +00001189 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001190 // Diagnose use of non-object type in iboutletcollection attribute.
1191 // FIXME. Gnu attribute extension ignores use of builtin types in
1192 // attributes. So, __attribute__((iboutletcollection(char))) will be
1193 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001194 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001195 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1196 return;
1197 }
Michael Han51d8c522013-01-24 16:46:58 +00001198 D->addAttr(::new (S.Context)
1199 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1200 QT, Attr.getParameterLoc(),
1201 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001202}
1203
Chandler Carruthd309c812011-07-01 23:49:16 +00001204static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001205 if (const RecordType *UT = T->getAsUnionType())
1206 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1207 RecordDecl *UD = UT->getDecl();
1208 for (RecordDecl::field_iterator it = UD->field_begin(),
1209 itend = UD->field_end(); it != itend; ++it) {
1210 QualType QT = it->getType();
1211 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1212 T = QT;
1213 return;
1214 }
1215 }
1216 }
1217}
1218
Nuno Lopes587de5b2012-05-24 00:22:00 +00001219static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001220 if (!isFunctionOrMethod(D)) {
1221 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001222 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001223 return;
1224 }
1225
Nuno Lopes587de5b2012-05-24 00:22:00 +00001226 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1227 return;
1228
Nuno Lopes587de5b2012-05-24 00:22:00 +00001229 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001230 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1231 Expr *Ex = Attr.getArg(i);
1232 uint64_t Idx;
1233 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1234 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001235 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001236
1237 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001238 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001239 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1241 << Attr.getName() << AANT_ArgumentIntegerConstant
1242 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001243 return;
1244 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001245 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001246 }
1247
1248 // check if the function returns a pointer
1249 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1250 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001251 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001252 }
1253
Michael Han51d8c522013-01-24 16:46:58 +00001254 D->addAttr(::new (S.Context)
1255 AllocSizeAttr(Attr.getRange(), S.Context,
1256 SizeArgs.data(), SizeArgs.size(),
1257 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001258}
1259
Chandler Carruth1b03c872011-07-02 00:01:44 +00001260static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001261 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1262 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001263 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001264 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001265 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001266 return;
1267 }
Mike Stumpbf916502009-07-24 19:02:52 +00001268
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001269 SmallVector<unsigned, 8> NonNullArgs;
1270 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1271 Expr *Ex = Attr.getArg(i);
1272 uint64_t Idx;
1273 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1274 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001275 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001276
1277 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001278 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001279 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001280
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001281 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001282 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001283 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001284 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001285 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001286 }
Mike Stumpbf916502009-07-24 19:02:52 +00001287
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001288 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001289 }
Mike Stumpbf916502009-07-24 19:02:52 +00001290
1291 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1292 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001293 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001294 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1295 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001296 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001297 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001298 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001299 }
Mike Stumpbf916502009-07-24 19:02:52 +00001300
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001301 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001302 if (NonNullArgs.empty()) {
1303 // Warn the trivial case only if attribute is not coming from a
1304 // macro instantiation.
1305 if (Attr.getLoc().isFileID())
1306 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001307 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001308 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001309 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001310
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001311 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001312 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001313 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001314 D->addAttr(::new (S.Context)
1315 NonNullAttr(Attr.getRange(), S.Context, start, size,
1316 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001317}
1318
Chandler Carruth1b03c872011-07-02 00:01:44 +00001319static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001320 // This attribute must be applied to a function declaration.
1321 // The first argument to the attribute must be a string,
1322 // the name of the resource, for example "malloc".
1323 // The following arguments must be argument indexes, the arguments must be
1324 // of integer type for Returns, otherwise of pointer type.
1325 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001326 // after being held. free() should be __attribute((ownership_takes)), whereas
1327 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001328
1329 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001330 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001331 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001332 return;
1333 }
1334 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001335 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001336 switch (AL.getKind()) {
1337 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001338 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001339 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001340 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1341 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 return;
1343 }
Jordy Rose2a479922010-08-12 08:54:03 +00001344 break;
1345 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001346 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001347 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001348 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1349 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001350 return;
1351 }
Jordy Rose2a479922010-08-12 08:54:03 +00001352 break;
1353 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001354 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 if (AL.getNumArgs() > 1) {
1356 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001357 << AL.getName() << AL.getNumArgs() + 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 return;
1359 }
Jordy Rose2a479922010-08-12 08:54:03 +00001360 break;
1361 default:
1362 // This should never happen given how we are called.
1363 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001364 }
1365
Chandler Carruth87c44602011-07-01 23:49:12 +00001366 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001367 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1368 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001369 return;
1370 }
1371
Chris Lattner5f9e2722011-07-23 10:55:15 +00001372 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001373
1374 // Normalize the argument, __foo__ becomes foo.
1375 if (Module.startswith("__") && Module.endswith("__"))
1376 Module = Module.substr(2, Module.size() - 4);
1377
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001378
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001379 SmallVector<unsigned, 8> OwnershipArgs;
1380 for (unsigned i = 0; i < AL.getNumArgs(); ++i) {
1381 Expr *Ex = AL.getArg(i);
1382 uint64_t Idx;
1383 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
1384 AL.getLoc(), i + 1, Ex, Idx))
1385 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001386
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001387 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001388 case OwnershipAttr::Takes:
1389 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001390 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001391 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001392 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1393 // FIXME: Should also highlight argument in decl.
1394 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001395 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001396 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001397 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398 continue;
1399 }
1400 break;
1401 }
Sean Huntcf807c42010-08-18 23:23:40 +00001402 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 if (AL.getNumArgs() > 1) {
1404 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001405 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001406 llvm::APSInt ArgNum(32);
1407 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1408 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1409 S.Diag(AL.getLoc(), diag::err_ownership_type)
1410 << "ownership_returns" << "integer"
1411 << IdxExpr->getSourceRange();
1412 return;
1413 }
1414 }
1415 break;
1416 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001417 } // switch
1418
1419 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001420 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001421 i = D->specific_attr_begin<OwnershipAttr>(),
1422 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001423 i != e; ++i) {
1424 if ((*i)->getOwnKind() != K) {
1425 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1426 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001427 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001428 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1429 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001430 }
1431 }
1432 }
1433 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001434 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001435 }
1436
1437 unsigned* start = OwnershipArgs.data();
1438 unsigned size = OwnershipArgs.size();
1439 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001440
1441 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001442 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1443 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001444 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001445 }
Sean Huntcf807c42010-08-18 23:23:40 +00001446
Michael Han51d8c522013-01-24 16:46:58 +00001447 D->addAttr(::new (S.Context)
1448 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1449 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001450}
1451
Chandler Carruth1b03c872011-07-02 00:01:44 +00001452static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001453 // Check the attribute arguments.
1454 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001455 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1456 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001457 return;
1458 }
1459
Chandler Carruth87c44602011-07-01 23:49:12 +00001460 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001462 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001463 return;
1464 }
1465
Chandler Carruth87c44602011-07-01 23:49:12 +00001466 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001467
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001468 // gcc rejects
1469 // class c {
1470 // static int a __attribute__((weakref ("v2")));
1471 // static int b() __attribute__((weakref ("f3")));
1472 // };
1473 // and ignores the attributes of
1474 // void f(void) {
1475 // static int a __attribute__((weakref ("v2")));
1476 // }
1477 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001478 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001479 if (!Ctx->isFileContext()) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001481 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001482 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001483 }
1484
1485 // The GCC manual says
1486 //
1487 // At present, a declaration to which `weakref' is attached can only
1488 // be `static'.
1489 //
1490 // It also says
1491 //
1492 // Without a TARGET,
1493 // given as an argument to `weakref' or to `alias', `weakref' is
1494 // equivalent to `weak'.
1495 //
1496 // gcc 4.4.1 will accept
1497 // int a7 __attribute__((weakref));
1498 // as
1499 // int a7 __attribute__((weak));
1500 // This looks like a bug in gcc. We reject that for now. We should revisit
1501 // it if this behaviour is actually used.
1502
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001503 // GCC rejects
1504 // static ((alias ("y"), weakref)).
1505 // Should we? How to check that weakref is before or after alias?
1506
1507 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001508 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001509 Arg = Arg->IgnoreParenCasts();
1510 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1511
Douglas Gregor5cee1192011-07-27 05:40:30 +00001512 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001514 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001515 return;
1516 }
1517 // GCC will accept anything as the argument of weakref. Should we
1518 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001519 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001520 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001521 }
1522
Michael Han51d8c522013-01-24 16:46:58 +00001523 D->addAttr(::new (S.Context)
1524 WeakRefAttr(Attr.getRange(), S.Context,
1525 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001526}
1527
Chandler Carruth1b03c872011-07-02 00:01:44 +00001528static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001529 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001530 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001531 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001532
Peter Collingbourne7a730022010-11-23 20:45:58 +00001533 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001534 Arg = Arg->IgnoreParenCasts();
1535 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001536
Douglas Gregor5cee1192011-07-27 05:40:30 +00001537 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1539 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001540 return;
1541 }
Mike Stumpbf916502009-07-24 19:02:52 +00001542
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001543 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001544 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1545 return;
1546 }
1547
Chris Lattner6b6b5372008-06-26 18:38:35 +00001548 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001549
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001550 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001551 Str->getString(),
1552 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001553}
1554
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001555static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1556 // Check the attribute arguments.
1557 if (!checkAttributeNumArgs(S, Attr, 0))
1558 return;
1559
1560 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1561 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1562 << Attr.getName() << ExpectedFunctionOrMethod;
1563 return;
1564 }
1565
Michael Han51d8c522013-01-24 16:46:58 +00001566 D->addAttr(::new (S.Context)
1567 MinSizeAttr(Attr.getRange(), S.Context,
1568 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001569}
1570
Benjamin Krameree409a92012-05-12 21:10:52 +00001571static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1572 // Check the attribute arguments.
1573 if (!checkAttributeNumArgs(S, Attr, 0))
1574 return;
1575
1576 if (!isa<FunctionDecl>(D)) {
1577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1578 << Attr.getName() << ExpectedFunction;
1579 return;
1580 }
1581
1582 if (D->hasAttr<HotAttr>()) {
1583 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1584 << Attr.getName() << "hot";
1585 return;
1586 }
1587
Michael Han51d8c522013-01-24 16:46:58 +00001588 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1589 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001590}
1591
1592static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1593 // Check the attribute arguments.
1594 if (!checkAttributeNumArgs(S, Attr, 0))
1595 return;
1596
1597 if (!isa<FunctionDecl>(D)) {
1598 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1599 << Attr.getName() << ExpectedFunction;
1600 return;
1601 }
1602
1603 if (D->hasAttr<ColdAttr>()) {
1604 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1605 << Attr.getName() << "cold";
1606 return;
1607 }
1608
Michael Han51d8c522013-01-24 16:46:58 +00001609 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1610 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001611}
1612
Chandler Carruth1b03c872011-07-02 00:01:44 +00001613static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001614 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001615 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001616 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001617
Chandler Carruth87c44602011-07-01 23:49:12 +00001618 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001620 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001621 return;
1622 }
1623
Michael Han51d8c522013-01-24 16:46:58 +00001624 D->addAttr(::new (S.Context)
1625 NakedAttr(Attr.getRange(), S.Context,
1626 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001627}
1628
Chandler Carruth1b03c872011-07-02 00:01:44 +00001629static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1630 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001631 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001632 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1634 << Attr.getName() << 0;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001635 return;
1636 }
1637
Chandler Carruth87c44602011-07-01 23:49:12 +00001638 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001639 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001640 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001641 return;
1642 }
Mike Stumpbf916502009-07-24 19:02:52 +00001643
Michael Han51d8c522013-01-24 16:46:58 +00001644 D->addAttr(::new (S.Context)
1645 AlwaysInlineAttr(Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001647}
1648
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001649static void handleTLSModelAttr(Sema &S, Decl *D,
1650 const AttributeList &Attr) {
1651 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001652 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001653 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001654
1655 Expr *Arg = Attr.getArg(0);
1656 Arg = Arg->IgnoreParenCasts();
1657 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1658
1659 // Check that it is a string.
1660 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1662 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001663 return;
1664 }
1665
Richard Smith38afbc72013-04-13 02:43:54 +00001666 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1668 << Attr.getName() << ExpectedTLSVar;
1669 return;
1670 }
1671
1672 // Check that the value.
1673 StringRef Model = Str->getString();
1674 if (Model != "global-dynamic" && Model != "local-dynamic"
1675 && Model != "initial-exec" && Model != "local-exec") {
1676 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1677 return;
1678 }
1679
Michael Han51d8c522013-01-24 16:46:58 +00001680 D->addAttr(::new (S.Context)
1681 TLSModelAttr(Attr.getRange(), S.Context, Model,
1682 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001683}
1684
Chandler Carruth1b03c872011-07-02 00:01:44 +00001685static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001686 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001687 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1689 << Attr.getName() << 0;
Ryan Flynn76168e22009-08-09 20:07:29 +00001690 return;
1691 }
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Chandler Carruth87c44602011-07-01 23:49:12 +00001693 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001694 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001695 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001696 D->addAttr(::new (S.Context)
1697 MallocAttr(Attr.getRange(), S.Context,
1698 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001699 return;
1700 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001701 }
1702
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001703 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001704}
1705
Chandler Carruth1b03c872011-07-02 00:01:44 +00001706static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001707 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001708 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001709 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001710
Michael Han51d8c522013-01-24 16:46:58 +00001711 D->addAttr(::new (S.Context)
1712 MayAliasAttr(Attr.getRange(), S.Context,
1713 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001714}
1715
Chandler Carruth1b03c872011-07-02 00:01:44 +00001716static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001717 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001718 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001719 D->addAttr(::new (S.Context)
1720 NoCommonAttr(Attr.getRange(), S.Context,
1721 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001722 else
1723 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001724 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001725}
1726
Chandler Carruth1b03c872011-07-02 00:01:44 +00001727static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001728 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001729
1730 if (S.LangOpts.CPlusPlus) {
1731 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1732 return;
1733 }
1734
Chandler Carruth87c44602011-07-01 23:49:12 +00001735 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001736 D->addAttr(::new (S.Context)
1737 CommonAttr(Attr.getRange(), S.Context,
1738 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001739 else
1740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001741 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001742}
1743
Chandler Carruth1b03c872011-07-02 00:01:44 +00001744static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001745 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001746
1747 if (S.CheckNoReturnAttr(attr)) return;
1748
Chandler Carruth87c44602011-07-01 23:49:12 +00001749 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001750 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001751 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001752 return;
1753 }
1754
Michael Han51d8c522013-01-24 16:46:58 +00001755 D->addAttr(::new (S.Context)
1756 NoReturnAttr(attr.getRange(), S.Context,
1757 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001758}
1759
1760bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001761 if (attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001762 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1763 << attr.getName() << 0;
John McCall711c52b2011-01-05 12:14:39 +00001764 attr.setInvalid();
1765 return true;
1766 }
1767
1768 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001769}
1770
Chandler Carruth1b03c872011-07-02 00:01:44 +00001771static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1772 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001773
1774 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1775 // because 'analyzer_noreturn' does not impact the type.
1776
Chandler Carruth1731e202011-07-11 23:30:35 +00001777 if(!checkAttributeNumArgs(S, Attr, 0))
1778 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001779
Chandler Carruth87c44602011-07-01 23:49:12 +00001780 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1781 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001782 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1783 && !VD->getType()->isFunctionPointerType())) {
1784 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001785 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001786 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001787 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001788 return;
1789 }
1790 }
1791
Michael Han51d8c522013-01-24 16:46:58 +00001792 D->addAttr(::new (S.Context)
1793 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1794 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001795}
1796
Richard Smithcd8ab512013-01-17 01:30:42 +00001797static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1798 const AttributeList &Attr) {
1799 // C++11 [dcl.attr.noreturn]p1:
1800 // The attribute may be applied to the declarator-id in a function
1801 // declaration.
1802 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1803 if (!FD) {
1804 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1805 << Attr.getName() << ExpectedFunctionOrMethod;
1806 return;
1807 }
1808
Michael Han51d8c522013-01-24 16:46:58 +00001809 D->addAttr(::new (S.Context)
1810 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1811 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001812}
1813
John Thompson35cc9622010-08-09 21:53:52 +00001814// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001815static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001816/*
1817 Returning a Vector Class in Registers
1818
Eric Christopherf48f3672010-12-01 22:13:54 +00001819 According to the PPU ABI specifications, a class with a single member of
1820 vector type is returned in memory when used as the return value of a function.
1821 This results in inefficient code when implementing vector classes. To return
1822 the value in a single vector register, add the vecreturn attribute to the
1823 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001824
1825 Example:
1826
1827 struct Vector
1828 {
1829 __vector float xyzw;
1830 } __attribute__((vecreturn));
1831
1832 Vector Add(Vector lhs, Vector rhs)
1833 {
1834 Vector result;
1835 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1836 return result; // This will be returned in a register
1837 }
1838*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001839 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001840 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001841 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001842 return;
1843 }
1844
Chandler Carruth87c44602011-07-01 23:49:12 +00001845 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001846 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1847 return;
1848 }
1849
Chandler Carruth87c44602011-07-01 23:49:12 +00001850 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001851 int count = 0;
1852
1853 if (!isa<CXXRecordDecl>(record)) {
1854 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1855 return;
1856 }
1857
1858 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1859 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1860 return;
1861 }
1862
Eric Christopherf48f3672010-12-01 22:13:54 +00001863 for (RecordDecl::field_iterator iter = record->field_begin();
1864 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001865 if ((count == 1) || !iter->getType()->isVectorType()) {
1866 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1867 return;
1868 }
1869 count++;
1870 }
1871
Michael Han51d8c522013-01-24 16:46:58 +00001872 D->addAttr(::new (S.Context)
1873 VecReturnAttr(Attr.getRange(), S.Context,
1874 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001875}
1876
Richard Smith3a2b7a12013-01-28 22:42:45 +00001877static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1878 const AttributeList &Attr) {
1879 if (isa<ParmVarDecl>(D)) {
1880 // [[carries_dependency]] can only be applied to a parameter if it is a
1881 // parameter of a function declaration or lambda.
1882 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1883 S.Diag(Attr.getLoc(),
1884 diag::err_carries_dependency_param_not_function_decl);
1885 return;
1886 }
1887 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001889 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001890 return;
1891 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001892
1893 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1894 Attr.getRange(), S.Context,
1895 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001896}
1897
Chandler Carruth1b03c872011-07-02 00:01:44 +00001898static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001899 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001900 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1902 << Attr.getName() << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001903 return;
1904 }
Mike Stumpbf916502009-07-24 19:02:52 +00001905
Chandler Carruth87c44602011-07-01 23:49:12 +00001906 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001907 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001908 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001909 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001910 return;
1911 }
Mike Stumpbf916502009-07-24 19:02:52 +00001912
Michael Han51d8c522013-01-24 16:46:58 +00001913 D->addAttr(::new (S.Context)
1914 UnusedAttr(Attr.getRange(), S.Context,
1915 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001916}
1917
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001918static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1919 const AttributeList &Attr) {
1920 // check the attribute arguments.
1921 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001922 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1923 << Attr.getName() << 0;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001924 return;
1925 }
1926
1927 if (!isa<FunctionDecl>(D)) {
1928 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1929 << Attr.getName() << ExpectedFunction;
1930 return;
1931 }
1932
Michael Han51d8c522013-01-24 16:46:58 +00001933 D->addAttr(::new (S.Context)
1934 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1935 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001936}
1937
Chandler Carruth1b03c872011-07-02 00:01:44 +00001938static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001939 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001940 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1942 << Attr.getName() << 0;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001943 return;
1944 }
Mike Stumpbf916502009-07-24 19:02:52 +00001945
Chandler Carruth87c44602011-07-01 23:49:12 +00001946 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001947 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001948 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1949 return;
1950 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001951 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001953 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001954 return;
1955 }
Mike Stumpbf916502009-07-24 19:02:52 +00001956
Michael Han51d8c522013-01-24 16:46:58 +00001957 D->addAttr(::new (S.Context)
1958 UsedAttr(Attr.getRange(), S.Context,
1959 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001960}
1961
Chandler Carruth1b03c872011-07-02 00:01:44 +00001962static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001963 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001964 if (Attr.getNumArgs() > 1) {
1965 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001966 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001967 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001968
1969 int priority = 65535; // FIXME: Do not hardcode such constants.
1970 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001971 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001972 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001973 if (E->isTypeDependent() || E->isValueDependent() ||
1974 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001975 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001976 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001977 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001978 return;
1979 }
1980 priority = Idx.getZExtValue();
1981 }
Mike Stumpbf916502009-07-24 19:02:52 +00001982
Chandler Carruth87c44602011-07-01 23:49:12 +00001983 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001984 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001985 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001986 return;
1987 }
1988
Michael Han51d8c522013-01-24 16:46:58 +00001989 D->addAttr(::new (S.Context)
1990 ConstructorAttr(Attr.getRange(), S.Context, priority,
1991 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001992}
1993
Chandler Carruth1b03c872011-07-02 00:01:44 +00001994static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001995 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001996 if (Attr.getNumArgs() > 1) {
1997 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001998 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001999 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002000
2001 int priority = 65535; // FIXME: Do not hardcode such constants.
2002 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002003 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002004 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002005 if (E->isTypeDependent() || E->isValueDependent() ||
2006 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002007 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002008 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002009 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002010 return;
2011 }
2012 priority = Idx.getZExtValue();
2013 }
Mike Stumpbf916502009-07-24 19:02:52 +00002014
Chandler Carruth87c44602011-07-01 23:49:12 +00002015 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002016 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002017 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002018 return;
2019 }
2020
Michael Han51d8c522013-01-24 16:46:58 +00002021 D->addAttr(::new (S.Context)
2022 DestructorAttr(Attr.getRange(), S.Context, priority,
2023 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002024}
2025
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002026template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002027static void handleAttrWithMessage(Sema &S, Decl *D,
2028 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002029 unsigned NumArgs = Attr.getNumArgs();
2030 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002031 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002032 return;
2033 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002034
2035 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002036 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002037 if (NumArgs == 1) {
2038 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002039 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002040 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type)
2041 << Attr.getName() << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002042 return;
2043 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002044 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002045 }
Mike Stumpbf916502009-07-24 19:02:52 +00002046
Michael Han51d8c522013-01-24 16:46:58 +00002047 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2048 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002049}
2050
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002051static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2052 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002053 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002054 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002055
Michael Han51d8c522013-01-24 16:46:58 +00002056 D->addAttr(::new (S.Context)
2057 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2058 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002059}
2060
Patrick Beardb2f68202012-04-06 18:12:22 +00002061static void handleObjCRootClassAttr(Sema &S, Decl *D,
2062 const AttributeList &Attr) {
2063 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2065 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002066 return;
2067 }
2068
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002069 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002070 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002071
Michael Han51d8c522013-01-24 16:46:58 +00002072 D->addAttr(::new (S.Context)
2073 ObjCRootClassAttr(Attr.getRange(), S.Context,
2074 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002075}
2076
Michael Han51d8c522013-01-24 16:46:58 +00002077static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2078 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002079 if (!isa<ObjCInterfaceDecl>(D)) {
2080 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2081 return;
2082 }
2083
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002084 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002085 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002086
Michael Han51d8c522013-01-24 16:46:58 +00002087 D->addAttr(::new (S.Context)
2088 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2089 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002090}
2091
Jordy Rosefad5de92012-05-08 03:27:22 +00002092static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2093 IdentifierInfo *Platform,
2094 VersionTuple Introduced,
2095 VersionTuple Deprecated,
2096 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002097 StringRef PlatformName
2098 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2099 if (PlatformName.empty())
2100 PlatformName = Platform->getName();
2101
2102 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2103 // of these steps are needed).
2104 if (!Introduced.empty() && !Deprecated.empty() &&
2105 !(Introduced <= Deprecated)) {
2106 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2107 << 1 << PlatformName << Deprecated.getAsString()
2108 << 0 << Introduced.getAsString();
2109 return true;
2110 }
2111
2112 if (!Introduced.empty() && !Obsoleted.empty() &&
2113 !(Introduced <= Obsoleted)) {
2114 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2115 << 2 << PlatformName << Obsoleted.getAsString()
2116 << 0 << Introduced.getAsString();
2117 return true;
2118 }
2119
2120 if (!Deprecated.empty() && !Obsoleted.empty() &&
2121 !(Deprecated <= Obsoleted)) {
2122 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2123 << 2 << PlatformName << Obsoleted.getAsString()
2124 << 1 << Deprecated.getAsString();
2125 return true;
2126 }
2127
2128 return false;
2129}
2130
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002131/// \brief Check whether the two versions match.
2132///
2133/// If either version tuple is empty, then they are assumed to match. If
2134/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2135static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2136 bool BeforeIsOkay) {
2137 if (X.empty() || Y.empty())
2138 return true;
2139
2140 if (X == Y)
2141 return true;
2142
2143 if (BeforeIsOkay && X < Y)
2144 return true;
2145
2146 return false;
2147}
2148
Rafael Espindola51be6e32013-01-08 22:04:34 +00002149AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002150 IdentifierInfo *Platform,
2151 VersionTuple Introduced,
2152 VersionTuple Deprecated,
2153 VersionTuple Obsoleted,
2154 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002155 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002156 bool Override,
2157 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002158 VersionTuple MergedIntroduced = Introduced;
2159 VersionTuple MergedDeprecated = Deprecated;
2160 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002161 bool FoundAny = false;
2162
Rafael Espindola98ae8342012-05-10 02:50:16 +00002163 if (D->hasAttrs()) {
2164 AttrVec &Attrs = D->getAttrs();
2165 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2166 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2167 if (!OldAA) {
2168 ++i;
2169 continue;
2170 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002171
Rafael Espindola98ae8342012-05-10 02:50:16 +00002172 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2173 if (OldPlatform != Platform) {
2174 ++i;
2175 continue;
2176 }
2177
2178 FoundAny = true;
2179 VersionTuple OldIntroduced = OldAA->getIntroduced();
2180 VersionTuple OldDeprecated = OldAA->getDeprecated();
2181 VersionTuple OldObsoleted = OldAA->getObsoleted();
2182 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002183
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002184 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2185 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2186 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2187 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002188 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002189 if (Override) {
2190 int Which = -1;
2191 VersionTuple FirstVersion;
2192 VersionTuple SecondVersion;
2193 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2194 Which = 0;
2195 FirstVersion = OldIntroduced;
2196 SecondVersion = Introduced;
2197 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2198 Which = 1;
2199 FirstVersion = Deprecated;
2200 SecondVersion = OldDeprecated;
2201 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2202 Which = 2;
2203 FirstVersion = Obsoleted;
2204 SecondVersion = OldObsoleted;
2205 }
2206
2207 if (Which == -1) {
2208 Diag(OldAA->getLocation(),
2209 diag::warn_mismatched_availability_override_unavail)
2210 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2211 } else {
2212 Diag(OldAA->getLocation(),
2213 diag::warn_mismatched_availability_override)
2214 << Which
2215 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2216 << FirstVersion.getAsString() << SecondVersion.getAsString();
2217 }
2218 Diag(Range.getBegin(), diag::note_overridden_method);
2219 } else {
2220 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2221 Diag(Range.getBegin(), diag::note_previous_attribute);
2222 }
2223
Rafael Espindola98ae8342012-05-10 02:50:16 +00002224 Attrs.erase(Attrs.begin() + i);
2225 --e;
2226 continue;
2227 }
2228
2229 VersionTuple MergedIntroduced2 = MergedIntroduced;
2230 VersionTuple MergedDeprecated2 = MergedDeprecated;
2231 VersionTuple MergedObsoleted2 = MergedObsoleted;
2232
2233 if (MergedIntroduced2.empty())
2234 MergedIntroduced2 = OldIntroduced;
2235 if (MergedDeprecated2.empty())
2236 MergedDeprecated2 = OldDeprecated;
2237 if (MergedObsoleted2.empty())
2238 MergedObsoleted2 = OldObsoleted;
2239
2240 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2241 MergedIntroduced2, MergedDeprecated2,
2242 MergedObsoleted2)) {
2243 Attrs.erase(Attrs.begin() + i);
2244 --e;
2245 continue;
2246 }
2247
2248 MergedIntroduced = MergedIntroduced2;
2249 MergedDeprecated = MergedDeprecated2;
2250 MergedObsoleted = MergedObsoleted2;
2251 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002252 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002253 }
2254
2255 if (FoundAny &&
2256 MergedIntroduced == Introduced &&
2257 MergedDeprecated == Deprecated &&
2258 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002259 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002260
Ted Kremenekcb344392013-04-06 00:34:27 +00002261 // Only create a new attribute if !Override, but we want to do
2262 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002263 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002264 MergedDeprecated, MergedObsoleted) &&
2265 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002266 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2267 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002268 Obsoleted, IsUnavailable, Message,
2269 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002270 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002271 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002272}
2273
Chandler Carruth1b03c872011-07-02 00:01:44 +00002274static void handleAvailabilityAttr(Sema &S, Decl *D,
2275 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002276 IdentifierInfo *Platform = Attr.getParameterName();
2277 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002278 unsigned Index = Attr.getAttributeSpellingListIndex();
2279
Rafael Espindola3b294362012-05-06 19:56:25 +00002280 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002281 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2282 << Platform;
2283
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002284 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2285 if (!ND) {
2286 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2287 return;
2288 }
2289
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002290 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2291 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2292 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002293 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002294 StringRef Str;
2295 const StringLiteral *SE =
2296 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2297 if (SE)
2298 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002299
Rafael Espindola51be6e32013-01-08 22:04:34 +00002300 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002301 Platform,
2302 Introduced.Version,
2303 Deprecated.Version,
2304 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002305 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002306 /*Override=*/false,
2307 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002308 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002309 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002310}
2311
John McCalld4c3d662013-02-20 01:54:26 +00002312template <class T>
2313static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2314 typename T::VisibilityType value,
2315 unsigned attrSpellingListIndex) {
2316 T *existingAttr = D->getAttr<T>();
2317 if (existingAttr) {
2318 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2319 if (existingValue == value)
2320 return NULL;
2321 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2322 S.Diag(range.getBegin(), diag::note_previous_attribute);
2323 D->dropAttr<T>();
2324 }
2325 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2326}
2327
Rafael Espindola599f1b72012-05-13 03:25:18 +00002328VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002329 VisibilityAttr::VisibilityType Vis,
2330 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002331 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2332 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002333}
2334
John McCalld4c3d662013-02-20 01:54:26 +00002335TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2336 TypeVisibilityAttr::VisibilityType Vis,
2337 unsigned AttrSpellingListIndex) {
2338 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2339 AttrSpellingListIndex);
2340}
2341
2342static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2343 bool isTypeVisibility) {
2344 // Visibility attributes don't mean anything on a typedef.
2345 if (isa<TypedefNameDecl>(D)) {
2346 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2347 << Attr.getName();
2348 return;
2349 }
2350
2351 // 'type_visibility' can only go on a type or namespace.
2352 if (isTypeVisibility &&
2353 !(isa<TagDecl>(D) ||
2354 isa<ObjCInterfaceDecl>(D) ||
2355 isa<NamespaceDecl>(D))) {
2356 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2357 << Attr.getName() << ExpectedTypeOrNamespace;
2358 return;
2359 }
2360
Chris Lattner6b6b5372008-06-26 18:38:35 +00002361 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002362 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002363 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002364
Peter Collingbourne7a730022010-11-23 20:45:58 +00002365 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002366 Arg = Arg->IgnoreParenCasts();
2367 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002368
Douglas Gregor5cee1192011-07-27 05:40:30 +00002369 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2371 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002372 return;
2373 }
Mike Stumpbf916502009-07-24 19:02:52 +00002374
Chris Lattner5f9e2722011-07-23 10:55:15 +00002375 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002376 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002377
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002378 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002379 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002380 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002381 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002382 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002383 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002384 else if (TypeStr == "protected") {
2385 // Complain about attempts to use protected visibility on targets
2386 // (like Darwin) that don't support it.
2387 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2388 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2389 type = VisibilityAttr::Default;
2390 } else {
2391 type = VisibilityAttr::Protected;
2392 }
2393 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002394 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002395 return;
2396 }
Mike Stumpbf916502009-07-24 19:02:52 +00002397
Michael Han51d8c522013-01-24 16:46:58 +00002398 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002399 clang::Attr *newAttr;
2400 if (isTypeVisibility) {
2401 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2402 (TypeVisibilityAttr::VisibilityType) type,
2403 Index);
2404 } else {
2405 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2406 }
2407 if (newAttr)
2408 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002409}
2410
Chandler Carruth1b03c872011-07-02 00:01:44 +00002411static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2412 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002413 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2414 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002416 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002417 return;
2418 }
2419
Chandler Carruth87c44602011-07-01 23:49:12 +00002420 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2421 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002422 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002423 << Attr.getName() << 1 << AANT_ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002424 } else {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2426 << Attr.getName() << 0;
John McCalld5313b02011-03-02 11:33:24 +00002427 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002428 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002429 return;
2430 }
2431
Chris Lattner5f9e2722011-07-23 10:55:15 +00002432 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002433 ObjCMethodFamilyAttr::FamilyKind family;
2434 if (param == "none")
2435 family = ObjCMethodFamilyAttr::OMF_None;
2436 else if (param == "alloc")
2437 family = ObjCMethodFamilyAttr::OMF_alloc;
2438 else if (param == "copy")
2439 family = ObjCMethodFamilyAttr::OMF_copy;
2440 else if (param == "init")
2441 family = ObjCMethodFamilyAttr::OMF_init;
2442 else if (param == "mutableCopy")
2443 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2444 else if (param == "new")
2445 family = ObjCMethodFamilyAttr::OMF_new;
2446 else {
2447 // Just warn and ignore it. This is future-proof against new
2448 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002449 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002450 return;
2451 }
2452
John McCallf85e1932011-06-15 23:02:42 +00002453 if (family == ObjCMethodFamilyAttr::OMF_init &&
2454 !method->getResultType()->isObjCObjectPointerType()) {
2455 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2456 << method->getResultType();
2457 // Ignore the attribute.
2458 return;
2459 }
2460
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002461 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002462 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002463}
2464
Chandler Carruth1b03c872011-07-02 00:01:44 +00002465static void handleObjCExceptionAttr(Sema &S, Decl *D,
2466 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002467 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002468 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002469
Chris Lattner0db29ec2009-02-14 08:09:34 +00002470 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2471 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002472 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2473 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002474 return;
2475 }
Mike Stumpbf916502009-07-24 19:02:52 +00002476
Michael Han51d8c522013-01-24 16:46:58 +00002477 D->addAttr(::new (S.Context)
2478 ObjCExceptionAttr(Attr.getRange(), S.Context,
2479 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002480}
2481
Chandler Carruth1b03c872011-07-02 00:01:44 +00002482static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002483 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002484 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002485 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002486 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002487 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002488 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2489 return;
2490 }
2491 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002492 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2493 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002494 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002495 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2496 return;
2497 }
2498 }
2499 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002500 // It is okay to include this attribute on properties, e.g.:
2501 //
2502 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2503 //
2504 // In this case it follows tradition and suppresses an error in the above
2505 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002506 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002507 }
Michael Han51d8c522013-01-24 16:46:58 +00002508 D->addAttr(::new (S.Context)
2509 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2510 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002511}
2512
Mike Stumpbf916502009-07-24 19:02:52 +00002513static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002514handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002515 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002516 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002517
2518 if (!isa<FunctionDecl>(D)) {
2519 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2520 return;
2521 }
2522
Michael Han51d8c522013-01-24 16:46:58 +00002523 D->addAttr(::new (S.Context)
2524 OverloadableAttr(Attr.getRange(), S.Context,
2525 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002526}
2527
Chandler Carruth1b03c872011-07-02 00:01:44 +00002528static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002529 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002531 << Attr.getName() << 1 << AANT_ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002532 return;
2533 }
Mike Stumpbf916502009-07-24 19:02:52 +00002534
Steve Naroff9eae5762008-09-18 16:44:58 +00002535 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2537 << Attr.getName() << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002538 return;
2539 }
Mike Stumpbf916502009-07-24 19:02:52 +00002540
Sean Huntcf807c42010-08-18 23:23:40 +00002541 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002542 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002543 type = BlocksAttr::ByRef;
2544 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002545 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002546 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002547 return;
2548 }
Mike Stumpbf916502009-07-24 19:02:52 +00002549
Michael Han51d8c522013-01-24 16:46:58 +00002550 D->addAttr(::new (S.Context)
2551 BlocksAttr(Attr.getRange(), S.Context, type,
2552 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002553}
2554
Chandler Carruth1b03c872011-07-02 00:01:44 +00002555static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002556 // check the attribute arguments.
2557 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002558 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002559 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002560 }
2561
John McCall3323fad2011-09-09 07:56:05 +00002562 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002563 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002564 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002565 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002566 if (E->isTypeDependent() || E->isValueDependent() ||
2567 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002568 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002569 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002570 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002571 return;
2572 }
Mike Stumpbf916502009-07-24 19:02:52 +00002573
John McCall3323fad2011-09-09 07:56:05 +00002574 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002575 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2576 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002577 return;
2578 }
John McCall3323fad2011-09-09 07:56:05 +00002579
2580 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002581 }
2582
John McCall3323fad2011-09-09 07:56:05 +00002583 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002584 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002585 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002586 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002587 if (E->isTypeDependent() || E->isValueDependent() ||
2588 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002589 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002590 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002591 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002592 return;
2593 }
2594 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002595
John McCall3323fad2011-09-09 07:56:05 +00002596 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002597 // FIXME: This error message could be improved, it would be nice
2598 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002599 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2600 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002601 return;
2602 }
2603 }
2604
Chandler Carruth87c44602011-07-01 23:49:12 +00002605 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002606 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002607 if (isa<FunctionNoProtoType>(FT)) {
2608 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2609 return;
2610 }
Mike Stumpbf916502009-07-24 19:02:52 +00002611
Chris Lattner897cd902009-03-17 23:03:47 +00002612 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002613 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002614 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002615 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002616 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002617 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002618 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002619 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002620 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002621 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2622 if (!BD->isVariadic()) {
2623 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2624 return;
2625 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002626 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002627 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002628 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002629 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002630 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002631 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002632 int m = Ty->isFunctionPointerType() ? 0 : 1;
2633 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002634 return;
2635 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002636 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002638 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002639 return;
2640 }
Anders Carlsson77091822008-10-05 18:05:59 +00002641 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002643 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002644 return;
2645 }
Michael Han51d8c522013-01-24 16:46:58 +00002646 D->addAttr(::new (S.Context)
2647 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2648 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002649}
2650
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002651static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2652 // Check the attribute arguments.
2653 if (!checkAttributeNumArgs(S, Attr, 0))
2654 return;
2655
2656 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2657 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2658 else
2659 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2660}
2661
Chandler Carruth1b03c872011-07-02 00:01:44 +00002662static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002663 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002664 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002665 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002666
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002667 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002669 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002670 return;
2671 }
Mike Stumpbf916502009-07-24 19:02:52 +00002672
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002673 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2674 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2675 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002676 return;
2677 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002678 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2679 if (MD->getResultType()->isVoidType()) {
2680 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2681 << Attr.getName() << 1;
2682 return;
2683 }
2684
Michael Han51d8c522013-01-24 16:46:58 +00002685 D->addAttr(::new (S.Context)
2686 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2687 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002688}
2689
Chandler Carruth1b03c872011-07-02 00:01:44 +00002690static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002691 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002692 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002693 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2694 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002695 return;
2696 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002697
Chandler Carruth87c44602011-07-01 23:49:12 +00002698 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002699 if (isa<CXXRecordDecl>(D)) {
2700 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2701 return;
2702 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2704 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002705 return;
2706 }
2707
Chandler Carruth87c44602011-07-01 23:49:12 +00002708 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002709
Michael Han51d8c522013-01-24 16:46:58 +00002710 nd->addAttr(::new (S.Context)
2711 WeakAttr(Attr.getRange(), S.Context,
2712 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002713}
2714
Chandler Carruth1b03c872011-07-02 00:01:44 +00002715static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002716 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002717 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002718 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002719
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002720
2721 // weak_import only applies to variable & function declarations.
2722 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002723 if (!D->canBeWeakImported(isDef)) {
2724 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002725 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2726 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002727 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002728 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002729 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002730 // Nothing to warn about here.
2731 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002733 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002734
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002735 return;
2736 }
2737
Michael Han51d8c522013-01-24 16:46:58 +00002738 D->addAttr(::new (S.Context)
2739 WeakImportAttr(Attr.getRange(), S.Context,
2740 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002741}
2742
Tanya Lattner0df579e2012-07-09 22:06:01 +00002743// Handles reqd_work_group_size and work_group_size_hint.
2744static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002745 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002746 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2747 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2748
Nate Begeman6f3d8382009-06-26 06:32:41 +00002749 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002750 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002751
2752 unsigned WGSize[3];
2753 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002754 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002755 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002756 if (E->isTypeDependent() || E->isValueDependent() ||
2757 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002758 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2759 << Attr.getName() << AANT_ArgumentIntegerConstant
2760 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002761 return;
2762 }
2763 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2764 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002765
2766 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2767 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2768 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2769 if (!(A->getXDim() == WGSize[0] &&
2770 A->getYDim() == WGSize[1] &&
2771 A->getZDim() == WGSize[2])) {
2772 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2773 Attr.getName();
2774 }
2775 }
2776
2777 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2778 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2779 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2780 if (!(A->getXDim() == WGSize[0] &&
2781 A->getYDim() == WGSize[1] &&
2782 A->getZDim() == WGSize[2])) {
2783 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2784 Attr.getName();
2785 }
2786 }
2787
2788 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2789 D->addAttr(::new (S.Context)
2790 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002791 WGSize[0], WGSize[1], WGSize[2],
2792 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002793 else
2794 D->addAttr(::new (S.Context)
2795 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002796 WGSize[0], WGSize[1], WGSize[2],
2797 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002798}
2799
Joey Gouly37453b92013-03-08 09:42:32 +00002800static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2801 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2802
2803 // Attribute has 1 argument.
2804 if (!checkAttributeNumArgs(S, Attr, 1))
2805 return;
2806
2807 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2808
2809 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2810 (ParmType->isBooleanType() ||
2811 !ParmType->isIntegralType(S.getASTContext()))) {
2812 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2813 << ParmType;
2814 return;
2815 }
2816
2817 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2818 D->hasAttr<VecTypeHintAttr>()) {
2819 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2820 if (A->getTypeHint() != ParmType) {
2821 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2822 return;
2823 }
2824 }
2825
2826 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2827 ParmType, Attr.getLoc()));
2828}
2829
Joey Gouly96cead52013-03-14 09:54:43 +00002830static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2831 if (!dyn_cast<VarDecl>(D))
2832 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2833 << 9;
2834 StringRef EndianType = Attr.getParameterName()->getName();
2835 if (EndianType != "host" && EndianType != "device")
2836 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2837}
2838
Rafael Espindola599f1b72012-05-13 03:25:18 +00002839SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002840 StringRef Name,
2841 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002842 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2843 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002844 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002845 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2846 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002847 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002848 }
Michael Han51d8c522013-01-24 16:46:58 +00002849 return ::new (Context) SectionAttr(Range, Context, Name,
2850 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002851}
2852
Chandler Carruth1b03c872011-07-02 00:01:44 +00002853static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002854 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002855 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002856 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002857
2858 // Make sure that there is a string literal as the sections's single
2859 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002860 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002861 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002862 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002863 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2864 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002865 return;
2866 }
Mike Stump1eb44332009-09-09 15:08:12 +00002867
Chris Lattner797c3c42009-08-10 19:03:04 +00002868 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002869 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002870 if (!Error.empty()) {
2871 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2872 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002873 return;
2874 }
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002876 // This attribute cannot be applied to local variables.
2877 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2878 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2879 return;
2880 }
Michael Han51d8c522013-01-24 16:46:58 +00002881
2882 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002883 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002884 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002885 if (NewAttr)
2886 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002887}
2888
Chris Lattner6b6b5372008-06-26 18:38:35 +00002889
Chandler Carruth1b03c872011-07-02 00:01:44 +00002890static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002891 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002892 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002893 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2894 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002895 return;
2896 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002897
Chandler Carruth87c44602011-07-01 23:49:12 +00002898 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002899 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002900 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002901 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002902 D->addAttr(::new (S.Context)
2903 NoThrowAttr(Attr.getRange(), S.Context,
2904 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002905 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002906}
2907
Chandler Carruth1b03c872011-07-02 00:01:44 +00002908static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002909 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002910 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002911 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2912 << Attr.getName() << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002913 return;
2914 }
Mike Stumpbf916502009-07-24 19:02:52 +00002915
Chandler Carruth87c44602011-07-01 23:49:12 +00002916 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002917 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002918 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002919 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002920 D->addAttr(::new (S.Context)
2921 ConstAttr(Attr.getRange(), S.Context,
2922 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002923 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002924}
2925
Chandler Carruth1b03c872011-07-02 00:01:44 +00002926static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002927 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002928 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002929 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002930
Michael Han51d8c522013-01-24 16:46:58 +00002931 D->addAttr(::new (S.Context)
2932 PureAttr(Attr.getRange(), S.Context,
2933 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002934}
2935
Chandler Carruth1b03c872011-07-02 00:01:44 +00002936static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002937 if (!Attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002938 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2939 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002940 return;
2941 }
Mike Stumpbf916502009-07-24 19:02:52 +00002942
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002943 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002944 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2945 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002946 return;
2947 }
Mike Stumpbf916502009-07-24 19:02:52 +00002948
Chandler Carruth87c44602011-07-01 23:49:12 +00002949 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002950
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002951 if (!VD || !VD->hasLocalStorage()) {
2952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2953 return;
2954 }
Mike Stumpbf916502009-07-24 19:02:52 +00002955
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002956 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002957 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002958 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002959 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2960 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002961 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002962 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002963 Attr.getParameterName();
2964 return;
2965 }
Mike Stumpbf916502009-07-24 19:02:52 +00002966
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002967 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2968 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002969 S.Diag(Attr.getParameterLoc(),
2970 diag::err_attribute_cleanup_arg_not_function)
2971 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002972 return;
2973 }
2974
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002975 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002976 S.Diag(Attr.getParameterLoc(),
2977 diag::err_attribute_cleanup_func_must_take_one_arg)
2978 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002979 return;
2980 }
Mike Stumpbf916502009-07-24 19:02:52 +00002981
Anders Carlsson89941c12009-02-07 23:16:50 +00002982 // We're currently more strict than GCC about what function types we accept.
2983 // If this ever proves to be a problem it should be easy to fix.
2984 QualType Ty = S.Context.getPointerType(VD->getType());
2985 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002986 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2987 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002988 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002989 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2990 Attr.getParameterName() << ParamTy << Ty;
2991 return;
2992 }
Mike Stumpbf916502009-07-24 19:02:52 +00002993
Michael Han51d8c522013-01-24 16:46:58 +00002994 D->addAttr(::new (S.Context)
2995 CleanupAttr(Attr.getRange(), S.Context, FD,
2996 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002997 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00002998 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002999}
3000
Mike Stumpbf916502009-07-24 19:02:52 +00003001/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003002/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003003static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003004 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003005 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003006
Chandler Carruth87c44602011-07-01 23:49:12 +00003007 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003008 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003009 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003010 return;
3011 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003012
Peter Collingbourne7a730022010-11-23 20:45:58 +00003013 Expr *IdxExpr = Attr.getArg(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003014 uint64_t ArgIdx;
3015 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
3016 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003017 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003018
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003019 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003020 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003021
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003022 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3023 if (not_nsstring_type &&
3024 !isCFStringType(Ty, S.Context) &&
3025 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003026 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003027 // FIXME: Should highlight the actual expression that has the wrong type.
3028 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003029 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003030 << IdxExpr->getSourceRange();
3031 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003032 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003033 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003034 if (!isNSStringType(Ty, S.Context) &&
3035 !isCFStringType(Ty, S.Context) &&
3036 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003037 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003038 // FIXME: Should highlight the actual expression that has the wrong type.
3039 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003040 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003041 << IdxExpr->getSourceRange();
3042 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003043 }
3044
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003045 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3046 // because that has corrected for the implicit this parameter, and is zero-
3047 // based. The attribute expects what the user wrote explicitly.
3048 llvm::APSInt Val;
3049 IdxExpr->EvaluateAsInt(Val, S.Context);
3050
Michael Han51d8c522013-01-24 16:46:58 +00003051 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003052 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003053 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003054}
3055
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003056enum FormatAttrKind {
3057 CFStringFormat,
3058 NSStringFormat,
3059 StrftimeFormat,
3060 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003061 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003062 InvalidFormat
3063};
3064
3065/// getFormatAttrKind - Map from format attribute names to supported format
3066/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003067static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003068 return llvm::StringSwitch<FormatAttrKind>(Format)
3069 // Check for formats that get handled specially.
3070 .Case("NSString", NSStringFormat)
3071 .Case("CFString", CFStringFormat)
3072 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003073
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003074 // Otherwise, check for supported formats.
3075 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3076 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3077 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003078
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003079 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3080 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003081}
3082
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003083/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003084/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003085static void handleInitPriorityAttr(Sema &S, Decl *D,
3086 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003087 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003088 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3089 return;
3090 }
3091
Chandler Carruth87c44602011-07-01 23:49:12 +00003092 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003093 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3094 Attr.setInvalid();
3095 return;
3096 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003097 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003098 if (S.Context.getAsArrayType(T))
3099 T = S.Context.getBaseElementType(T);
3100 if (!T->getAs<RecordType>()) {
3101 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3102 Attr.setInvalid();
3103 return;
3104 }
3105
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003106 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003107 Attr.setInvalid();
3108 return;
3109 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003110 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003111
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003112 llvm::APSInt priority(32);
3113 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3114 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003115 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3116 << Attr.getName() << AANT_ArgumentIntegerConstant
3117 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003118 Attr.setInvalid();
3119 return;
3120 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003121 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003122 if (prioritynum < 101 || prioritynum > 65535) {
3123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3124 << priorityExpr->getSourceRange();
3125 Attr.setInvalid();
3126 return;
3127 }
Michael Han51d8c522013-01-24 16:46:58 +00003128 D->addAttr(::new (S.Context)
3129 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3130 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003131}
3132
Rafael Espindola599f1b72012-05-13 03:25:18 +00003133FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003134 int FormatIdx, int FirstArg,
3135 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003136 // Check whether we already have an equivalent format attribute.
3137 for (specific_attr_iterator<FormatAttr>
3138 i = D->specific_attr_begin<FormatAttr>(),
3139 e = D->specific_attr_end<FormatAttr>();
3140 i != e ; ++i) {
3141 FormatAttr *f = *i;
3142 if (f->getType() == Format &&
3143 f->getFormatIdx() == FormatIdx &&
3144 f->getFirstArg() == FirstArg) {
3145 // If we don't have a valid location for this attribute, adopt the
3146 // location.
3147 if (f->getLocation().isInvalid())
3148 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003149 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003150 }
3151 }
3152
Michael Han51d8c522013-01-24 16:46:58 +00003153 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3154 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003155}
3156
Mike Stumpbf916502009-07-24 19:02:52 +00003157/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003158/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003159static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003160
Chris Lattner545dd342008-06-28 23:36:30 +00003161 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003163 << Attr.getName() << 1 << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003164 return;
3165 }
3166
Chris Lattner545dd342008-06-28 23:36:30 +00003167 if (Attr.getNumArgs() != 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3169 << Attr.getName() << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003170 return;
3171 }
3172
Chandler Carruth87c44602011-07-01 23:49:12 +00003173 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003174 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003175 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003176 return;
3177 }
3178
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003179 // In C++ the implicit 'this' function parameter also counts, and they are
3180 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003181 bool HasImplicitThisParam = isInstanceMethod(D);
3182 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003183 unsigned FirstIdx = 1;
3184
Chris Lattner5f9e2722011-07-23 10:55:15 +00003185 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003186
3187 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003188 if (Format.startswith("__") && Format.endswith("__"))
3189 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003190
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003191 // Check for supported formats.
3192 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003193
3194 if (Kind == IgnoredFormat)
3195 return;
3196
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003197 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003198 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003199 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003200 return;
3201 }
3202
3203 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003204 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003205 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003206 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3207 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003209 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003210 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003211 return;
3212 }
3213
3214 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003216 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003217 return;
3218 }
3219
3220 // FIXME: Do we need to bounds check?
3221 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003222
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003223 if (HasImplicitThisParam) {
3224 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003225 S.Diag(Attr.getLoc(),
3226 diag::err_format_attribute_implicit_this_format_string)
3227 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003228 return;
3229 }
3230 ArgIdx--;
3231 }
Mike Stump1eb44332009-09-09 15:08:12 +00003232
Chris Lattner6b6b5372008-06-26 18:38:35 +00003233 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003234 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003235
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003236 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003237 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003238 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3239 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003240 return;
3241 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003242 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003243 // FIXME: do we need to check if the type is NSString*? What are the
3244 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003245 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003246 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003247 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3248 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003249 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003250 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003251 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003252 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003253 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003254 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3255 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003256 return;
3257 }
3258
3259 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003260 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003261 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003262 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3263 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003264 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003265 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003266 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003267 return;
3268 }
3269
3270 // check if the function is variadic if the 3rd argument non-zero
3271 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003272 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003273 ++NumArgs; // +1 for ...
3274 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003275 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003276 return;
3277 }
3278 }
3279
Chris Lattner3c73c412008-11-19 08:23:25 +00003280 // strftime requires FirstArg to be 0 because it doesn't read from any
3281 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003282 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003283 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003284 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3285 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003286 return;
3287 }
3288 // if 0 it disables parameter checking (to use with e.g. va_list)
3289 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003290 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003291 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003292 return;
3293 }
3294
Rafael Espindola599f1b72012-05-13 03:25:18 +00003295 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3296 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003297 FirstArg.getZExtValue(),
3298 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003299 if (NewAttr)
3300 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003301}
3302
Chandler Carruth1b03c872011-07-02 00:01:44 +00003303static void handleTransparentUnionAttr(Sema &S, Decl *D,
3304 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003305 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003306 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003308
Chris Lattner6b6b5372008-06-26 18:38:35 +00003309
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003310 // Try to find the underlying union declaration.
3311 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003312 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003313 if (TD && TD->getUnderlyingType()->isUnionType())
3314 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3315 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003316 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003317
3318 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003319 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003320 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003321 return;
3322 }
3323
John McCall5e1cdac2011-10-07 06:10:15 +00003324 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003325 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003326 diag::warn_transparent_union_attribute_not_definition);
3327 return;
3328 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003329
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003330 RecordDecl::field_iterator Field = RD->field_begin(),
3331 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003332 if (Field == FieldEnd) {
3333 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3334 return;
3335 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003336
David Blaikie581deb32012-06-06 20:45:41 +00003337 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003338 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003339 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003340 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003341 diag::warn_transparent_union_attribute_floating)
3342 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003343 return;
3344 }
3345
3346 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3347 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3348 for (; Field != FieldEnd; ++Field) {
3349 QualType FieldType = Field->getType();
3350 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3351 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3352 // Warn if we drop the attribute.
3353 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003354 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003355 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003356 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003357 diag::warn_transparent_union_attribute_field_size_align)
3358 << isSize << Field->getDeclName() << FieldBits;
3359 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003360 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003361 diag::note_transparent_union_first_field_size_align)
3362 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003363 return;
3364 }
3365 }
3366
Michael Han51d8c522013-01-24 16:46:58 +00003367 RD->addAttr(::new (S.Context)
3368 TransparentUnionAttr(Attr.getRange(), S.Context,
3369 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003370}
3371
Chandler Carruth1b03c872011-07-02 00:01:44 +00003372static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003373 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003374 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003375 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003376
Peter Collingbourne7a730022010-11-23 20:45:58 +00003377 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003378 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003379
Chris Lattner6b6b5372008-06-26 18:38:35 +00003380 // Make sure that there is a string literal as the annotation's single
3381 // argument.
3382 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003383 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3384 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003385 return;
3386 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003387
3388 // Don't duplicate annotations that are already set.
3389 for (specific_attr_iterator<AnnotateAttr>
3390 i = D->specific_attr_begin<AnnotateAttr>(),
3391 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3392 if ((*i)->getAnnotation() == SE->getString())
3393 return;
3394 }
Michael Han51d8c522013-01-24 16:46:58 +00003395
3396 D->addAttr(::new (S.Context)
3397 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3398 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003399}
3400
Chandler Carruth1b03c872011-07-02 00:01:44 +00003401static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003402 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003403 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003404 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3405 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003406 return;
3407 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003408
Richard Smithbe507b62013-02-01 08:12:08 +00003409 if (Attr.getNumArgs() == 0) {
3410 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3411 true, 0, Attr.getAttributeSpellingListIndex()));
3412 return;
3413 }
3414
Richard Smithf6565a92013-02-22 08:32:16 +00003415 Expr *E = Attr.getArg(0);
3416 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3417 S.Diag(Attr.getEllipsisLoc(),
3418 diag::err_pack_expansion_without_parameter_packs);
3419 return;
3420 }
3421
3422 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3423 return;
3424
3425 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3426 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003427}
3428
3429void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003430 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003431 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3432 SourceLocation AttrLoc = AttrRange.getBegin();
3433
Richard Smith4cd81c52013-01-29 09:02:09 +00003434 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003435 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003436 // C++11 [dcl.align]p1:
3437 // An alignment-specifier may be applied to a variable or to a class
3438 // data member, but it shall not be applied to a bit-field, a function
3439 // parameter, the formal parameter of a catch clause, or a variable
3440 // declared with the register storage class specifier. An
3441 // alignment-specifier may also be applied to the declaration of a class
3442 // or enumeration type.
3443 // C11 6.7.5/2:
3444 // An alignment attribute shall not be specified in a declaration of
3445 // a typedef, or a bit-field, or a function, or a parameter, or an
3446 // object declared with the register storage-class specifier.
3447 int DiagKind = -1;
3448 if (isa<ParmVarDecl>(D)) {
3449 DiagKind = 0;
3450 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3451 if (VD->getStorageClass() == SC_Register)
3452 DiagKind = 1;
3453 if (VD->isExceptionVariable())
3454 DiagKind = 2;
3455 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3456 if (FD->isBitField())
3457 DiagKind = 3;
3458 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003459 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3460 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003461 << (TmpAttr.isC11() ? ExpectedVariableOrField
3462 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003463 return;
3464 }
3465 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003466 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003467 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003468 return;
3469 }
3470 }
3471
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003472 if (E->isTypeDependent() || E->isValueDependent()) {
3473 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003474 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3475 AA->setPackExpansion(IsPackExpansion);
3476 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003477 return;
3478 }
Michael Hana31f65b2013-02-01 01:19:17 +00003479
Sean Huntcf807c42010-08-18 23:23:40 +00003480 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003481 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003482 ExprResult ICE
3483 = VerifyIntegerConstantExpression(E, &Alignment,
3484 diag::err_aligned_attribute_argument_not_int,
3485 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003486 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003487 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003488
3489 // C++11 [dcl.align]p2:
3490 // -- if the constant expression evaluates to zero, the alignment
3491 // specifier shall have no effect
3492 // C11 6.7.5p6:
3493 // An alignment specification of zero has no effect.
3494 if (!(TmpAttr.isAlignas() && !Alignment) &&
3495 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003496 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3497 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003498 return;
3499 }
Michael Hana31f65b2013-02-01 01:19:17 +00003500
Richard Smithbe507b62013-02-01 08:12:08 +00003501 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003502 // We've already verified it's a power of 2, now let's make sure it's
3503 // 8192 or less.
3504 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003505 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003506 << E->getSourceRange();
3507 return;
3508 }
3509 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003510
Richard Smithf6565a92013-02-22 08:32:16 +00003511 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3512 ICE.take(), SpellingListIndex);
3513 AA->setPackExpansion(IsPackExpansion);
3514 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003515}
3516
Michael Hana31f65b2013-02-01 01:19:17 +00003517void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003518 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003519 // FIXME: Cache the number on the Attr object if non-dependent?
3520 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003521 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3522 SpellingListIndex);
3523 AA->setPackExpansion(IsPackExpansion);
3524 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003525}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003526
Richard Smithbe507b62013-02-01 08:12:08 +00003527void Sema::CheckAlignasUnderalignment(Decl *D) {
3528 assert(D->hasAttrs() && "no attributes on decl");
3529
3530 QualType Ty;
3531 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3532 Ty = VD->getType();
3533 else
3534 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003535 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003536 return;
3537
3538 // C++11 [dcl.align]p5, C11 6.7.5/4:
3539 // The combined effect of all alignment attributes in a declaration shall
3540 // not specify an alignment that is less strict than the alignment that
3541 // would otherwise be required for the entity being declared.
3542 AlignedAttr *AlignasAttr = 0;
3543 unsigned Align = 0;
3544 for (specific_attr_iterator<AlignedAttr>
3545 I = D->specific_attr_begin<AlignedAttr>(),
3546 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3547 if (I->isAlignmentDependent())
3548 return;
3549 if (I->isAlignas())
3550 AlignasAttr = *I;
3551 Align = std::max(Align, I->getAlignment(Context));
3552 }
3553
3554 if (AlignasAttr && Align) {
3555 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3556 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3557 if (NaturalAlign > RequestedAlign)
3558 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3559 << Ty << (unsigned)NaturalAlign.getQuantity();
3560 }
3561}
3562
Chandler Carruthd309c812011-07-01 23:49:16 +00003563/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003564/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003565///
Mike Stumpbf916502009-07-24 19:02:52 +00003566/// Despite what would be logical, the mode attribute is a decl attribute, not a
3567/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3568/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003569static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003570 // This attribute isn't documented, but glibc uses it. It changes
3571 // the width of an int or unsigned int to the specified size.
3572
3573 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003574 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003575 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003576
Chris Lattnerfbf13472008-06-27 22:18:37 +00003577
3578 IdentifierInfo *Name = Attr.getParameterName();
3579 if (!Name) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3581 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003582 return;
3583 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003584
Chris Lattner5f9e2722011-07-23 10:55:15 +00003585 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003586
3587 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003588 if (Str.startswith("__") && Str.endswith("__"))
3589 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003590
3591 unsigned DestWidth = 0;
3592 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003593 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003594 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003595 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003596 switch (Str[0]) {
3597 case 'Q': DestWidth = 8; break;
3598 case 'H': DestWidth = 16; break;
3599 case 'S': DestWidth = 32; break;
3600 case 'D': DestWidth = 64; break;
3601 case 'X': DestWidth = 96; break;
3602 case 'T': DestWidth = 128; break;
3603 }
3604 if (Str[1] == 'F') {
3605 IntegerMode = false;
3606 } else if (Str[1] == 'C') {
3607 IntegerMode = false;
3608 ComplexMode = true;
3609 } else if (Str[1] != 'I') {
3610 DestWidth = 0;
3611 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003612 break;
3613 case 4:
3614 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3615 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003616 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003617 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003618 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003619 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003620 break;
3621 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003622 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003623 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003624 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003625 case 11:
3626 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003627 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003628 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003629 }
3630
3631 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003632 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003633 OldTy = TD->getUnderlyingType();
3634 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3635 OldTy = VD->getType();
3636 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003637 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003638 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003639 return;
3640 }
Eli Friedman73397492009-03-03 06:41:03 +00003641
John McCall183700f2009-09-21 23:43:11 +00003642 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003643 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3644 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003645 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003646 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3647 } else if (ComplexMode) {
3648 if (!OldTy->isComplexType())
3649 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3650 } else {
3651 if (!OldTy->isFloatingType())
3652 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3653 }
3654
Mike Stump390b4cc2009-05-16 07:39:55 +00003655 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3656 // and friends, at least with glibc.
3657 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3658 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003659 // FIXME: Make sure floating-point mappings are accurate
3660 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003661 QualType NewTy;
3662 switch (DestWidth) {
3663 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003664 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003665 return;
3666 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003667 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003668 return;
3669 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003670 if (!IntegerMode) {
3671 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3672 return;
3673 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003674 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003675 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003676 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003677 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003678 break;
3679 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003680 if (!IntegerMode) {
3681 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3682 return;
3683 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003684 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003685 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003686 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003687 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003688 break;
3689 case 32:
3690 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003691 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003692 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003693 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003694 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003695 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003696 break;
3697 case 64:
3698 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003699 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003700 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003701 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003702 NewTy = S.Context.LongTy;
3703 else
3704 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003705 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003706 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003707 NewTy = S.Context.UnsignedLongTy;
3708 else
3709 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003710 break;
Eli Friedman73397492009-03-03 06:41:03 +00003711 case 96:
3712 NewTy = S.Context.LongDoubleTy;
3713 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003714 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003715 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3716 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003717 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3718 return;
3719 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003720 if (IntegerMode) {
3721 if (OldTy->isSignedIntegerType())
3722 NewTy = S.Context.Int128Ty;
3723 else
3724 NewTy = S.Context.UnsignedInt128Ty;
3725 } else
3726 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003727 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003728 }
3729
Eli Friedman73397492009-03-03 06:41:03 +00003730 if (ComplexMode) {
3731 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003732 }
3733
3734 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003735 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3736 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3737 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003738 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003739
3740 D->addAttr(::new (S.Context)
3741 ModeAttr(Attr.getRange(), S.Context, Name,
3742 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003743}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003744
Chandler Carruth1b03c872011-07-02 00:01:44 +00003745static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003746 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003747 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003748 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003749
Nick Lewycky78d1a102012-07-24 01:40:49 +00003750 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3751 if (!VD->hasGlobalStorage())
3752 S.Diag(Attr.getLoc(),
3753 diag::warn_attribute_requires_functions_or_static_globals)
3754 << Attr.getName();
3755 } else if (!isFunctionOrMethod(D)) {
3756 S.Diag(Attr.getLoc(),
3757 diag::warn_attribute_requires_functions_or_static_globals)
3758 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003759 return;
3760 }
Mike Stumpbf916502009-07-24 19:02:52 +00003761
Michael Han51d8c522013-01-24 16:46:58 +00003762 D->addAttr(::new (S.Context)
3763 NoDebugAttr(Attr.getRange(), S.Context,
3764 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003765}
3766
Chandler Carruth1b03c872011-07-02 00:01:44 +00003767static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003768 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003769 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003770 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003771
Mike Stumpbf916502009-07-24 19:02:52 +00003772
Chandler Carruth87c44602011-07-01 23:49:12 +00003773 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003774 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003775 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003776 return;
3777 }
Mike Stumpbf916502009-07-24 19:02:52 +00003778
Michael Han51d8c522013-01-24 16:46:58 +00003779 D->addAttr(::new (S.Context)
3780 NoInlineAttr(Attr.getRange(), S.Context,
3781 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003782}
3783
Chandler Carruth1b03c872011-07-02 00:01:44 +00003784static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3785 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003786 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003787 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003788 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003789
Chris Lattner7255a2d2010-06-22 00:03:40 +00003790
Chandler Carruth87c44602011-07-01 23:49:12 +00003791 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003792 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003793 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003794 return;
3795 }
3796
Michael Han51d8c522013-01-24 16:46:58 +00003797 D->addAttr(::new (S.Context)
3798 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3799 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003800}
3801
Chandler Carruth1b03c872011-07-02 00:01:44 +00003802static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003803 if (S.LangOpts.CUDA) {
3804 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003805 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3807 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003808 return;
3809 }
3810
Chandler Carruth87c44602011-07-01 23:49:12 +00003811 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003812 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003813 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003814 return;
3815 }
3816
Michael Han51d8c522013-01-24 16:46:58 +00003817 D->addAttr(::new (S.Context)
3818 CUDAConstantAttr(Attr.getRange(), S.Context,
3819 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003820 } else {
3821 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3822 }
3823}
3824
Chandler Carruth1b03c872011-07-02 00:01:44 +00003825static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003826 if (S.LangOpts.CUDA) {
3827 // check the attribute arguments.
3828 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003829 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3830 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003831 return;
3832 }
3833
Chandler Carruth87c44602011-07-01 23:49:12 +00003834 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003836 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003837 return;
3838 }
3839
Michael Han51d8c522013-01-24 16:46:58 +00003840 D->addAttr(::new (S.Context)
3841 CUDADeviceAttr(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) << "device";
3845 }
3846}
3847
Chandler Carruth1b03c872011-07-02 00:01:44 +00003848static void handleGlobalAttr(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;
Peter Collingbourneced76712010-12-01 03:15:31 +00003853
Chandler Carruth87c44602011-07-01 23:49:12 +00003854 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003856 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003857 return;
3858 }
3859
Chandler Carruth87c44602011-07-01 23:49:12 +00003860 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003861 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003862 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003863 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003864 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3865 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003866 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003867 "void");
3868 } else {
3869 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3870 << FD->getType();
3871 }
3872 return;
3873 }
3874
Michael Han51d8c522013-01-24 16:46:58 +00003875 D->addAttr(::new (S.Context)
3876 CUDAGlobalAttr(Attr.getRange(), S.Context,
3877 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 } else {
3879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3880 }
3881}
3882
Chandler Carruth1b03c872011-07-02 00:01:44 +00003883static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 if (S.LangOpts.CUDA) {
3885 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003886 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003887 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003888
Peter Collingbourneced76712010-12-01 03:15:31 +00003889
Chandler Carruth87c44602011-07-01 23:49:12 +00003890 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003891 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003892 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003893 return;
3894 }
3895
Michael Han51d8c522013-01-24 16:46:58 +00003896 D->addAttr(::new (S.Context)
3897 CUDAHostAttr(Attr.getRange(), S.Context,
3898 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003899 } else {
3900 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3901 }
3902}
3903
Chandler Carruth1b03c872011-07-02 00:01:44 +00003904static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003905 if (S.LangOpts.CUDA) {
3906 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003907 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003908 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003909
Chandler Carruth87c44602011-07-01 23:49:12 +00003910 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003912 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003913 return;
3914 }
3915
Michael Han51d8c522013-01-24 16:46:58 +00003916 D->addAttr(::new (S.Context)
3917 CUDASharedAttr(Attr.getRange(), S.Context,
3918 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003919 } else {
3920 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3921 }
3922}
3923
Chandler Carruth1b03c872011-07-02 00:01:44 +00003924static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003925 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003926 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003927 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003928
Chandler Carruth87c44602011-07-01 23:49:12 +00003929 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003930 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003931 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003932 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003933 return;
3934 }
Mike Stumpbf916502009-07-24 19:02:52 +00003935
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003936 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003937 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003938 return;
3939 }
Mike Stumpbf916502009-07-24 19:02:52 +00003940
Michael Han51d8c522013-01-24 16:46:58 +00003941 D->addAttr(::new (S.Context)
3942 GNUInlineAttr(Attr.getRange(), S.Context,
3943 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003944}
3945
Chandler Carruth1b03c872011-07-02 00:01:44 +00003946static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003947 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003948
Aaron Ballmanfff32482012-12-09 17:45:41 +00003949 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003950 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003951 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3952 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003953 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003954 return;
3955
Chandler Carruth87c44602011-07-01 23:49:12 +00003956 if (!isa<ObjCMethodDecl>(D)) {
3957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3958 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003959 return;
3960 }
3961
Chandler Carruth87c44602011-07-01 23:49:12 +00003962 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003963 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003964 D->addAttr(::new (S.Context)
3965 FastCallAttr(Attr.getRange(), S.Context,
3966 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003967 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003968 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003969 D->addAttr(::new (S.Context)
3970 StdCallAttr(Attr.getRange(), S.Context,
3971 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003972 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003973 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003974 D->addAttr(::new (S.Context)
3975 ThisCallAttr(Attr.getRange(), S.Context,
3976 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003977 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003978 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003979 D->addAttr(::new (S.Context)
3980 CDeclAttr(Attr.getRange(), S.Context,
3981 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003982 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003983 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003984 D->addAttr(::new (S.Context)
3985 PascalAttr(Attr.getRange(), S.Context,
3986 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003987 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003988 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003989 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003990 switch (CC) {
3991 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003992 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003993 break;
3994 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003995 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003996 break;
3997 default:
3998 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003999 }
4000
Michael Han51d8c522013-01-24 16:46:58 +00004001 D->addAttr(::new (S.Context)
4002 PcsAttr(Attr.getRange(), S.Context, PCS,
4003 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004004 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004005 }
Derek Schuff263366f2012-10-16 22:30:41 +00004006 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004007 D->addAttr(::new (S.Context)
4008 PnaclCallAttr(Attr.getRange(), S.Context,
4009 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004010 return;
Guy Benyei38980082012-12-25 08:53:55 +00004011 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004012 D->addAttr(::new (S.Context)
4013 IntelOclBiccAttr(Attr.getRange(), S.Context,
4014 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004015 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004016
Abramo Bagnarae215f722010-04-30 13:10:51 +00004017 default:
4018 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004019 }
4020}
4021
Chandler Carruth1b03c872011-07-02 00:01:44 +00004022static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00004023 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004024 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004025}
4026
Guy Benyei1db70402013-03-24 13:58:12 +00004027static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4028 assert(!Attr.isInvalid());
4029
4030 Expr *E = Attr.getArg(0);
4031 llvm::APSInt ArgNum(32);
4032 if (E->isTypeDependent() || E->isValueDependent() ||
4033 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4035 << Attr.getName() << AANT_ArgumentIntegerConstant
4036 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004037 return;
4038 }
4039
4040 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4041 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4042}
4043
Aaron Ballmanfff32482012-12-09 17:45:41 +00004044bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4045 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004046 if (attr.isInvalid())
4047 return true;
4048
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004049 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4050 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004051 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4052 << attr.getName() << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004053 attr.setInvalid();
4054 return true;
4055 }
4056
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004057 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4058 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004059 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004060 case AttributeList::AT_CDecl: CC = CC_C; break;
4061 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4062 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4063 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4064 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4065 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004066 Expr *Arg = attr.getArg(0);
4067 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004068 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004069 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4070 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004071 attr.setInvalid();
4072 return true;
4073 }
4074
Chris Lattner5f9e2722011-07-23 10:55:15 +00004075 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004076 if (StrRef == "aapcs") {
4077 CC = CC_AAPCS;
4078 break;
4079 } else if (StrRef == "aapcs-vfp") {
4080 CC = CC_AAPCS_VFP;
4081 break;
4082 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004083
4084 attr.setInvalid();
4085 Diag(attr.getLoc(), diag::err_invalid_pcs);
4086 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004087 }
Derek Schuff263366f2012-10-16 22:30:41 +00004088 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004089 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004090 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004091 }
4092
Aaron Ballman82bfa192012-10-02 14:26:08 +00004093 const TargetInfo &TI = Context.getTargetInfo();
4094 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4095 if (A == TargetInfo::CCCR_Warning) {
4096 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004097
4098 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4099 if (FD)
4100 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4101 TargetInfo::CCMT_NonMember;
4102 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004103 }
4104
John McCall711c52b2011-01-05 12:14:39 +00004105 return false;
4106}
4107
Chandler Carruth1b03c872011-07-02 00:01:44 +00004108static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004109 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004110
4111 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004112 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004113 return;
4114
Chandler Carruth87c44602011-07-01 23:49:12 +00004115 if (!isa<ObjCMethodDecl>(D)) {
4116 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4117 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004118 return;
4119 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004120
Michael Han51d8c522013-01-24 16:46:58 +00004121 D->addAttr(::new (S.Context)
4122 RegparmAttr(Attr.getRange(), S.Context, numParams,
4123 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004124}
4125
4126/// Checks a regparm attribute, returning true if it is ill-formed and
4127/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004128bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4129 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004130 return true;
4131
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004132 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004133 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004134 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004135 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004136
Chandler Carruth87c44602011-07-01 23:49:12 +00004137 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004138 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004139 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004140 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004141 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4142 << Attr.getName() << AANT_ArgumentIntegerConstant
4143 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004144 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004145 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004146 }
4147
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004148 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004149 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004150 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004151 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004152 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004153 }
4154
John McCall711c52b2011-01-05 12:14:39 +00004155 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004156 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004157 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004158 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004159 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004160 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004161 }
4162
John McCall711c52b2011-01-05 12:14:39 +00004163 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004164}
4165
Chandler Carruth1b03c872011-07-02 00:01:44 +00004166static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004167 if (S.LangOpts.CUDA) {
4168 // check the attribute arguments.
4169 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004170 // FIXME: 0 is not okay.
4171 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004172 return;
4173 }
4174
Chandler Carruth87c44602011-07-01 23:49:12 +00004175 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004176 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004177 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004178 return;
4179 }
4180
4181 Expr *MaxThreadsExpr = Attr.getArg(0);
4182 llvm::APSInt MaxThreads(32);
4183 if (MaxThreadsExpr->isTypeDependent() ||
4184 MaxThreadsExpr->isValueDependent() ||
4185 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004187 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004188 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004189 return;
4190 }
4191
4192 llvm::APSInt MinBlocks(32);
4193 if (Attr.getNumArgs() > 1) {
4194 Expr *MinBlocksExpr = Attr.getArg(1);
4195 if (MinBlocksExpr->isTypeDependent() ||
4196 MinBlocksExpr->isValueDependent() ||
4197 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004198 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004199 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004200 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004201 return;
4202 }
4203 }
4204
Michael Han51d8c522013-01-24 16:46:58 +00004205 D->addAttr(::new (S.Context)
4206 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4207 MaxThreads.getZExtValue(),
4208 MinBlocks.getZExtValue(),
4209 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004210 } else {
4211 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4212 }
4213}
4214
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004215static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4216 const AttributeList &Attr) {
4217 StringRef AttrName = Attr.getName()->getName();
4218 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004220 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004221 return;
4222 }
4223
4224 if (Attr.getNumArgs() != 2) {
4225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004226 << Attr.getName() << /* required args = */ 3;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004227 return;
4228 }
4229
4230 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4231
4232 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4233 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4234 << Attr.getName() << ExpectedFunctionOrMethod;
4235 return;
4236 }
4237
4238 uint64_t ArgumentIdx;
4239 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4240 Attr.getLoc(), 2,
4241 Attr.getArg(0), ArgumentIdx))
4242 return;
4243
4244 uint64_t TypeTagIdx;
4245 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4246 Attr.getLoc(), 3,
4247 Attr.getArg(1), TypeTagIdx))
4248 return;
4249
4250 bool IsPointer = (AttrName == "pointer_with_type_tag");
4251 if (IsPointer) {
4252 // Ensure that buffer has a pointer type.
4253 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4254 if (!BufferTy->isPointerType()) {
4255 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004256 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004257 }
4258 }
4259
Michael Han51d8c522013-01-24 16:46:58 +00004260 D->addAttr(::new (S.Context)
4261 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4262 ArgumentIdx, TypeTagIdx, IsPointer,
4263 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004264}
4265
4266static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4267 const AttributeList &Attr) {
4268 IdentifierInfo *PointerKind = Attr.getParameterName();
4269 if (!PointerKind) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004271 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004272 return;
4273 }
4274
4275 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4276
Michael Han51d8c522013-01-24 16:46:58 +00004277 D->addAttr(::new (S.Context)
4278 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4279 MatchingCType,
4280 Attr.getLayoutCompatible(),
4281 Attr.getMustBeNull(),
4282 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004283}
4284
Chris Lattner0744e5f2008-06-29 00:23:49 +00004285//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004286// Checker-specific attribute handlers.
4287//===----------------------------------------------------------------------===//
4288
John McCallc7ad3812011-01-25 03:31:58 +00004289static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004290 return type->isDependentType() ||
4291 type->isObjCObjectPointerType() ||
4292 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004293}
4294static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004295 return type->isDependentType() ||
4296 type->isPointerType() ||
4297 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004298}
4299
Chandler Carruth1b03c872011-07-02 00:01:44 +00004300static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004301 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004302 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004303 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004304 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004305 return;
4306 }
4307
4308 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004309 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004310 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4311 cf = false;
4312 } else {
4313 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4314 cf = true;
4315 }
4316
4317 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004318 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004319 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004320 return;
4321 }
4322
4323 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004324 param->addAttr(::new (S.Context)
4325 CFConsumedAttr(Attr.getRange(), S.Context,
4326 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004327 else
Michael Han51d8c522013-01-24 16:46:58 +00004328 param->addAttr(::new (S.Context)
4329 NSConsumedAttr(Attr.getRange(), S.Context,
4330 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004331}
4332
Chandler Carruth1b03c872011-07-02 00:01:44 +00004333static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4334 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004335 if (!isa<ObjCMethodDecl>(D)) {
4336 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004337 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004338 return;
4339 }
4340
Michael Han51d8c522013-01-24 16:46:58 +00004341 D->addAttr(::new (S.Context)
4342 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4343 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004344}
4345
Chandler Carruth1b03c872011-07-02 00:01:44 +00004346static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4347 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004348
John McCallc7ad3812011-01-25 03:31:58 +00004349 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004350
Chandler Carruth87c44602011-07-01 23:49:12 +00004351 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004352 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004353 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004354 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004355 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004356 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4357 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004358 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004359 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004360 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004361 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004362 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004363 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004364 return;
4365 }
Mike Stumpbf916502009-07-24 19:02:52 +00004366
John McCallc7ad3812011-01-25 03:31:58 +00004367 bool typeOK;
4368 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004369 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004370 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004371 case AttributeList::AT_NSReturnsAutoreleased:
4372 case AttributeList::AT_NSReturnsRetained:
4373 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004374 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4375 cf = false;
4376 break;
4377
Sean Hunt8e083e72012-06-19 23:57:03 +00004378 case AttributeList::AT_CFReturnsRetained:
4379 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004380 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4381 cf = true;
4382 break;
4383 }
4384
4385 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004386 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004387 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004388 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004389 }
Mike Stumpbf916502009-07-24 19:02:52 +00004390
Chandler Carruth87c44602011-07-01 23:49:12 +00004391 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004392 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004393 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004395 D->addAttr(::new (S.Context)
4396 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4397 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004398 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004399 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004400 D->addAttr(::new (S.Context)
4401 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4402 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004403 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004404 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004405 D->addAttr(::new (S.Context)
4406 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4407 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004408 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004409 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004410 D->addAttr(::new (S.Context)
4411 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4412 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004413 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004414 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004415 D->addAttr(::new (S.Context)
4416 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4417 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004418 return;
4419 };
4420}
4421
John McCalldc7c5ad2011-07-22 08:53:00 +00004422static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4423 const AttributeList &attr) {
4424 SourceLocation loc = attr.getLoc();
4425
4426 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4427
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004428 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004429 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004430 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004431 return;
4432 }
4433
4434 // Check that the method returns a normal pointer.
4435 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004436
4437 if (!resultType->isReferenceType() &&
4438 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004439 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4440 << SourceRange(loc)
4441 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4442
4443 // Drop the attribute.
4444 return;
4445 }
4446
Michael Han51d8c522013-01-24 16:46:58 +00004447 method->addAttr(::new (S.Context)
4448 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4449 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004450}
4451
Fariborz Jahanian84101132012-09-07 23:46:23 +00004452static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4453 const AttributeList &attr) {
4454 SourceLocation loc = attr.getLoc();
4455 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4456
4457 if (!method) {
4458 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4459 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4460 return;
4461 }
4462 DeclContext *DC = method->getDeclContext();
4463 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4464 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4465 << attr.getName() << 0;
4466 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4467 return;
4468 }
4469 if (method->getMethodFamily() == OMF_dealloc) {
4470 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4471 << attr.getName() << 1;
4472 return;
4473 }
4474
Michael Han51d8c522013-01-24 16:46:58 +00004475 method->addAttr(::new (S.Context)
4476 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4477 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004478}
4479
John McCall8dfac0b2011-09-30 05:12:12 +00004480/// Handle cf_audited_transfer and cf_unknown_transfer.
4481static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4482 if (!isa<FunctionDecl>(D)) {
4483 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004484 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004485 return;
4486 }
4487
Sean Hunt8e083e72012-06-19 23:57:03 +00004488 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004489
4490 // Check whether there's a conflicting attribute already present.
4491 Attr *Existing;
4492 if (IsAudited) {
4493 Existing = D->getAttr<CFUnknownTransferAttr>();
4494 } else {
4495 Existing = D->getAttr<CFAuditedTransferAttr>();
4496 }
4497 if (Existing) {
4498 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4499 << A.getName()
4500 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4501 << A.getRange() << Existing->getRange();
4502 return;
4503 }
4504
4505 // All clear; add the attribute.
4506 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004507 D->addAttr(::new (S.Context)
4508 CFAuditedTransferAttr(A.getRange(), S.Context,
4509 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004510 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004511 D->addAttr(::new (S.Context)
4512 CFUnknownTransferAttr(A.getRange(), S.Context,
4513 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004514 }
4515}
4516
John McCallfe98da02011-09-29 07:17:38 +00004517static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4518 const AttributeList &Attr) {
4519 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4520 if (!RD || RD->isUnion()) {
4521 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004522 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004523 }
4524
4525 IdentifierInfo *ParmName = Attr.getParameterName();
4526
4527 // In Objective-C, verify that the type names an Objective-C type.
4528 // We don't want to check this outside of ObjC because people sometimes
4529 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004530 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004531 // Check for an existing type with this name.
4532 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4533 Sema::LookupOrdinaryName);
4534 if (S.LookupName(R, Sc)) {
4535 NamedDecl *Target = R.getFoundDecl();
4536 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4537 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4538 S.Diag(Target->getLocStart(), diag::note_declared_at);
4539 }
4540 }
4541 }
4542
Michael Han51d8c522013-01-24 16:46:58 +00004543 D->addAttr(::new (S.Context)
4544 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4545 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004546}
4547
Chandler Carruth1b03c872011-07-02 00:01:44 +00004548static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4549 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004550 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004551
Chandler Carruth87c44602011-07-01 23:49:12 +00004552 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004553 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004554}
4555
Chandler Carruth1b03c872011-07-02 00:01:44 +00004556static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4557 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004558 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004559 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004560 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004561 return;
4562 }
4563
Chandler Carruth87c44602011-07-01 23:49:12 +00004564 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004565 QualType type = vd->getType();
4566
4567 if (!type->isDependentType() &&
4568 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004569 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004570 << type;
4571 return;
4572 }
4573
4574 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4575
4576 // If we have no lifetime yet, check the lifetime we're presumably
4577 // going to infer.
4578 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4579 lifetime = type->getObjCARCImplicitLifetime();
4580
4581 switch (lifetime) {
4582 case Qualifiers::OCL_None:
4583 assert(type->isDependentType() &&
4584 "didn't infer lifetime for non-dependent type?");
4585 break;
4586
4587 case Qualifiers::OCL_Weak: // meaningful
4588 case Qualifiers::OCL_Strong: // meaningful
4589 break;
4590
4591 case Qualifiers::OCL_ExplicitNone:
4592 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004593 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004594 << (lifetime == Qualifiers::OCL_Autoreleasing);
4595 break;
4596 }
4597
Chandler Carruth87c44602011-07-01 23:49:12 +00004598 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004599 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4600 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004601}
4602
Francois Pichet11542142010-12-19 06:50:37 +00004603//===----------------------------------------------------------------------===//
4604// Microsoft specific attribute handlers.
4605//===----------------------------------------------------------------------===//
4606
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004607// Check if MS extensions or some other language extensions are enabled. If
4608// not, issue a diagnostic that the given attribute is unused.
4609static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4610 bool OtherExtension = false) {
4611 if (S.LangOpts.MicrosoftExt || OtherExtension)
4612 return true;
4613 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4614 return false;
4615}
4616
Chandler Carruth1b03c872011-07-02 00:01:44 +00004617static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004618 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4619 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004620
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004621 // check the attribute arguments.
4622 if (!checkAttributeNumArgs(S, Attr, 1))
4623 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004624
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004625 Expr *Arg = Attr.getArg(0);
4626 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4627 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004628 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4629 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004630 return;
4631 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004632
David Majnemerbb0ed292013-08-09 08:56:20 +00004633 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4634 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004635 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004636 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4637 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004638
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004639 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004640 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004641 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4642 return;
4643 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004644
David Majnemerbb0ed292013-08-09 08:56:20 +00004645 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004646 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004647 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004648 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4649 return;
4650 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004651 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004652 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4653 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004654 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004655 }
Francois Pichet11542142010-12-19 06:50:37 +00004656
David Majnemerbb0ed292013-08-09 08:56:20 +00004657 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4658 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004659}
4660
John McCallc052dbb2012-05-22 21:28:12 +00004661static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004662 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004663 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004664
4665 AttributeList::Kind Kind = Attr.getKind();
4666 if (Kind == AttributeList::AT_SingleInheritance)
4667 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004668 ::new (S.Context)
4669 SingleInheritanceAttr(Attr.getRange(), S.Context,
4670 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004671 else if (Kind == AttributeList::AT_MultipleInheritance)
4672 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004673 ::new (S.Context)
4674 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4675 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004676 else if (Kind == AttributeList::AT_VirtualInheritance)
4677 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004678 ::new (S.Context)
4679 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4680 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004681}
4682
4683static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004684 if (!checkMicrosoftExt(S, Attr))
4685 return;
4686
4687 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004688 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004689 D->addAttr(
4690 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4691 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004692}
4693
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004694static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004695 if (!checkMicrosoftExt(S, Attr))
4696 return;
4697 D->addAttr(::new (S.Context)
4698 ForceInlineAttr(Attr.getRange(), S.Context,
4699 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004700}
4701
Reid Klecknera7225342013-05-20 14:02:37 +00004702static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4703 if (!checkMicrosoftExt(S, Attr))
4704 return;
4705 // Check linkage after possibly merging declaratinos. See
4706 // checkAttributesAfterMerging().
4707 D->addAttr(::new (S.Context)
4708 SelectAnyAttr(Attr.getRange(), S.Context,
4709 Attr.getAttributeSpellingListIndex()));
4710}
4711
Ted Kremenekb71368d2009-05-09 02:44:38 +00004712//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004713// Top Level Sema Entry Points
4714//===----------------------------------------------------------------------===//
4715
Chandler Carruth1b03c872011-07-02 00:01:44 +00004716static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4717 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004718 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4720 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4721 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004722 default:
4723 break;
4724 }
4725}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004726
Chandler Carruth1b03c872011-07-02 00:01:44 +00004727static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4728 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004729 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004730 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4731 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4732 case AttributeList::AT_IBOutletCollection:
4733 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004734 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004735 case AttributeList::AT_ObjCGC:
4736 case AttributeList::AT_VectorSize:
4737 case AttributeList::AT_NeonVectorType:
4738 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004739 case AttributeList::AT_Ptr32:
4740 case AttributeList::AT_Ptr64:
4741 case AttributeList::AT_SPtr:
4742 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004743 // Ignore these, these are type attributes, handled by
4744 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004745 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004746 case AttributeList::AT_CUDADevice:
4747 case AttributeList::AT_CUDAHost:
4748 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004749 // Ignore, this is a non-inheritable attribute, handled
4750 // by ProcessNonInheritableDeclAttr.
4751 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004752 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4753 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4754 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4755 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004756 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004757 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004758 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004759 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4761 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4762 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004763 handleDependencyAttr(S, scope, D, Attr);
4764 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004765 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4766 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4767 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004768 case AttributeList::AT_CXX11NoReturn:
4769 handleCXX11NoReturnAttr(S, D, Attr);
4770 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004771 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004772 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004773 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4775 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004776 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004777 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004778 case AttributeList::AT_MinSize:
4779 handleMinSizeAttr(S, D, Attr);
4780 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004781 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4782 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4783 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4784 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4785 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004786 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004787 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004788 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4789 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4790 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4791 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4792 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004793 case AttributeList::AT_ownership_returns:
4794 case AttributeList::AT_ownership_takes:
4795 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004796 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4798 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4799 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4800 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4801 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4802 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4803 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004804
Sean Hunt8e083e72012-06-19 23:57:03 +00004805 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004806 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004808 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004809
Sean Hunt8e083e72012-06-19 23:57:03 +00004810 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004811 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4812
Fariborz Jahanian84101132012-09-07 23:46:23 +00004813 case AttributeList::AT_ObjCRequiresSuper:
4814 handleObjCRequiresSuperAttr(S, D, Attr); break;
4815
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004817 handleNSBridgedAttr(S, scope, D, Attr); break;
4818
Sean Hunt8e083e72012-06-19 23:57:03 +00004819 case AttributeList::AT_CFAuditedTransfer:
4820 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004821 handleCFTransferAttr(S, D, Attr); break;
4822
Ted Kremenekb71368d2009-05-09 02:44:38 +00004823 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004824 case AttributeList::AT_CFConsumed:
4825 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4826 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004827 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004828
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 case AttributeList::AT_NSReturnsAutoreleased:
4830 case AttributeList::AT_NSReturnsNotRetained:
4831 case AttributeList::AT_CFReturnsNotRetained:
4832 case AttributeList::AT_NSReturnsRetained:
4833 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004834 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004835
Tanya Lattner0df579e2012-07-09 22:06:01 +00004836 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004837 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004838 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004839
Joey Gouly37453b92013-03-08 09:42:32 +00004840 case AttributeList::AT_VecTypeHint:
4841 handleVecTypeHint(S, D, Attr); break;
4842
Joey Gouly96cead52013-03-14 09:54:43 +00004843 case AttributeList::AT_Endian:
4844 handleEndianAttr(S, D, Attr);
4845 break;
4846
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004848 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004849
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4851 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4852 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004853 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004854 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004855 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004856 handleArcWeakrefUnavailableAttr (S, D, Attr);
4857 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004858 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004859 handleObjCRootClassAttr(S, D, Attr);
4860 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004861 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004862 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004863 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004864 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4865 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004866 handleReturnsTwiceAttr(S, D, Attr);
4867 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004869 case AttributeList::AT_Visibility:
4870 handleVisibilityAttr(S, D, Attr, false);
4871 break;
4872 case AttributeList::AT_TypeVisibility:
4873 handleVisibilityAttr(S, D, Attr, true);
4874 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004875 case AttributeList::AT_WarnUnused:
4876 handleWarnUnusedAttr(S, D, Attr);
4877 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004879 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004880 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4881 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4882 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4883 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004884 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004885 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004886 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004887 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004888 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004889 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004890 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004891 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004892 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4893 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4894 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4895 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4896 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4897 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4898 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4899 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4900 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004901 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004902 // Just ignore
4903 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004904 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004905 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004906 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004907 case AttributeList::AT_StdCall:
4908 case AttributeList::AT_CDecl:
4909 case AttributeList::AT_FastCall:
4910 case AttributeList::AT_ThisCall:
4911 case AttributeList::AT_Pascal:
4912 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004913 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004914 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004915 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004916 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004917 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004918 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004919 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004920 case AttributeList::AT_OpenCLImageAccess:
4921 handleOpenCLImageAccessAttr(S, D, Attr);
4922 break;
John McCallc052dbb2012-05-22 21:28:12 +00004923
4924 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004925 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004926 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004927 handleMsStructAttr(S, D, Attr);
4928 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004929 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004930 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004931 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004932 case AttributeList::AT_SingleInheritance:
4933 case AttributeList::AT_MultipleInheritance:
4934 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004935 handleInheritanceAttr(S, D, Attr);
4936 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004937 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004938 handlePortabilityAttr(S, D, Attr);
4939 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004940 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004941 handleForceInlineAttr(S, D, Attr);
4942 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004943 case AttributeList::AT_SelectAny:
4944 handleSelectAnyAttr(S, D, Attr);
4945 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004946
4947 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004948 case AttributeList::AT_AssertExclusiveLock:
4949 handleAssertExclusiveLockAttr(S, D, Attr);
4950 break;
4951 case AttributeList::AT_AssertSharedLock:
4952 handleAssertSharedLockAttr(S, D, Attr);
4953 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004954 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004955 handleGuardedVarAttr(S, D, Attr);
4956 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004957 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004958 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004959 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004960 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004961 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004962 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004963 case AttributeList::AT_NoSanitizeAddress:
4964 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004965 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004966 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004967 handleNoThreadSafetyAnalysis(S, D, Attr);
4968 break;
4969 case AttributeList::AT_NoSanitizeThread:
4970 handleNoSanitizeThread(S, D, Attr);
4971 break;
4972 case AttributeList::AT_NoSanitizeMemory:
4973 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004974 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004975 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004976 handleLockableAttr(S, D, Attr);
4977 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004978 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004979 handleGuardedByAttr(S, D, Attr);
4980 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004981 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004982 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004983 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004984 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004985 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004986 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004987 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004988 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004989 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004990 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004991 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004992 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004993 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004994 handleLockReturnedAttr(S, D, Attr);
4995 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004996 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004997 handleLocksExcludedAttr(S, D, Attr);
4998 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004999 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005000 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005001 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005002 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005003 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005004 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005005 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005006 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005007 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005008 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005009 handleUnlockFunAttr(S, D, Attr);
5010 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005011 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005012 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005013 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005014 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005015 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005016 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005017
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005018 // Uniqueness analysis attributes.
5019 case AttributeList::AT_Consumes:
5020 handleConsumesAttr(S, D, Attr);
5021 break;
5022 case AttributeList::AT_CallableWhenUnconsumed:
5023 handleCallableWhenUnconsumedAttr(S, D, Attr);
5024 break;
5025 case AttributeList::AT_TestsConsumed:
5026 handleTestsConsumedAttr(S, D, Attr);
5027 break;
5028 case AttributeList::AT_TestsUnconsumed:
5029 handleTestsUnconsumedAttr(S, D, Attr);
5030 break;
5031
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005032 // Type safety attributes.
5033 case AttributeList::AT_ArgumentWithTypeTag:
5034 handleArgumentWithTypeTagAttr(S, D, Attr);
5035 break;
5036 case AttributeList::AT_TypeTagForDatatype:
5037 handleTypeTagForDatatypeAttr(S, D, Attr);
5038 break;
5039
Chris Lattner803d0802008-06-29 00:43:07 +00005040 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005041 // Ask target about the attribute.
5042 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5043 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005044 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5045 diag::warn_unhandled_ms_attribute_ignored :
5046 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005047 break;
5048 }
5049}
5050
Peter Collingbourne60700392011-01-21 02:08:45 +00005051/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5052/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00005053/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00005054static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5055 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00005056 bool NonInheritable, bool Inheritable,
5057 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005058 if (Attr.isInvalid())
5059 return;
5060
Richard Smithcd8ab512013-01-17 01:30:42 +00005061 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5062 // instead.
5063 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5064 return;
5065
Peter Collingbourne60700392011-01-21 02:08:45 +00005066 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005067 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005068
5069 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005070 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005071}
5072
Chris Lattner803d0802008-06-29 00:43:07 +00005073/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5074/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005075void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005076 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005077 bool NonInheritable, bool Inheritable,
5078 bool IncludeCXX11Attributes) {
5079 for (const AttributeList* l = AttrList; l; l = l->getNext())
5080 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5081 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005082
5083 // GCC accepts
5084 // static int a9 __attribute__((weakref));
5085 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005086 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005087 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005088 cast<NamedDecl>(D)->getNameAsString();
5089 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005090 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005091 }
5092}
5093
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005094// Annotation attributes are the only attributes allowed after an access
5095// specifier.
5096bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5097 const AttributeList *AttrList) {
5098 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005099 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005100 handleAnnotateAttr(*this, ASDecl, *l);
5101 } else {
5102 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5103 return true;
5104 }
5105 }
5106
5107 return false;
5108}
5109
John McCalle82247a2011-10-01 05:17:03 +00005110/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5111/// contains any decl attributes that we should warn about.
5112static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5113 for ( ; A; A = A->getNext()) {
5114 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005115 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005116 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5117
5118 if (A->getKind() == AttributeList::UnknownAttribute) {
5119 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5120 << A->getName() << A->getRange();
5121 } else {
5122 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5123 << A->getName() << A->getRange();
5124 }
5125 }
5126}
5127
5128/// checkUnusedDeclAttributes - Given a declarator which is not being
5129/// used to build a declaration, complain about any decl attributes
5130/// which might be lying around on it.
5131void Sema::checkUnusedDeclAttributes(Declarator &D) {
5132 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5133 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5134 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5135 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5136}
5137
Ryan Flynne25ff832009-07-30 03:15:39 +00005138/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005139/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005140NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5141 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005142 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005143 NamedDecl *NewD = 0;
5144 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005145 FunctionDecl *NewFD;
5146 // FIXME: Missing call to CheckFunctionDeclaration().
5147 // FIXME: Mangling?
5148 // FIXME: Is the qualifier info correct?
5149 // FIXME: Is the DeclContext correct?
5150 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5151 Loc, Loc, DeclarationName(II),
5152 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005153 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005154 FD->hasPrototype(),
5155 false/*isConstexprSpecified*/);
5156 NewD = NewFD;
5157
5158 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005159 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005160
5161 // Fake up parameter variables; they are declared as if this were
5162 // a typedef.
5163 QualType FDTy = FD->getType();
5164 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5165 SmallVector<ParmVarDecl*, 16> Params;
5166 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5167 AE = FT->arg_type_end(); AI != AE; ++AI) {
5168 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5169 Param->setScopeInfo(0, Params.size());
5170 Params.push_back(Param);
5171 }
David Blaikie4278c652011-09-21 18:16:56 +00005172 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005173 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005174 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5175 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005176 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005177 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005178 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005179 if (VD->getQualifier()) {
5180 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005181 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005182 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005183 }
5184 return NewD;
5185}
5186
James Dennett1dfbd922012-06-14 21:40:34 +00005187/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005188/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005189void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005190 if (W.getUsed()) return; // only do this once
5191 W.setUsed(true);
5192 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5193 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005194 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005195 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5196 NDId->getName()));
5197 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005198 WeakTopLevelDecl.push_back(NewD);
5199 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5200 // to insert Decl at TU scope, sorry.
5201 DeclContext *SavedContext = CurContext;
5202 CurContext = Context.getTranslationUnitDecl();
5203 PushOnScopeChains(NewD, S);
5204 CurContext = SavedContext;
5205 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005206 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005207 }
5208}
5209
Rafael Espindola65611bf2013-03-02 21:41:48 +00005210void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5211 // It's valid to "forward-declare" #pragma weak, in which case we
5212 // have to do this.
5213 LoadExternalWeakUndeclaredIdentifiers();
5214 if (!WeakUndeclaredIdentifiers.empty()) {
5215 NamedDecl *ND = NULL;
5216 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5217 if (VD->isExternC())
5218 ND = VD;
5219 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5220 if (FD->isExternC())
5221 ND = FD;
5222 if (ND) {
5223 if (IdentifierInfo *Id = ND->getIdentifier()) {
5224 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5225 = WeakUndeclaredIdentifiers.find(Id);
5226 if (I != WeakUndeclaredIdentifiers.end()) {
5227 WeakInfo W = I->second;
5228 DeclApplyPragmaWeak(S, ND, W);
5229 WeakUndeclaredIdentifiers[Id] = W;
5230 }
5231 }
5232 }
5233 }
5234}
5235
Chris Lattner0744e5f2008-06-29 00:23:49 +00005236/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5237/// it, apply them to D. This is a bit tricky because PD can have attributes
5238/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005239void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5240 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005241 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005242 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005243 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005244
Chris Lattner0744e5f2008-06-29 00:23:49 +00005245 // Walk the declarator structure, applying decl attributes that were in a type
5246 // position to the decl itself. This handles cases like:
5247 // int *__attr__(x)** D;
5248 // when X is a decl attribute.
5249 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5250 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005251 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5252 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005253
Chris Lattner0744e5f2008-06-29 00:23:49 +00005254 // Finally, apply any attributes on the decl itself.
5255 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005256 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005257}
John McCall54abf7d2009-11-04 02:18:39 +00005258
John McCallf85e1932011-06-15 23:02:42 +00005259/// Is the given declaration allowed to use a forbidden type?
5260static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5261 // Private ivars are always okay. Unfortunately, people don't
5262 // always properly make their ivars private, even in system headers.
5263 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005264 // Function declarations in sys headers will be marked unavailable.
5265 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5266 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005267 return false;
5268
5269 // Require it to be declared in a system header.
5270 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5271}
5272
5273/// Handle a delayed forbidden-type diagnostic.
5274static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5275 Decl *decl) {
5276 if (decl && isForbiddenTypeAllowed(S, decl)) {
5277 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5278 "this system declaration uses an unsupported type"));
5279 return;
5280 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005281 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005282 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005283 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005284 // kind of forbidden type messages on unavailable functions.
5285 if (FD->hasAttr<UnavailableAttr>() &&
5286 diag.getForbiddenTypeDiagnostic() ==
5287 diag::err_arc_array_param_no_ownership) {
5288 diag.Triggered = true;
5289 return;
5290 }
5291 }
John McCallf85e1932011-06-15 23:02:42 +00005292
5293 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5294 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5295 diag.Triggered = true;
5296}
5297
John McCall92576642012-05-07 06:16:41 +00005298void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5299 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005300 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005301 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005302
John McCall92576642012-05-07 06:16:41 +00005303 // When delaying diagnostics to run in the context of a parsed
5304 // declaration, we only want to actually emit anything if parsing
5305 // succeeds.
5306 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005307
John McCall92576642012-05-07 06:16:41 +00005308 // We emit all the active diagnostics in this pool or any of its
5309 // parents. In general, we'll get one pool for the decl spec
5310 // and a child pool for each declarator; in a decl group like:
5311 // deprecated_typedef foo, *bar, baz();
5312 // only the declarator pops will be passed decls. This is correct;
5313 // we really do need to consider delayed diagnostics from the decl spec
5314 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005315 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005316 do {
John McCall13489672012-05-07 06:16:58 +00005317 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005318 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5319 // This const_cast is a bit lame. Really, Triggered should be mutable.
5320 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005321 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005322 continue;
5323
John McCalleee1d542011-02-14 07:13:47 +00005324 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005325 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005326 // Don't bother giving deprecation diagnostics if the decl is invalid.
5327 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005328 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005329 break;
5330
5331 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005332 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005333 break;
John McCallf85e1932011-06-15 23:02:42 +00005334
5335 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005336 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005337 break;
John McCall2f514482010-01-27 03:50:35 +00005338 }
5339 }
John McCall92576642012-05-07 06:16:41 +00005340 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005341}
5342
John McCall13489672012-05-07 06:16:58 +00005343/// Given a set of delayed diagnostics, re-emit them as if they had
5344/// been delayed in the current context instead of in the given pool.
5345/// Essentially, this just moves them to the current pool.
5346void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5347 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5348 assert(curPool && "re-emitting in undelayed context not supported");
5349 curPool->steal(pool);
5350}
5351
John McCall54abf7d2009-11-04 02:18:39 +00005352static bool isDeclDeprecated(Decl *D) {
5353 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005354 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005355 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005356 // A category implicitly has the availability of the interface.
5357 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5358 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005359 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5360 return false;
5361}
5362
Eli Friedmanc3b23082012-08-08 21:52:41 +00005363static void
5364DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5365 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005366 const ObjCInterfaceDecl *UnknownObjCClass,
5367 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005368 DeclarationName Name = D->getDeclName();
5369 if (!Message.empty()) {
5370 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5371 S.Diag(D->getLocation(),
5372 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5373 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005374 if (ObjCPropery)
5375 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5376 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005377 } else if (!UnknownObjCClass) {
5378 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5379 S.Diag(D->getLocation(),
5380 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5381 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005382 if (ObjCPropery)
5383 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5384 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005385 } else {
5386 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5387 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5388 }
5389}
5390
John McCall9c3087b2010-08-26 02:13:20 +00005391void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005392 Decl *Ctx) {
5393 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005394 return;
5395
John McCall2f514482010-01-27 03:50:35 +00005396 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005397 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5398 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005399 DD.getUnknownObjCClass(),
5400 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005401}
5402
Chris Lattner5f9e2722011-07-23 10:55:15 +00005403void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005404 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005405 const ObjCInterfaceDecl *UnknownObjCClass,
5406 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005407 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005408 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005409 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5410 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005411 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005412 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005413 return;
5414 }
5415
5416 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005417 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005418 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005419 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005420}