blob: 016c0f1c0b6b3a9dfa28bf5c47eb1a706ade69fc [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
Aaron Ballman437d43f2013-07-23 14:03:57 +000059/// These constants match the enumerated choices of
60/// err_attribute_argument_n_type.
61enum AttributeArgumentNType {
62 ArgumentIntOrBool,
63 ArgumentIntegerConstant,
64 ArgumentString,
65 ArgumentIdentifier
66};
67
Chris Lattnere5c5ee12008-06-29 00:16:31 +000068//===----------------------------------------------------------------------===//
69// Helper functions
70//===----------------------------------------------------------------------===//
71
Chandler Carruth87c44602011-07-01 23:49:12 +000072static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000073 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000074 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000075 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000076 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000077 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000078 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000079 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000080 Ty = decl->getUnderlyingType();
81 else
82 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000083
Chris Lattner6b6b5372008-06-26 18:38:35 +000084 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000085 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000086 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000087 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000088
John McCall183700f2009-09-21 23:43:11 +000089 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000090}
91
Daniel Dunbar35682492008-09-26 04:12:28 +000092// FIXME: We should provide an abstraction around a method or function
93// to provide the following bits of information.
94
Nuno Lopesd20254f2009-12-20 23:11:08 +000095/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000096/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000097static bool isFunction(const Decl *D) {
98 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000099}
100
101/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000102/// type (function or function-typed variable) or an Objective-C
103/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000105 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +0000106}
107
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000108/// isFunctionOrMethodOrBlock - Return true if the given decl has function
109/// type (function or function-typed variable) or an Objective-C
110/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000111static bool isFunctionOrMethodOrBlock(const Decl *D) {
112 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000113 return true;
114 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116 QualType Ty = V->getType();
117 return Ty->isBlockPointerType();
118 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000120}
121
John McCall711c52b2011-01-05 12:14:39 +0000122/// Return true if the given decl has a declarator that should have
123/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000124static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000125 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000126 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
127 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000128}
129
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000130/// hasFunctionProto - Return true if the given decl has a argument
131/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000132/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000133static bool hasFunctionProto(const Decl *D) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000136 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000137 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000138 return true;
139 }
140}
141
142/// getFunctionOrMethodNumArgs - Return number of function or method
143/// arguments. It is an error to call this on a K&R function (use
144/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000145static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
146 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000147 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000148 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000149 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000151}
152
Chandler Carruth87c44602011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000155 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000157 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000160}
161
Chandler Carruth87c44602011-07-01 23:49:12 +0000162static QualType getFunctionOrMethodResultType(const Decl *D) {
163 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000164 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000165 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000166}
167
Chandler Carruth87c44602011-07-01 23:49:12 +0000168static bool isFunctionOrMethodVariadic(const Decl *D) {
169 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000170 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000171 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000172 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000173 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000174 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000175 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000176 }
177}
178
Chandler Carruth87c44602011-07-01 23:49:12 +0000179static bool isInstanceMethod(const Decl *D) {
180 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000181 return MethodDecl->isInstance();
182 return false;
183}
184
Chris Lattner6b6b5372008-06-26 18:38:35 +0000185static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000186 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000187 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000188 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000189
John McCall506b57e2010-05-17 21:00:27 +0000190 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
191 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000193
John McCall506b57e2010-05-17 21:00:27 +0000194 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000195
Chris Lattner6b6b5372008-06-26 18:38:35 +0000196 // FIXME: Should we walk the chain of classes?
197 return ClsName == &Ctx.Idents.get("NSString") ||
198 ClsName == &Ctx.Idents.get("NSMutableString");
199}
200
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000201static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000202 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 if (!PT)
204 return false;
205
Ted Kremenek6217b802009-07-29 21:53:49 +0000206 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000207 if (!RT)
208 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000209
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000210 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000211 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000212 return false;
213
214 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
215}
216
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000217/// \brief Check if the attribute has exactly as many args as Num. May
218/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000219static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
220 unsigned int Num) {
221 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
223 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000224 return false;
225 }
226
227 return true;
228}
229
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000230
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000231/// \brief Check if the attribute has at least as many args as Num. May
232/// output an error.
233static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
234 unsigned int Num) {
235 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000236 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
237 return false;
238 }
239
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000240 return true;
241}
242
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000243/// \brief Check if IdxExpr is a valid argument index for a function or
244/// instance method D. May output an error.
245///
246/// \returns true if IdxExpr is a valid index.
247static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
248 StringRef AttrName,
249 SourceLocation AttrLoc,
250 unsigned AttrArgNum,
251 const Expr *IdxExpr,
252 uint64_t &Idx)
253{
254 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
255
256 // In C++ the implicit 'this' function parameter also counts.
257 // Parameters are counted from one.
258 const bool HasImplicitThisParam = isInstanceMethod(D);
259 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
260 const unsigned FirstIdx = 1;
261
262 llvm::APSInt IdxInt;
263 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
264 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000265 std::string Name = std::string("'") + AttrName.str() + std::string("'");
266 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
267 << AttrArgNum << ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000268 return false;
269 }
270
271 Idx = IdxInt.getLimitedValue();
272 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
273 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
274 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
275 return false;
276 }
277 Idx--; // Convert to zero-based.
278 if (HasImplicitThisParam) {
279 if (Idx == 0) {
280 S.Diag(AttrLoc,
281 diag::err_attribute_invalid_implicit_this_argument)
282 << AttrName << IdxExpr->getSourceRange();
283 return false;
284 }
285 --Idx;
286 }
287
288 return true;
289}
290
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000291///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000292/// \brief Check if passed in Decl is a field or potentially shared global var
293/// \return true if the Decl is a field or potentially shared global variable
294///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000295static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000296 if (isa<FieldDecl>(D))
297 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000298 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000299 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000300
301 return false;
302}
303
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000304/// \brief Check if the passed-in expression is of type int or bool.
305static bool isIntOrBool(Expr *Exp) {
306 QualType QT = Exp->getType();
307 return QT->isBooleanType() || QT->isIntegerType();
308}
309
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000310
311// Check to see if the type is a smart pointer of some kind. We assume
312// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000313static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
314 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
315 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000316 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000317 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000318
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000319 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
320 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000321 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000322 return false;
323
324 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000325}
326
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000327/// \brief Check if passed in Decl is a pointer type.
328/// Note that this function may produce an error message.
329/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000330static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
331 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000332 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000333 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000334 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000335 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000336
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000337 if (const RecordType *RT = QT->getAs<RecordType>()) {
338 // If it's an incomplete type, it could be a smart pointer; skip it.
339 // (We don't want to force template instantiation if we can avoid it,
340 // since that would alter the order in which templates are instantiated.)
341 if (RT->isIncompleteType())
342 return true;
343
344 if (threadSafetyCheckIsSmartPointer(S, RT))
345 return true;
346 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000347
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000348 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000349 << Attr.getName()->getName() << QT;
350 } else {
351 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
352 << Attr.getName();
353 }
354 return false;
355}
356
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000357/// \brief Checks that the passed in QualType either is of RecordType or points
358/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000359static const RecordType *getRecordType(QualType QT) {
360 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000361 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000362
363 // Now check if we point to record type.
364 if (const PointerType *PT = QT->getAs<PointerType>())
365 return PT->getPointeeType()->getAs<RecordType>();
366
367 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000368}
369
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000370
Jordy Rosefad5de92012-05-08 03:27:22 +0000371static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
372 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000373 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
374 if (RT->getDecl()->getAttr<LockableAttr>())
375 return true;
376 return false;
377}
378
379
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000380/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000381/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000382static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
383 QualType Ty) {
384 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000385
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000386 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000387 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000388 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000389 << Attr.getName() << Ty.getAsString();
390 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000391 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000392
Michael Hanf1aae3b2012-08-03 17:40:43 +0000393 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000394 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000395 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000396
397 // Allow smart pointers to be used as lockable objects.
398 // FIXME -- Check the type that the smart pointer points to.
399 if (threadSafetyCheckIsSmartPointer(S, RT))
400 return;
401
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000402 // Check if the type is lockable.
403 RecordDecl *RD = RT->getDecl();
404 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000405 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000406
407 // Else check if any base classes are lockable.
408 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
409 CXXBasePaths BPaths(false, false);
410 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
411 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000412 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000413
414 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
415 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000416}
417
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000418/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000419/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000420/// \param Sidx The attribute argument index to start checking with.
421/// \param ParamIdxOk Whether an argument can be indexing into a function
422/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000423static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000424 const AttributeList &Attr,
425 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000426 int Sidx = 0,
427 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000428 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000429 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000430
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000431 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000432 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000433 Args.push_back(ArgExp);
434 continue;
435 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000436
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000437 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000438 if (StrLit->getLength() == 0 ||
439 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000440 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000441 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000442 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000443 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000444 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000445
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000446 // We allow constant strings to be used as a placeholder for expressions
447 // that are not valid C++ syntax, but warn that they are ignored.
448 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
449 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000450 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000451 continue;
452 }
453
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000454 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000455
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000456 // A pointer to member expression of the form &MyClass::mu is treated
457 // specially -- we need to look at the type of the member.
458 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
459 if (UOp->getOpcode() == UO_AddrOf)
460 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
461 if (DRE->getDecl()->isCXXInstanceMember())
462 ArgTy = DRE->getDecl()->getType();
463
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000464 // First see if we can just cast to record type, or point to record type.
465 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000466
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 // Now check if we index into a record type function param.
468 if(!RT && ParamIdxOk) {
469 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
471 if(FD && IL) {
472 unsigned int NumParams = FD->getNumParams();
473 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000474 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
475 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
476 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
478 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000479 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000480 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000481 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482 }
483 }
484
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000485 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000486
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000487 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000488 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000489}
490
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000491//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000492// Attribute Implementations
493//===----------------------------------------------------------------------===//
494
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000495// FIXME: All this manual attribute parsing code is gross. At the
496// least add some helper functions to check most argument patterns (#
497// and types of args).
498
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000499enum ThreadAttributeDeclKind {
500 ThreadExpectedFieldOrGlobalVar,
501 ThreadExpectedFunctionOrMethod,
502 ThreadExpectedClassOrStruct
503};
504
Michael Hanf1aae3b2012-08-03 17:40:43 +0000505static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000506 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000507 assert(!Attr.isInvalid());
508
509 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000510 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000511
512 // D must be either a member field or global (potentially shared) variable.
513 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000514 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
515 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000516 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000517 }
518
Michael Handc691572012-07-23 18:48:41 +0000519 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000520}
521
Michael Handc691572012-07-23 18:48:41 +0000522static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
523 if (!checkGuardedVarAttrCommon(S, D, Attr))
524 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000525
Michael Han51d8c522013-01-24 16:46:58 +0000526 D->addAttr(::new (S.Context)
527 GuardedVarAttr(Attr.getRange(), S.Context,
528 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000529}
530
Michael Hanf1aae3b2012-08-03 17:40:43 +0000531static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000532 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000533 if (!checkGuardedVarAttrCommon(S, D, Attr))
534 return;
535
536 if (!threadSafetyCheckIsPointer(S, D, Attr))
537 return;
538
Michael Han51d8c522013-01-24 16:46:58 +0000539 D->addAttr(::new (S.Context)
540 PtGuardedVarAttr(Attr.getRange(), S.Context,
541 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000542}
543
Michael Hanf1aae3b2012-08-03 17:40:43 +0000544static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
545 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000546 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000547 assert(!Attr.isInvalid());
548
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000549 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000550 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000551
552 // D must be either a member field or global (potentially shared) variable.
553 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000554 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
555 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000556 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000557 }
558
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000559 SmallVector<Expr*, 1> Args;
560 // check that all arguments are lockable objects
561 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
562 unsigned Size = Args.size();
563 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000564 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000565
Michael Handc691572012-07-23 18:48:41 +0000566 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000567
Michael Handc691572012-07-23 18:48:41 +0000568 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000569}
570
Michael Handc691572012-07-23 18:48:41 +0000571static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
572 Expr *Arg = 0;
573 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
574 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000575
Michael Handc691572012-07-23 18:48:41 +0000576 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
577}
578
Michael Hanf1aae3b2012-08-03 17:40:43 +0000579static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000580 const AttributeList &Attr) {
581 Expr *Arg = 0;
582 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
583 return;
584
585 if (!threadSafetyCheckIsPointer(S, D, Attr))
586 return;
587
588 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
589 S.Context, Arg));
590}
591
Michael Hanf1aae3b2012-08-03 17:40:43 +0000592static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000593 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000594 assert(!Attr.isInvalid());
595
596 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000597 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000598
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000599 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000600 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000601 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
602 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000603 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000604 }
605
Michael Handc691572012-07-23 18:48:41 +0000606 return true;
607}
608
609static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
610 if (!checkLockableAttrCommon(S, D, Attr))
611 return;
612
613 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
614}
615
Michael Hanf1aae3b2012-08-03 17:40:43 +0000616static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000617 const AttributeList &Attr) {
618 if (!checkLockableAttrCommon(S, D, Attr))
619 return;
620
Michael Han51d8c522013-01-24 16:46:58 +0000621 D->addAttr(::new (S.Context)
622 ScopedLockableAttr(Attr.getRange(), S.Context,
623 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000624}
625
Kostya Serebryany85aee962013-02-26 06:58:27 +0000626static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
627 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000628 assert(!Attr.isInvalid());
629
630 if (!checkAttributeNumArgs(S, Attr, 0))
631 return;
632
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000633 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000634 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
635 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000636 return;
637 }
638
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000639 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000640 S.Context));
641}
642
Kostya Serebryany85aee962013-02-26 06:58:27 +0000643static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000644 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000645 assert(!Attr.isInvalid());
646
647 if (!checkAttributeNumArgs(S, Attr, 0))
648 return;
649
650 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000652 << Attr.getName() << ExpectedFunctionOrMethod;
653 return;
654 }
655
Michael Han51d8c522013-01-24 16:46:58 +0000656 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000657 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
658 Attr.getAttributeSpellingListIndex()));
659}
660
661static void handleNoSanitizeMemory(Sema &S, Decl *D,
662 const AttributeList &Attr) {
663 assert(!Attr.isInvalid());
664
665 if (!checkAttributeNumArgs(S, Attr, 0))
666 return;
667
668 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
670 << Attr.getName() << ExpectedFunctionOrMethod;
671 return;
672 }
673
674 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
675 S.Context));
676}
677
678static void handleNoSanitizeThread(Sema &S, Decl *D,
679 const AttributeList &Attr) {
680 assert(!Attr.isInvalid());
681
682 if (!checkAttributeNumArgs(S, Attr, 0))
683 return;
684
685 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
686 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
687 << Attr.getName() << ExpectedFunctionOrMethod;
688 return;
689 }
690
691 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
692 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000693}
694
Michael Hanf1aae3b2012-08-03 17:40:43 +0000695static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
696 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000697 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000698 assert(!Attr.isInvalid());
699
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000700 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000701 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000702
703 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000704 ValueDecl *VD = dyn_cast<ValueDecl>(D);
705 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000706 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
707 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000708 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000709 }
710
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000711 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000712 QualType QT = VD->getType();
713 if (!QT->isDependentType()) {
714 const RecordType *RT = getRecordType(QT);
715 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000716 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000717 << Attr.getName();
718 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000719 }
720 }
721
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000722 // Check that all arguments are lockable objects.
723 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000724 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000725 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000726
Michael Handc691572012-07-23 18:48:41 +0000727 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000728}
729
Michael Hanf1aae3b2012-08-03 17:40:43 +0000730static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000731 const AttributeList &Attr) {
732 SmallVector<Expr*, 1> Args;
733 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
734 return;
735
736 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000737 D->addAttr(::new (S.Context)
738 AcquiredAfterAttr(Attr.getRange(), S.Context,
739 StartArg, Args.size(),
740 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000741}
742
Michael Hanf1aae3b2012-08-03 17:40:43 +0000743static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000744 const AttributeList &Attr) {
745 SmallVector<Expr*, 1> Args;
746 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
747 return;
748
749 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000750 D->addAttr(::new (S.Context)
751 AcquiredBeforeAttr(Attr.getRange(), S.Context,
752 StartArg, Args.size(),
753 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000754}
755
Michael Hanf1aae3b2012-08-03 17:40:43 +0000756static bool checkLockFunAttrCommon(Sema &S, Decl *D,
757 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000758 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000759 assert(!Attr.isInvalid());
760
761 // zero or more arguments ok
762
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000763 // check that the attribute is applied to a function
764 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000765 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
766 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000767 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000768 }
769
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000770 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000771 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000772
Michael Handc691572012-07-23 18:48:41 +0000773 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000774}
775
Michael Hanf1aae3b2012-08-03 17:40:43 +0000776static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000777 const AttributeList &Attr) {
778 SmallVector<Expr*, 1> Args;
779 if (!checkLockFunAttrCommon(S, D, Attr, Args))
780 return;
781
782 unsigned Size = Args.size();
783 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000784 D->addAttr(::new (S.Context)
785 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
786 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000787}
788
Michael Hanf1aae3b2012-08-03 17:40:43 +0000789static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000790 const AttributeList &Attr) {
791 SmallVector<Expr*, 1> Args;
792 if (!checkLockFunAttrCommon(S, D, Attr, Args))
793 return;
794
795 unsigned Size = Args.size();
796 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000797 D->addAttr(::new (S.Context)
798 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
799 StartArg, Size,
800 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000801}
802
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000803static void handleAssertSharedLockAttr(Sema &S, Decl *D,
804 const AttributeList &Attr) {
805 SmallVector<Expr*, 1> Args;
806 if (!checkLockFunAttrCommon(S, D, Attr, Args))
807 return;
808
809 unsigned Size = Args.size();
810 Expr **StartArg = Size == 0 ? 0 : &Args[0];
811 D->addAttr(::new (S.Context)
812 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
813 Attr.getAttributeSpellingListIndex()));
814}
815
816static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
817 const AttributeList &Attr) {
818 SmallVector<Expr*, 1> Args;
819 if (!checkLockFunAttrCommon(S, D, Attr, Args))
820 return;
821
822 unsigned Size = Args.size();
823 Expr **StartArg = Size == 0 ? 0 : &Args[0];
824 D->addAttr(::new (S.Context)
825 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
826 StartArg, Size,
827 Attr.getAttributeSpellingListIndex()));
828}
829
830
Michael Hanf1aae3b2012-08-03 17:40:43 +0000831static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
832 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000833 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000834 assert(!Attr.isInvalid());
835
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000836 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000837 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000838
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000840 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
841 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000842 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000843 }
844
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000845 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000846 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
847 << Attr.getName() << 1 << ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000848 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000849 }
850
851 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000852 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000853
Michael Handc691572012-07-23 18:48:41 +0000854 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000855}
856
Michael Hanf1aae3b2012-08-03 17:40:43 +0000857static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000858 const AttributeList &Attr) {
859 SmallVector<Expr*, 2> Args;
860 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
861 return;
862
863 unsigned Size = Args.size();
864 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000865 D->addAttr(::new (S.Context)
866 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
867 Attr.getArg(0), StartArg, Size,
868 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000869}
870
Michael Hanf1aae3b2012-08-03 17:40:43 +0000871static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000872 const AttributeList &Attr) {
873 SmallVector<Expr*, 2> Args;
874 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
875 return;
876
877 unsigned Size = Args.size();
878 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000879 D->addAttr(::new (S.Context)
880 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
881 Attr.getArg(0), StartArg, Size,
882 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000883}
884
Michael Hanf1aae3b2012-08-03 17:40:43 +0000885static bool checkLocksRequiredCommon(Sema &S, Decl *D,
886 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000887 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000888 assert(!Attr.isInvalid());
889
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000890 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000891 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000892
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000893 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000894 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
895 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000896 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000897 }
898
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000899 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000900 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000901 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000902 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000903
Michael Handc691572012-07-23 18:48:41 +0000904 return true;
905}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000906
Michael Hanf1aae3b2012-08-03 17:40:43 +0000907static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000908 const AttributeList &Attr) {
909 SmallVector<Expr*, 1> Args;
910 if (!checkLocksRequiredCommon(S, D, Attr, Args))
911 return;
912
913 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000914 D->addAttr(::new (S.Context)
915 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
916 StartArg, Args.size(),
917 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000918}
919
Michael Hanf1aae3b2012-08-03 17:40:43 +0000920static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000921 const AttributeList &Attr) {
922 SmallVector<Expr*, 1> Args;
923 if (!checkLocksRequiredCommon(S, D, Attr, Args))
924 return;
925
926 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000927 D->addAttr(::new (S.Context)
928 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
929 StartArg, Args.size(),
930 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000931}
932
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000933static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000934 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000935 assert(!Attr.isInvalid());
936
937 // zero or more arguments ok
938
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000939 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000940 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
941 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000942 return;
943 }
944
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000945 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000946 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000947 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000948 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000949 Expr **StartArg = Size == 0 ? 0 : &Args[0];
950
Michael Han51d8c522013-01-24 16:46:58 +0000951 D->addAttr(::new (S.Context)
952 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
953 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000954}
955
956static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000957 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000958 assert(!Attr.isInvalid());
959
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000960 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000961 return;
962
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000963 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000964 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
965 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000966 return;
967 }
968
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000969 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000970 SmallVector<Expr*, 1> Args;
971 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
972 unsigned Size = Args.size();
973 if (Size == 0)
974 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000975
Michael Han51d8c522013-01-24 16:46:58 +0000976 D->addAttr(::new (S.Context)
977 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
978 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000979}
980
981static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000982 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000983 assert(!Attr.isInvalid());
984
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000985 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000986 return;
987
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000988 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000989 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
990 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000991 return;
992 }
993
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000994 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000995 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000996 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000997 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000998 if (Size == 0)
999 return;
1000 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001001
Michael Han51d8c522013-01-24 16:46:58 +00001002 D->addAttr(::new (S.Context)
1003 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1004 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001005}
1006
1007
Chandler Carruth1b03c872011-07-02 00:01:44 +00001008static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1009 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001010 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1011 if (TD == 0) {
1012 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1013 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001014 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001015 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001016 }
Mike Stumpbf916502009-07-24 19:02:52 +00001017
Richard Smitha4fa9002013-01-13 02:11:23 +00001018 // Remember this typedef decl, we will need it later for diagnostics.
1019 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001020}
1021
Chandler Carruth1b03c872011-07-02 00:01:44 +00001022static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001023 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001024 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001025 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001026
Chandler Carruth87c44602011-07-01 23:49:12 +00001027 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001028 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001029 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001030 // If the alignment is less than or equal to 8 bits, the packed attribute
1031 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001032 if (!FD->getType()->isDependentType() &&
1033 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001034 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001035 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001036 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001037 else
Michael Han51d8c522013-01-24 16:46:58 +00001038 FD->addAttr(::new (S.Context)
1039 PackedAttr(Attr.getRange(), S.Context,
1040 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001041 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001042 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001043}
1044
Chandler Carruth1b03c872011-07-02 00:01:44 +00001045static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001046 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001047 RD->addAttr(::new (S.Context)
1048 MsStructAttr(Attr.getRange(), S.Context,
1049 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001050 else
1051 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1052}
1053
Chandler Carruth1b03c872011-07-02 00:01:44 +00001054static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001055 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001056 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001057 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001058
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001059 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001060 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001061 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001062 D->addAttr(::new (S.Context)
1063 IBActionAttr(Attr.getRange(), S.Context,
1064 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001065 return;
1066 }
1067
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001068 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001069}
1070
Ted Kremenek2f041d02011-09-29 07:02:25 +00001071static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1072 // The IBOutlet/IBOutletCollection attributes only apply to instance
1073 // variables or properties of Objective-C classes. The outlet must also
1074 // have an object reference type.
1075 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1076 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001077 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001078 << Attr.getName() << VD->getType() << 0;
1079 return false;
1080 }
1081 }
1082 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1083 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001084 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001085 << Attr.getName() << PD->getType() << 1;
1086 return false;
1087 }
1088 }
1089 else {
1090 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1091 return false;
1092 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001093
Ted Kremenek2f041d02011-09-29 07:02:25 +00001094 return true;
1095}
1096
Chandler Carruth1b03c872011-07-02 00:01:44 +00001097static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001098 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001099 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001100 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001101
1102 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001103 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001104
Michael Han51d8c522013-01-24 16:46:58 +00001105 D->addAttr(::new (S.Context)
1106 IBOutletAttr(Attr.getRange(), S.Context,
1107 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001108}
1109
Chandler Carruth1b03c872011-07-02 00:01:44 +00001110static void handleIBOutletCollection(Sema &S, Decl *D,
1111 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001112
1113 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001114 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001115 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1116 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001117 return;
1118 }
1119
Ted Kremenek2f041d02011-09-29 07:02:25 +00001120 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001121 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001122
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001123 IdentifierInfo *II = Attr.getParameterName();
1124 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001125 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001126
John McCallb3d87482010-08-24 05:47:05 +00001127 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001128 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001129 if (!TypeRep) {
1130 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1131 return;
1132 }
John McCallb3d87482010-08-24 05:47:05 +00001133 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001134 // Diagnose use of non-object type in iboutletcollection attribute.
1135 // FIXME. Gnu attribute extension ignores use of builtin types in
1136 // attributes. So, __attribute__((iboutletcollection(char))) will be
1137 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001138 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001139 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1140 return;
1141 }
Michael Han51d8c522013-01-24 16:46:58 +00001142 D->addAttr(::new (S.Context)
1143 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1144 QT, Attr.getParameterLoc(),
1145 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001146}
1147
Chandler Carruthd309c812011-07-01 23:49:16 +00001148static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001149 if (const RecordType *UT = T->getAsUnionType())
1150 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1151 RecordDecl *UD = UT->getDecl();
1152 for (RecordDecl::field_iterator it = UD->field_begin(),
1153 itend = UD->field_end(); it != itend; ++it) {
1154 QualType QT = it->getType();
1155 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1156 T = QT;
1157 return;
1158 }
1159 }
1160 }
1161}
1162
Nuno Lopes587de5b2012-05-24 00:22:00 +00001163static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001164 if (!isFunctionOrMethod(D)) {
1165 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1166 << "alloc_size" << ExpectedFunctionOrMethod;
1167 return;
1168 }
1169
Nuno Lopes587de5b2012-05-24 00:22:00 +00001170 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1171 return;
1172
1173 // In C++ the implicit 'this' function parameter also counts, and they are
1174 // counted from one.
1175 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidis28965bf2013-02-22 06:58:28 +00001176 unsigned NumArgs;
1177 if (hasFunctionProto(D))
1178 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1179 else
1180 NumArgs = 0;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001181
1182 SmallVector<unsigned, 8> SizeArgs;
1183
1184 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1185 E = Attr.arg_end(); I!=E; ++I) {
1186 // The argument must be an integer constant expression.
1187 Expr *Ex = *I;
1188 llvm::APSInt ArgNum;
1189 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1190 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1192 << "alloc_size" << Ex->getSourceRange();
1193 return;
1194 }
1195
1196 uint64_t x = ArgNum.getZExtValue();
1197
1198 if (x < 1 || x > NumArgs) {
1199 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1200 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1201 return;
1202 }
1203
1204 --x;
1205 if (HasImplicitThisParam) {
1206 if (x == 0) {
1207 S.Diag(Attr.getLoc(),
1208 diag::err_attribute_invalid_implicit_this_argument)
1209 << "alloc_size" << Ex->getSourceRange();
1210 return;
1211 }
1212 --x;
1213 }
1214
1215 // check if the function argument is of an integer type
1216 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1217 if (!T->isIntegerType()) {
1218 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1219 << "alloc_size" << Ex->getSourceRange();
1220 return;
1221 }
1222
Nuno Lopes587de5b2012-05-24 00:22:00 +00001223 SizeArgs.push_back(x);
1224 }
1225
1226 // check if the function returns a pointer
1227 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1228 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1229 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1230 }
1231
Michael Han51d8c522013-01-24 16:46:58 +00001232 D->addAttr(::new (S.Context)
1233 AllocSizeAttr(Attr.getRange(), S.Context,
1234 SizeArgs.data(), SizeArgs.size(),
1235 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001236}
1237
Chandler Carruth1b03c872011-07-02 00:01:44 +00001238static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001239 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1240 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001241 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001242 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001243 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001244 return;
1245 }
Mike Stumpbf916502009-07-24 19:02:52 +00001246
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001247 // In C++ the implicit 'this' function parameter also counts, and they are
1248 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001249 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001250 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001251
1252 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001253 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001254
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001255 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1256 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001257 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001258 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001259 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001260 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1261 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001262 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1263 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001264 return;
1265 }
Mike Stumpbf916502009-07-24 19:02:52 +00001266
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001267 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001268
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001269 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001271 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001272 return;
1273 }
Mike Stumpbf916502009-07-24 19:02:52 +00001274
Ted Kremenek465172f2008-07-21 22:09:15 +00001275 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001276 if (HasImplicitThisParam) {
1277 if (x == 0) {
1278 S.Diag(Attr.getLoc(),
1279 diag::err_attribute_invalid_implicit_this_argument)
1280 << "nonnull" << Ex->getSourceRange();
1281 return;
1282 }
1283 --x;
1284 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001285
1286 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001287 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001288 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001289
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001290 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001291 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001292 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001293 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001294 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001295 }
Mike Stumpbf916502009-07-24 19:02:52 +00001296
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001297 NonNullArgs.push_back(x);
1298 }
Mike Stumpbf916502009-07-24 19:02:52 +00001299
1300 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1301 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001302 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001303 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1304 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001305 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001306 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001307 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001308 }
Mike Stumpbf916502009-07-24 19:02:52 +00001309
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001310 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001311 if (NonNullArgs.empty()) {
1312 // Warn the trivial case only if attribute is not coming from a
1313 // macro instantiation.
1314 if (Attr.getLoc().isFileID())
1315 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001316 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001317 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001318 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001319
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001320 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001321 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001322 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001323 D->addAttr(::new (S.Context)
1324 NonNullAttr(Attr.getRange(), S.Context, start, size,
1325 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001326}
1327
Chandler Carruth1b03c872011-07-02 00:01:44 +00001328static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001329 // This attribute must be applied to a function declaration.
1330 // The first argument to the attribute must be a string,
1331 // the name of the resource, for example "malloc".
1332 // The following arguments must be argument indexes, the arguments must be
1333 // of integer type for Returns, otherwise of pointer type.
1334 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001335 // after being held. free() should be __attribute((ownership_takes)), whereas
1336 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001337
1338 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001339 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1340 << AL.getName()->getName() << 1 << ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341 return;
1342 }
1343 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001344 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001345 switch (AL.getKind()) {
1346 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001347 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001348 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001349 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1350 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001351 return;
1352 }
Jordy Rose2a479922010-08-12 08:54:03 +00001353 break;
1354 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001355 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001356 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001357 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1358 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001359 return;
1360 }
Jordy Rose2a479922010-08-12 08:54:03 +00001361 break;
1362 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001363 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001364 if (AL.getNumArgs() > 1) {
1365 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001366 << AL.getName() << AL.getNumArgs() + 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001367 return;
1368 }
Jordy Rose2a479922010-08-12 08:54:03 +00001369 break;
1370 default:
1371 // This should never happen given how we are called.
1372 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001373 }
1374
Chandler Carruth87c44602011-07-01 23:49:12 +00001375 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001376 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1377 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001378 return;
1379 }
1380
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001381 // In C++ the implicit 'this' function parameter also counts, and they are
1382 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001383 bool HasImplicitThisParam = isInstanceMethod(D);
1384 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001385
Chris Lattner5f9e2722011-07-23 10:55:15 +00001386 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001387
1388 // Normalize the argument, __foo__ becomes foo.
1389 if (Module.startswith("__") && Module.endswith("__"))
1390 Module = Module.substr(2, Module.size() - 4);
1391
Chris Lattner5f9e2722011-07-23 10:55:15 +00001392 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001393
Jordy Rose2a479922010-08-12 08:54:03 +00001394 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1395 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001396
Peter Collingbourne7a730022010-11-23 20:45:58 +00001397 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398 llvm::APSInt ArgNum(32);
1399 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1400 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1401 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1402 << AL.getName()->getName() << IdxExpr->getSourceRange();
1403 continue;
1404 }
1405
1406 unsigned x = (unsigned) ArgNum.getZExtValue();
1407
1408 if (x > NumArgs || x < 1) {
1409 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1410 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1411 continue;
1412 }
1413 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001414 if (HasImplicitThisParam) {
1415 if (x == 0) {
1416 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1417 << "ownership" << IdxExpr->getSourceRange();
1418 return;
1419 }
1420 --x;
1421 }
1422
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001423 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001424 case OwnershipAttr::Takes:
1425 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001426 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001427 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001428 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1429 // FIXME: Should also highlight argument in decl.
1430 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001431 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001432 << "pointer"
1433 << IdxExpr->getSourceRange();
1434 continue;
1435 }
1436 break;
1437 }
Sean Huntcf807c42010-08-18 23:23:40 +00001438 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001439 if (AL.getNumArgs() > 1) {
1440 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001441 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001442 llvm::APSInt ArgNum(32);
1443 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1444 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1445 S.Diag(AL.getLoc(), diag::err_ownership_type)
1446 << "ownership_returns" << "integer"
1447 << IdxExpr->getSourceRange();
1448 return;
1449 }
1450 }
1451 break;
1452 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001453 } // switch
1454
1455 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001456 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001457 i = D->specific_attr_begin<OwnershipAttr>(),
1458 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001459 i != e; ++i) {
1460 if ((*i)->getOwnKind() != K) {
1461 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1462 I!=E; ++I) {
1463 if (x == *I) {
1464 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1465 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001466 }
1467 }
1468 }
1469 }
1470 OwnershipArgs.push_back(x);
1471 }
1472
1473 unsigned* start = OwnershipArgs.data();
1474 unsigned size = OwnershipArgs.size();
1475 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001476
1477 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001478 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1479 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001480 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001481 }
Sean Huntcf807c42010-08-18 23:23:40 +00001482
Michael Han51d8c522013-01-24 16:46:58 +00001483 D->addAttr(::new (S.Context)
1484 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1485 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001486}
1487
Chandler Carruth1b03c872011-07-02 00:01:44 +00001488static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001489 // Check the attribute arguments.
1490 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1492 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001493 return;
1494 }
1495
Chandler Carruth87c44602011-07-01 23:49:12 +00001496 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001497 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001498 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001499 return;
1500 }
1501
Chandler Carruth87c44602011-07-01 23:49:12 +00001502 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001503
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001504 // gcc rejects
1505 // class c {
1506 // static int a __attribute__((weakref ("v2")));
1507 // static int b() __attribute__((weakref ("f3")));
1508 // };
1509 // and ignores the attributes of
1510 // void f(void) {
1511 // static int a __attribute__((weakref ("v2")));
1512 // }
1513 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001514 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001515 if (!Ctx->isFileContext()) {
1516 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001517 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001518 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001519 }
1520
1521 // The GCC manual says
1522 //
1523 // At present, a declaration to which `weakref' is attached can only
1524 // be `static'.
1525 //
1526 // It also says
1527 //
1528 // Without a TARGET,
1529 // given as an argument to `weakref' or to `alias', `weakref' is
1530 // equivalent to `weak'.
1531 //
1532 // gcc 4.4.1 will accept
1533 // int a7 __attribute__((weakref));
1534 // as
1535 // int a7 __attribute__((weak));
1536 // This looks like a bug in gcc. We reject that for now. We should revisit
1537 // it if this behaviour is actually used.
1538
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001539 // GCC rejects
1540 // static ((alias ("y"), weakref)).
1541 // Should we? How to check that weakref is before or after alias?
1542
1543 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001544 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001545 Arg = Arg->IgnoreParenCasts();
1546 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1547
Douglas Gregor5cee1192011-07-27 05:40:30 +00001548 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1550 << Attr.getName() << 1 << ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001551 return;
1552 }
1553 // GCC will accept anything as the argument of weakref. Should we
1554 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001555 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001556 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001557 }
1558
Michael Han51d8c522013-01-24 16:46:58 +00001559 D->addAttr(::new (S.Context)
1560 WeakRefAttr(Attr.getRange(), S.Context,
1561 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001562}
1563
Chandler Carruth1b03c872011-07-02 00:01:44 +00001564static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001565 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001566 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001567 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001568
Peter Collingbourne7a730022010-11-23 20:45:58 +00001569 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001570 Arg = Arg->IgnoreParenCasts();
1571 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001572
Douglas Gregor5cee1192011-07-27 05:40:30 +00001573 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001574 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1575 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001576 return;
1577 }
Mike Stumpbf916502009-07-24 19:02:52 +00001578
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001579 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001580 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1581 return;
1582 }
1583
Chris Lattner6b6b5372008-06-26 18:38:35 +00001584 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001585
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001586 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001587 Str->getString(),
1588 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001589}
1590
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001591static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1592 // Check the attribute arguments.
1593 if (!checkAttributeNumArgs(S, Attr, 0))
1594 return;
1595
1596 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1598 << Attr.getName() << ExpectedFunctionOrMethod;
1599 return;
1600 }
1601
Michael Han51d8c522013-01-24 16:46:58 +00001602 D->addAttr(::new (S.Context)
1603 MinSizeAttr(Attr.getRange(), S.Context,
1604 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001605}
1606
Benjamin Krameree409a92012-05-12 21:10:52 +00001607static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1608 // Check the attribute arguments.
1609 if (!checkAttributeNumArgs(S, Attr, 0))
1610 return;
1611
1612 if (!isa<FunctionDecl>(D)) {
1613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1614 << Attr.getName() << ExpectedFunction;
1615 return;
1616 }
1617
1618 if (D->hasAttr<HotAttr>()) {
1619 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1620 << Attr.getName() << "hot";
1621 return;
1622 }
1623
Michael Han51d8c522013-01-24 16:46:58 +00001624 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1625 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001626}
1627
1628static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1629 // Check the attribute arguments.
1630 if (!checkAttributeNumArgs(S, Attr, 0))
1631 return;
1632
1633 if (!isa<FunctionDecl>(D)) {
1634 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1635 << Attr.getName() << ExpectedFunction;
1636 return;
1637 }
1638
1639 if (D->hasAttr<ColdAttr>()) {
1640 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1641 << Attr.getName() << "cold";
1642 return;
1643 }
1644
Michael Han51d8c522013-01-24 16:46:58 +00001645 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001647}
1648
Chandler Carruth1b03c872011-07-02 00:01:44 +00001649static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001650 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001651 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001652 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001653
Chandler Carruth87c44602011-07-01 23:49:12 +00001654 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001656 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001657 return;
1658 }
1659
Michael Han51d8c522013-01-24 16:46:58 +00001660 D->addAttr(::new (S.Context)
1661 NakedAttr(Attr.getRange(), S.Context,
1662 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001663}
1664
Chandler Carruth1b03c872011-07-02 00:01:44 +00001665static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1666 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001667 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001668 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1670 << Attr.getName() << 0;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001671 return;
1672 }
1673
Chandler Carruth87c44602011-07-01 23:49:12 +00001674 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001676 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001677 return;
1678 }
Mike Stumpbf916502009-07-24 19:02:52 +00001679
Michael Han51d8c522013-01-24 16:46:58 +00001680 D->addAttr(::new (S.Context)
1681 AlwaysInlineAttr(Attr.getRange(), S.Context,
1682 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001683}
1684
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001685static void handleTLSModelAttr(Sema &S, Decl *D,
1686 const AttributeList &Attr) {
1687 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001688 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001689 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001690
1691 Expr *Arg = Attr.getArg(0);
1692 Arg = Arg->IgnoreParenCasts();
1693 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1694
1695 // Check that it is a string.
1696 if (!Str) {
1697 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1698 return;
1699 }
1700
Richard Smith38afbc72013-04-13 02:43:54 +00001701 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1703 << Attr.getName() << ExpectedTLSVar;
1704 return;
1705 }
1706
1707 // Check that the value.
1708 StringRef Model = Str->getString();
1709 if (Model != "global-dynamic" && Model != "local-dynamic"
1710 && Model != "initial-exec" && Model != "local-exec") {
1711 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1712 return;
1713 }
1714
Michael Han51d8c522013-01-24 16:46:58 +00001715 D->addAttr(::new (S.Context)
1716 TLSModelAttr(Attr.getRange(), S.Context, Model,
1717 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001718}
1719
Chandler Carruth1b03c872011-07-02 00:01:44 +00001720static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001721 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001722 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1724 << Attr.getName() << 0;
Ryan Flynn76168e22009-08-09 20:07:29 +00001725 return;
1726 }
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Chandler Carruth87c44602011-07-01 23:49:12 +00001728 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001729 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001730 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001731 D->addAttr(::new (S.Context)
1732 MallocAttr(Attr.getRange(), S.Context,
1733 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001734 return;
1735 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001736 }
1737
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001738 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001739}
1740
Chandler Carruth1b03c872011-07-02 00:01:44 +00001741static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001742 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001743 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001744 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001745
Michael Han51d8c522013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context)
1747 MayAliasAttr(Attr.getRange(), S.Context,
1748 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001749}
1750
Chandler Carruth1b03c872011-07-02 00:01:44 +00001751static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001752 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001753 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001754 D->addAttr(::new (S.Context)
1755 NoCommonAttr(Attr.getRange(), S.Context,
1756 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001757 else
1758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001759 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001760}
1761
Chandler Carruth1b03c872011-07-02 00:01:44 +00001762static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001763 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001764
1765 if (S.LangOpts.CPlusPlus) {
1766 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1767 return;
1768 }
1769
Chandler Carruth87c44602011-07-01 23:49:12 +00001770 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001771 D->addAttr(::new (S.Context)
1772 CommonAttr(Attr.getRange(), S.Context,
1773 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001774 else
1775 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001776 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001777}
1778
Chandler Carruth1b03c872011-07-02 00:01:44 +00001779static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001780 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001781
1782 if (S.CheckNoReturnAttr(attr)) return;
1783
Chandler Carruth87c44602011-07-01 23:49:12 +00001784 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001785 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001786 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001787 return;
1788 }
1789
Michael Han51d8c522013-01-24 16:46:58 +00001790 D->addAttr(::new (S.Context)
1791 NoReturnAttr(attr.getRange(), S.Context,
1792 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001793}
1794
1795bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001796 if (attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001797 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1798 << attr.getName() << 0;
John McCall711c52b2011-01-05 12:14:39 +00001799 attr.setInvalid();
1800 return true;
1801 }
1802
1803 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001804}
1805
Chandler Carruth1b03c872011-07-02 00:01:44 +00001806static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1807 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001808
1809 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1810 // because 'analyzer_noreturn' does not impact the type.
1811
Chandler Carruth1731e202011-07-11 23:30:35 +00001812 if(!checkAttributeNumArgs(S, Attr, 0))
1813 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001814
Chandler Carruth87c44602011-07-01 23:49:12 +00001815 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1816 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001817 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1818 && !VD->getType()->isFunctionPointerType())) {
1819 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001820 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001821 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001822 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001823 return;
1824 }
1825 }
1826
Michael Han51d8c522013-01-24 16:46:58 +00001827 D->addAttr(::new (S.Context)
1828 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1829 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001830}
1831
Richard Smithcd8ab512013-01-17 01:30:42 +00001832static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1833 const AttributeList &Attr) {
1834 // C++11 [dcl.attr.noreturn]p1:
1835 // The attribute may be applied to the declarator-id in a function
1836 // declaration.
1837 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1838 if (!FD) {
1839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1840 << Attr.getName() << ExpectedFunctionOrMethod;
1841 return;
1842 }
1843
Michael Han51d8c522013-01-24 16:46:58 +00001844 D->addAttr(::new (S.Context)
1845 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1846 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001847}
1848
John Thompson35cc9622010-08-09 21:53:52 +00001849// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001850static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001851/*
1852 Returning a Vector Class in Registers
1853
Eric Christopherf48f3672010-12-01 22:13:54 +00001854 According to the PPU ABI specifications, a class with a single member of
1855 vector type is returned in memory when used as the return value of a function.
1856 This results in inefficient code when implementing vector classes. To return
1857 the value in a single vector register, add the vecreturn attribute to the
1858 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001859
1860 Example:
1861
1862 struct Vector
1863 {
1864 __vector float xyzw;
1865 } __attribute__((vecreturn));
1866
1867 Vector Add(Vector lhs, Vector rhs)
1868 {
1869 Vector result;
1870 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1871 return result; // This will be returned in a register
1872 }
1873*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001874 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001876 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001877 return;
1878 }
1879
Chandler Carruth87c44602011-07-01 23:49:12 +00001880 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001881 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1882 return;
1883 }
1884
Chandler Carruth87c44602011-07-01 23:49:12 +00001885 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001886 int count = 0;
1887
1888 if (!isa<CXXRecordDecl>(record)) {
1889 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1890 return;
1891 }
1892
1893 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1894 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1895 return;
1896 }
1897
Eric Christopherf48f3672010-12-01 22:13:54 +00001898 for (RecordDecl::field_iterator iter = record->field_begin();
1899 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001900 if ((count == 1) || !iter->getType()->isVectorType()) {
1901 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1902 return;
1903 }
1904 count++;
1905 }
1906
Michael Han51d8c522013-01-24 16:46:58 +00001907 D->addAttr(::new (S.Context)
1908 VecReturnAttr(Attr.getRange(), S.Context,
1909 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001910}
1911
Richard Smith3a2b7a12013-01-28 22:42:45 +00001912static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1913 const AttributeList &Attr) {
1914 if (isa<ParmVarDecl>(D)) {
1915 // [[carries_dependency]] can only be applied to a parameter if it is a
1916 // parameter of a function declaration or lambda.
1917 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1918 S.Diag(Attr.getLoc(),
1919 diag::err_carries_dependency_param_not_function_decl);
1920 return;
1921 }
1922 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001923 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001924 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001925 return;
1926 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001927
1928 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1929 Attr.getRange(), S.Context,
1930 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001931}
1932
Chandler Carruth1b03c872011-07-02 00:01:44 +00001933static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001934 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001935 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1937 << Attr.getName() << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001938 return;
1939 }
Mike Stumpbf916502009-07-24 19:02:52 +00001940
Chandler Carruth87c44602011-07-01 23:49:12 +00001941 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001942 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001944 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001945 return;
1946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Michael Han51d8c522013-01-24 16:46:58 +00001948 D->addAttr(::new (S.Context)
1949 UnusedAttr(Attr.getRange(), S.Context,
1950 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001951}
1952
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001953static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1954 const AttributeList &Attr) {
1955 // check the attribute arguments.
1956 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001957 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1958 << Attr.getName() << 0;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001959 return;
1960 }
1961
1962 if (!isa<FunctionDecl>(D)) {
1963 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1964 << Attr.getName() << ExpectedFunction;
1965 return;
1966 }
1967
Michael Han51d8c522013-01-24 16:46:58 +00001968 D->addAttr(::new (S.Context)
1969 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1970 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001971}
1972
Chandler Carruth1b03c872011-07-02 00:01:44 +00001973static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001974 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001975 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001976 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1977 << Attr.getName() << 0;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001978 return;
1979 }
Mike Stumpbf916502009-07-24 19:02:52 +00001980
Chandler Carruth87c44602011-07-01 23:49:12 +00001981 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001982 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001983 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1984 return;
1985 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001986 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001987 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001988 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001989 return;
1990 }
Mike Stumpbf916502009-07-24 19:02:52 +00001991
Michael Han51d8c522013-01-24 16:46:58 +00001992 D->addAttr(::new (S.Context)
1993 UsedAttr(Attr.getRange(), S.Context,
1994 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001995}
1996
Chandler Carruth1b03c872011-07-02 00:01:44 +00001997static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001998 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001999 if (Attr.getNumArgs() > 1) {
2000 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002001 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002002 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002003
2004 int priority = 65535; // FIXME: Do not hardcode such constants.
2005 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002006 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002007 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002008 if (E->isTypeDependent() || E->isValueDependent() ||
2009 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2011 << Attr.getName() << 1 << ArgumentIntegerConstant
2012 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002013 return;
2014 }
2015 priority = Idx.getZExtValue();
2016 }
Mike Stumpbf916502009-07-24 19:02:52 +00002017
Chandler Carruth87c44602011-07-01 23:49:12 +00002018 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002019 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002020 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002021 return;
2022 }
2023
Michael Han51d8c522013-01-24 16:46:58 +00002024 D->addAttr(::new (S.Context)
2025 ConstructorAttr(Attr.getRange(), S.Context, priority,
2026 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002027}
2028
Chandler Carruth1b03c872011-07-02 00:01:44 +00002029static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002030 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002031 if (Attr.getNumArgs() > 1) {
2032 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002033 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002034 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002035
2036 int priority = 65535; // FIXME: Do not hardcode such constants.
2037 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002038 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002039 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002040 if (E->isTypeDependent() || E->isValueDependent() ||
2041 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002042 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2043 << Attr.getName() << 1 << ArgumentIntegerConstant
2044 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002045 return;
2046 }
2047 priority = Idx.getZExtValue();
2048 }
Mike Stumpbf916502009-07-24 19:02:52 +00002049
Chandler Carruth87c44602011-07-01 23:49:12 +00002050 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002052 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002053 return;
2054 }
2055
Michael Han51d8c522013-01-24 16:46:58 +00002056 D->addAttr(::new (S.Context)
2057 DestructorAttr(Attr.getRange(), S.Context, priority,
2058 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002059}
2060
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002061template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002062static void handleAttrWithMessage(Sema &S, Decl *D,
2063 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002064 unsigned NumArgs = Attr.getNumArgs();
2065 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002066 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002067 return;
2068 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002069
2070 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002071 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002072 if (NumArgs == 1) {
2073 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002074 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002075 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002076 << Attr.getName();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002077 return;
2078 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002079 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002080 }
Mike Stumpbf916502009-07-24 19:02:52 +00002081
Michael Han51d8c522013-01-24 16:46:58 +00002082 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2083 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002084}
2085
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002086static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2087 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002088 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002089 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002090
Michael Han51d8c522013-01-24 16:46:58 +00002091 D->addAttr(::new (S.Context)
2092 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2093 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002094}
2095
Patrick Beardb2f68202012-04-06 18:12:22 +00002096static void handleObjCRootClassAttr(Sema &S, Decl *D,
2097 const AttributeList &Attr) {
2098 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002099 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2100 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002101 return;
2102 }
2103
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002104 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002105 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002106
Michael Han51d8c522013-01-24 16:46:58 +00002107 D->addAttr(::new (S.Context)
2108 ObjCRootClassAttr(Attr.getRange(), S.Context,
2109 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002110}
2111
Michael Han51d8c522013-01-24 16:46:58 +00002112static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2113 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002114 if (!isa<ObjCInterfaceDecl>(D)) {
2115 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2116 return;
2117 }
2118
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002119 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002120 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002121
Michael Han51d8c522013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context)
2123 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2124 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002125}
2126
Jordy Rosefad5de92012-05-08 03:27:22 +00002127static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2128 IdentifierInfo *Platform,
2129 VersionTuple Introduced,
2130 VersionTuple Deprecated,
2131 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002132 StringRef PlatformName
2133 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2134 if (PlatformName.empty())
2135 PlatformName = Platform->getName();
2136
2137 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2138 // of these steps are needed).
2139 if (!Introduced.empty() && !Deprecated.empty() &&
2140 !(Introduced <= Deprecated)) {
2141 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2142 << 1 << PlatformName << Deprecated.getAsString()
2143 << 0 << Introduced.getAsString();
2144 return true;
2145 }
2146
2147 if (!Introduced.empty() && !Obsoleted.empty() &&
2148 !(Introduced <= Obsoleted)) {
2149 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2150 << 2 << PlatformName << Obsoleted.getAsString()
2151 << 0 << Introduced.getAsString();
2152 return true;
2153 }
2154
2155 if (!Deprecated.empty() && !Obsoleted.empty() &&
2156 !(Deprecated <= Obsoleted)) {
2157 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2158 << 2 << PlatformName << Obsoleted.getAsString()
2159 << 1 << Deprecated.getAsString();
2160 return true;
2161 }
2162
2163 return false;
2164}
2165
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002166/// \brief Check whether the two versions match.
2167///
2168/// If either version tuple is empty, then they are assumed to match. If
2169/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2170static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2171 bool BeforeIsOkay) {
2172 if (X.empty() || Y.empty())
2173 return true;
2174
2175 if (X == Y)
2176 return true;
2177
2178 if (BeforeIsOkay && X < Y)
2179 return true;
2180
2181 return false;
2182}
2183
Rafael Espindola51be6e32013-01-08 22:04:34 +00002184AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002185 IdentifierInfo *Platform,
2186 VersionTuple Introduced,
2187 VersionTuple Deprecated,
2188 VersionTuple Obsoleted,
2189 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002190 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002191 bool Override,
2192 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002193 VersionTuple MergedIntroduced = Introduced;
2194 VersionTuple MergedDeprecated = Deprecated;
2195 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002196 bool FoundAny = false;
2197
Rafael Espindola98ae8342012-05-10 02:50:16 +00002198 if (D->hasAttrs()) {
2199 AttrVec &Attrs = D->getAttrs();
2200 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2201 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2202 if (!OldAA) {
2203 ++i;
2204 continue;
2205 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002206
Rafael Espindola98ae8342012-05-10 02:50:16 +00002207 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2208 if (OldPlatform != Platform) {
2209 ++i;
2210 continue;
2211 }
2212
2213 FoundAny = true;
2214 VersionTuple OldIntroduced = OldAA->getIntroduced();
2215 VersionTuple OldDeprecated = OldAA->getDeprecated();
2216 VersionTuple OldObsoleted = OldAA->getObsoleted();
2217 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002218
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002219 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2220 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2221 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2222 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002223 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002224 if (Override) {
2225 int Which = -1;
2226 VersionTuple FirstVersion;
2227 VersionTuple SecondVersion;
2228 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2229 Which = 0;
2230 FirstVersion = OldIntroduced;
2231 SecondVersion = Introduced;
2232 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2233 Which = 1;
2234 FirstVersion = Deprecated;
2235 SecondVersion = OldDeprecated;
2236 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2237 Which = 2;
2238 FirstVersion = Obsoleted;
2239 SecondVersion = OldObsoleted;
2240 }
2241
2242 if (Which == -1) {
2243 Diag(OldAA->getLocation(),
2244 diag::warn_mismatched_availability_override_unavail)
2245 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2246 } else {
2247 Diag(OldAA->getLocation(),
2248 diag::warn_mismatched_availability_override)
2249 << Which
2250 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2251 << FirstVersion.getAsString() << SecondVersion.getAsString();
2252 }
2253 Diag(Range.getBegin(), diag::note_overridden_method);
2254 } else {
2255 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2256 Diag(Range.getBegin(), diag::note_previous_attribute);
2257 }
2258
Rafael Espindola98ae8342012-05-10 02:50:16 +00002259 Attrs.erase(Attrs.begin() + i);
2260 --e;
2261 continue;
2262 }
2263
2264 VersionTuple MergedIntroduced2 = MergedIntroduced;
2265 VersionTuple MergedDeprecated2 = MergedDeprecated;
2266 VersionTuple MergedObsoleted2 = MergedObsoleted;
2267
2268 if (MergedIntroduced2.empty())
2269 MergedIntroduced2 = OldIntroduced;
2270 if (MergedDeprecated2.empty())
2271 MergedDeprecated2 = OldDeprecated;
2272 if (MergedObsoleted2.empty())
2273 MergedObsoleted2 = OldObsoleted;
2274
2275 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2276 MergedIntroduced2, MergedDeprecated2,
2277 MergedObsoleted2)) {
2278 Attrs.erase(Attrs.begin() + i);
2279 --e;
2280 continue;
2281 }
2282
2283 MergedIntroduced = MergedIntroduced2;
2284 MergedDeprecated = MergedDeprecated2;
2285 MergedObsoleted = MergedObsoleted2;
2286 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002287 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002288 }
2289
2290 if (FoundAny &&
2291 MergedIntroduced == Introduced &&
2292 MergedDeprecated == Deprecated &&
2293 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002294 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002295
Ted Kremenekcb344392013-04-06 00:34:27 +00002296 // Only create a new attribute if !Override, but we want to do
2297 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002298 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002299 MergedDeprecated, MergedObsoleted) &&
2300 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002301 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2302 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002303 Obsoleted, IsUnavailable, Message,
2304 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002305 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002306 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002307}
2308
Chandler Carruth1b03c872011-07-02 00:01:44 +00002309static void handleAvailabilityAttr(Sema &S, Decl *D,
2310 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002311 IdentifierInfo *Platform = Attr.getParameterName();
2312 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002313 unsigned Index = Attr.getAttributeSpellingListIndex();
2314
Rafael Espindola3b294362012-05-06 19:56:25 +00002315 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002316 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2317 << Platform;
2318
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002319 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2320 if (!ND) {
2321 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2322 return;
2323 }
2324
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002325 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2326 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2327 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002328 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002329 StringRef Str;
2330 const StringLiteral *SE =
2331 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2332 if (SE)
2333 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002334
Rafael Espindola51be6e32013-01-08 22:04:34 +00002335 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002336 Platform,
2337 Introduced.Version,
2338 Deprecated.Version,
2339 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002340 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002341 /*Override=*/false,
2342 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002343 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002344 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002345}
2346
John McCalld4c3d662013-02-20 01:54:26 +00002347template <class T>
2348static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2349 typename T::VisibilityType value,
2350 unsigned attrSpellingListIndex) {
2351 T *existingAttr = D->getAttr<T>();
2352 if (existingAttr) {
2353 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2354 if (existingValue == value)
2355 return NULL;
2356 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2357 S.Diag(range.getBegin(), diag::note_previous_attribute);
2358 D->dropAttr<T>();
2359 }
2360 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2361}
2362
Rafael Espindola599f1b72012-05-13 03:25:18 +00002363VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002364 VisibilityAttr::VisibilityType Vis,
2365 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002366 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2367 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002368}
2369
John McCalld4c3d662013-02-20 01:54:26 +00002370TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2371 TypeVisibilityAttr::VisibilityType Vis,
2372 unsigned AttrSpellingListIndex) {
2373 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2374 AttrSpellingListIndex);
2375}
2376
2377static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2378 bool isTypeVisibility) {
2379 // Visibility attributes don't mean anything on a typedef.
2380 if (isa<TypedefNameDecl>(D)) {
2381 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2382 << Attr.getName();
2383 return;
2384 }
2385
2386 // 'type_visibility' can only go on a type or namespace.
2387 if (isTypeVisibility &&
2388 !(isa<TagDecl>(D) ||
2389 isa<ObjCInterfaceDecl>(D) ||
2390 isa<NamespaceDecl>(D))) {
2391 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2392 << Attr.getName() << ExpectedTypeOrNamespace;
2393 return;
2394 }
2395
Chris Lattner6b6b5372008-06-26 18:38:35 +00002396 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002397 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002398 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002399
Peter Collingbourne7a730022010-11-23 20:45:58 +00002400 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002401 Arg = Arg->IgnoreParenCasts();
2402 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002403
Douglas Gregor5cee1192011-07-27 05:40:30 +00002404 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2406 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002407 return;
2408 }
Mike Stumpbf916502009-07-24 19:02:52 +00002409
Chris Lattner5f9e2722011-07-23 10:55:15 +00002410 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002411 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002412
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002413 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002414 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002415 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002416 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002417 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002418 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002419 else if (TypeStr == "protected") {
2420 // Complain about attempts to use protected visibility on targets
2421 // (like Darwin) that don't support it.
2422 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2423 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2424 type = VisibilityAttr::Default;
2425 } else {
2426 type = VisibilityAttr::Protected;
2427 }
2428 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002429 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002430 return;
2431 }
Mike Stumpbf916502009-07-24 19:02:52 +00002432
Michael Han51d8c522013-01-24 16:46:58 +00002433 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002434 clang::Attr *newAttr;
2435 if (isTypeVisibility) {
2436 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2437 (TypeVisibilityAttr::VisibilityType) type,
2438 Index);
2439 } else {
2440 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2441 }
2442 if (newAttr)
2443 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002444}
2445
Chandler Carruth1b03c872011-07-02 00:01:44 +00002446static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2447 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002448 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2449 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002451 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002452 return;
2453 }
2454
Chandler Carruth87c44602011-07-01 23:49:12 +00002455 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2456 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2458 << Attr.getName() << 1 << ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002459 } else {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002460 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2461 << Attr.getName() << 0;
John McCalld5313b02011-03-02 11:33:24 +00002462 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002463 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002464 return;
2465 }
2466
Chris Lattner5f9e2722011-07-23 10:55:15 +00002467 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002468 ObjCMethodFamilyAttr::FamilyKind family;
2469 if (param == "none")
2470 family = ObjCMethodFamilyAttr::OMF_None;
2471 else if (param == "alloc")
2472 family = ObjCMethodFamilyAttr::OMF_alloc;
2473 else if (param == "copy")
2474 family = ObjCMethodFamilyAttr::OMF_copy;
2475 else if (param == "init")
2476 family = ObjCMethodFamilyAttr::OMF_init;
2477 else if (param == "mutableCopy")
2478 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2479 else if (param == "new")
2480 family = ObjCMethodFamilyAttr::OMF_new;
2481 else {
2482 // Just warn and ignore it. This is future-proof against new
2483 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002484 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002485 return;
2486 }
2487
John McCallf85e1932011-06-15 23:02:42 +00002488 if (family == ObjCMethodFamilyAttr::OMF_init &&
2489 !method->getResultType()->isObjCObjectPointerType()) {
2490 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2491 << method->getResultType();
2492 // Ignore the attribute.
2493 return;
2494 }
2495
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002496 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002497 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002498}
2499
Chandler Carruth1b03c872011-07-02 00:01:44 +00002500static void handleObjCExceptionAttr(Sema &S, Decl *D,
2501 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002502 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002503 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002504
Chris Lattner0db29ec2009-02-14 08:09:34 +00002505 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2506 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002507 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2508 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002509 return;
2510 }
Mike Stumpbf916502009-07-24 19:02:52 +00002511
Michael Han51d8c522013-01-24 16:46:58 +00002512 D->addAttr(::new (S.Context)
2513 ObjCExceptionAttr(Attr.getRange(), S.Context,
2514 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002515}
2516
Chandler Carruth1b03c872011-07-02 00:01:44 +00002517static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002518 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002519 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002520 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002521 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002522 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002523 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2524 return;
2525 }
2526 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002527 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2528 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002529 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002530 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2531 return;
2532 }
2533 }
2534 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002535 // It is okay to include this attribute on properties, e.g.:
2536 //
2537 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2538 //
2539 // In this case it follows tradition and suppresses an error in the above
2540 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002541 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002542 }
Michael Han51d8c522013-01-24 16:46:58 +00002543 D->addAttr(::new (S.Context)
2544 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2545 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002546}
2547
Mike Stumpbf916502009-07-24 19:02:52 +00002548static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002549handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002550 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002551 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002552
2553 if (!isa<FunctionDecl>(D)) {
2554 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2555 return;
2556 }
2557
Michael Han51d8c522013-01-24 16:46:58 +00002558 D->addAttr(::new (S.Context)
2559 OverloadableAttr(Attr.getRange(), S.Context,
2560 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002561}
2562
Chandler Carruth1b03c872011-07-02 00:01:44 +00002563static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002564 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2566 << Attr.getName() << 1 << ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002567 return;
2568 }
Mike Stumpbf916502009-07-24 19:02:52 +00002569
Steve Naroff9eae5762008-09-18 16:44:58 +00002570 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002571 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2572 << Attr.getName() << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002573 return;
2574 }
Mike Stumpbf916502009-07-24 19:02:52 +00002575
Sean Huntcf807c42010-08-18 23:23:40 +00002576 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002577 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002578 type = BlocksAttr::ByRef;
2579 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002580 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002581 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002582 return;
2583 }
Mike Stumpbf916502009-07-24 19:02:52 +00002584
Michael Han51d8c522013-01-24 16:46:58 +00002585 D->addAttr(::new (S.Context)
2586 BlocksAttr(Attr.getRange(), S.Context, type,
2587 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002588}
2589
Chandler Carruth1b03c872011-07-02 00:01:44 +00002590static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002591 // check the attribute arguments.
2592 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002593 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002594 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002595 }
2596
John McCall3323fad2011-09-09 07:56:05 +00002597 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002598 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002599 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002600 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002601 if (E->isTypeDependent() || E->isValueDependent() ||
2602 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002603 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmand7329282013-07-23 17:35:26 +00002604 << Attr.getName() << 1 << ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002605 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002606 return;
2607 }
Mike Stumpbf916502009-07-24 19:02:52 +00002608
John McCall3323fad2011-09-09 07:56:05 +00002609 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002610 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2611 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002612 return;
2613 }
John McCall3323fad2011-09-09 07:56:05 +00002614
2615 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002616 }
2617
John McCall3323fad2011-09-09 07:56:05 +00002618 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002619 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002620 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002621 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002622 if (E->isTypeDependent() || E->isValueDependent() ||
2623 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002624 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmand7329282013-07-23 17:35:26 +00002625 << Attr.getName() << 2 << ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002626 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002627 return;
2628 }
2629 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002630
John McCall3323fad2011-09-09 07:56:05 +00002631 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002632 // FIXME: This error message could be improved, it would be nice
2633 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002634 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2635 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002636 return;
2637 }
2638 }
2639
Chandler Carruth87c44602011-07-01 23:49:12 +00002640 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002641 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002642 if (isa<FunctionNoProtoType>(FT)) {
2643 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2644 return;
2645 }
Mike Stumpbf916502009-07-24 19:02:52 +00002646
Chris Lattner897cd902009-03-17 23:03:47 +00002647 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002648 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002649 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002650 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002651 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002652 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002653 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002654 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002655 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002656 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2657 if (!BD->isVariadic()) {
2658 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2659 return;
2660 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002661 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002662 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002663 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002664 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002665 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002666 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002667 int m = Ty->isFunctionPointerType() ? 0 : 1;
2668 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002669 return;
2670 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002671 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002673 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002674 return;
2675 }
Anders Carlsson77091822008-10-05 18:05:59 +00002676 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002678 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002679 return;
2680 }
Michael Han51d8c522013-01-24 16:46:58 +00002681 D->addAttr(::new (S.Context)
2682 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2683 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002684}
2685
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002686static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2687 // Check the attribute arguments.
2688 if (!checkAttributeNumArgs(S, Attr, 0))
2689 return;
2690
2691 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2692 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2693 else
2694 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2695}
2696
Chandler Carruth1b03c872011-07-02 00:01:44 +00002697static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002698 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002699 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002700 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002701
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002702 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002704 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002705 return;
2706 }
Mike Stumpbf916502009-07-24 19:02:52 +00002707
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002708 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2709 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2710 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002711 return;
2712 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002713 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2714 if (MD->getResultType()->isVoidType()) {
2715 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2716 << Attr.getName() << 1;
2717 return;
2718 }
2719
Michael Han51d8c522013-01-24 16:46:58 +00002720 D->addAttr(::new (S.Context)
2721 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2722 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002723}
2724
Chandler Carruth1b03c872011-07-02 00:01:44 +00002725static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002726 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002727 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2729 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002730 return;
2731 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002732
Chandler Carruth87c44602011-07-01 23:49:12 +00002733 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002734 if (isa<CXXRecordDecl>(D)) {
2735 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2736 return;
2737 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2739 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002740 return;
2741 }
2742
Chandler Carruth87c44602011-07-01 23:49:12 +00002743 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002744
Michael Han51d8c522013-01-24 16:46:58 +00002745 nd->addAttr(::new (S.Context)
2746 WeakAttr(Attr.getRange(), S.Context,
2747 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002748}
2749
Chandler Carruth1b03c872011-07-02 00:01:44 +00002750static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002751 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002752 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002753 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002754
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002755
2756 // weak_import only applies to variable & function declarations.
2757 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002758 if (!D->canBeWeakImported(isDef)) {
2759 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002760 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2761 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002762 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002763 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002764 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002765 // Nothing to warn about here.
2766 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002768 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002769
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002770 return;
2771 }
2772
Michael Han51d8c522013-01-24 16:46:58 +00002773 D->addAttr(::new (S.Context)
2774 WeakImportAttr(Attr.getRange(), S.Context,
2775 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002776}
2777
Tanya Lattner0df579e2012-07-09 22:06:01 +00002778// Handles reqd_work_group_size and work_group_size_hint.
2779static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002780 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002781 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2782 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2783
Nate Begeman6f3d8382009-06-26 06:32:41 +00002784 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002785 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002786
2787 unsigned WGSize[3];
2788 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002789 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002790 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002791 if (E->isTypeDependent() || E->isValueDependent() ||
2792 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002794 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002795 return;
2796 }
2797 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2798 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002799
2800 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2801 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2802 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2803 if (!(A->getXDim() == WGSize[0] &&
2804 A->getYDim() == WGSize[1] &&
2805 A->getZDim() == WGSize[2])) {
2806 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2807 Attr.getName();
2808 }
2809 }
2810
2811 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2812 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2813 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2814 if (!(A->getXDim() == WGSize[0] &&
2815 A->getYDim() == WGSize[1] &&
2816 A->getZDim() == WGSize[2])) {
2817 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2818 Attr.getName();
2819 }
2820 }
2821
2822 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2823 D->addAttr(::new (S.Context)
2824 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002825 WGSize[0], WGSize[1], WGSize[2],
2826 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002827 else
2828 D->addAttr(::new (S.Context)
2829 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002830 WGSize[0], WGSize[1], WGSize[2],
2831 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002832}
2833
Joey Gouly37453b92013-03-08 09:42:32 +00002834static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2835 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2836
2837 // Attribute has 1 argument.
2838 if (!checkAttributeNumArgs(S, Attr, 1))
2839 return;
2840
2841 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2842
2843 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2844 (ParmType->isBooleanType() ||
2845 !ParmType->isIntegralType(S.getASTContext()))) {
2846 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2847 << ParmType;
2848 return;
2849 }
2850
2851 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2852 D->hasAttr<VecTypeHintAttr>()) {
2853 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2854 if (A->getTypeHint() != ParmType) {
2855 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2856 return;
2857 }
2858 }
2859
2860 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2861 ParmType, Attr.getLoc()));
2862}
2863
Joey Gouly96cead52013-03-14 09:54:43 +00002864static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2865 if (!dyn_cast<VarDecl>(D))
2866 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2867 << 9;
2868 StringRef EndianType = Attr.getParameterName()->getName();
2869 if (EndianType != "host" && EndianType != "device")
2870 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2871}
2872
Rafael Espindola599f1b72012-05-13 03:25:18 +00002873SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002874 StringRef Name,
2875 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002876 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2877 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002878 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002879 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2880 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002881 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002882 }
Michael Han51d8c522013-01-24 16:46:58 +00002883 return ::new (Context) SectionAttr(Range, Context, Name,
2884 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002885}
2886
Chandler Carruth1b03c872011-07-02 00:01:44 +00002887static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002888 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002889 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002890 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002891
2892 // Make sure that there is a string literal as the sections's single
2893 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002894 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002895 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002896 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002897 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002898 return;
2899 }
Mike Stump1eb44332009-09-09 15:08:12 +00002900
Chris Lattner797c3c42009-08-10 19:03:04 +00002901 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002902 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002903 if (!Error.empty()) {
2904 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2905 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002906 return;
2907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002909 // This attribute cannot be applied to local variables.
2910 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2911 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2912 return;
2913 }
Michael Han51d8c522013-01-24 16:46:58 +00002914
2915 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002916 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002917 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002918 if (NewAttr)
2919 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002920}
2921
Chris Lattner6b6b5372008-06-26 18:38:35 +00002922
Chandler Carruth1b03c872011-07-02 00:01:44 +00002923static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002924 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002925 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2927 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002928 return;
2929 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002930
Chandler Carruth87c44602011-07-01 23:49:12 +00002931 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002932 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002933 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002934 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002935 D->addAttr(::new (S.Context)
2936 NoThrowAttr(Attr.getRange(), S.Context,
2937 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002938 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002939}
2940
Chandler Carruth1b03c872011-07-02 00:01:44 +00002941static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002942 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002943 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002944 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2945 << Attr.getName() << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002946 return;
2947 }
Mike Stumpbf916502009-07-24 19:02:52 +00002948
Chandler Carruth87c44602011-07-01 23:49:12 +00002949 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002950 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002951 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002952 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002953 D->addAttr(::new (S.Context)
2954 ConstAttr(Attr.getRange(), S.Context,
2955 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002956 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002957}
2958
Chandler Carruth1b03c872011-07-02 00:01:44 +00002959static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002960 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002961 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002962 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002963
Michael Han51d8c522013-01-24 16:46:58 +00002964 D->addAttr(::new (S.Context)
2965 PureAttr(Attr.getRange(), S.Context,
2966 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002967}
2968
Chandler Carruth1b03c872011-07-02 00:01:44 +00002969static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002970 if (!Attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002971 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2972 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002973 return;
2974 }
Mike Stumpbf916502009-07-24 19:02:52 +00002975
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002976 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002977 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2978 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002979 return;
2980 }
Mike Stumpbf916502009-07-24 19:02:52 +00002981
Chandler Carruth87c44602011-07-01 23:49:12 +00002982 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002983
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002984 if (!VD || !VD->hasLocalStorage()) {
2985 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2986 return;
2987 }
Mike Stumpbf916502009-07-24 19:02:52 +00002988
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002989 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002990 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002991 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002992 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2993 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002994 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002995 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002996 Attr.getParameterName();
2997 return;
2998 }
Mike Stumpbf916502009-07-24 19:02:52 +00002999
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003000 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
3001 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00003002 S.Diag(Attr.getParameterLoc(),
3003 diag::err_attribute_cleanup_arg_not_function)
3004 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003005 return;
3006 }
3007
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003008 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00003009 S.Diag(Attr.getParameterLoc(),
3010 diag::err_attribute_cleanup_func_must_take_one_arg)
3011 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003012 return;
3013 }
Mike Stumpbf916502009-07-24 19:02:52 +00003014
Anders Carlsson89941c12009-02-07 23:16:50 +00003015 // We're currently more strict than GCC about what function types we accept.
3016 // If this ever proves to be a problem it should be easy to fix.
3017 QualType Ty = S.Context.getPointerType(VD->getType());
3018 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00003019 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3020 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00003021 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00003022 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3023 Attr.getParameterName() << ParamTy << Ty;
3024 return;
3025 }
Mike Stumpbf916502009-07-24 19:02:52 +00003026
Michael Han51d8c522013-01-24 16:46:58 +00003027 D->addAttr(::new (S.Context)
3028 CleanupAttr(Attr.getRange(), S.Context, FD,
3029 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00003030 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00003031 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003032}
3033
Mike Stumpbf916502009-07-24 19:02:52 +00003034/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003035/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003036static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003037 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003038 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003039
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003042 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003043 return;
3044 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003045
3046 // In C++ the implicit 'this' function parameter also counts, and they are
3047 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003048 bool HasImplicitThisParam = isInstanceMethod(D);
3049 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003050 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003051
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003052 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003053 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003054 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003055 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3056 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003057 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3058 << Attr.getName() << 2 << ArgumentIntegerConstant
3059 << IdxExpr->getSourceRange();
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003060 return;
3061 }
Mike Stumpbf916502009-07-24 19:02:52 +00003062
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003063 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3065 << "format" << 2 << IdxExpr->getSourceRange();
3066 return;
3067 }
Mike Stumpbf916502009-07-24 19:02:52 +00003068
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003069 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003070
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003071 if (HasImplicitThisParam) {
3072 if (ArgIdx == 0) {
3073 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3074 << "format_arg" << IdxExpr->getSourceRange();
3075 return;
3076 }
3077 ArgIdx--;
3078 }
3079
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003080 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003081 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003082
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003083 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3084 if (not_nsstring_type &&
3085 !isCFStringType(Ty, S.Context) &&
3086 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003087 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003088 // FIXME: Should highlight the actual expression that has the wrong type.
3089 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003090 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003091 << IdxExpr->getSourceRange();
3092 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003093 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003094 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003095 if (!isNSStringType(Ty, S.Context) &&
3096 !isCFStringType(Ty, S.Context) &&
3097 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003098 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003099 // FIXME: Should highlight the actual expression that has the wrong type.
3100 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003101 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003102 << IdxExpr->getSourceRange();
3103 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003104 }
3105
Michael Han51d8c522013-01-24 16:46:58 +00003106 D->addAttr(::new (S.Context)
3107 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3108 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003109}
3110
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003111enum FormatAttrKind {
3112 CFStringFormat,
3113 NSStringFormat,
3114 StrftimeFormat,
3115 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003116 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003117 InvalidFormat
3118};
3119
3120/// getFormatAttrKind - Map from format attribute names to supported format
3121/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003122static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003123 return llvm::StringSwitch<FormatAttrKind>(Format)
3124 // Check for formats that get handled specially.
3125 .Case("NSString", NSStringFormat)
3126 .Case("CFString", CFStringFormat)
3127 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003128
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003129 // Otherwise, check for supported formats.
3130 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3131 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3132 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003133
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003134 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3135 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003136}
3137
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003138/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003139/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003140static void handleInitPriorityAttr(Sema &S, Decl *D,
3141 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003142 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003143 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3144 return;
3145 }
3146
Chandler Carruth87c44602011-07-01 23:49:12 +00003147 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003148 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3149 Attr.setInvalid();
3150 return;
3151 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003152 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003153 if (S.Context.getAsArrayType(T))
3154 T = S.Context.getBaseElementType(T);
3155 if (!T->getAs<RecordType>()) {
3156 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3157 Attr.setInvalid();
3158 return;
3159 }
3160
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003161 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003162 Attr.setInvalid();
3163 return;
3164 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003165 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003166
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003167 llvm::APSInt priority(32);
3168 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3169 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3170 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3171 << "init_priority" << priorityExpr->getSourceRange();
3172 Attr.setInvalid();
3173 return;
3174 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003175 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003176 if (prioritynum < 101 || prioritynum > 65535) {
3177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3178 << priorityExpr->getSourceRange();
3179 Attr.setInvalid();
3180 return;
3181 }
Michael Han51d8c522013-01-24 16:46:58 +00003182 D->addAttr(::new (S.Context)
3183 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3184 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003185}
3186
Rafael Espindola599f1b72012-05-13 03:25:18 +00003187FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003188 int FormatIdx, int FirstArg,
3189 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003190 // Check whether we already have an equivalent format attribute.
3191 for (specific_attr_iterator<FormatAttr>
3192 i = D->specific_attr_begin<FormatAttr>(),
3193 e = D->specific_attr_end<FormatAttr>();
3194 i != e ; ++i) {
3195 FormatAttr *f = *i;
3196 if (f->getType() == Format &&
3197 f->getFormatIdx() == FormatIdx &&
3198 f->getFirstArg() == FirstArg) {
3199 // If we don't have a valid location for this attribute, adopt the
3200 // location.
3201 if (f->getLocation().isInvalid())
3202 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003203 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003204 }
3205 }
3206
Michael Han51d8c522013-01-24 16:46:58 +00003207 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3208 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003209}
3210
Mike Stumpbf916502009-07-24 19:02:52 +00003211/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003212/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003213static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003214
Chris Lattner545dd342008-06-28 23:36:30 +00003215 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003216 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3217 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003218 return;
3219 }
3220
Chris Lattner545dd342008-06-28 23:36:30 +00003221 if (Attr.getNumArgs() != 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3223 << Attr.getName() << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003224 return;
3225 }
3226
Chandler Carruth87c44602011-07-01 23:49:12 +00003227 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003228 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003229 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003230 return;
3231 }
3232
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003233 // In C++ the implicit 'this' function parameter also counts, and they are
3234 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003235 bool HasImplicitThisParam = isInstanceMethod(D);
3236 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003237 unsigned FirstIdx = 1;
3238
Chris Lattner5f9e2722011-07-23 10:55:15 +00003239 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003240
3241 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003242 if (Format.startswith("__") && Format.endswith("__"))
3243 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003244
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003245 // Check for supported formats.
3246 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003247
3248 if (Kind == IgnoredFormat)
3249 return;
3250
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003251 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003252 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003253 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003254 return;
3255 }
3256
3257 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003258 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003259 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003260 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3261 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003262 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3263 << Attr.getName() << 2 << ArgumentIntegerConstant
3264 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003265 return;
3266 }
3267
3268 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003270 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003271 return;
3272 }
3273
3274 // FIXME: Do we need to bounds check?
3275 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003276
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003277 if (HasImplicitThisParam) {
3278 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003279 S.Diag(Attr.getLoc(),
3280 diag::err_format_attribute_implicit_this_format_string)
3281 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003282 return;
3283 }
3284 ArgIdx--;
3285 }
Mike Stump1eb44332009-09-09 15:08:12 +00003286
Chris Lattner6b6b5372008-06-26 18:38:35 +00003287 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003288 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003289
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003290 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003291 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003292 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3293 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003294 return;
3295 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003296 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003297 // FIXME: do we need to check if the type is NSString*? What are the
3298 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003299 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003300 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003301 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3302 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003303 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003304 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003305 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003306 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003307 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003308 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3309 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003310 return;
3311 }
3312
3313 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003314 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003315 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003316 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3317 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003318 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3319 << Attr.getName() << 3 << ArgumentIntegerConstant
3320 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003321 return;
3322 }
3323
3324 // check if the function is variadic if the 3rd argument non-zero
3325 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003326 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003327 ++NumArgs; // +1 for ...
3328 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003329 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003330 return;
3331 }
3332 }
3333
Chris Lattner3c73c412008-11-19 08:23:25 +00003334 // strftime requires FirstArg to be 0 because it doesn't read from any
3335 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003336 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003337 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003338 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3339 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003340 return;
3341 }
3342 // if 0 it disables parameter checking (to use with e.g. va_list)
3343 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003345 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003346 return;
3347 }
3348
Rafael Espindola599f1b72012-05-13 03:25:18 +00003349 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3350 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003351 FirstArg.getZExtValue(),
3352 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003353 if (NewAttr)
3354 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003355}
3356
Chandler Carruth1b03c872011-07-02 00:01:44 +00003357static void handleTransparentUnionAttr(Sema &S, Decl *D,
3358 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003359 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003360 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003361 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003362
Chris Lattner6b6b5372008-06-26 18:38:35 +00003363
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003364 // Try to find the underlying union declaration.
3365 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003366 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003367 if (TD && TD->getUnderlyingType()->isUnionType())
3368 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3369 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003370 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003371
3372 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003373 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003374 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003375 return;
3376 }
3377
John McCall5e1cdac2011-10-07 06:10:15 +00003378 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003379 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003380 diag::warn_transparent_union_attribute_not_definition);
3381 return;
3382 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003383
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003384 RecordDecl::field_iterator Field = RD->field_begin(),
3385 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003386 if (Field == FieldEnd) {
3387 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3388 return;
3389 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003390
David Blaikie581deb32012-06-06 20:45:41 +00003391 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003392 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003393 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003394 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003395 diag::warn_transparent_union_attribute_floating)
3396 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003397 return;
3398 }
3399
3400 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3401 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3402 for (; Field != FieldEnd; ++Field) {
3403 QualType FieldType = Field->getType();
3404 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3405 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3406 // Warn if we drop the attribute.
3407 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003408 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003409 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003410 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003411 diag::warn_transparent_union_attribute_field_size_align)
3412 << isSize << Field->getDeclName() << FieldBits;
3413 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003414 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003415 diag::note_transparent_union_first_field_size_align)
3416 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003417 return;
3418 }
3419 }
3420
Michael Han51d8c522013-01-24 16:46:58 +00003421 RD->addAttr(::new (S.Context)
3422 TransparentUnionAttr(Attr.getRange(), S.Context,
3423 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003424}
3425
Chandler Carruth1b03c872011-07-02 00:01:44 +00003426static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003427 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003428 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003429 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003430
Peter Collingbourne7a730022010-11-23 20:45:58 +00003431 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003432 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003433
Chris Lattner6b6b5372008-06-26 18:38:35 +00003434 // Make sure that there is a string literal as the annotation's single
3435 // argument.
3436 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003437 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003438 return;
3439 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003440
3441 // Don't duplicate annotations that are already set.
3442 for (specific_attr_iterator<AnnotateAttr>
3443 i = D->specific_attr_begin<AnnotateAttr>(),
3444 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3445 if ((*i)->getAnnotation() == SE->getString())
3446 return;
3447 }
Michael Han51d8c522013-01-24 16:46:58 +00003448
3449 D->addAttr(::new (S.Context)
3450 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3451 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003452}
3453
Chandler Carruth1b03c872011-07-02 00:01:44 +00003454static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003455 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003456 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3458 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003459 return;
3460 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003461
Richard Smithbe507b62013-02-01 08:12:08 +00003462 if (Attr.getNumArgs() == 0) {
3463 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3464 true, 0, Attr.getAttributeSpellingListIndex()));
3465 return;
3466 }
3467
Richard Smithf6565a92013-02-22 08:32:16 +00003468 Expr *E = Attr.getArg(0);
3469 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3470 S.Diag(Attr.getEllipsisLoc(),
3471 diag::err_pack_expansion_without_parameter_packs);
3472 return;
3473 }
3474
3475 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3476 return;
3477
3478 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3479 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003480}
3481
3482void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003483 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003484 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3485 SourceLocation AttrLoc = AttrRange.getBegin();
3486
Richard Smith4cd81c52013-01-29 09:02:09 +00003487 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003488 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003489 // C++11 [dcl.align]p1:
3490 // An alignment-specifier may be applied to a variable or to a class
3491 // data member, but it shall not be applied to a bit-field, a function
3492 // parameter, the formal parameter of a catch clause, or a variable
3493 // declared with the register storage class specifier. An
3494 // alignment-specifier may also be applied to the declaration of a class
3495 // or enumeration type.
3496 // C11 6.7.5/2:
3497 // An alignment attribute shall not be specified in a declaration of
3498 // a typedef, or a bit-field, or a function, or a parameter, or an
3499 // object declared with the register storage-class specifier.
3500 int DiagKind = -1;
3501 if (isa<ParmVarDecl>(D)) {
3502 DiagKind = 0;
3503 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3504 if (VD->getStorageClass() == SC_Register)
3505 DiagKind = 1;
3506 if (VD->isExceptionVariable())
3507 DiagKind = 2;
3508 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3509 if (FD->isBitField())
3510 DiagKind = 3;
3511 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003512 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3513 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003514 << (TmpAttr.isC11() ? ExpectedVariableOrField
3515 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003516 return;
3517 }
3518 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003519 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003520 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003521 return;
3522 }
3523 }
3524
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003525 if (E->isTypeDependent() || E->isValueDependent()) {
3526 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003527 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3528 AA->setPackExpansion(IsPackExpansion);
3529 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003530 return;
3531 }
Michael Hana31f65b2013-02-01 01:19:17 +00003532
Sean Huntcf807c42010-08-18 23:23:40 +00003533 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003534 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003535 ExprResult ICE
3536 = VerifyIntegerConstantExpression(E, &Alignment,
3537 diag::err_aligned_attribute_argument_not_int,
3538 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003539 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003540 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003541
3542 // C++11 [dcl.align]p2:
3543 // -- if the constant expression evaluates to zero, the alignment
3544 // specifier shall have no effect
3545 // C11 6.7.5p6:
3546 // An alignment specification of zero has no effect.
3547 if (!(TmpAttr.isAlignas() && !Alignment) &&
3548 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003549 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3550 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003551 return;
3552 }
Michael Hana31f65b2013-02-01 01:19:17 +00003553
Richard Smithbe507b62013-02-01 08:12:08 +00003554 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003555 // We've already verified it's a power of 2, now let's make sure it's
3556 // 8192 or less.
3557 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003558 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003559 << E->getSourceRange();
3560 return;
3561 }
3562 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003563
Richard Smithf6565a92013-02-22 08:32:16 +00003564 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3565 ICE.take(), SpellingListIndex);
3566 AA->setPackExpansion(IsPackExpansion);
3567 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003568}
3569
Michael Hana31f65b2013-02-01 01:19:17 +00003570void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003571 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003572 // FIXME: Cache the number on the Attr object if non-dependent?
3573 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003574 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3575 SpellingListIndex);
3576 AA->setPackExpansion(IsPackExpansion);
3577 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003578}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003579
Richard Smithbe507b62013-02-01 08:12:08 +00003580void Sema::CheckAlignasUnderalignment(Decl *D) {
3581 assert(D->hasAttrs() && "no attributes on decl");
3582
3583 QualType Ty;
3584 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3585 Ty = VD->getType();
3586 else
3587 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003588 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003589 return;
3590
3591 // C++11 [dcl.align]p5, C11 6.7.5/4:
3592 // The combined effect of all alignment attributes in a declaration shall
3593 // not specify an alignment that is less strict than the alignment that
3594 // would otherwise be required for the entity being declared.
3595 AlignedAttr *AlignasAttr = 0;
3596 unsigned Align = 0;
3597 for (specific_attr_iterator<AlignedAttr>
3598 I = D->specific_attr_begin<AlignedAttr>(),
3599 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3600 if (I->isAlignmentDependent())
3601 return;
3602 if (I->isAlignas())
3603 AlignasAttr = *I;
3604 Align = std::max(Align, I->getAlignment(Context));
3605 }
3606
3607 if (AlignasAttr && Align) {
3608 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3609 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3610 if (NaturalAlign > RequestedAlign)
3611 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3612 << Ty << (unsigned)NaturalAlign.getQuantity();
3613 }
3614}
3615
Chandler Carruthd309c812011-07-01 23:49:16 +00003616/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003617/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003618///
Mike Stumpbf916502009-07-24 19:02:52 +00003619/// Despite what would be logical, the mode attribute is a decl attribute, not a
3620/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3621/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003622static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003623 // This attribute isn't documented, but glibc uses it. It changes
3624 // the width of an int or unsigned int to the specified size.
3625
3626 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003627 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003628 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003629
Chris Lattnerfbf13472008-06-27 22:18:37 +00003630
3631 IdentifierInfo *Name = Attr.getParameterName();
3632 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003633 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003634 return;
3635 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003636
Chris Lattner5f9e2722011-07-23 10:55:15 +00003637 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003638
3639 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003640 if (Str.startswith("__") && Str.endswith("__"))
3641 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003642
3643 unsigned DestWidth = 0;
3644 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003645 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003646 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003647 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003648 switch (Str[0]) {
3649 case 'Q': DestWidth = 8; break;
3650 case 'H': DestWidth = 16; break;
3651 case 'S': DestWidth = 32; break;
3652 case 'D': DestWidth = 64; break;
3653 case 'X': DestWidth = 96; break;
3654 case 'T': DestWidth = 128; break;
3655 }
3656 if (Str[1] == 'F') {
3657 IntegerMode = false;
3658 } else if (Str[1] == 'C') {
3659 IntegerMode = false;
3660 ComplexMode = true;
3661 } else if (Str[1] != 'I') {
3662 DestWidth = 0;
3663 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003664 break;
3665 case 4:
3666 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3667 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003668 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003669 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003670 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003671 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003672 break;
3673 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003674 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003675 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003676 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003677 case 11:
3678 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003679 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003680 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003681 }
3682
3683 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003684 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003685 OldTy = TD->getUnderlyingType();
3686 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3687 OldTy = VD->getType();
3688 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003689 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003690 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003691 return;
3692 }
Eli Friedman73397492009-03-03 06:41:03 +00003693
John McCall183700f2009-09-21 23:43:11 +00003694 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003695 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3696 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003697 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003698 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3699 } else if (ComplexMode) {
3700 if (!OldTy->isComplexType())
3701 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3702 } else {
3703 if (!OldTy->isFloatingType())
3704 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3705 }
3706
Mike Stump390b4cc2009-05-16 07:39:55 +00003707 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3708 // and friends, at least with glibc.
3709 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3710 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003711 // FIXME: Make sure floating-point mappings are accurate
3712 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003713 QualType NewTy;
3714 switch (DestWidth) {
3715 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003716 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003717 return;
3718 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003719 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003720 return;
3721 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003722 if (!IntegerMode) {
3723 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3724 return;
3725 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003726 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003727 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003728 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003729 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003730 break;
3731 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003732 if (!IntegerMode) {
3733 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3734 return;
3735 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003736 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003737 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003738 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003739 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003740 break;
3741 case 32:
3742 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003743 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003744 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003745 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003746 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003747 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003748 break;
3749 case 64:
3750 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003751 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003752 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003753 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003754 NewTy = S.Context.LongTy;
3755 else
3756 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003757 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003758 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003759 NewTy = S.Context.UnsignedLongTy;
3760 else
3761 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003762 break;
Eli Friedman73397492009-03-03 06:41:03 +00003763 case 96:
3764 NewTy = S.Context.LongDoubleTy;
3765 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003766 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003767 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3768 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003769 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3770 return;
3771 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003772 if (IntegerMode) {
3773 if (OldTy->isSignedIntegerType())
3774 NewTy = S.Context.Int128Ty;
3775 else
3776 NewTy = S.Context.UnsignedInt128Ty;
3777 } else
3778 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003779 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003780 }
3781
Eli Friedman73397492009-03-03 06:41:03 +00003782 if (ComplexMode) {
3783 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003784 }
3785
3786 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003787 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3788 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3789 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003790 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003791
3792 D->addAttr(::new (S.Context)
3793 ModeAttr(Attr.getRange(), S.Context, Name,
3794 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003795}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003796
Chandler Carruth1b03c872011-07-02 00:01:44 +00003797static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003798 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003799 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003800 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003801
Nick Lewycky78d1a102012-07-24 01:40:49 +00003802 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3803 if (!VD->hasGlobalStorage())
3804 S.Diag(Attr.getLoc(),
3805 diag::warn_attribute_requires_functions_or_static_globals)
3806 << Attr.getName();
3807 } else if (!isFunctionOrMethod(D)) {
3808 S.Diag(Attr.getLoc(),
3809 diag::warn_attribute_requires_functions_or_static_globals)
3810 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003811 return;
3812 }
Mike Stumpbf916502009-07-24 19:02:52 +00003813
Michael Han51d8c522013-01-24 16:46:58 +00003814 D->addAttr(::new (S.Context)
3815 NoDebugAttr(Attr.getRange(), S.Context,
3816 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003817}
3818
Chandler Carruth1b03c872011-07-02 00:01:44 +00003819static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003820 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003821 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003822 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003823
Mike Stumpbf916502009-07-24 19:02:52 +00003824
Chandler Carruth87c44602011-07-01 23:49:12 +00003825 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003827 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003828 return;
3829 }
Mike Stumpbf916502009-07-24 19:02:52 +00003830
Michael Han51d8c522013-01-24 16:46:58 +00003831 D->addAttr(::new (S.Context)
3832 NoInlineAttr(Attr.getRange(), S.Context,
3833 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003834}
3835
Chandler Carruth1b03c872011-07-02 00:01:44 +00003836static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3837 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003838 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003839 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003840 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003841
Chris Lattner7255a2d2010-06-22 00:03:40 +00003842
Chandler Carruth87c44602011-07-01 23:49:12 +00003843 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003844 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003845 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003846 return;
3847 }
3848
Michael Han51d8c522013-01-24 16:46:58 +00003849 D->addAttr(::new (S.Context)
3850 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3851 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003852}
3853
Chandler Carruth1b03c872011-07-02 00:01:44 +00003854static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003855 if (S.LangOpts.CUDA) {
3856 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003857 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003858 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3859 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003860 return;
3861 }
3862
Chandler Carruth87c44602011-07-01 23:49:12 +00003863 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003864 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003865 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003866 return;
3867 }
3868
Michael Han51d8c522013-01-24 16:46:58 +00003869 D->addAttr(::new (S.Context)
3870 CUDAConstantAttr(Attr.getRange(), S.Context,
3871 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003872 } else {
3873 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3874 }
3875}
3876
Chandler Carruth1b03c872011-07-02 00:01:44 +00003877static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 if (S.LangOpts.CUDA) {
3879 // check the attribute arguments.
3880 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003881 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3882 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003883 return;
3884 }
3885
Chandler Carruth87c44602011-07-01 23:49:12 +00003886 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003887 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003888 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003889 return;
3890 }
3891
Michael Han51d8c522013-01-24 16:46:58 +00003892 D->addAttr(::new (S.Context)
3893 CUDADeviceAttr(Attr.getRange(), S.Context,
3894 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003895 } else {
3896 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3897 }
3898}
3899
Chandler Carruth1b03c872011-07-02 00:01:44 +00003900static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003901 if (S.LangOpts.CUDA) {
3902 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003903 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003904 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003905
Chandler Carruth87c44602011-07-01 23:49:12 +00003906 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003908 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003909 return;
3910 }
3911
Chandler Carruth87c44602011-07-01 23:49:12 +00003912 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003913 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003914 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003915 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003916 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3917 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003918 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003919 "void");
3920 } else {
3921 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3922 << FD->getType();
3923 }
3924 return;
3925 }
3926
Michael Han51d8c522013-01-24 16:46:58 +00003927 D->addAttr(::new (S.Context)
3928 CUDAGlobalAttr(Attr.getRange(), S.Context,
3929 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003930 } else {
3931 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3932 }
3933}
3934
Chandler Carruth1b03c872011-07-02 00:01:44 +00003935static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003936 if (S.LangOpts.CUDA) {
3937 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003938 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003939 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003940
Peter Collingbourneced76712010-12-01 03:15:31 +00003941
Chandler Carruth87c44602011-07-01 23:49:12 +00003942 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003944 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003945 return;
3946 }
3947
Michael Han51d8c522013-01-24 16:46:58 +00003948 D->addAttr(::new (S.Context)
3949 CUDAHostAttr(Attr.getRange(), S.Context,
3950 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003951 } else {
3952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3953 }
3954}
3955
Chandler Carruth1b03c872011-07-02 00:01:44 +00003956static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003957 if (S.LangOpts.CUDA) {
3958 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003959 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003960 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003961
Chandler Carruth87c44602011-07-01 23:49:12 +00003962 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003963 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003964 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003965 return;
3966 }
3967
Michael Han51d8c522013-01-24 16:46:58 +00003968 D->addAttr(::new (S.Context)
3969 CUDASharedAttr(Attr.getRange(), S.Context,
3970 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003971 } else {
3972 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3973 }
3974}
3975
Chandler Carruth1b03c872011-07-02 00:01:44 +00003976static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003977 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003978 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003979 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003980
Chandler Carruth87c44602011-07-01 23:49:12 +00003981 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003982 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003984 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003985 return;
3986 }
Mike Stumpbf916502009-07-24 19:02:52 +00003987
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003988 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003989 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003990 return;
3991 }
Mike Stumpbf916502009-07-24 19:02:52 +00003992
Michael Han51d8c522013-01-24 16:46:58 +00003993 D->addAttr(::new (S.Context)
3994 GNUInlineAttr(Attr.getRange(), S.Context,
3995 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003996}
3997
Chandler Carruth1b03c872011-07-02 00:01:44 +00003998static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003999 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004000
Aaron Ballmanfff32482012-12-09 17:45:41 +00004001 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00004002 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00004003 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4004 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00004005 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00004006 return;
4007
Chandler Carruth87c44602011-07-01 23:49:12 +00004008 if (!isa<ObjCMethodDecl>(D)) {
4009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4010 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00004011 return;
4012 }
4013
Chandler Carruth87c44602011-07-01 23:49:12 +00004014 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004015 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00004016 D->addAttr(::new (S.Context)
4017 FastCallAttr(Attr.getRange(), S.Context,
4018 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004019 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004020 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00004021 D->addAttr(::new (S.Context)
4022 StdCallAttr(Attr.getRange(), S.Context,
4023 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004024 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004025 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00004026 D->addAttr(::new (S.Context)
4027 ThisCallAttr(Attr.getRange(), S.Context,
4028 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00004029 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004030 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00004031 D->addAttr(::new (S.Context)
4032 CDeclAttr(Attr.getRange(), S.Context,
4033 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004034 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004035 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004036 D->addAttr(::new (S.Context)
4037 PascalAttr(Attr.getRange(), S.Context,
4038 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004039 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004040 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004041 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004042 switch (CC) {
4043 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004044 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004045 break;
4046 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004047 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004048 break;
4049 default:
4050 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004051 }
4052
Michael Han51d8c522013-01-24 16:46:58 +00004053 D->addAttr(::new (S.Context)
4054 PcsAttr(Attr.getRange(), S.Context, PCS,
4055 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004056 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004057 }
Derek Schuff263366f2012-10-16 22:30:41 +00004058 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004059 D->addAttr(::new (S.Context)
4060 PnaclCallAttr(Attr.getRange(), S.Context,
4061 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004062 return;
Guy Benyei38980082012-12-25 08:53:55 +00004063 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004064 D->addAttr(::new (S.Context)
4065 IntelOclBiccAttr(Attr.getRange(), S.Context,
4066 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004067 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004068
Abramo Bagnarae215f722010-04-30 13:10:51 +00004069 default:
4070 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004071 }
4072}
4073
Chandler Carruth1b03c872011-07-02 00:01:44 +00004074static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00004075 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004076 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004077}
4078
Guy Benyei1db70402013-03-24 13:58:12 +00004079static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4080 assert(!Attr.isInvalid());
4081
4082 Expr *E = Attr.getArg(0);
4083 llvm::APSInt ArgNum(32);
4084 if (E->isTypeDependent() || E->isValueDependent() ||
4085 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4087 << Attr.getName()->getName() << E->getSourceRange();
4088 return;
4089 }
4090
4091 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4092 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4093}
4094
Aaron Ballmanfff32482012-12-09 17:45:41 +00004095bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4096 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004097 if (attr.isInvalid())
4098 return true;
4099
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004100 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4101 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004102 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4103 << attr.getName() << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004104 attr.setInvalid();
4105 return true;
4106 }
4107
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004108 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4109 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004110 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004111 case AttributeList::AT_CDecl: CC = CC_C; break;
4112 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4113 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4114 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4115 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4116 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004117 Expr *Arg = attr.getArg(0);
4118 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004119 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004120 Diag(attr.getLoc(), diag::err_attribute_argument_n_type)
4121 << attr.getName() << 1 << ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004122 attr.setInvalid();
4123 return true;
4124 }
4125
Chris Lattner5f9e2722011-07-23 10:55:15 +00004126 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004127 if (StrRef == "aapcs") {
4128 CC = CC_AAPCS;
4129 break;
4130 } else if (StrRef == "aapcs-vfp") {
4131 CC = CC_AAPCS_VFP;
4132 break;
4133 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004134
4135 attr.setInvalid();
4136 Diag(attr.getLoc(), diag::err_invalid_pcs);
4137 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004138 }
Derek Schuff263366f2012-10-16 22:30:41 +00004139 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004140 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004141 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004142 }
4143
Aaron Ballman82bfa192012-10-02 14:26:08 +00004144 const TargetInfo &TI = Context.getTargetInfo();
4145 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4146 if (A == TargetInfo::CCCR_Warning) {
4147 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004148
4149 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4150 if (FD)
4151 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4152 TargetInfo::CCMT_NonMember;
4153 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004154 }
4155
John McCall711c52b2011-01-05 12:14:39 +00004156 return false;
4157}
4158
Chandler Carruth1b03c872011-07-02 00:01:44 +00004159static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004160 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004161
4162 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004163 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004164 return;
4165
Chandler Carruth87c44602011-07-01 23:49:12 +00004166 if (!isa<ObjCMethodDecl>(D)) {
4167 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4168 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004169 return;
4170 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004171
Michael Han51d8c522013-01-24 16:46:58 +00004172 D->addAttr(::new (S.Context)
4173 RegparmAttr(Attr.getRange(), S.Context, numParams,
4174 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004175}
4176
4177/// Checks a regparm attribute, returning true if it is ill-formed and
4178/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004179bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4180 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004181 return true;
4182
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004183 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004184 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004185 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004186 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004187
Chandler Carruth87c44602011-07-01 23:49:12 +00004188 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004189 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004190 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004191 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004192 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004193 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004194 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004195 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004196 }
4197
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004198 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004199 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004200 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004201 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004202 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004203 }
4204
John McCall711c52b2011-01-05 12:14:39 +00004205 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004206 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004207 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004208 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004209 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004210 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004211 }
4212
John McCall711c52b2011-01-05 12:14:39 +00004213 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004214}
4215
Chandler Carruth1b03c872011-07-02 00:01:44 +00004216static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004217 if (S.LangOpts.CUDA) {
4218 // check the attribute arguments.
4219 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004220 // FIXME: 0 is not okay.
4221 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004222 return;
4223 }
4224
Chandler Carruth87c44602011-07-01 23:49:12 +00004225 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004227 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004228 return;
4229 }
4230
4231 Expr *MaxThreadsExpr = Attr.getArg(0);
4232 llvm::APSInt MaxThreads(32);
4233 if (MaxThreadsExpr->isTypeDependent() ||
4234 MaxThreadsExpr->isValueDependent() ||
4235 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004236 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4237 << Attr.getName() << 1 << ArgumentIntegerConstant
4238 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004239 return;
4240 }
4241
4242 llvm::APSInt MinBlocks(32);
4243 if (Attr.getNumArgs() > 1) {
4244 Expr *MinBlocksExpr = Attr.getArg(1);
4245 if (MinBlocksExpr->isTypeDependent() ||
4246 MinBlocksExpr->isValueDependent() ||
4247 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004248 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4249 << Attr.getName() << 2 << ArgumentIntegerConstant
4250 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004251 return;
4252 }
4253 }
4254
Michael Han51d8c522013-01-24 16:46:58 +00004255 D->addAttr(::new (S.Context)
4256 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4257 MaxThreads.getZExtValue(),
4258 MinBlocks.getZExtValue(),
4259 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004260 } else {
4261 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4262 }
4263}
4264
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004265static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4266 const AttributeList &Attr) {
4267 StringRef AttrName = Attr.getName()->getName();
4268 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4270 << Attr.getName() << /* arg num = */ 1 << ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004271 return;
4272 }
4273
4274 if (Attr.getNumArgs() != 2) {
4275 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004276 << Attr.getName() << /* required args = */ 3;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004277 return;
4278 }
4279
4280 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4281
4282 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4284 << Attr.getName() << ExpectedFunctionOrMethod;
4285 return;
4286 }
4287
4288 uint64_t ArgumentIdx;
4289 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4290 Attr.getLoc(), 2,
4291 Attr.getArg(0), ArgumentIdx))
4292 return;
4293
4294 uint64_t TypeTagIdx;
4295 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4296 Attr.getLoc(), 3,
4297 Attr.getArg(1), TypeTagIdx))
4298 return;
4299
4300 bool IsPointer = (AttrName == "pointer_with_type_tag");
4301 if (IsPointer) {
4302 // Ensure that buffer has a pointer type.
4303 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4304 if (!BufferTy->isPointerType()) {
4305 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004306 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004307 }
4308 }
4309
Michael Han51d8c522013-01-24 16:46:58 +00004310 D->addAttr(::new (S.Context)
4311 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4312 ArgumentIdx, TypeTagIdx, IsPointer,
4313 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004314}
4315
4316static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4317 const AttributeList &Attr) {
4318 IdentifierInfo *PointerKind = Attr.getParameterName();
4319 if (!PointerKind) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4321 << Attr.getName() << 1 << ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004322 return;
4323 }
4324
4325 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4326
Michael Han51d8c522013-01-24 16:46:58 +00004327 D->addAttr(::new (S.Context)
4328 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4329 MatchingCType,
4330 Attr.getLayoutCompatible(),
4331 Attr.getMustBeNull(),
4332 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004333}
4334
Chris Lattner0744e5f2008-06-29 00:23:49 +00004335//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004336// Checker-specific attribute handlers.
4337//===----------------------------------------------------------------------===//
4338
John McCallc7ad3812011-01-25 03:31:58 +00004339static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004340 return type->isDependentType() ||
4341 type->isObjCObjectPointerType() ||
4342 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004343}
4344static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004345 return type->isDependentType() ||
4346 type->isPointerType() ||
4347 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004348}
4349
Chandler Carruth1b03c872011-07-02 00:01:44 +00004350static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004351 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004352 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004353 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004354 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004355 return;
4356 }
4357
4358 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004359 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004360 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4361 cf = false;
4362 } else {
4363 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4364 cf = true;
4365 }
4366
4367 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004368 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004369 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004370 return;
4371 }
4372
4373 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004374 param->addAttr(::new (S.Context)
4375 CFConsumedAttr(Attr.getRange(), S.Context,
4376 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004377 else
Michael Han51d8c522013-01-24 16:46:58 +00004378 param->addAttr(::new (S.Context)
4379 NSConsumedAttr(Attr.getRange(), S.Context,
4380 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004381}
4382
Chandler Carruth1b03c872011-07-02 00:01:44 +00004383static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4384 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004385 if (!isa<ObjCMethodDecl>(D)) {
4386 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004387 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004388 return;
4389 }
4390
Michael Han51d8c522013-01-24 16:46:58 +00004391 D->addAttr(::new (S.Context)
4392 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4393 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004394}
4395
Chandler Carruth1b03c872011-07-02 00:01:44 +00004396static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4397 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004398
John McCallc7ad3812011-01-25 03:31:58 +00004399 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004400
Chandler Carruth87c44602011-07-01 23:49:12 +00004401 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004402 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004403 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004404 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004405 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004406 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4407 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004408 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004409 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004410 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004411 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004412 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004413 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004414 return;
4415 }
Mike Stumpbf916502009-07-24 19:02:52 +00004416
John McCallc7ad3812011-01-25 03:31:58 +00004417 bool typeOK;
4418 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004419 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004420 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004421 case AttributeList::AT_NSReturnsAutoreleased:
4422 case AttributeList::AT_NSReturnsRetained:
4423 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004424 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4425 cf = false;
4426 break;
4427
Sean Hunt8e083e72012-06-19 23:57:03 +00004428 case AttributeList::AT_CFReturnsRetained:
4429 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004430 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4431 cf = true;
4432 break;
4433 }
4434
4435 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004436 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004437 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004438 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004439 }
Mike Stumpbf916502009-07-24 19:02:52 +00004440
Chandler Carruth87c44602011-07-01 23:49:12 +00004441 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004442 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004443 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004444 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004445 D->addAttr(::new (S.Context)
4446 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4447 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004448 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004449 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004450 D->addAttr(::new (S.Context)
4451 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4452 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004453 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004454 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004455 D->addAttr(::new (S.Context)
4456 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4457 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004458 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004459 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004460 D->addAttr(::new (S.Context)
4461 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4462 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004463 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004464 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004465 D->addAttr(::new (S.Context)
4466 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4467 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004468 return;
4469 };
4470}
4471
John McCalldc7c5ad2011-07-22 08:53:00 +00004472static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4473 const AttributeList &attr) {
4474 SourceLocation loc = attr.getLoc();
4475
4476 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4477
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004478 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004479 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004480 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004481 return;
4482 }
4483
4484 // Check that the method returns a normal pointer.
4485 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004486
4487 if (!resultType->isReferenceType() &&
4488 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004489 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4490 << SourceRange(loc)
4491 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4492
4493 // Drop the attribute.
4494 return;
4495 }
4496
Michael Han51d8c522013-01-24 16:46:58 +00004497 method->addAttr(::new (S.Context)
4498 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4499 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004500}
4501
Fariborz Jahanian84101132012-09-07 23:46:23 +00004502static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4503 const AttributeList &attr) {
4504 SourceLocation loc = attr.getLoc();
4505 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4506
4507 if (!method) {
4508 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4509 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4510 return;
4511 }
4512 DeclContext *DC = method->getDeclContext();
4513 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4514 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4515 << attr.getName() << 0;
4516 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4517 return;
4518 }
4519 if (method->getMethodFamily() == OMF_dealloc) {
4520 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4521 << attr.getName() << 1;
4522 return;
4523 }
4524
Michael Han51d8c522013-01-24 16:46:58 +00004525 method->addAttr(::new (S.Context)
4526 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4527 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004528}
4529
John McCall8dfac0b2011-09-30 05:12:12 +00004530/// Handle cf_audited_transfer and cf_unknown_transfer.
4531static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4532 if (!isa<FunctionDecl>(D)) {
4533 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004534 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004535 return;
4536 }
4537
Sean Hunt8e083e72012-06-19 23:57:03 +00004538 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004539
4540 // Check whether there's a conflicting attribute already present.
4541 Attr *Existing;
4542 if (IsAudited) {
4543 Existing = D->getAttr<CFUnknownTransferAttr>();
4544 } else {
4545 Existing = D->getAttr<CFAuditedTransferAttr>();
4546 }
4547 if (Existing) {
4548 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4549 << A.getName()
4550 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4551 << A.getRange() << Existing->getRange();
4552 return;
4553 }
4554
4555 // All clear; add the attribute.
4556 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004557 D->addAttr(::new (S.Context)
4558 CFAuditedTransferAttr(A.getRange(), S.Context,
4559 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004560 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004561 D->addAttr(::new (S.Context)
4562 CFUnknownTransferAttr(A.getRange(), S.Context,
4563 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004564 }
4565}
4566
John McCallfe98da02011-09-29 07:17:38 +00004567static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4568 const AttributeList &Attr) {
4569 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4570 if (!RD || RD->isUnion()) {
4571 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004572 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004573 }
4574
4575 IdentifierInfo *ParmName = Attr.getParameterName();
4576
4577 // In Objective-C, verify that the type names an Objective-C type.
4578 // We don't want to check this outside of ObjC because people sometimes
4579 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004580 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004581 // Check for an existing type with this name.
4582 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4583 Sema::LookupOrdinaryName);
4584 if (S.LookupName(R, Sc)) {
4585 NamedDecl *Target = R.getFoundDecl();
4586 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4587 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4588 S.Diag(Target->getLocStart(), diag::note_declared_at);
4589 }
4590 }
4591 }
4592
Michael Han51d8c522013-01-24 16:46:58 +00004593 D->addAttr(::new (S.Context)
4594 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4595 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004596}
4597
Chandler Carruth1b03c872011-07-02 00:01:44 +00004598static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4599 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004600 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004601
Chandler Carruth87c44602011-07-01 23:49:12 +00004602 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004603 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004604}
4605
Chandler Carruth1b03c872011-07-02 00:01:44 +00004606static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4607 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004608 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004609 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004610 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004611 return;
4612 }
4613
Chandler Carruth87c44602011-07-01 23:49:12 +00004614 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004615 QualType type = vd->getType();
4616
4617 if (!type->isDependentType() &&
4618 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004619 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004620 << type;
4621 return;
4622 }
4623
4624 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4625
4626 // If we have no lifetime yet, check the lifetime we're presumably
4627 // going to infer.
4628 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4629 lifetime = type->getObjCARCImplicitLifetime();
4630
4631 switch (lifetime) {
4632 case Qualifiers::OCL_None:
4633 assert(type->isDependentType() &&
4634 "didn't infer lifetime for non-dependent type?");
4635 break;
4636
4637 case Qualifiers::OCL_Weak: // meaningful
4638 case Qualifiers::OCL_Strong: // meaningful
4639 break;
4640
4641 case Qualifiers::OCL_ExplicitNone:
4642 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004643 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004644 << (lifetime == Qualifiers::OCL_Autoreleasing);
4645 break;
4646 }
4647
Chandler Carruth87c44602011-07-01 23:49:12 +00004648 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004649 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4650 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004651}
4652
Francois Pichet11542142010-12-19 06:50:37 +00004653//===----------------------------------------------------------------------===//
4654// Microsoft specific attribute handlers.
4655//===----------------------------------------------------------------------===//
4656
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004657// Check if MS extensions or some other language extensions are enabled. If
4658// not, issue a diagnostic that the given attribute is unused.
4659static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4660 bool OtherExtension = false) {
4661 if (S.LangOpts.MicrosoftExt || OtherExtension)
4662 return true;
4663 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4664 return false;
4665}
4666
Chandler Carruth1b03c872011-07-02 00:01:44 +00004667static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004668 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4669 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004670
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004671 // check the attribute arguments.
4672 if (!checkAttributeNumArgs(S, Attr, 1))
4673 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004674
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004675 Expr *Arg = Attr.getArg(0);
4676 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4677 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004678 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4679 << Attr.getName() << 1 << ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004680 return;
4681 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004682
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004683 StringRef StrRef = Str->getString();
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004684
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004685 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4686 StrRef.back() == '}';
Francois Pichetd3d3be92010-12-20 01:41:49 +00004687
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004688 // Validate GUID length.
4689 if (IsCurly && StrRef.size() != 38) {
4690 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4691 return;
4692 }
4693 if (!IsCurly && StrRef.size() != 36) {
4694 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4695 return;
4696 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004697
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004698 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4699 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4700 StringRef::iterator I = StrRef.begin();
4701 if (IsCurly) // Skip the optional '{'
4702 ++I;
4703
4704 for (int i = 0; i < 36; ++i) {
4705 if (i == 8 || i == 13 || i == 18 || i == 23) {
4706 if (*I != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004707 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4708 return;
4709 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004710 } else if (!isHexDigit(*I)) {
4711 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4712 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004713 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004714 I++;
4715 }
Francois Pichet11542142010-12-19 06:50:37 +00004716
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004717 D->addAttr(::new (S.Context)
4718 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4719 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004720}
4721
John McCallc052dbb2012-05-22 21:28:12 +00004722static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004723 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004724 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004725
4726 AttributeList::Kind Kind = Attr.getKind();
4727 if (Kind == AttributeList::AT_SingleInheritance)
4728 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004729 ::new (S.Context)
4730 SingleInheritanceAttr(Attr.getRange(), S.Context,
4731 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004732 else if (Kind == AttributeList::AT_MultipleInheritance)
4733 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004734 ::new (S.Context)
4735 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4736 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004737 else if (Kind == AttributeList::AT_VirtualInheritance)
4738 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004739 ::new (S.Context)
4740 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4741 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004742}
4743
4744static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004745 if (!checkMicrosoftExt(S, Attr))
4746 return;
4747
4748 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004749 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004750 D->addAttr(
4751 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4752 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004753}
4754
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004755static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004756 if (!checkMicrosoftExt(S, Attr))
4757 return;
4758 D->addAttr(::new (S.Context)
4759 ForceInlineAttr(Attr.getRange(), S.Context,
4760 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004761}
4762
Reid Klecknera7225342013-05-20 14:02:37 +00004763static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4764 if (!checkMicrosoftExt(S, Attr))
4765 return;
4766 // Check linkage after possibly merging declaratinos. See
4767 // checkAttributesAfterMerging().
4768 D->addAttr(::new (S.Context)
4769 SelectAnyAttr(Attr.getRange(), S.Context,
4770 Attr.getAttributeSpellingListIndex()));
4771}
4772
Ted Kremenekb71368d2009-05-09 02:44:38 +00004773//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004774// Top Level Sema Entry Points
4775//===----------------------------------------------------------------------===//
4776
Chandler Carruth1b03c872011-07-02 00:01:44 +00004777static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4778 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004779 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004780 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4781 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4782 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004783 default:
4784 break;
4785 }
4786}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004787
Chandler Carruth1b03c872011-07-02 00:01:44 +00004788static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4789 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004790 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004791 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4792 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4793 case AttributeList::AT_IBOutletCollection:
4794 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004795 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004796 case AttributeList::AT_ObjCGC:
4797 case AttributeList::AT_VectorSize:
4798 case AttributeList::AT_NeonVectorType:
4799 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004800 case AttributeList::AT_Ptr32:
4801 case AttributeList::AT_Ptr64:
4802 case AttributeList::AT_SPtr:
4803 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004804 // Ignore these, these are type attributes, handled by
4805 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004806 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_CUDADevice:
4808 case AttributeList::AT_CUDAHost:
4809 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004810 // Ignore, this is a non-inheritable attribute, handled
4811 // by ProcessNonInheritableDeclAttr.
4812 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004813 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4814 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4815 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4816 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004817 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004818 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004819 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004820 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004821 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4822 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4823 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004824 handleDependencyAttr(S, scope, D, Attr);
4825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4827 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4828 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004829 case AttributeList::AT_CXX11NoReturn:
4830 handleCXX11NoReturnAttr(S, D, Attr);
4831 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004832 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004833 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004834 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004835 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4836 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004837 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004838 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004839 case AttributeList::AT_MinSize:
4840 handleMinSizeAttr(S, D, Attr);
4841 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4843 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4844 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4845 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4846 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004847 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004848 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004849 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4850 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4851 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4852 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4853 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004854 case AttributeList::AT_ownership_returns:
4855 case AttributeList::AT_ownership_takes:
4856 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004857 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004858 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4859 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4860 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4861 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4862 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4863 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4864 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004865
Sean Hunt8e083e72012-06-19 23:57:03 +00004866 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004867 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004869 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004870
Sean Hunt8e083e72012-06-19 23:57:03 +00004871 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004872 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4873
Fariborz Jahanian84101132012-09-07 23:46:23 +00004874 case AttributeList::AT_ObjCRequiresSuper:
4875 handleObjCRequiresSuperAttr(S, D, Attr); break;
4876
Sean Hunt8e083e72012-06-19 23:57:03 +00004877 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004878 handleNSBridgedAttr(S, scope, D, Attr); break;
4879
Sean Hunt8e083e72012-06-19 23:57:03 +00004880 case AttributeList::AT_CFAuditedTransfer:
4881 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004882 handleCFTransferAttr(S, D, Attr); break;
4883
Ted Kremenekb71368d2009-05-09 02:44:38 +00004884 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004885 case AttributeList::AT_CFConsumed:
4886 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4887 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004888 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004889
Sean Hunt8e083e72012-06-19 23:57:03 +00004890 case AttributeList::AT_NSReturnsAutoreleased:
4891 case AttributeList::AT_NSReturnsNotRetained:
4892 case AttributeList::AT_CFReturnsNotRetained:
4893 case AttributeList::AT_NSReturnsRetained:
4894 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004895 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004896
Tanya Lattner0df579e2012-07-09 22:06:01 +00004897 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004898 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004899 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004900
Joey Gouly37453b92013-03-08 09:42:32 +00004901 case AttributeList::AT_VecTypeHint:
4902 handleVecTypeHint(S, D, Attr); break;
4903
Joey Gouly96cead52013-03-14 09:54:43 +00004904 case AttributeList::AT_Endian:
4905 handleEndianAttr(S, D, Attr);
4906 break;
4907
Sean Hunt8e083e72012-06-19 23:57:03 +00004908 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004909 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004910
Sean Hunt8e083e72012-06-19 23:57:03 +00004911 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4912 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4913 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004914 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004915 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004916 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004917 handleArcWeakrefUnavailableAttr (S, D, Attr);
4918 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004919 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004920 handleObjCRootClassAttr(S, D, Attr);
4921 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004922 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004923 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004924 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004925 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4926 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004927 handleReturnsTwiceAttr(S, D, Attr);
4928 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004929 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004930 case AttributeList::AT_Visibility:
4931 handleVisibilityAttr(S, D, Attr, false);
4932 break;
4933 case AttributeList::AT_TypeVisibility:
4934 handleVisibilityAttr(S, D, Attr, true);
4935 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004936 case AttributeList::AT_WarnUnused:
4937 handleWarnUnusedAttr(S, D, Attr);
4938 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004939 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004940 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004941 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4942 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4943 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4944 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004945 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004946 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004947 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004948 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004949 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004950 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004951 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004952 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004953 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4954 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4955 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4956 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4957 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4958 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4959 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4960 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4961 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004962 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004963 // Just ignore
4964 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004965 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004966 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004967 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004968 case AttributeList::AT_StdCall:
4969 case AttributeList::AT_CDecl:
4970 case AttributeList::AT_FastCall:
4971 case AttributeList::AT_ThisCall:
4972 case AttributeList::AT_Pascal:
4973 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004974 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004975 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004976 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004977 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004978 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004979 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004980 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004981 case AttributeList::AT_OpenCLImageAccess:
4982 handleOpenCLImageAccessAttr(S, D, Attr);
4983 break;
John McCallc052dbb2012-05-22 21:28:12 +00004984
4985 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004986 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004987 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004988 handleMsStructAttr(S, D, Attr);
4989 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004990 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004991 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004992 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004993 case AttributeList::AT_SingleInheritance:
4994 case AttributeList::AT_MultipleInheritance:
4995 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004996 handleInheritanceAttr(S, D, Attr);
4997 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004998 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004999 handlePortabilityAttr(S, D, Attr);
5000 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005001 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00005002 handleForceInlineAttr(S, D, Attr);
5003 break;
Reid Klecknera7225342013-05-20 14:02:37 +00005004 case AttributeList::AT_SelectAny:
5005 handleSelectAnyAttr(S, D, Attr);
5006 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005007
5008 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00005009 case AttributeList::AT_AssertExclusiveLock:
5010 handleAssertExclusiveLockAttr(S, D, Attr);
5011 break;
5012 case AttributeList::AT_AssertSharedLock:
5013 handleAssertSharedLockAttr(S, D, Attr);
5014 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005015 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005016 handleGuardedVarAttr(S, D, Attr);
5017 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005018 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00005019 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005020 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005021 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00005022 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005023 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00005024 case AttributeList::AT_NoSanitizeAddress:
5025 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00005026 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005027 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00005028 handleNoThreadSafetyAnalysis(S, D, Attr);
5029 break;
5030 case AttributeList::AT_NoSanitizeThread:
5031 handleNoSanitizeThread(S, D, Attr);
5032 break;
5033 case AttributeList::AT_NoSanitizeMemory:
5034 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005035 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005036 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005037 handleLockableAttr(S, D, Attr);
5038 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005039 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005040 handleGuardedByAttr(S, D, Attr);
5041 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005042 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005043 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005044 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005045 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005046 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005047 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005048 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005049 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005050 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005051 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005052 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005053 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005054 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005055 handleLockReturnedAttr(S, D, Attr);
5056 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005057 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005058 handleLocksExcludedAttr(S, D, Attr);
5059 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005060 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005061 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005062 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005063 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005064 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005065 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005066 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005067 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005068 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005069 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005070 handleUnlockFunAttr(S, D, Attr);
5071 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005072 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005073 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005074 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005075 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005076 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005077 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005078
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005079 // Type safety attributes.
5080 case AttributeList::AT_ArgumentWithTypeTag:
5081 handleArgumentWithTypeTagAttr(S, D, Attr);
5082 break;
5083 case AttributeList::AT_TypeTagForDatatype:
5084 handleTypeTagForDatatypeAttr(S, D, Attr);
5085 break;
5086
Chris Lattner803d0802008-06-29 00:43:07 +00005087 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005088 // Ask target about the attribute.
5089 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5090 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005091 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5092 diag::warn_unhandled_ms_attribute_ignored :
5093 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005094 break;
5095 }
5096}
5097
Peter Collingbourne60700392011-01-21 02:08:45 +00005098/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5099/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00005100/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00005101static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5102 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00005103 bool NonInheritable, bool Inheritable,
5104 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005105 if (Attr.isInvalid())
5106 return;
5107
Richard Smithcd8ab512013-01-17 01:30:42 +00005108 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5109 // instead.
5110 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5111 return;
5112
Peter Collingbourne60700392011-01-21 02:08:45 +00005113 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005114 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005115
5116 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005117 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005118}
5119
Chris Lattner803d0802008-06-29 00:43:07 +00005120/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5121/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005122void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005123 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005124 bool NonInheritable, bool Inheritable,
5125 bool IncludeCXX11Attributes) {
5126 for (const AttributeList* l = AttrList; l; l = l->getNext())
5127 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5128 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005129
5130 // GCC accepts
5131 // static int a9 __attribute__((weakref));
5132 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005133 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005134 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005135 cast<NamedDecl>(D)->getNameAsString();
5136 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005137 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005138 }
5139}
5140
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005141// Annotation attributes are the only attributes allowed after an access
5142// specifier.
5143bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5144 const AttributeList *AttrList) {
5145 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005146 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005147 handleAnnotateAttr(*this, ASDecl, *l);
5148 } else {
5149 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5150 return true;
5151 }
5152 }
5153
5154 return false;
5155}
5156
John McCalle82247a2011-10-01 05:17:03 +00005157/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5158/// contains any decl attributes that we should warn about.
5159static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5160 for ( ; A; A = A->getNext()) {
5161 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005162 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005163 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5164
5165 if (A->getKind() == AttributeList::UnknownAttribute) {
5166 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5167 << A->getName() << A->getRange();
5168 } else {
5169 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5170 << A->getName() << A->getRange();
5171 }
5172 }
5173}
5174
5175/// checkUnusedDeclAttributes - Given a declarator which is not being
5176/// used to build a declaration, complain about any decl attributes
5177/// which might be lying around on it.
5178void Sema::checkUnusedDeclAttributes(Declarator &D) {
5179 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5180 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5181 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5182 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5183}
5184
Ryan Flynne25ff832009-07-30 03:15:39 +00005185/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005186/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005187NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5188 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005189 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005190 NamedDecl *NewD = 0;
5191 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005192 FunctionDecl *NewFD;
5193 // FIXME: Missing call to CheckFunctionDeclaration().
5194 // FIXME: Mangling?
5195 // FIXME: Is the qualifier info correct?
5196 // FIXME: Is the DeclContext correct?
5197 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5198 Loc, Loc, DeclarationName(II),
5199 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005200 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005201 FD->hasPrototype(),
5202 false/*isConstexprSpecified*/);
5203 NewD = NewFD;
5204
5205 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005206 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005207
5208 // Fake up parameter variables; they are declared as if this were
5209 // a typedef.
5210 QualType FDTy = FD->getType();
5211 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5212 SmallVector<ParmVarDecl*, 16> Params;
5213 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5214 AE = FT->arg_type_end(); AI != AE; ++AI) {
5215 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5216 Param->setScopeInfo(0, Params.size());
5217 Params.push_back(Param);
5218 }
David Blaikie4278c652011-09-21 18:16:56 +00005219 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005220 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005221 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5222 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005223 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005224 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005225 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005226 if (VD->getQualifier()) {
5227 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005228 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005229 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005230 }
5231 return NewD;
5232}
5233
James Dennett1dfbd922012-06-14 21:40:34 +00005234/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005235/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005236void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005237 if (W.getUsed()) return; // only do this once
5238 W.setUsed(true);
5239 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5240 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005241 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005242 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5243 NDId->getName()));
5244 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005245 WeakTopLevelDecl.push_back(NewD);
5246 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5247 // to insert Decl at TU scope, sorry.
5248 DeclContext *SavedContext = CurContext;
5249 CurContext = Context.getTranslationUnitDecl();
5250 PushOnScopeChains(NewD, S);
5251 CurContext = SavedContext;
5252 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005253 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005254 }
5255}
5256
Rafael Espindola65611bf2013-03-02 21:41:48 +00005257void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5258 // It's valid to "forward-declare" #pragma weak, in which case we
5259 // have to do this.
5260 LoadExternalWeakUndeclaredIdentifiers();
5261 if (!WeakUndeclaredIdentifiers.empty()) {
5262 NamedDecl *ND = NULL;
5263 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5264 if (VD->isExternC())
5265 ND = VD;
5266 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5267 if (FD->isExternC())
5268 ND = FD;
5269 if (ND) {
5270 if (IdentifierInfo *Id = ND->getIdentifier()) {
5271 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5272 = WeakUndeclaredIdentifiers.find(Id);
5273 if (I != WeakUndeclaredIdentifiers.end()) {
5274 WeakInfo W = I->second;
5275 DeclApplyPragmaWeak(S, ND, W);
5276 WeakUndeclaredIdentifiers[Id] = W;
5277 }
5278 }
5279 }
5280 }
5281}
5282
Chris Lattner0744e5f2008-06-29 00:23:49 +00005283/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5284/// it, apply them to D. This is a bit tricky because PD can have attributes
5285/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005286void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5287 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005288 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005289 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005290 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005291
Chris Lattner0744e5f2008-06-29 00:23:49 +00005292 // Walk the declarator structure, applying decl attributes that were in a type
5293 // position to the decl itself. This handles cases like:
5294 // int *__attr__(x)** D;
5295 // when X is a decl attribute.
5296 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5297 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005298 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5299 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005300
Chris Lattner0744e5f2008-06-29 00:23:49 +00005301 // Finally, apply any attributes on the decl itself.
5302 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005303 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005304}
John McCall54abf7d2009-11-04 02:18:39 +00005305
John McCallf85e1932011-06-15 23:02:42 +00005306/// Is the given declaration allowed to use a forbidden type?
5307static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5308 // Private ivars are always okay. Unfortunately, people don't
5309 // always properly make their ivars private, even in system headers.
5310 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005311 // Function declarations in sys headers will be marked unavailable.
5312 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5313 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005314 return false;
5315
5316 // Require it to be declared in a system header.
5317 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5318}
5319
5320/// Handle a delayed forbidden-type diagnostic.
5321static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5322 Decl *decl) {
5323 if (decl && isForbiddenTypeAllowed(S, decl)) {
5324 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5325 "this system declaration uses an unsupported type"));
5326 return;
5327 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005328 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005329 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005330 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005331 // kind of forbidden type messages on unavailable functions.
5332 if (FD->hasAttr<UnavailableAttr>() &&
5333 diag.getForbiddenTypeDiagnostic() ==
5334 diag::err_arc_array_param_no_ownership) {
5335 diag.Triggered = true;
5336 return;
5337 }
5338 }
John McCallf85e1932011-06-15 23:02:42 +00005339
5340 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5341 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5342 diag.Triggered = true;
5343}
5344
John McCall92576642012-05-07 06:16:41 +00005345void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5346 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005347 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005348 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005349
John McCall92576642012-05-07 06:16:41 +00005350 // When delaying diagnostics to run in the context of a parsed
5351 // declaration, we only want to actually emit anything if parsing
5352 // succeeds.
5353 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005354
John McCall92576642012-05-07 06:16:41 +00005355 // We emit all the active diagnostics in this pool or any of its
5356 // parents. In general, we'll get one pool for the decl spec
5357 // and a child pool for each declarator; in a decl group like:
5358 // deprecated_typedef foo, *bar, baz();
5359 // only the declarator pops will be passed decls. This is correct;
5360 // we really do need to consider delayed diagnostics from the decl spec
5361 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005362 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005363 do {
John McCall13489672012-05-07 06:16:58 +00005364 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005365 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5366 // This const_cast is a bit lame. Really, Triggered should be mutable.
5367 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005368 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005369 continue;
5370
John McCalleee1d542011-02-14 07:13:47 +00005371 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005372 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005373 // Don't bother giving deprecation diagnostics if the decl is invalid.
5374 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005375 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005376 break;
5377
5378 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005379 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005380 break;
John McCallf85e1932011-06-15 23:02:42 +00005381
5382 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005383 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005384 break;
John McCall2f514482010-01-27 03:50:35 +00005385 }
5386 }
John McCall92576642012-05-07 06:16:41 +00005387 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005388}
5389
John McCall13489672012-05-07 06:16:58 +00005390/// Given a set of delayed diagnostics, re-emit them as if they had
5391/// been delayed in the current context instead of in the given pool.
5392/// Essentially, this just moves them to the current pool.
5393void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5394 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5395 assert(curPool && "re-emitting in undelayed context not supported");
5396 curPool->steal(pool);
5397}
5398
John McCall54abf7d2009-11-04 02:18:39 +00005399static bool isDeclDeprecated(Decl *D) {
5400 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005401 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005402 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005403 // A category implicitly has the availability of the interface.
5404 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5405 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005406 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5407 return false;
5408}
5409
Eli Friedmanc3b23082012-08-08 21:52:41 +00005410static void
5411DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5412 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005413 const ObjCInterfaceDecl *UnknownObjCClass,
5414 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005415 DeclarationName Name = D->getDeclName();
5416 if (!Message.empty()) {
5417 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5418 S.Diag(D->getLocation(),
5419 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5420 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005421 if (ObjCPropery)
5422 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5423 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005424 } else if (!UnknownObjCClass) {
5425 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5426 S.Diag(D->getLocation(),
5427 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5428 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005429 if (ObjCPropery)
5430 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5431 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005432 } else {
5433 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5434 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5435 }
5436}
5437
John McCall9c3087b2010-08-26 02:13:20 +00005438void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005439 Decl *Ctx) {
5440 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005441 return;
5442
John McCall2f514482010-01-27 03:50:35 +00005443 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005444 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5445 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005446 DD.getUnknownObjCClass(),
5447 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005448}
5449
Chris Lattner5f9e2722011-07-23 10:55:15 +00005450void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005451 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005452 const ObjCInterfaceDecl *UnknownObjCClass,
5453 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005454 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005455 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005456 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5457 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005458 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005459 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005460 return;
5461 }
5462
5463 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005464 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005465 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005466 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005467}