blob: ea35330129b2d00c9051cabce29b4902e62a45b4 [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"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000028using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030
John McCall883cc2c2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000040 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000041 ExpectedFunctionMethodOrParameter,
42 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000043 ExpectedVariable,
44 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000045 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000046 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000047 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000048 ExpectedVariableFunctionOrTag,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000049 ExpectedTLSVar
John McCall883cc2c2011-03-02 12:29:23 +000050};
51
Chris Lattnere5c5ee12008-06-29 00:16:31 +000052//===----------------------------------------------------------------------===//
53// Helper functions
54//===----------------------------------------------------------------------===//
55
Chandler Carruth87c44602011-07-01 23:49:12 +000056static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000057 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000059 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000063 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000064 Ty = decl->getUnderlyingType();
65 else
66 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000067
Chris Lattner6b6b5372008-06-26 18:38:35 +000068 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000070 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000071 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000072
John McCall183700f2009-09-21 23:43:11 +000073 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000074}
75
Daniel Dunbar35682492008-09-26 04:12:28 +000076// FIXME: We should provide an abstraction around a method or function
77// to provide the following bits of information.
78
Nuno Lopesd20254f2009-12-20 23:11:08 +000079/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000080/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000081static bool isFunction(const Decl *D) {
82 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000083}
84
85/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086/// type (function or function-typed variable) or an Objective-C
87/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000088static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000089 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000090}
91
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000092/// isFunctionOrMethodOrBlock - Return true if the given decl has function
93/// type (function or function-typed variable) or an Objective-C
94/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000095static bool isFunctionOrMethodOrBlock(const Decl *D) {
96 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000097 return true;
98 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000099 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100 QualType Ty = V->getType();
101 return Ty->isBlockPointerType();
102 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000103 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000104}
105
John McCall711c52b2011-01-05 12:14:39 +0000106/// Return true if the given decl has a declarator that should have
107/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000109 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000110 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
111 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000112}
113
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000114/// hasFunctionProto - Return true if the given decl has a argument
115/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000117static bool hasFunctionProto(const Decl *D) {
118 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000119 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000120 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000121 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000122 return true;
123 }
124}
125
126/// getFunctionOrMethodNumArgs - Return number of function or method
127/// arguments. It is an error to call this on a K&R function (use
128/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000129static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
130 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000131 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000133 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000134 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000135}
136
Chandler Carruth87c44602011-07-01 23:49:12 +0000137static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000139 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000140 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000141 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000142
Chandler Carruth87c44602011-07-01 23:49:12 +0000143 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000144}
145
Chandler Carruth87c44602011-07-01 23:49:12 +0000146static QualType getFunctionOrMethodResultType(const Decl *D) {
147 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000149 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000150}
151
Chandler Carruth87c44602011-07-01 23:49:12 +0000152static bool isFunctionOrMethodVariadic(const Decl *D) {
153 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000154 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000155 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000157 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000158 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000159 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000160 }
161}
162
Chandler Carruth87c44602011-07-01 23:49:12 +0000163static bool isInstanceMethod(const Decl *D) {
164 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000165 return MethodDecl->isInstance();
166 return false;
167}
168
Chris Lattner6b6b5372008-06-26 18:38:35 +0000169static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000170 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000171 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000173
John McCall506b57e2010-05-17 21:00:27 +0000174 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
175 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000177
John McCall506b57e2010-05-17 21:00:27 +0000178 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000179
Chris Lattner6b6b5372008-06-26 18:38:35 +0000180 // FIXME: Should we walk the chain of classes?
181 return ClsName == &Ctx.Idents.get("NSString") ||
182 ClsName == &Ctx.Idents.get("NSMutableString");
183}
184
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000187 if (!PT)
188 return false;
189
Ted Kremenek6217b802009-07-29 21:53:49 +0000190 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000191 if (!RT)
192 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000193
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000195 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000196 return false;
197
198 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
199}
200
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000201/// \brief Check if the attribute has exactly as many args as Num. May
202/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000203static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
204 unsigned int Num) {
205 if (Attr.getNumArgs() != Num) {
206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
207 return false;
208 }
209
210 return true;
211}
212
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000213
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000214/// \brief Check if the attribute has at least as many args as Num. May
215/// output an error.
216static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
217 unsigned int Num) {
218 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000219 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
220 return false;
221 }
222
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000223 return true;
224}
225
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000226/// \brief Check if IdxExpr is a valid argument index for a function or
227/// instance method D. May output an error.
228///
229/// \returns true if IdxExpr is a valid index.
230static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
231 StringRef AttrName,
232 SourceLocation AttrLoc,
233 unsigned AttrArgNum,
234 const Expr *IdxExpr,
235 uint64_t &Idx)
236{
237 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
238
239 // In C++ the implicit 'this' function parameter also counts.
240 // Parameters are counted from one.
241 const bool HasImplicitThisParam = isInstanceMethod(D);
242 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
243 const unsigned FirstIdx = 1;
244
245 llvm::APSInt IdxInt;
246 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
247 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
248 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
249 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
250 return false;
251 }
252
253 Idx = IdxInt.getLimitedValue();
254 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
255 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
256 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
257 return false;
258 }
259 Idx--; // Convert to zero-based.
260 if (HasImplicitThisParam) {
261 if (Idx == 0) {
262 S.Diag(AttrLoc,
263 diag::err_attribute_invalid_implicit_this_argument)
264 << AttrName << IdxExpr->getSourceRange();
265 return false;
266 }
267 --Idx;
268 }
269
270 return true;
271}
272
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000273///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000274/// \brief Check if passed in Decl is a field or potentially shared global var
275/// \return true if the Decl is a field or potentially shared global variable
276///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000277static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000278 if (isa<FieldDecl>(D))
279 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000280 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000281 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
282
283 return false;
284}
285
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000286/// \brief Check if the passed-in expression is of type int or bool.
287static bool isIntOrBool(Expr *Exp) {
288 QualType QT = Exp->getType();
289 return QT->isBooleanType() || QT->isIntegerType();
290}
291
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000292
293// Check to see if the type is a smart pointer of some kind. We assume
294// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000295static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
296 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
297 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000298 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000299 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000300
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000301 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
302 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000303 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000304 return false;
305
306 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000307}
308
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000309/// \brief Check if passed in Decl is a pointer type.
310/// Note that this function may produce an error message.
311/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000312static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
313 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000314 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000315 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000316 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000317 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000318
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000319 if (const RecordType *RT = QT->getAs<RecordType>()) {
320 // If it's an incomplete type, it could be a smart pointer; skip it.
321 // (We don't want to force template instantiation if we can avoid it,
322 // since that would alter the order in which templates are instantiated.)
323 if (RT->isIncompleteType())
324 return true;
325
326 if (threadSafetyCheckIsSmartPointer(S, RT))
327 return true;
328 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000329
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000330 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000331 << Attr.getName()->getName() << QT;
332 } else {
333 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
334 << Attr.getName();
335 }
336 return false;
337}
338
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000339/// \brief Checks that the passed in QualType either is of RecordType or points
340/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000341static const RecordType *getRecordType(QualType QT) {
342 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000343 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000344
345 // Now check if we point to record type.
346 if (const PointerType *PT = QT->getAs<PointerType>())
347 return PT->getPointeeType()->getAs<RecordType>();
348
349 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350}
351
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000352
Jordy Rosefad5de92012-05-08 03:27:22 +0000353static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
354 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000355 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
356 if (RT->getDecl()->getAttr<LockableAttr>())
357 return true;
358 return false;
359}
360
361
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000362/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000363/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000364static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
365 QualType Ty) {
366 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000367
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000368 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000369 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000370 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000371 << Attr.getName() << Ty.getAsString();
372 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000373 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000374
Michael Hanf1aae3b2012-08-03 17:40:43 +0000375 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000376 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000377 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000378
379 // Allow smart pointers to be used as lockable objects.
380 // FIXME -- Check the type that the smart pointer points to.
381 if (threadSafetyCheckIsSmartPointer(S, RT))
382 return;
383
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000384 // Check if the type is lockable.
385 RecordDecl *RD = RT->getDecl();
386 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000387 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000388
389 // Else check if any base classes are lockable.
390 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
391 CXXBasePaths BPaths(false, false);
392 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
393 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000394 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000395
396 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
397 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000398}
399
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000400/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000401/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000402/// \param Sidx The attribute argument index to start checking with.
403/// \param ParamIdxOk Whether an argument can be indexing into a function
404/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000405static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000406 const AttributeList &Attr,
407 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000408 int Sidx = 0,
409 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000410 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000411 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000412
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000413 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000414 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000415 Args.push_back(ArgExp);
416 continue;
417 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000418
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000419 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000420 if (StrLit->getLength() == 0 ||
421 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000422 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000423 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000424 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000425 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000426 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000427
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000428 // We allow constant strings to be used as a placeholder for expressions
429 // that are not valid C++ syntax, but warn that they are ignored.
430 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
431 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000432 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000433 continue;
434 }
435
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000436 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000437
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000438 // A pointer to member expression of the form &MyClass::mu is treated
439 // specially -- we need to look at the type of the member.
440 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
441 if (UOp->getOpcode() == UO_AddrOf)
442 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
443 if (DRE->getDecl()->isCXXInstanceMember())
444 ArgTy = DRE->getDecl()->getType();
445
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000446 // First see if we can just cast to record type, or point to record type.
447 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000448
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000449 // Now check if we index into a record type function param.
450 if(!RT && ParamIdxOk) {
451 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000452 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
453 if(FD && IL) {
454 unsigned int NumParams = FD->getNumParams();
455 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
457 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
458 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
460 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000461 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000463 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000464 }
465 }
466
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000467 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000469 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000471}
472
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000473//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000474// Attribute Implementations
475//===----------------------------------------------------------------------===//
476
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000477// FIXME: All this manual attribute parsing code is gross. At the
478// least add some helper functions to check most argument patterns (#
479// and types of args).
480
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000481enum ThreadAttributeDeclKind {
482 ThreadExpectedFieldOrGlobalVar,
483 ThreadExpectedFunctionOrMethod,
484 ThreadExpectedClassOrStruct
485};
486
Michael Hanf1aae3b2012-08-03 17:40:43 +0000487static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000488 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000489 assert(!Attr.isInvalid());
490
491 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000492 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000493
494 // D must be either a member field or global (potentially shared) variable.
495 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000496 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
497 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000498 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000499 }
500
Michael Handc691572012-07-23 18:48:41 +0000501 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000502}
503
Michael Handc691572012-07-23 18:48:41 +0000504static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
505 if (!checkGuardedVarAttrCommon(S, D, Attr))
506 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000507
Michael Han51d8c522013-01-24 16:46:58 +0000508 D->addAttr(::new (S.Context)
509 GuardedVarAttr(Attr.getRange(), S.Context,
510 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000511}
512
Michael Hanf1aae3b2012-08-03 17:40:43 +0000513static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000514 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000515 if (!checkGuardedVarAttrCommon(S, D, Attr))
516 return;
517
518 if (!threadSafetyCheckIsPointer(S, D, Attr))
519 return;
520
Michael Han51d8c522013-01-24 16:46:58 +0000521 D->addAttr(::new (S.Context)
522 PtGuardedVarAttr(Attr.getRange(), S.Context,
523 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000524}
525
Michael Hanf1aae3b2012-08-03 17:40:43 +0000526static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
527 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000528 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000529 assert(!Attr.isInvalid());
530
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000531 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000532 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000533
534 // D must be either a member field or global (potentially shared) variable.
535 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000536 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
537 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000538 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000539 }
540
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000541 SmallVector<Expr*, 1> Args;
542 // check that all arguments are lockable objects
543 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
544 unsigned Size = Args.size();
545 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000546 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000547
Michael Handc691572012-07-23 18:48:41 +0000548 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000549
Michael Handc691572012-07-23 18:48:41 +0000550 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000551}
552
Michael Handc691572012-07-23 18:48:41 +0000553static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
554 Expr *Arg = 0;
555 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
556 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000557
Michael Handc691572012-07-23 18:48:41 +0000558 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
559}
560
Michael Hanf1aae3b2012-08-03 17:40:43 +0000561static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000562 const AttributeList &Attr) {
563 Expr *Arg = 0;
564 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
565 return;
566
567 if (!threadSafetyCheckIsPointer(S, D, Attr))
568 return;
569
570 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
571 S.Context, Arg));
572}
573
Michael Hanf1aae3b2012-08-03 17:40:43 +0000574static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000575 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000576 assert(!Attr.isInvalid());
577
578 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000579 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000580
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000581 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000582 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000583 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
584 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000585 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000586 }
587
Michael Handc691572012-07-23 18:48:41 +0000588 return true;
589}
590
591static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
592 if (!checkLockableAttrCommon(S, D, Attr))
593 return;
594
595 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
596}
597
Michael Hanf1aae3b2012-08-03 17:40:43 +0000598static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000599 const AttributeList &Attr) {
600 if (!checkLockableAttrCommon(S, D, Attr))
601 return;
602
Michael Han51d8c522013-01-24 16:46:58 +0000603 D->addAttr(::new (S.Context)
604 ScopedLockableAttr(Attr.getRange(), S.Context,
605 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000606}
607
608static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
609 const AttributeList &Attr) {
610 assert(!Attr.isInvalid());
611
612 if (!checkAttributeNumArgs(S, Attr, 0))
613 return;
614
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000615 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000616 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
617 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000618 return;
619 }
620
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000621 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000622 S.Context));
623}
624
Kostya Serebryany71efba02012-01-24 19:25:38 +0000625static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000626 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000627 assert(!Attr.isInvalid());
628
629 if (!checkAttributeNumArgs(S, Attr, 0))
630 return;
631
632 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
633 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
634 << Attr.getName() << ExpectedFunctionOrMethod;
635 return;
636 }
637
Michael Han51d8c522013-01-24 16:46:58 +0000638 D->addAttr(::new (S.Context)
639 NoAddressSafetyAnalysisAttr(Attr.getRange(), S.Context,
640 Attr.getAttributeSpellingListIndex()));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000641}
642
Michael Hanf1aae3b2012-08-03 17:40:43 +0000643static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
644 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000645 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000646 assert(!Attr.isInvalid());
647
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000648 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000649 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000650
651 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000652 ValueDecl *VD = dyn_cast<ValueDecl>(D);
653 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000654 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
655 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000656 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000657 }
658
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000659 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000660 QualType QT = VD->getType();
661 if (!QT->isDependentType()) {
662 const RecordType *RT = getRecordType(QT);
663 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000664 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000665 << Attr.getName();
666 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000667 }
668 }
669
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000670 // Check that all arguments are lockable objects.
671 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000672 if (Args.size() == 0)
673 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000674
Michael Handc691572012-07-23 18:48:41 +0000675 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000676}
677
Michael Hanf1aae3b2012-08-03 17:40:43 +0000678static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000679 const AttributeList &Attr) {
680 SmallVector<Expr*, 1> Args;
681 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
682 return;
683
684 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000685 D->addAttr(::new (S.Context)
686 AcquiredAfterAttr(Attr.getRange(), S.Context,
687 StartArg, Args.size(),
688 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000689}
690
Michael Hanf1aae3b2012-08-03 17:40:43 +0000691static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000692 const AttributeList &Attr) {
693 SmallVector<Expr*, 1> Args;
694 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
695 return;
696
697 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000698 D->addAttr(::new (S.Context)
699 AcquiredBeforeAttr(Attr.getRange(), S.Context,
700 StartArg, Args.size(),
701 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000702}
703
Michael Hanf1aae3b2012-08-03 17:40:43 +0000704static bool checkLockFunAttrCommon(Sema &S, Decl *D,
705 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000706 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000707 assert(!Attr.isInvalid());
708
709 // zero or more arguments ok
710
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000711 // check that the attribute is applied to a function
712 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000713 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
714 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000715 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000716 }
717
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000718 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000719 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000720
Michael Handc691572012-07-23 18:48:41 +0000721 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000722}
723
Michael Hanf1aae3b2012-08-03 17:40:43 +0000724static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkLockFunAttrCommon(S, D, Attr, Args))
728 return;
729
730 unsigned Size = Args.size();
731 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000732 D->addAttr(::new (S.Context)
733 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
734 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000735}
736
Michael Hanf1aae3b2012-08-03 17:40:43 +0000737static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000738 const AttributeList &Attr) {
739 SmallVector<Expr*, 1> Args;
740 if (!checkLockFunAttrCommon(S, D, Attr, Args))
741 return;
742
743 unsigned Size = Args.size();
744 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000745 D->addAttr(::new (S.Context)
746 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
747 StartArg, Size,
748 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000749}
750
Michael Hanf1aae3b2012-08-03 17:40:43 +0000751static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
752 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000753 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000754 assert(!Attr.isInvalid());
755
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000756 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000757 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000758
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000759 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000760 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
761 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000762 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000763 }
764
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000765 if (!isIntOrBool(Attr.getArg(0))) {
766 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000767 << Attr.getName();
768 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000769 }
770
771 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000772 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000773
Michael Handc691572012-07-23 18:48:41 +0000774 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000775}
776
Michael Hanf1aae3b2012-08-03 17:40:43 +0000777static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 2> Args;
780 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000785 D->addAttr(::new (S.Context)
786 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
787 Attr.getArg(0), StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000792 const AttributeList &Attr) {
793 SmallVector<Expr*, 2> Args;
794 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000799 D->addAttr(::new (S.Context)
800 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
801 Attr.getArg(0), StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000803}
804
Michael Hanf1aae3b2012-08-03 17:40:43 +0000805static bool checkLocksRequiredCommon(Sema &S, Decl *D,
806 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000807 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000808 assert(!Attr.isInvalid());
809
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000810 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000811 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000812
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000813 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000814 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
815 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000816 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000817 }
818
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000819 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000820 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000821 if (Args.size() == 0)
822 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000823
Michael Handc691572012-07-23 18:48:41 +0000824 return true;
825}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000826
Michael Hanf1aae3b2012-08-03 17:40:43 +0000827static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000828 const AttributeList &Attr) {
829 SmallVector<Expr*, 1> Args;
830 if (!checkLocksRequiredCommon(S, D, Attr, Args))
831 return;
832
833 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000834 D->addAttr(::new (S.Context)
835 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
836 StartArg, Args.size(),
837 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000838}
839
Michael Hanf1aae3b2012-08-03 17:40:43 +0000840static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000841 const AttributeList &Attr) {
842 SmallVector<Expr*, 1> Args;
843 if (!checkLocksRequiredCommon(S, D, Attr, Args))
844 return;
845
846 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000847 D->addAttr(::new (S.Context)
848 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
849 StartArg, Args.size(),
850 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000851}
852
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000853static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000854 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000855 assert(!Attr.isInvalid());
856
857 // zero or more arguments ok
858
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000859 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000860 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
861 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000862 return;
863 }
864
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000865 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000866 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000867 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000868 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000869 Expr **StartArg = Size == 0 ? 0 : &Args[0];
870
Michael Han51d8c522013-01-24 16:46:58 +0000871 D->addAttr(::new (S.Context)
872 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
873 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000874}
875
876static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000877 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000878 assert(!Attr.isInvalid());
879
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000880 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000881 return;
882
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000883 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000884 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
885 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000886 return;
887 }
888
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000889 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000890 SmallVector<Expr*, 1> Args;
891 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
892 unsigned Size = Args.size();
893 if (Size == 0)
894 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000895
Michael Han51d8c522013-01-24 16:46:58 +0000896 D->addAttr(::new (S.Context)
897 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
898 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000899}
900
901static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000902 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000903 assert(!Attr.isInvalid());
904
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000906 return;
907
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000908 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000909 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
910 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000911 return;
912 }
913
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000914 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000915 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000916 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000917 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000918 if (Size == 0)
919 return;
920 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000921
Michael Han51d8c522013-01-24 16:46:58 +0000922 D->addAttr(::new (S.Context)
923 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
924 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000925}
926
927
Chandler Carruth1b03c872011-07-02 00:01:44 +0000928static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
929 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +0000930 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
931 if (TD == 0) {
932 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
933 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +0000934 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000935 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000936 }
Mike Stumpbf916502009-07-24 19:02:52 +0000937
Richard Smitha4fa9002013-01-13 02:11:23 +0000938 // Remember this typedef decl, we will need it later for diagnostics.
939 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000940}
941
Chandler Carruth1b03c872011-07-02 00:01:44 +0000942static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000943 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000944 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000945 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000946
Chandler Carruth87c44602011-07-01 23:49:12 +0000947 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000948 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000949 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000950 // If the alignment is less than or equal to 8 bits, the packed attribute
951 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000952 if (!FD->getType()->isDependentType() &&
953 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000954 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000955 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000956 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000957 else
Michael Han51d8c522013-01-24 16:46:58 +0000958 FD->addAttr(::new (S.Context)
959 PackedAttr(Attr.getRange(), S.Context,
960 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000962 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000963}
964
Chandler Carruth1b03c872011-07-02 00:01:44 +0000965static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000966 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +0000967 RD->addAttr(::new (S.Context)
968 MsStructAttr(Attr.getRange(), S.Context,
969 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000970 else
971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
972}
973
Chandler Carruth1b03c872011-07-02 00:01:44 +0000974static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000975 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000976 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000977 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000978
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000979 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000980 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000981 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +0000982 D->addAttr(::new (S.Context)
983 IBActionAttr(Attr.getRange(), S.Context,
984 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000985 return;
986 }
987
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000988 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000989}
990
Ted Kremenek2f041d02011-09-29 07:02:25 +0000991static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
992 // The IBOutlet/IBOutletCollection attributes only apply to instance
993 // variables or properties of Objective-C classes. The outlet must also
994 // have an object reference type.
995 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
996 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000997 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000998 << Attr.getName() << VD->getType() << 0;
999 return false;
1000 }
1001 }
1002 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1003 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001004 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001005 << Attr.getName() << PD->getType() << 1;
1006 return false;
1007 }
1008 }
1009 else {
1010 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1011 return false;
1012 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001013
Ted Kremenek2f041d02011-09-29 07:02:25 +00001014 return true;
1015}
1016
Chandler Carruth1b03c872011-07-02 00:01:44 +00001017static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001018 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001019 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001020 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001021
1022 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001023 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001024
Michael Han51d8c522013-01-24 16:46:58 +00001025 D->addAttr(::new (S.Context)
1026 IBOutletAttr(Attr.getRange(), S.Context,
1027 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001028}
1029
Chandler Carruth1b03c872011-07-02 00:01:44 +00001030static void handleIBOutletCollection(Sema &S, Decl *D,
1031 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001032
1033 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001034 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001035 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1036 return;
1037 }
1038
Ted Kremenek2f041d02011-09-29 07:02:25 +00001039 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001040 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001041
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001042 IdentifierInfo *II = Attr.getParameterName();
1043 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001044 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001045
John McCallb3d87482010-08-24 05:47:05 +00001046 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001047 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001048 if (!TypeRep) {
1049 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1050 return;
1051 }
John McCallb3d87482010-08-24 05:47:05 +00001052 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001053 // Diagnose use of non-object type in iboutletcollection attribute.
1054 // FIXME. Gnu attribute extension ignores use of builtin types in
1055 // attributes. So, __attribute__((iboutletcollection(char))) will be
1056 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001057 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001058 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1059 return;
1060 }
Michael Han51d8c522013-01-24 16:46:58 +00001061 D->addAttr(::new (S.Context)
1062 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1063 QT, Attr.getParameterLoc(),
1064 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001065}
1066
Chandler Carruthd309c812011-07-01 23:49:16 +00001067static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001068 if (const RecordType *UT = T->getAsUnionType())
1069 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1070 RecordDecl *UD = UT->getDecl();
1071 for (RecordDecl::field_iterator it = UD->field_begin(),
1072 itend = UD->field_end(); it != itend; ++it) {
1073 QualType QT = it->getType();
1074 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1075 T = QT;
1076 return;
1077 }
1078 }
1079 }
1080}
1081
Nuno Lopes587de5b2012-05-24 00:22:00 +00001082static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001083 if (!isFunctionOrMethod(D)) {
1084 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1085 << "alloc_size" << ExpectedFunctionOrMethod;
1086 return;
1087 }
1088
Nuno Lopes587de5b2012-05-24 00:22:00 +00001089 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1090 return;
1091
1092 // In C++ the implicit 'this' function parameter also counts, and they are
1093 // counted from one.
1094 bool HasImplicitThisParam = isInstanceMethod(D);
1095 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1096
1097 SmallVector<unsigned, 8> SizeArgs;
1098
1099 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1100 E = Attr.arg_end(); I!=E; ++I) {
1101 // The argument must be an integer constant expression.
1102 Expr *Ex = *I;
1103 llvm::APSInt ArgNum;
1104 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1105 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1107 << "alloc_size" << Ex->getSourceRange();
1108 return;
1109 }
1110
1111 uint64_t x = ArgNum.getZExtValue();
1112
1113 if (x < 1 || x > NumArgs) {
1114 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1115 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1116 return;
1117 }
1118
1119 --x;
1120 if (HasImplicitThisParam) {
1121 if (x == 0) {
1122 S.Diag(Attr.getLoc(),
1123 diag::err_attribute_invalid_implicit_this_argument)
1124 << "alloc_size" << Ex->getSourceRange();
1125 return;
1126 }
1127 --x;
1128 }
1129
1130 // check if the function argument is of an integer type
1131 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1132 if (!T->isIntegerType()) {
1133 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1134 << "alloc_size" << Ex->getSourceRange();
1135 return;
1136 }
1137
Nuno Lopes587de5b2012-05-24 00:22:00 +00001138 SizeArgs.push_back(x);
1139 }
1140
1141 // check if the function returns a pointer
1142 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1143 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1144 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1145 }
1146
Michael Han51d8c522013-01-24 16:46:58 +00001147 D->addAttr(::new (S.Context)
1148 AllocSizeAttr(Attr.getRange(), S.Context,
1149 SizeArgs.data(), SizeArgs.size(),
1150 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001151}
1152
Chandler Carruth1b03c872011-07-02 00:01:44 +00001153static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001154 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1155 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001156 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001158 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001159 return;
1160 }
Mike Stumpbf916502009-07-24 19:02:52 +00001161
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001162 // In C++ the implicit 'this' function parameter also counts, and they are
1163 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001164 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001165 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001166
1167 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001168 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001169
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001170 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1171 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001172 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001173 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001174 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001175 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1176 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1178 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001179 return;
1180 }
Mike Stumpbf916502009-07-24 19:02:52 +00001181
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001182 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001183
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001184 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001186 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001187 return;
1188 }
Mike Stumpbf916502009-07-24 19:02:52 +00001189
Ted Kremenek465172f2008-07-21 22:09:15 +00001190 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001191 if (HasImplicitThisParam) {
1192 if (x == 0) {
1193 S.Diag(Attr.getLoc(),
1194 diag::err_attribute_invalid_implicit_this_argument)
1195 << "nonnull" << Ex->getSourceRange();
1196 return;
1197 }
1198 --x;
1199 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001200
1201 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001202 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001203 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001204
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001205 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001206 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001207 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001208 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001209 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001210 }
Mike Stumpbf916502009-07-24 19:02:52 +00001211
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001212 NonNullArgs.push_back(x);
1213 }
Mike Stumpbf916502009-07-24 19:02:52 +00001214
1215 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1216 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001217 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001218 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1219 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001220 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001221 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001222 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001223 }
Mike Stumpbf916502009-07-24 19:02:52 +00001224
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001225 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001226 if (NonNullArgs.empty()) {
1227 // Warn the trivial case only if attribute is not coming from a
1228 // macro instantiation.
1229 if (Attr.getLoc().isFileID())
1230 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001231 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001232 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001233 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001234
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001235 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001236 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001237 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001238 D->addAttr(::new (S.Context)
1239 NonNullAttr(Attr.getRange(), S.Context, start, size,
1240 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001241}
1242
Chandler Carruth1b03c872011-07-02 00:01:44 +00001243static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001244 // This attribute must be applied to a function declaration.
1245 // The first argument to the attribute must be a string,
1246 // the name of the resource, for example "malloc".
1247 // The following arguments must be argument indexes, the arguments must be
1248 // of integer type for Returns, otherwise of pointer type.
1249 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001250 // after being held. free() should be __attribute((ownership_takes)), whereas
1251 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001252
1253 if (!AL.getParameterName()) {
1254 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1255 << AL.getName()->getName() << 1;
1256 return;
1257 }
1258 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001259 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001260 switch (AL.getKind()) {
1261 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001262 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001263 if (AL.getNumArgs() < 1) {
1264 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1265 return;
1266 }
Jordy Rose2a479922010-08-12 08:54:03 +00001267 break;
1268 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001269 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001270 if (AL.getNumArgs() < 1) {
1271 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1272 return;
1273 }
Jordy Rose2a479922010-08-12 08:54:03 +00001274 break;
1275 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001276 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001277 if (AL.getNumArgs() > 1) {
1278 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1279 << AL.getNumArgs() + 1;
1280 return;
1281 }
Jordy Rose2a479922010-08-12 08:54:03 +00001282 break;
1283 default:
1284 // This should never happen given how we are called.
1285 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001286 }
1287
Chandler Carruth87c44602011-07-01 23:49:12 +00001288 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001289 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1290 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001291 return;
1292 }
1293
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001294 // In C++ the implicit 'this' function parameter also counts, and they are
1295 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001296 bool HasImplicitThisParam = isInstanceMethod(D);
1297 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001298
Chris Lattner5f9e2722011-07-23 10:55:15 +00001299 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001300
1301 // Normalize the argument, __foo__ becomes foo.
1302 if (Module.startswith("__") && Module.endswith("__"))
1303 Module = Module.substr(2, Module.size() - 4);
1304
Chris Lattner5f9e2722011-07-23 10:55:15 +00001305 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001306
Jordy Rose2a479922010-08-12 08:54:03 +00001307 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1308 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001309
Peter Collingbourne7a730022010-11-23 20:45:58 +00001310 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001311 llvm::APSInt ArgNum(32);
1312 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1313 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1314 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1315 << AL.getName()->getName() << IdxExpr->getSourceRange();
1316 continue;
1317 }
1318
1319 unsigned x = (unsigned) ArgNum.getZExtValue();
1320
1321 if (x > NumArgs || x < 1) {
1322 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1323 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1324 continue;
1325 }
1326 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001327 if (HasImplicitThisParam) {
1328 if (x == 0) {
1329 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1330 << "ownership" << IdxExpr->getSourceRange();
1331 return;
1332 }
1333 --x;
1334 }
1335
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001336 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001337 case OwnershipAttr::Takes:
1338 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001339 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001340 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1342 // FIXME: Should also highlight argument in decl.
1343 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001344 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001345 << "pointer"
1346 << IdxExpr->getSourceRange();
1347 continue;
1348 }
1349 break;
1350 }
Sean Huntcf807c42010-08-18 23:23:40 +00001351 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001352 if (AL.getNumArgs() > 1) {
1353 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001354 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 llvm::APSInt ArgNum(32);
1356 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1357 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1358 S.Diag(AL.getLoc(), diag::err_ownership_type)
1359 << "ownership_returns" << "integer"
1360 << IdxExpr->getSourceRange();
1361 return;
1362 }
1363 }
1364 break;
1365 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001366 } // switch
1367
1368 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001369 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001370 i = D->specific_attr_begin<OwnershipAttr>(),
1371 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001372 i != e; ++i) {
1373 if ((*i)->getOwnKind() != K) {
1374 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1375 I!=E; ++I) {
1376 if (x == *I) {
1377 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1378 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001379 }
1380 }
1381 }
1382 }
1383 OwnershipArgs.push_back(x);
1384 }
1385
1386 unsigned* start = OwnershipArgs.data();
1387 unsigned size = OwnershipArgs.size();
1388 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001389
1390 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1391 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1392 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001393 }
Sean Huntcf807c42010-08-18 23:23:40 +00001394
Michael Han51d8c522013-01-24 16:46:58 +00001395 D->addAttr(::new (S.Context)
1396 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1397 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398}
1399
Chandler Carruth1b03c872011-07-02 00:01:44 +00001400static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001401 // Check the attribute arguments.
1402 if (Attr.getNumArgs() > 1) {
1403 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1404 return;
1405 }
1406
Chandler Carruth87c44602011-07-01 23:49:12 +00001407 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001408 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001409 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001410 return;
1411 }
1412
Chandler Carruth87c44602011-07-01 23:49:12 +00001413 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001414
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001415 // gcc rejects
1416 // class c {
1417 // static int a __attribute__((weakref ("v2")));
1418 // static int b() __attribute__((weakref ("f3")));
1419 // };
1420 // and ignores the attributes of
1421 // void f(void) {
1422 // static int a __attribute__((weakref ("v2")));
1423 // }
1424 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001425 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001426 if (!Ctx->isFileContext()) {
1427 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001428 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001429 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001430 }
1431
1432 // The GCC manual says
1433 //
1434 // At present, a declaration to which `weakref' is attached can only
1435 // be `static'.
1436 //
1437 // It also says
1438 //
1439 // Without a TARGET,
1440 // given as an argument to `weakref' or to `alias', `weakref' is
1441 // equivalent to `weak'.
1442 //
1443 // gcc 4.4.1 will accept
1444 // int a7 __attribute__((weakref));
1445 // as
1446 // int a7 __attribute__((weak));
1447 // This looks like a bug in gcc. We reject that for now. We should revisit
1448 // it if this behaviour is actually used.
1449
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001450 // GCC rejects
1451 // static ((alias ("y"), weakref)).
1452 // Should we? How to check that weakref is before or after alias?
1453
1454 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001455 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001456 Arg = Arg->IgnoreParenCasts();
1457 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1458
Douglas Gregor5cee1192011-07-27 05:40:30 +00001459 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001460 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1461 << "weakref" << 1;
1462 return;
1463 }
1464 // GCC will accept anything as the argument of weakref. Should we
1465 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001466 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001467 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001468 }
1469
Michael Han51d8c522013-01-24 16:46:58 +00001470 D->addAttr(::new (S.Context)
1471 WeakRefAttr(Attr.getRange(), S.Context,
1472 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001473}
1474
Chandler Carruth1b03c872011-07-02 00:01:44 +00001475static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001477 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001479 return;
1480 }
Mike Stumpbf916502009-07-24 19:02:52 +00001481
Peter Collingbourne7a730022010-11-23 20:45:58 +00001482 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001483 Arg = Arg->IgnoreParenCasts();
1484 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001485
Douglas Gregor5cee1192011-07-27 05:40:30 +00001486 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001487 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001488 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001489 return;
1490 }
Mike Stumpbf916502009-07-24 19:02:52 +00001491
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001492 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001493 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1494 return;
1495 }
1496
Chris Lattner6b6b5372008-06-26 18:38:35 +00001497 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001498
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001499 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001500 Str->getString(),
1501 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001502}
1503
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001504static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1505 // Check the attribute arguments.
1506 if (!checkAttributeNumArgs(S, Attr, 0))
1507 return;
1508
1509 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1511 << Attr.getName() << ExpectedFunctionOrMethod;
1512 return;
1513 }
1514
Michael Han51d8c522013-01-24 16:46:58 +00001515 D->addAttr(::new (S.Context)
1516 MinSizeAttr(Attr.getRange(), S.Context,
1517 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001518}
1519
Benjamin Krameree409a92012-05-12 21:10:52 +00001520static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1521 // Check the attribute arguments.
1522 if (!checkAttributeNumArgs(S, Attr, 0))
1523 return;
1524
1525 if (!isa<FunctionDecl>(D)) {
1526 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1527 << Attr.getName() << ExpectedFunction;
1528 return;
1529 }
1530
1531 if (D->hasAttr<HotAttr>()) {
1532 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1533 << Attr.getName() << "hot";
1534 return;
1535 }
1536
Michael Han51d8c522013-01-24 16:46:58 +00001537 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1538 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001539}
1540
1541static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1542 // Check the attribute arguments.
1543 if (!checkAttributeNumArgs(S, Attr, 0))
1544 return;
1545
1546 if (!isa<FunctionDecl>(D)) {
1547 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1548 << Attr.getName() << ExpectedFunction;
1549 return;
1550 }
1551
1552 if (D->hasAttr<ColdAttr>()) {
1553 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1554 << Attr.getName() << "cold";
1555 return;
1556 }
1557
Michael Han51d8c522013-01-24 16:46:58 +00001558 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1559 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001560}
1561
Chandler Carruth1b03c872011-07-02 00:01:44 +00001562static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001563 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001564 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001565 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001566
Chandler Carruth87c44602011-07-01 23:49:12 +00001567 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001568 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001569 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001570 return;
1571 }
1572
Michael Han51d8c522013-01-24 16:46:58 +00001573 D->addAttr(::new (S.Context)
1574 NakedAttr(Attr.getRange(), S.Context,
1575 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001576}
1577
Chandler Carruth1b03c872011-07-02 00:01:44 +00001578static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1579 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001580 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001581 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1583 return;
1584 }
1585
Chandler Carruth87c44602011-07-01 23:49:12 +00001586 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001588 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001589 return;
1590 }
Mike Stumpbf916502009-07-24 19:02:52 +00001591
Michael Han51d8c522013-01-24 16:46:58 +00001592 D->addAttr(::new (S.Context)
1593 AlwaysInlineAttr(Attr.getRange(), S.Context,
1594 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001595}
1596
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001597static void handleTLSModelAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
1599 // Check the attribute arguments.
1600 if (Attr.getNumArgs() != 1) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1602 return;
1603 }
1604
1605 Expr *Arg = Attr.getArg(0);
1606 Arg = Arg->IgnoreParenCasts();
1607 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1608
1609 // Check that it is a string.
1610 if (!Str) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1612 return;
1613 }
1614
1615 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1617 << Attr.getName() << ExpectedTLSVar;
1618 return;
1619 }
1620
1621 // Check that the value.
1622 StringRef Model = Str->getString();
1623 if (Model != "global-dynamic" && Model != "local-dynamic"
1624 && Model != "initial-exec" && Model != "local-exec") {
1625 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1626 return;
1627 }
1628
Michael Han51d8c522013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context)
1630 TLSModelAttr(Attr.getRange(), S.Context, Model,
1631 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001632}
1633
Chandler Carruth1b03c872011-07-02 00:01:44 +00001634static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001635 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001636 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1638 return;
1639 }
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Chandler Carruth87c44602011-07-01 23:49:12 +00001641 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001642 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001643 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001644 D->addAttr(::new (S.Context)
1645 MallocAttr(Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001647 return;
1648 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001649 }
1650
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001652}
1653
Chandler Carruth1b03c872011-07-02 00:01:44 +00001654static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001655 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001656 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001657 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001658
Michael Han51d8c522013-01-24 16:46:58 +00001659 D->addAttr(::new (S.Context)
1660 MayAliasAttr(Attr.getRange(), S.Context,
1661 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001662}
1663
Chandler Carruth1b03c872011-07-02 00:01:44 +00001664static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001665 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001666 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001667 D->addAttr(::new (S.Context)
1668 NoCommonAttr(Attr.getRange(), S.Context,
1669 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001670 else
1671 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001672 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001673}
1674
Chandler Carruth1b03c872011-07-02 00:01:44 +00001675static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001676 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001677 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001678 D->addAttr(::new (S.Context)
1679 CommonAttr(Attr.getRange(), S.Context,
1680 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001681 else
1682 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001683 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001684}
1685
Chandler Carruth1b03c872011-07-02 00:01:44 +00001686static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001687 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001688
1689 if (S.CheckNoReturnAttr(attr)) return;
1690
Chandler Carruth87c44602011-07-01 23:49:12 +00001691 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001692 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001693 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001694 return;
1695 }
1696
Michael Han51d8c522013-01-24 16:46:58 +00001697 D->addAttr(::new (S.Context)
1698 NoReturnAttr(attr.getRange(), S.Context,
1699 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001700}
1701
1702bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001703 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001704 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1705 attr.setInvalid();
1706 return true;
1707 }
1708
1709 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001710}
1711
Chandler Carruth1b03c872011-07-02 00:01:44 +00001712static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1713 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001714
1715 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1716 // because 'analyzer_noreturn' does not impact the type.
1717
Chandler Carruth1731e202011-07-11 23:30:35 +00001718 if(!checkAttributeNumArgs(S, Attr, 0))
1719 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001720
Chandler Carruth87c44602011-07-01 23:49:12 +00001721 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1722 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001723 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1724 && !VD->getType()->isFunctionPointerType())) {
1725 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001726 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001727 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001728 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001729 return;
1730 }
1731 }
1732
Michael Han51d8c522013-01-24 16:46:58 +00001733 D->addAttr(::new (S.Context)
1734 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1735 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001736}
1737
Richard Smithcd8ab512013-01-17 01:30:42 +00001738static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1739 const AttributeList &Attr) {
1740 // C++11 [dcl.attr.noreturn]p1:
1741 // The attribute may be applied to the declarator-id in a function
1742 // declaration.
1743 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1744 if (!FD) {
1745 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1746 << Attr.getName() << ExpectedFunctionOrMethod;
1747 return;
1748 }
1749
Michael Han51d8c522013-01-24 16:46:58 +00001750 D->addAttr(::new (S.Context)
1751 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1752 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001753}
1754
John Thompson35cc9622010-08-09 21:53:52 +00001755// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001756static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001757/*
1758 Returning a Vector Class in Registers
1759
Eric Christopherf48f3672010-12-01 22:13:54 +00001760 According to the PPU ABI specifications, a class with a single member of
1761 vector type is returned in memory when used as the return value of a function.
1762 This results in inefficient code when implementing vector classes. To return
1763 the value in a single vector register, add the vecreturn attribute to the
1764 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001765
1766 Example:
1767
1768 struct Vector
1769 {
1770 __vector float xyzw;
1771 } __attribute__((vecreturn));
1772
1773 Vector Add(Vector lhs, Vector rhs)
1774 {
1775 Vector result;
1776 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1777 return result; // This will be returned in a register
1778 }
1779*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001780 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001782 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001783 return;
1784 }
1785
Chandler Carruth87c44602011-07-01 23:49:12 +00001786 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001787 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1788 return;
1789 }
1790
Chandler Carruth87c44602011-07-01 23:49:12 +00001791 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001792 int count = 0;
1793
1794 if (!isa<CXXRecordDecl>(record)) {
1795 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1796 return;
1797 }
1798
1799 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1800 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1801 return;
1802 }
1803
Eric Christopherf48f3672010-12-01 22:13:54 +00001804 for (RecordDecl::field_iterator iter = record->field_begin();
1805 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001806 if ((count == 1) || !iter->getType()->isVectorType()) {
1807 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1808 return;
1809 }
1810 count++;
1811 }
1812
Michael Han51d8c522013-01-24 16:46:58 +00001813 D->addAttr(::new (S.Context)
1814 VecReturnAttr(Attr.getRange(), S.Context,
1815 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001816}
1817
Chandler Carruth1b03c872011-07-02 00:01:44 +00001818static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001819 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001821 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001822 return;
1823 }
1824 // FIXME: Actually store the attribute on the declaration
1825}
1826
Chandler Carruth1b03c872011-07-02 00:01:44 +00001827static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001828 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001829 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001831 return;
1832 }
Mike Stumpbf916502009-07-24 19:02:52 +00001833
Chandler Carruth87c44602011-07-01 23:49:12 +00001834 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001835 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001837 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001838 return;
1839 }
Mike Stumpbf916502009-07-24 19:02:52 +00001840
Michael Han51d8c522013-01-24 16:46:58 +00001841 D->addAttr(::new (S.Context)
1842 UnusedAttr(Attr.getRange(), S.Context,
1843 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001844}
1845
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001846static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1847 const AttributeList &Attr) {
1848 // check the attribute arguments.
1849 if (Attr.hasParameterOrArguments()) {
1850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1851 return;
1852 }
1853
1854 if (!isa<FunctionDecl>(D)) {
1855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1856 << Attr.getName() << ExpectedFunction;
1857 return;
1858 }
1859
Michael Han51d8c522013-01-24 16:46:58 +00001860 D->addAttr(::new (S.Context)
1861 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1862 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001863}
1864
Chandler Carruth1b03c872011-07-02 00:01:44 +00001865static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001866 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001867 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001868 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1869 return;
1870 }
Mike Stumpbf916502009-07-24 19:02:52 +00001871
Chandler Carruth87c44602011-07-01 23:49:12 +00001872 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001873 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001874 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1875 return;
1876 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001877 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001879 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001880 return;
1881 }
Mike Stumpbf916502009-07-24 19:02:52 +00001882
Michael Han51d8c522013-01-24 16:46:58 +00001883 D->addAttr(::new (S.Context)
1884 UsedAttr(Attr.getRange(), S.Context,
1885 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001886}
1887
Chandler Carruth1b03c872011-07-02 00:01:44 +00001888static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001889 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001890 if (Attr.getNumArgs() > 1) {
1891 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001892 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001893 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001894
1895 int priority = 65535; // FIXME: Do not hardcode such constants.
1896 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001897 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001898 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001899 if (E->isTypeDependent() || E->isValueDependent() ||
1900 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001902 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001903 return;
1904 }
1905 priority = Idx.getZExtValue();
1906 }
Mike Stumpbf916502009-07-24 19:02:52 +00001907
Chandler Carruth87c44602011-07-01 23:49:12 +00001908 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001909 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001910 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001911 return;
1912 }
1913
Michael Han51d8c522013-01-24 16:46:58 +00001914 D->addAttr(::new (S.Context)
1915 ConstructorAttr(Attr.getRange(), S.Context, priority,
1916 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001917}
1918
Chandler Carruth1b03c872011-07-02 00:01:44 +00001919static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001920 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001921 if (Attr.getNumArgs() > 1) {
1922 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001923 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001924 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001925
1926 int priority = 65535; // FIXME: Do not hardcode such constants.
1927 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001928 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001929 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001930 if (E->isTypeDependent() || E->isValueDependent() ||
1931 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001933 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001934 return;
1935 }
1936 priority = Idx.getZExtValue();
1937 }
Mike Stumpbf916502009-07-24 19:02:52 +00001938
Chandler Carruth87c44602011-07-01 23:49:12 +00001939 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001941 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001942 return;
1943 }
1944
Michael Han51d8c522013-01-24 16:46:58 +00001945 D->addAttr(::new (S.Context)
1946 DestructorAttr(Attr.getRange(), S.Context, priority,
1947 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001948}
1949
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001950template <typename AttrTy>
1951static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1952 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001953 unsigned NumArgs = Attr.getNumArgs();
1954 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001955 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001956 return;
1957 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001958
1959 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001960 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001961 if (NumArgs == 1) {
1962 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001963 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001964 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001965 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001966 return;
1967 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001968 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001969 }
Mike Stumpbf916502009-07-24 19:02:52 +00001970
Michael Han51d8c522013-01-24 16:46:58 +00001971 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1972 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001973}
1974
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001975static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1976 const AttributeList &Attr) {
1977 unsigned NumArgs = Attr.getNumArgs();
1978 if (NumArgs > 0) {
1979 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1980 return;
1981 }
1982
Michael Han51d8c522013-01-24 16:46:58 +00001983 D->addAttr(::new (S.Context)
1984 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
1985 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001986}
1987
Patrick Beardb2f68202012-04-06 18:12:22 +00001988static void handleObjCRootClassAttr(Sema &S, Decl *D,
1989 const AttributeList &Attr) {
1990 if (!isa<ObjCInterfaceDecl>(D)) {
1991 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1992 return;
1993 }
1994
1995 unsigned NumArgs = Attr.getNumArgs();
1996 if (NumArgs > 0) {
1997 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1998 return;
1999 }
2000
Michael Han51d8c522013-01-24 16:46:58 +00002001 D->addAttr(::new (S.Context)
2002 ObjCRootClassAttr(Attr.getRange(), S.Context,
2003 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002004}
2005
Michael Han51d8c522013-01-24 16:46:58 +00002006static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2007 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002008 if (!isa<ObjCInterfaceDecl>(D)) {
2009 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2010 return;
2011 }
2012
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002013 unsigned NumArgs = Attr.getNumArgs();
2014 if (NumArgs > 0) {
2015 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2016 return;
2017 }
2018
Michael Han51d8c522013-01-24 16:46:58 +00002019 D->addAttr(::new (S.Context)
2020 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2021 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002022}
2023
Jordy Rosefad5de92012-05-08 03:27:22 +00002024static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2025 IdentifierInfo *Platform,
2026 VersionTuple Introduced,
2027 VersionTuple Deprecated,
2028 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002029 StringRef PlatformName
2030 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2031 if (PlatformName.empty())
2032 PlatformName = Platform->getName();
2033
2034 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2035 // of these steps are needed).
2036 if (!Introduced.empty() && !Deprecated.empty() &&
2037 !(Introduced <= Deprecated)) {
2038 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2039 << 1 << PlatformName << Deprecated.getAsString()
2040 << 0 << Introduced.getAsString();
2041 return true;
2042 }
2043
2044 if (!Introduced.empty() && !Obsoleted.empty() &&
2045 !(Introduced <= Obsoleted)) {
2046 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2047 << 2 << PlatformName << Obsoleted.getAsString()
2048 << 0 << Introduced.getAsString();
2049 return true;
2050 }
2051
2052 if (!Deprecated.empty() && !Obsoleted.empty() &&
2053 !(Deprecated <= Obsoleted)) {
2054 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2055 << 2 << PlatformName << Obsoleted.getAsString()
2056 << 1 << Deprecated.getAsString();
2057 return true;
2058 }
2059
2060 return false;
2061}
2062
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002063/// \brief Check whether the two versions match.
2064///
2065/// If either version tuple is empty, then they are assumed to match. If
2066/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2067static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2068 bool BeforeIsOkay) {
2069 if (X.empty() || Y.empty())
2070 return true;
2071
2072 if (X == Y)
2073 return true;
2074
2075 if (BeforeIsOkay && X < Y)
2076 return true;
2077
2078 return false;
2079}
2080
Rafael Espindola51be6e32013-01-08 22:04:34 +00002081AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002082 IdentifierInfo *Platform,
2083 VersionTuple Introduced,
2084 VersionTuple Deprecated,
2085 VersionTuple Obsoleted,
2086 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002087 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002088 bool Override,
2089 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002090 VersionTuple MergedIntroduced = Introduced;
2091 VersionTuple MergedDeprecated = Deprecated;
2092 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002093 bool FoundAny = false;
2094
Rafael Espindola98ae8342012-05-10 02:50:16 +00002095 if (D->hasAttrs()) {
2096 AttrVec &Attrs = D->getAttrs();
2097 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2098 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2099 if (!OldAA) {
2100 ++i;
2101 continue;
2102 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002103
Rafael Espindola98ae8342012-05-10 02:50:16 +00002104 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2105 if (OldPlatform != Platform) {
2106 ++i;
2107 continue;
2108 }
2109
2110 FoundAny = true;
2111 VersionTuple OldIntroduced = OldAA->getIntroduced();
2112 VersionTuple OldDeprecated = OldAA->getDeprecated();
2113 VersionTuple OldObsoleted = OldAA->getObsoleted();
2114 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002115
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002116 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2117 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2118 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2119 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002120 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002121 if (Override) {
2122 int Which = -1;
2123 VersionTuple FirstVersion;
2124 VersionTuple SecondVersion;
2125 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2126 Which = 0;
2127 FirstVersion = OldIntroduced;
2128 SecondVersion = Introduced;
2129 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2130 Which = 1;
2131 FirstVersion = Deprecated;
2132 SecondVersion = OldDeprecated;
2133 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2134 Which = 2;
2135 FirstVersion = Obsoleted;
2136 SecondVersion = OldObsoleted;
2137 }
2138
2139 if (Which == -1) {
2140 Diag(OldAA->getLocation(),
2141 diag::warn_mismatched_availability_override_unavail)
2142 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2143 } else {
2144 Diag(OldAA->getLocation(),
2145 diag::warn_mismatched_availability_override)
2146 << Which
2147 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2148 << FirstVersion.getAsString() << SecondVersion.getAsString();
2149 }
2150 Diag(Range.getBegin(), diag::note_overridden_method);
2151 } else {
2152 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2153 Diag(Range.getBegin(), diag::note_previous_attribute);
2154 }
2155
Rafael Espindola98ae8342012-05-10 02:50:16 +00002156 Attrs.erase(Attrs.begin() + i);
2157 --e;
2158 continue;
2159 }
2160
2161 VersionTuple MergedIntroduced2 = MergedIntroduced;
2162 VersionTuple MergedDeprecated2 = MergedDeprecated;
2163 VersionTuple MergedObsoleted2 = MergedObsoleted;
2164
2165 if (MergedIntroduced2.empty())
2166 MergedIntroduced2 = OldIntroduced;
2167 if (MergedDeprecated2.empty())
2168 MergedDeprecated2 = OldDeprecated;
2169 if (MergedObsoleted2.empty())
2170 MergedObsoleted2 = OldObsoleted;
2171
2172 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2173 MergedIntroduced2, MergedDeprecated2,
2174 MergedObsoleted2)) {
2175 Attrs.erase(Attrs.begin() + i);
2176 --e;
2177 continue;
2178 }
2179
2180 MergedIntroduced = MergedIntroduced2;
2181 MergedDeprecated = MergedDeprecated2;
2182 MergedObsoleted = MergedObsoleted2;
2183 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002184 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002185 }
2186
2187 if (FoundAny &&
2188 MergedIntroduced == Introduced &&
2189 MergedDeprecated == Deprecated &&
2190 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002191 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002192
Rafael Espindola98ae8342012-05-10 02:50:16 +00002193 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002194 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002195 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2196 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002197 Obsoleted, IsUnavailable, Message,
2198 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002199 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002200 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002201}
2202
Chandler Carruth1b03c872011-07-02 00:01:44 +00002203static void handleAvailabilityAttr(Sema &S, Decl *D,
2204 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002205 IdentifierInfo *Platform = Attr.getParameterName();
2206 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002207 unsigned Index = Attr.getAttributeSpellingListIndex();
2208
Rafael Espindola3b294362012-05-06 19:56:25 +00002209 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002210 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2211 << Platform;
2212
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002213 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2214 if (!ND) {
2215 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2216 return;
2217 }
2218
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002219 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2220 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2221 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002222 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002223 StringRef Str;
2224 const StringLiteral *SE =
2225 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2226 if (SE)
2227 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002228
Rafael Espindola51be6e32013-01-08 22:04:34 +00002229 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002230 Platform,
2231 Introduced.Version,
2232 Deprecated.Version,
2233 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002234 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002235 /*Override=*/false,
2236 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002237 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002238 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002239}
2240
Rafael Espindola599f1b72012-05-13 03:25:18 +00002241VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002242 VisibilityAttr::VisibilityType Vis,
2243 unsigned AttrSpellingListIndex) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002244 if (isa<TypedefNameDecl>(D)) {
2245 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002246 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002247 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002248 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2249 if (ExistingAttr) {
2250 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2251 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002252 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002253 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2254 Diag(Range.getBegin(), diag::note_previous_attribute);
2255 D->dropAttr<VisibilityAttr>();
2256 }
Michael Han51d8c522013-01-24 16:46:58 +00002257 return ::new (Context) VisibilityAttr(Range, Context, Vis,
2258 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002259}
2260
Chandler Carruth1b03c872011-07-02 00:01:44 +00002261static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002262 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002263 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002264 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002265
Peter Collingbourne7a730022010-11-23 20:45:58 +00002266 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002267 Arg = Arg->IgnoreParenCasts();
2268 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002269
Douglas Gregor5cee1192011-07-27 05:40:30 +00002270 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002271 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002272 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002273 return;
2274 }
Mike Stumpbf916502009-07-24 19:02:52 +00002275
Chris Lattner5f9e2722011-07-23 10:55:15 +00002276 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002277 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002278
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002279 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002280 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002281 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002282 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002283 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002284 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002285 else if (TypeStr == "protected") {
2286 // Complain about attempts to use protected visibility on targets
2287 // (like Darwin) that don't support it.
2288 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2289 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2290 type = VisibilityAttr::Default;
2291 } else {
2292 type = VisibilityAttr::Protected;
2293 }
2294 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002295 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002296 return;
2297 }
Mike Stumpbf916502009-07-24 19:02:52 +00002298
Michael Han51d8c522013-01-24 16:46:58 +00002299 unsigned Index = Attr.getAttributeSpellingListIndex();
2300 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type,
2301 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002302 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002303 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002304}
2305
Chandler Carruth1b03c872011-07-02 00:01:44 +00002306static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2307 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002308 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2309 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002311 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002312 return;
2313 }
2314
Chandler Carruth87c44602011-07-01 23:49:12 +00002315 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2316 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2317 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002318 << "objc_method_family" << 1;
2319 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002320 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002321 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002322 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002323 return;
2324 }
2325
Chris Lattner5f9e2722011-07-23 10:55:15 +00002326 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002327 ObjCMethodFamilyAttr::FamilyKind family;
2328 if (param == "none")
2329 family = ObjCMethodFamilyAttr::OMF_None;
2330 else if (param == "alloc")
2331 family = ObjCMethodFamilyAttr::OMF_alloc;
2332 else if (param == "copy")
2333 family = ObjCMethodFamilyAttr::OMF_copy;
2334 else if (param == "init")
2335 family = ObjCMethodFamilyAttr::OMF_init;
2336 else if (param == "mutableCopy")
2337 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2338 else if (param == "new")
2339 family = ObjCMethodFamilyAttr::OMF_new;
2340 else {
2341 // Just warn and ignore it. This is future-proof against new
2342 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002343 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002344 return;
2345 }
2346
John McCallf85e1932011-06-15 23:02:42 +00002347 if (family == ObjCMethodFamilyAttr::OMF_init &&
2348 !method->getResultType()->isObjCObjectPointerType()) {
2349 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2350 << method->getResultType();
2351 // Ignore the attribute.
2352 return;
2353 }
2354
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002355 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002356 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002357}
2358
Chandler Carruth1b03c872011-07-02 00:01:44 +00002359static void handleObjCExceptionAttr(Sema &S, Decl *D,
2360 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002361 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002362 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002363
Chris Lattner0db29ec2009-02-14 08:09:34 +00002364 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2365 if (OCI == 0) {
2366 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2367 return;
2368 }
Mike Stumpbf916502009-07-24 19:02:52 +00002369
Michael Han51d8c522013-01-24 16:46:58 +00002370 D->addAttr(::new (S.Context)
2371 ObjCExceptionAttr(Attr.getRange(), S.Context,
2372 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002373}
2374
Chandler Carruth1b03c872011-07-02 00:01:44 +00002375static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002376 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002378 return;
2379 }
Richard Smith162e1c12011-04-15 14:24:37 +00002380 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002381 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002382 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002383 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2384 return;
2385 }
2386 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002387 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2388 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002389 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002390 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2391 return;
2392 }
2393 }
2394 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002395 // It is okay to include this attribute on properties, e.g.:
2396 //
2397 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2398 //
2399 // In this case it follows tradition and suppresses an error in the above
2400 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002401 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002402 }
Michael Han51d8c522013-01-24 16:46:58 +00002403 D->addAttr(::new (S.Context)
2404 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2405 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002406}
2407
Mike Stumpbf916502009-07-24 19:02:52 +00002408static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002409handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002410 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002412 return;
2413 }
2414
2415 if (!isa<FunctionDecl>(D)) {
2416 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2417 return;
2418 }
2419
Michael Han51d8c522013-01-24 16:46:58 +00002420 D->addAttr(::new (S.Context)
2421 OverloadableAttr(Attr.getRange(), S.Context,
2422 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002423}
2424
Chandler Carruth1b03c872011-07-02 00:01:44 +00002425static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002426 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002427 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002428 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002429 return;
2430 }
Mike Stumpbf916502009-07-24 19:02:52 +00002431
Steve Naroff9eae5762008-09-18 16:44:58 +00002432 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002434 return;
2435 }
Mike Stumpbf916502009-07-24 19:02:52 +00002436
Sean Huntcf807c42010-08-18 23:23:40 +00002437 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002438 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002439 type = BlocksAttr::ByRef;
2440 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002441 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002442 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002443 return;
2444 }
Mike Stumpbf916502009-07-24 19:02:52 +00002445
Michael Han51d8c522013-01-24 16:46:58 +00002446 D->addAttr(::new (S.Context)
2447 BlocksAttr(Attr.getRange(), S.Context, type,
2448 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002449}
2450
Chandler Carruth1b03c872011-07-02 00:01:44 +00002451static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002452 // check the attribute arguments.
2453 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002454 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002455 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002456 }
2457
John McCall3323fad2011-09-09 07:56:05 +00002458 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002459 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002460 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002461 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002462 if (E->isTypeDependent() || E->isValueDependent() ||
2463 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002465 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002466 return;
2467 }
Mike Stumpbf916502009-07-24 19:02:52 +00002468
John McCall3323fad2011-09-09 07:56:05 +00002469 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002470 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2471 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002472 return;
2473 }
John McCall3323fad2011-09-09 07:56:05 +00002474
2475 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002476 }
2477
John McCall3323fad2011-09-09 07:56:05 +00002478 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002479 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002480 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002481 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002482 if (E->isTypeDependent() || E->isValueDependent() ||
2483 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002484 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002485 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002486 return;
2487 }
2488 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002489
John McCall3323fad2011-09-09 07:56:05 +00002490 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002491 // FIXME: This error message could be improved, it would be nice
2492 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002493 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2494 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002495 return;
2496 }
2497 }
2498
Chandler Carruth87c44602011-07-01 23:49:12 +00002499 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002500 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002501 if (isa<FunctionNoProtoType>(FT)) {
2502 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2503 return;
2504 }
Mike Stumpbf916502009-07-24 19:02:52 +00002505
Chris Lattner897cd902009-03-17 23:03:47 +00002506 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002507 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002508 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002509 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002510 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002511 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002512 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002513 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002514 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002515 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2516 if (!BD->isVariadic()) {
2517 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2518 return;
2519 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002520 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002521 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002522 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002523 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002524 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002525 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002526 int m = Ty->isFunctionPointerType() ? 0 : 1;
2527 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002528 return;
2529 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002530 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002532 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002533 return;
2534 }
Anders Carlsson77091822008-10-05 18:05:59 +00002535 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002536 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002537 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002538 return;
2539 }
Michael Han51d8c522013-01-24 16:46:58 +00002540 D->addAttr(::new (S.Context)
2541 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2542 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002543}
2544
Chandler Carruth1b03c872011-07-02 00:01:44 +00002545static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002546 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002547 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002548 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002549
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002550 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002552 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002553 return;
2554 }
Mike Stumpbf916502009-07-24 19:02:52 +00002555
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002556 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2557 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2558 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002559 return;
2560 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002561 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2562 if (MD->getResultType()->isVoidType()) {
2563 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2564 << Attr.getName() << 1;
2565 return;
2566 }
2567
Michael Han51d8c522013-01-24 16:46:58 +00002568 D->addAttr(::new (S.Context)
2569 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2570 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002571}
2572
Chandler Carruth1b03c872011-07-02 00:01:44 +00002573static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002574 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002575 if (Attr.hasParameterOrArguments()) {
2576 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002577 return;
2578 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002579
Chandler Carruth87c44602011-07-01 23:49:12 +00002580 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002581 if (isa<CXXRecordDecl>(D)) {
2582 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2583 return;
2584 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002585 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2586 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002587 return;
2588 }
2589
Chandler Carruth87c44602011-07-01 23:49:12 +00002590 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002591
Michael Han51d8c522013-01-24 16:46:58 +00002592 nd->addAttr(::new (S.Context)
2593 WeakAttr(Attr.getRange(), S.Context,
2594 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002595}
2596
Chandler Carruth1b03c872011-07-02 00:01:44 +00002597static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002598 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002599 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002600 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002601
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002602
2603 // weak_import only applies to variable & function declarations.
2604 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002605 if (!D->canBeWeakImported(isDef)) {
2606 if (isDef)
2607 S.Diag(Attr.getLoc(),
2608 diag::warn_attribute_weak_import_invalid_on_definition)
2609 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002610 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002611 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002612 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002613 // Nothing to warn about here.
2614 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002616 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002617
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002618 return;
2619 }
2620
Michael Han51d8c522013-01-24 16:46:58 +00002621 D->addAttr(::new (S.Context)
2622 WeakImportAttr(Attr.getRange(), S.Context,
2623 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002624}
2625
Tanya Lattner0df579e2012-07-09 22:06:01 +00002626// Handles reqd_work_group_size and work_group_size_hint.
2627static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002628 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002629 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2630 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2631
Nate Begeman6f3d8382009-06-26 06:32:41 +00002632 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002633 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002634
2635 unsigned WGSize[3];
2636 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002637 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002638 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002639 if (E->isTypeDependent() || E->isValueDependent() ||
2640 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002641 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002642 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002643 return;
2644 }
2645 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2646 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002647
2648 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2649 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2650 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2651 if (!(A->getXDim() == WGSize[0] &&
2652 A->getYDim() == WGSize[1] &&
2653 A->getZDim() == WGSize[2])) {
2654 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2655 Attr.getName();
2656 }
2657 }
2658
2659 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2660 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2661 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2662 if (!(A->getXDim() == WGSize[0] &&
2663 A->getYDim() == WGSize[1] &&
2664 A->getZDim() == WGSize[2])) {
2665 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2666 Attr.getName();
2667 }
2668 }
2669
2670 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2671 D->addAttr(::new (S.Context)
2672 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002673 WGSize[0], WGSize[1], WGSize[2],
2674 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002675 else
2676 D->addAttr(::new (S.Context)
2677 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002678 WGSize[0], WGSize[1], WGSize[2],
2679 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002680}
2681
Rafael Espindola599f1b72012-05-13 03:25:18 +00002682SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002683 StringRef Name,
2684 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002685 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2686 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002687 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002688 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2689 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002690 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002691 }
Michael Han51d8c522013-01-24 16:46:58 +00002692 return ::new (Context) SectionAttr(Range, Context, Name,
2693 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002694}
2695
Chandler Carruth1b03c872011-07-02 00:01:44 +00002696static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002697 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002698 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002699 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002700
2701 // Make sure that there is a string literal as the sections's single
2702 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002703 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002704 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002705 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002706 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002707 return;
2708 }
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Chris Lattner797c3c42009-08-10 19:03:04 +00002710 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002711 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002712 if (!Error.empty()) {
2713 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2714 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002715 return;
2716 }
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002718 // This attribute cannot be applied to local variables.
2719 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2720 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2721 return;
2722 }
Michael Han51d8c522013-01-24 16:46:58 +00002723
2724 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002725 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002726 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002727 if (NewAttr)
2728 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002729}
2730
Chris Lattner6b6b5372008-06-26 18:38:35 +00002731
Chandler Carruth1b03c872011-07-02 00:01:44 +00002732static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002733 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002734 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002736 return;
2737 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002738
Chandler Carruth87c44602011-07-01 23:49:12 +00002739 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002740 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002741 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002742 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002743 D->addAttr(::new (S.Context)
2744 NoThrowAttr(Attr.getRange(), S.Context,
2745 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002746 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002747}
2748
Chandler Carruth1b03c872011-07-02 00:01:44 +00002749static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002750 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002751 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002753 return;
2754 }
Mike Stumpbf916502009-07-24 19:02:52 +00002755
Chandler Carruth87c44602011-07-01 23:49:12 +00002756 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002757 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002758 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002759 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002760 D->addAttr(::new (S.Context)
2761 ConstAttr(Attr.getRange(), S.Context,
2762 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002763 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002764}
2765
Chandler Carruth1b03c872011-07-02 00:01:44 +00002766static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002767 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002768 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002769 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002770
Michael Han51d8c522013-01-24 16:46:58 +00002771 D->addAttr(::new (S.Context)
2772 PureAttr(Attr.getRange(), S.Context,
2773 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002774}
2775
Chandler Carruth1b03c872011-07-02 00:01:44 +00002776static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002777 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002778 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2779 return;
2780 }
Mike Stumpbf916502009-07-24 19:02:52 +00002781
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002782 if (Attr.getNumArgs() != 0) {
2783 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2784 return;
2785 }
Mike Stumpbf916502009-07-24 19:02:52 +00002786
Chandler Carruth87c44602011-07-01 23:49:12 +00002787 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002788
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002789 if (!VD || !VD->hasLocalStorage()) {
2790 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2791 return;
2792 }
Mike Stumpbf916502009-07-24 19:02:52 +00002793
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002794 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002795 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002796 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002797 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2798 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002799 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002800 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002801 Attr.getParameterName();
2802 return;
2803 }
Mike Stumpbf916502009-07-24 19:02:52 +00002804
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002805 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2806 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002807 S.Diag(Attr.getParameterLoc(),
2808 diag::err_attribute_cleanup_arg_not_function)
2809 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002810 return;
2811 }
2812
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002813 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002814 S.Diag(Attr.getParameterLoc(),
2815 diag::err_attribute_cleanup_func_must_take_one_arg)
2816 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002817 return;
2818 }
Mike Stumpbf916502009-07-24 19:02:52 +00002819
Anders Carlsson89941c12009-02-07 23:16:50 +00002820 // We're currently more strict than GCC about what function types we accept.
2821 // If this ever proves to be a problem it should be easy to fix.
2822 QualType Ty = S.Context.getPointerType(VD->getType());
2823 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002824 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2825 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002826 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002827 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2828 Attr.getParameterName() << ParamTy << Ty;
2829 return;
2830 }
Mike Stumpbf916502009-07-24 19:02:52 +00002831
Michael Han51d8c522013-01-24 16:46:58 +00002832 D->addAttr(::new (S.Context)
2833 CleanupAttr(Attr.getRange(), S.Context, FD,
2834 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002835 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002836}
2837
Mike Stumpbf916502009-07-24 19:02:52 +00002838/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002839/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002840static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002841 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002842 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002843
Chandler Carruth87c44602011-07-01 23:49:12 +00002844 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002845 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002846 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002847 return;
2848 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002849
2850 // In C++ the implicit 'this' function parameter also counts, and they are
2851 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002852 bool HasImplicitThisParam = isInstanceMethod(D);
2853 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002854 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002855
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002856 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002857 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002858 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002859 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2860 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002861 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2862 << "format" << 2 << IdxExpr->getSourceRange();
2863 return;
2864 }
Mike Stumpbf916502009-07-24 19:02:52 +00002865
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002866 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2867 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2868 << "format" << 2 << IdxExpr->getSourceRange();
2869 return;
2870 }
Mike Stumpbf916502009-07-24 19:02:52 +00002871
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002872 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002873
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002874 if (HasImplicitThisParam) {
2875 if (ArgIdx == 0) {
2876 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2877 << "format_arg" << IdxExpr->getSourceRange();
2878 return;
2879 }
2880 ArgIdx--;
2881 }
2882
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002883 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002884 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002885
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002886 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2887 if (not_nsstring_type &&
2888 !isCFStringType(Ty, S.Context) &&
2889 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002890 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002891 // FIXME: Should highlight the actual expression that has the wrong type.
2892 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002893 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002894 << IdxExpr->getSourceRange();
2895 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002896 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002897 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002898 if (!isNSStringType(Ty, S.Context) &&
2899 !isCFStringType(Ty, S.Context) &&
2900 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002901 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002902 // FIXME: Should highlight the actual expression that has the wrong type.
2903 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002904 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002905 << IdxExpr->getSourceRange();
2906 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002907 }
2908
Michael Han51d8c522013-01-24 16:46:58 +00002909 D->addAttr(::new (S.Context)
2910 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
2911 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002912}
2913
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002914enum FormatAttrKind {
2915 CFStringFormat,
2916 NSStringFormat,
2917 StrftimeFormat,
2918 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002919 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002920 InvalidFormat
2921};
2922
2923/// getFormatAttrKind - Map from format attribute names to supported format
2924/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002925static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002926 return llvm::StringSwitch<FormatAttrKind>(Format)
2927 // Check for formats that get handled specially.
2928 .Case("NSString", NSStringFormat)
2929 .Case("CFString", CFStringFormat)
2930 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002931
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002932 // Otherwise, check for supported formats.
2933 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2934 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2935 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002936
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002937 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2938 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002939}
2940
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002941/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002942/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002943static void handleInitPriorityAttr(Sema &S, Decl *D,
2944 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002945 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002946 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2947 return;
2948 }
2949
Chandler Carruth87c44602011-07-01 23:49:12 +00002950 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002951 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2952 Attr.setInvalid();
2953 return;
2954 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002955 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002956 if (S.Context.getAsArrayType(T))
2957 T = S.Context.getBaseElementType(T);
2958 if (!T->getAs<RecordType>()) {
2959 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2960 Attr.setInvalid();
2961 return;
2962 }
2963
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002964 if (Attr.getNumArgs() != 1) {
2965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2966 Attr.setInvalid();
2967 return;
2968 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002969 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002970
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002971 llvm::APSInt priority(32);
2972 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2973 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2974 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2975 << "init_priority" << priorityExpr->getSourceRange();
2976 Attr.setInvalid();
2977 return;
2978 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002979 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002980 if (prioritynum < 101 || prioritynum > 65535) {
2981 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2982 << priorityExpr->getSourceRange();
2983 Attr.setInvalid();
2984 return;
2985 }
Michael Han51d8c522013-01-24 16:46:58 +00002986 D->addAttr(::new (S.Context)
2987 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2988 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002989}
2990
Rafael Espindola599f1b72012-05-13 03:25:18 +00002991FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00002992 int FormatIdx, int FirstArg,
2993 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002994 // Check whether we already have an equivalent format attribute.
2995 for (specific_attr_iterator<FormatAttr>
2996 i = D->specific_attr_begin<FormatAttr>(),
2997 e = D->specific_attr_end<FormatAttr>();
2998 i != e ; ++i) {
2999 FormatAttr *f = *i;
3000 if (f->getType() == Format &&
3001 f->getFormatIdx() == FormatIdx &&
3002 f->getFirstArg() == FirstArg) {
3003 // If we don't have a valid location for this attribute, adopt the
3004 // location.
3005 if (f->getLocation().isInvalid())
3006 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003007 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003008 }
3009 }
3010
Michael Han51d8c522013-01-24 16:46:58 +00003011 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3012 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003013}
3014
Mike Stumpbf916502009-07-24 19:02:52 +00003015/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003016/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003017static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003018
Chris Lattner545dd342008-06-28 23:36:30 +00003019 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00003021 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003022 return;
3023 }
3024
Chris Lattner545dd342008-06-28 23:36:30 +00003025 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003027 return;
3028 }
3029
Chandler Carruth87c44602011-07-01 23:49:12 +00003030 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003031 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003032 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003033 return;
3034 }
3035
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003036 // In C++ the implicit 'this' function parameter also counts, and they are
3037 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003038 bool HasImplicitThisParam = isInstanceMethod(D);
3039 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003040 unsigned FirstIdx = 1;
3041
Chris Lattner5f9e2722011-07-23 10:55:15 +00003042 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003043
3044 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003045 if (Format.startswith("__") && Format.endswith("__"))
3046 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003047
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003048 // Check for supported formats.
3049 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003050
3051 if (Kind == IgnoredFormat)
3052 return;
3053
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003054 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003055 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003056 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003057 return;
3058 }
3059
3060 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003061 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003062 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003063 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3064 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003065 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003066 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003067 return;
3068 }
3069
3070 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003071 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003072 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003073 return;
3074 }
3075
3076 // FIXME: Do we need to bounds check?
3077 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003078
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003079 if (HasImplicitThisParam) {
3080 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003081 S.Diag(Attr.getLoc(),
3082 diag::err_format_attribute_implicit_this_format_string)
3083 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003084 return;
3085 }
3086 ArgIdx--;
3087 }
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Chris Lattner6b6b5372008-06-26 18:38:35 +00003089 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003090 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003091
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003092 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003093 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003094 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3095 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003096 return;
3097 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003098 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003099 // FIXME: do we need to check if the type is NSString*? What are the
3100 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003101 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003102 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003103 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3104 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003105 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003106 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003107 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003108 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003109 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003110 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3111 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003112 return;
3113 }
3114
3115 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003116 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003117 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003118 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3119 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003121 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003122 return;
3123 }
3124
3125 // check if the function is variadic if the 3rd argument non-zero
3126 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003127 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003128 ++NumArgs; // +1 for ...
3129 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003130 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131 return;
3132 }
3133 }
3134
Chris Lattner3c73c412008-11-19 08:23:25 +00003135 // strftime requires FirstArg to be 0 because it doesn't read from any
3136 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003137 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003138 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003139 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3140 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141 return;
3142 }
3143 // if 0 it disables parameter checking (to use with e.g. va_list)
3144 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003146 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003147 return;
3148 }
3149
Rafael Espindola599f1b72012-05-13 03:25:18 +00003150 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3151 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003152 FirstArg.getZExtValue(),
3153 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003154 if (NewAttr)
3155 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003156}
3157
Chandler Carruth1b03c872011-07-02 00:01:44 +00003158static void handleTransparentUnionAttr(Sema &S, Decl *D,
3159 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003160 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003161 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003162 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003163
Chris Lattner6b6b5372008-06-26 18:38:35 +00003164
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003165 // Try to find the underlying union declaration.
3166 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003167 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003168 if (TD && TD->getUnderlyingType()->isUnionType())
3169 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3170 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003171 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003172
3173 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003174 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003175 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003176 return;
3177 }
3178
John McCall5e1cdac2011-10-07 06:10:15 +00003179 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003180 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003181 diag::warn_transparent_union_attribute_not_definition);
3182 return;
3183 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003184
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003185 RecordDecl::field_iterator Field = RD->field_begin(),
3186 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003187 if (Field == FieldEnd) {
3188 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3189 return;
3190 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003191
David Blaikie581deb32012-06-06 20:45:41 +00003192 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003193 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003194 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003195 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003196 diag::warn_transparent_union_attribute_floating)
3197 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003198 return;
3199 }
3200
3201 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3202 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3203 for (; Field != FieldEnd; ++Field) {
3204 QualType FieldType = Field->getType();
3205 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3206 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3207 // Warn if we drop the attribute.
3208 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003209 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003210 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003211 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003212 diag::warn_transparent_union_attribute_field_size_align)
3213 << isSize << Field->getDeclName() << FieldBits;
3214 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003215 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003216 diag::note_transparent_union_first_field_size_align)
3217 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003218 return;
3219 }
3220 }
3221
Michael Han51d8c522013-01-24 16:46:58 +00003222 RD->addAttr(::new (S.Context)
3223 TransparentUnionAttr(Attr.getRange(), S.Context,
3224 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003225}
3226
Chandler Carruth1b03c872011-07-02 00:01:44 +00003227static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003228 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003229 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003230 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003231
Peter Collingbourne7a730022010-11-23 20:45:58 +00003232 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003233 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003234
Chris Lattner6b6b5372008-06-26 18:38:35 +00003235 // Make sure that there is a string literal as the annotation's single
3236 // argument.
3237 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003238 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003239 return;
3240 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003241
3242 // Don't duplicate annotations that are already set.
3243 for (specific_attr_iterator<AnnotateAttr>
3244 i = D->specific_attr_begin<AnnotateAttr>(),
3245 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3246 if ((*i)->getAnnotation() == SE->getString())
3247 return;
3248 }
Michael Han51d8c522013-01-24 16:46:58 +00003249
3250 D->addAttr(::new (S.Context)
3251 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3252 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003253}
3254
Chandler Carruth1b03c872011-07-02 00:01:44 +00003255static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003256 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003257 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003258 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003259 return;
3260 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003261
Sean Huntbbd37c62009-11-21 08:43:09 +00003262 //FIXME: The C++0x version of this attribute has more limited applicabilty
3263 // than GNU's, and should error out when it is used to specify a
3264 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003265
Chris Lattner545dd342008-06-28 23:36:30 +00003266 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003267 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00003268 true, 0, Attr.isDeclspecAttribute(),
3269 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003270 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003271 }
Mike Stumpbf916502009-07-24 19:02:52 +00003272
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003273 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
Michael Han51d8c522013-01-24 16:46:58 +00003274 Attr.isDeclspecAttribute(),
3275 Attr.getAttributeSpellingListIndex());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003276}
3277
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003278void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Michael Han51d8c522013-01-24 16:46:58 +00003279 bool isDeclSpec, unsigned SpellingListIndex) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003280 // FIXME: Handle pack-expansions here.
3281 if (DiagnoseUnexpandedParameterPack(E))
3282 return;
3283
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003284 if (E->isTypeDependent() || E->isValueDependent()) {
3285 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003286 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
Michael Han51d8c522013-01-24 16:46:58 +00003287 isDeclSpec, SpellingListIndex));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003288 return;
3289 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003290
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003291 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003292 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003293 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003294 ExprResult ICE
3295 = VerifyIntegerConstantExpression(E, &Alignment,
3296 diag::err_aligned_attribute_argument_not_int,
3297 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003298 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003299 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003300 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003301 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3302 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003303 return;
3304 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003305 if (isDeclSpec) {
3306 // We've already verified it's a power of 2, now let's make sure it's
3307 // 8192 or less.
3308 if (Alignment.getZExtValue() > 8192) {
3309 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3310 << E->getSourceRange();
3311 return;
3312 }
3313 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003314
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003315 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
Michael Han51d8c522013-01-24 16:46:58 +00003316 isDeclSpec, SpellingListIndex));
Sean Huntcf807c42010-08-18 23:23:40 +00003317}
3318
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003319void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3320 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003321 // FIXME: Cache the number on the Attr object if non-dependent?
3322 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003323 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3324 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003325 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003326}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003327
Chandler Carruthd309c812011-07-01 23:49:16 +00003328/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003329/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003330///
Mike Stumpbf916502009-07-24 19:02:52 +00003331/// Despite what would be logical, the mode attribute is a decl attribute, not a
3332/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3333/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003334static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003335 // This attribute isn't documented, but glibc uses it. It changes
3336 // the width of an int or unsigned int to the specified size.
3337
3338 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003339 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003340 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003341
Chris Lattnerfbf13472008-06-27 22:18:37 +00003342
3343 IdentifierInfo *Name = Attr.getParameterName();
3344 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003345 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003346 return;
3347 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003348
Chris Lattner5f9e2722011-07-23 10:55:15 +00003349 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003350
3351 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003352 if (Str.startswith("__") && Str.endswith("__"))
3353 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003354
3355 unsigned DestWidth = 0;
3356 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003357 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003358 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003359 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003360 switch (Str[0]) {
3361 case 'Q': DestWidth = 8; break;
3362 case 'H': DestWidth = 16; break;
3363 case 'S': DestWidth = 32; break;
3364 case 'D': DestWidth = 64; break;
3365 case 'X': DestWidth = 96; break;
3366 case 'T': DestWidth = 128; break;
3367 }
3368 if (Str[1] == 'F') {
3369 IntegerMode = false;
3370 } else if (Str[1] == 'C') {
3371 IntegerMode = false;
3372 ComplexMode = true;
3373 } else if (Str[1] != 'I') {
3374 DestWidth = 0;
3375 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003376 break;
3377 case 4:
3378 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3379 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003380 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003381 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003382 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003383 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003384 break;
3385 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003386 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003387 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003388 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003389 case 11:
3390 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003391 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003392 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003393 }
3394
3395 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003396 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003397 OldTy = TD->getUnderlyingType();
3398 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3399 OldTy = VD->getType();
3400 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003401 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003402 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003403 return;
3404 }
Eli Friedman73397492009-03-03 06:41:03 +00003405
John McCall183700f2009-09-21 23:43:11 +00003406 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003407 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3408 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003409 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003410 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3411 } else if (ComplexMode) {
3412 if (!OldTy->isComplexType())
3413 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3414 } else {
3415 if (!OldTy->isFloatingType())
3416 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3417 }
3418
Mike Stump390b4cc2009-05-16 07:39:55 +00003419 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3420 // and friends, at least with glibc.
3421 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3422 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003423 // FIXME: Make sure floating-point mappings are accurate
3424 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003425 QualType NewTy;
3426 switch (DestWidth) {
3427 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003428 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003429 return;
3430 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003431 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003432 return;
3433 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003434 if (!IntegerMode) {
3435 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3436 return;
3437 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003438 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003439 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003440 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003441 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003442 break;
3443 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003444 if (!IntegerMode) {
3445 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3446 return;
3447 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003448 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003449 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003450 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003451 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003452 break;
3453 case 32:
3454 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003455 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003456 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003457 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003458 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003459 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003460 break;
3461 case 64:
3462 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003463 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003464 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003465 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003466 NewTy = S.Context.LongTy;
3467 else
3468 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003469 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003470 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003471 NewTy = S.Context.UnsignedLongTy;
3472 else
3473 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003474 break;
Eli Friedman73397492009-03-03 06:41:03 +00003475 case 96:
3476 NewTy = S.Context.LongDoubleTy;
3477 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003478 case 128:
3479 if (!IntegerMode) {
3480 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3481 return;
3482 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003483 if (OldTy->isSignedIntegerType())
3484 NewTy = S.Context.Int128Ty;
3485 else
3486 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003487 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003488 }
3489
Eli Friedman73397492009-03-03 06:41:03 +00003490 if (ComplexMode) {
3491 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003492 }
3493
3494 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003495 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003496 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003497 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003498 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003499 cast<ValueDecl>(D)->setType(NewTy);
3500}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003501
Chandler Carruth1b03c872011-07-02 00:01:44 +00003502static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003503 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003504 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003505 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003506
Nick Lewycky78d1a102012-07-24 01:40:49 +00003507 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3508 if (!VD->hasGlobalStorage())
3509 S.Diag(Attr.getLoc(),
3510 diag::warn_attribute_requires_functions_or_static_globals)
3511 << Attr.getName();
3512 } else if (!isFunctionOrMethod(D)) {
3513 S.Diag(Attr.getLoc(),
3514 diag::warn_attribute_requires_functions_or_static_globals)
3515 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003516 return;
3517 }
Mike Stumpbf916502009-07-24 19:02:52 +00003518
Michael Han51d8c522013-01-24 16:46:58 +00003519 D->addAttr(::new (S.Context)
3520 NoDebugAttr(Attr.getRange(), S.Context,
3521 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003522}
3523
Chandler Carruth1b03c872011-07-02 00:01:44 +00003524static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003525 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003526 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003527 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003528
Mike Stumpbf916502009-07-24 19:02:52 +00003529
Chandler Carruth87c44602011-07-01 23:49:12 +00003530 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003532 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003533 return;
3534 }
Mike Stumpbf916502009-07-24 19:02:52 +00003535
Michael Han51d8c522013-01-24 16:46:58 +00003536 D->addAttr(::new (S.Context)
3537 NoInlineAttr(Attr.getRange(), S.Context,
3538 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003539}
3540
Chandler Carruth1b03c872011-07-02 00:01:44 +00003541static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3542 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003543 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003544 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003545 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003546
Chris Lattner7255a2d2010-06-22 00:03:40 +00003547
Chandler Carruth87c44602011-07-01 23:49:12 +00003548 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003550 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003551 return;
3552 }
3553
Michael Han51d8c522013-01-24 16:46:58 +00003554 D->addAttr(::new (S.Context)
3555 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3556 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003557}
3558
Chandler Carruth1b03c872011-07-02 00:01:44 +00003559static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003560 if (S.LangOpts.CUDA) {
3561 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003562 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003563 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3564 return;
3565 }
3566
Chandler Carruth87c44602011-07-01 23:49:12 +00003567 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003568 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003569 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003570 return;
3571 }
3572
Michael Han51d8c522013-01-24 16:46:58 +00003573 D->addAttr(::new (S.Context)
3574 CUDAConstantAttr(Attr.getRange(), S.Context,
3575 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003576 } else {
3577 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3578 }
3579}
3580
Chandler Carruth1b03c872011-07-02 00:01:44 +00003581static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003582 if (S.LangOpts.CUDA) {
3583 // check the attribute arguments.
3584 if (Attr.getNumArgs() != 0) {
3585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3586 return;
3587 }
3588
Chandler Carruth87c44602011-07-01 23:49:12 +00003589 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003591 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003592 return;
3593 }
3594
Michael Han51d8c522013-01-24 16:46:58 +00003595 D->addAttr(::new (S.Context)
3596 CUDADeviceAttr(Attr.getRange(), S.Context,
3597 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003598 } else {
3599 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3600 }
3601}
3602
Chandler Carruth1b03c872011-07-02 00:01:44 +00003603static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003604 if (S.LangOpts.CUDA) {
3605 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003606 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003607 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003608
Chandler Carruth87c44602011-07-01 23:49:12 +00003609 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003610 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003611 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003612 return;
3613 }
3614
Chandler Carruth87c44602011-07-01 23:49:12 +00003615 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003616 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003617 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003618 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3619 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3620 << FD->getType()
3621 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3622 "void");
3623 } else {
3624 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3625 << FD->getType();
3626 }
3627 return;
3628 }
3629
Michael Han51d8c522013-01-24 16:46:58 +00003630 D->addAttr(::new (S.Context)
3631 CUDAGlobalAttr(Attr.getRange(), S.Context,
3632 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003633 } else {
3634 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3635 }
3636}
3637
Chandler Carruth1b03c872011-07-02 00:01:44 +00003638static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003639 if (S.LangOpts.CUDA) {
3640 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003641 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003642 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003643
Peter Collingbourneced76712010-12-01 03:15:31 +00003644
Chandler Carruth87c44602011-07-01 23:49:12 +00003645 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003646 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003647 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003648 return;
3649 }
3650
Michael Han51d8c522013-01-24 16:46:58 +00003651 D->addAttr(::new (S.Context)
3652 CUDAHostAttr(Attr.getRange(), S.Context,
3653 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003654 } else {
3655 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3656 }
3657}
3658
Chandler Carruth1b03c872011-07-02 00:01:44 +00003659static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003660 if (S.LangOpts.CUDA) {
3661 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003662 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003663 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003664
Chandler Carruth87c44602011-07-01 23:49:12 +00003665 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003667 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003668 return;
3669 }
3670
Michael Han51d8c522013-01-24 16:46:58 +00003671 D->addAttr(::new (S.Context)
3672 CUDASharedAttr(Attr.getRange(), S.Context,
3673 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003674 } else {
3675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3676 }
3677}
3678
Chandler Carruth1b03c872011-07-02 00:01:44 +00003679static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003680 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003681 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003682 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003683
Chandler Carruth87c44602011-07-01 23:49:12 +00003684 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003685 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003686 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003687 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003688 return;
3689 }
Mike Stumpbf916502009-07-24 19:02:52 +00003690
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003691 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003692 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003693 return;
3694 }
Mike Stumpbf916502009-07-24 19:02:52 +00003695
Michael Han51d8c522013-01-24 16:46:58 +00003696 D->addAttr(::new (S.Context)
3697 GNUInlineAttr(Attr.getRange(), S.Context,
3698 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003699}
3700
Chandler Carruth1b03c872011-07-02 00:01:44 +00003701static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003702 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003703
Aaron Ballmanfff32482012-12-09 17:45:41 +00003704 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003705 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003706 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3707 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003708 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003709 return;
3710
Chandler Carruth87c44602011-07-01 23:49:12 +00003711 if (!isa<ObjCMethodDecl>(D)) {
3712 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3713 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003714 return;
3715 }
3716
Chandler Carruth87c44602011-07-01 23:49:12 +00003717 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003718 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003719 D->addAttr(::new (S.Context)
3720 FastCallAttr(Attr.getRange(), S.Context,
3721 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003722 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003723 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003724 D->addAttr(::new (S.Context)
3725 StdCallAttr(Attr.getRange(), S.Context,
3726 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003727 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003728 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003729 D->addAttr(::new (S.Context)
3730 ThisCallAttr(Attr.getRange(), S.Context,
3731 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003732 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003733 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003734 D->addAttr(::new (S.Context)
3735 CDeclAttr(Attr.getRange(), S.Context,
3736 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003737 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003738 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003739 D->addAttr(::new (S.Context)
3740 PascalAttr(Attr.getRange(), S.Context,
3741 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003742 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003743 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003744 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003745 switch (CC) {
3746 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003747 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003748 break;
3749 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003750 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003751 break;
3752 default:
3753 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003754 }
3755
Michael Han51d8c522013-01-24 16:46:58 +00003756 D->addAttr(::new (S.Context)
3757 PcsAttr(Attr.getRange(), S.Context, PCS,
3758 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003759 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003760 }
Derek Schuff263366f2012-10-16 22:30:41 +00003761 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003762 D->addAttr(::new (S.Context)
3763 PnaclCallAttr(Attr.getRange(), S.Context,
3764 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003765 return;
Guy Benyei38980082012-12-25 08:53:55 +00003766 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003767 D->addAttr(::new (S.Context)
3768 IntelOclBiccAttr(Attr.getRange(), S.Context,
3769 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003770 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003771
Abramo Bagnarae215f722010-04-30 13:10:51 +00003772 default:
3773 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003774 }
3775}
3776
Chandler Carruth1b03c872011-07-02 00:01:44 +00003777static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003778 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003779 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003780}
3781
Aaron Ballmanfff32482012-12-09 17:45:41 +00003782bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3783 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003784 if (attr.isInvalid())
3785 return true;
3786
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003787 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3788 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3789 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003790 attr.setInvalid();
3791 return true;
3792 }
3793
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003794 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3795 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003796 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003797 case AttributeList::AT_CDecl: CC = CC_C; break;
3798 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3799 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3800 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3801 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3802 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003803 Expr *Arg = attr.getArg(0);
3804 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003805 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003806 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3807 << "pcs" << 1;
3808 attr.setInvalid();
3809 return true;
3810 }
3811
Chris Lattner5f9e2722011-07-23 10:55:15 +00003812 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003813 if (StrRef == "aapcs") {
3814 CC = CC_AAPCS;
3815 break;
3816 } else if (StrRef == "aapcs-vfp") {
3817 CC = CC_AAPCS_VFP;
3818 break;
3819 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003820
3821 attr.setInvalid();
3822 Diag(attr.getLoc(), diag::err_invalid_pcs);
3823 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003824 }
Derek Schuff263366f2012-10-16 22:30:41 +00003825 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003826 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003827 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003828 }
3829
Aaron Ballman82bfa192012-10-02 14:26:08 +00003830 const TargetInfo &TI = Context.getTargetInfo();
3831 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3832 if (A == TargetInfo::CCCR_Warning) {
3833 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003834
3835 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3836 if (FD)
3837 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3838 TargetInfo::CCMT_NonMember;
3839 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003840 }
3841
John McCall711c52b2011-01-05 12:14:39 +00003842 return false;
3843}
3844
Chandler Carruth1b03c872011-07-02 00:01:44 +00003845static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003846 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003847
3848 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003849 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003850 return;
3851
Chandler Carruth87c44602011-07-01 23:49:12 +00003852 if (!isa<ObjCMethodDecl>(D)) {
3853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3854 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003855 return;
3856 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003857
Michael Han51d8c522013-01-24 16:46:58 +00003858 D->addAttr(::new (S.Context)
3859 RegparmAttr(Attr.getRange(), S.Context, numParams,
3860 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003861}
3862
3863/// Checks a regparm attribute, returning true if it is ill-formed and
3864/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003865bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3866 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003867 return true;
3868
Chandler Carruth87c44602011-07-01 23:49:12 +00003869 if (Attr.getNumArgs() != 1) {
3870 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3871 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003872 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003873 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003874
Chandler Carruth87c44602011-07-01 23:49:12 +00003875 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003876 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003877 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003878 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003879 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003880 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003881 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003882 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003883 }
3884
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003885 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003886 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003887 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003888 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003889 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003890 }
3891
John McCall711c52b2011-01-05 12:14:39 +00003892 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003893 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003894 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003895 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003896 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003897 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003898 }
3899
John McCall711c52b2011-01-05 12:14:39 +00003900 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003901}
3902
Chandler Carruth1b03c872011-07-02 00:01:44 +00003903static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003904 if (S.LangOpts.CUDA) {
3905 // check the attribute arguments.
3906 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003907 // FIXME: 0 is not okay.
3908 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003909 return;
3910 }
3911
Chandler Carruth87c44602011-07-01 23:49:12 +00003912 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003913 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003914 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003915 return;
3916 }
3917
3918 Expr *MaxThreadsExpr = Attr.getArg(0);
3919 llvm::APSInt MaxThreads(32);
3920 if (MaxThreadsExpr->isTypeDependent() ||
3921 MaxThreadsExpr->isValueDependent() ||
3922 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3923 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3924 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3925 return;
3926 }
3927
3928 llvm::APSInt MinBlocks(32);
3929 if (Attr.getNumArgs() > 1) {
3930 Expr *MinBlocksExpr = Attr.getArg(1);
3931 if (MinBlocksExpr->isTypeDependent() ||
3932 MinBlocksExpr->isValueDependent() ||
3933 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3934 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3935 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3936 return;
3937 }
3938 }
3939
Michael Han51d8c522013-01-24 16:46:58 +00003940 D->addAttr(::new (S.Context)
3941 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3942 MaxThreads.getZExtValue(),
3943 MinBlocks.getZExtValue(),
3944 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00003945 } else {
3946 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3947 }
3948}
3949
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003950static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3951 const AttributeList &Attr) {
3952 StringRef AttrName = Attr.getName()->getName();
3953 if (!Attr.getParameterName()) {
3954 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3955 << Attr.getName() << /* arg num = */ 1;
3956 return;
3957 }
3958
3959 if (Attr.getNumArgs() != 2) {
3960 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3961 << /* required args = */ 3;
3962 return;
3963 }
3964
3965 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3966
3967 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3968 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3969 << Attr.getName() << ExpectedFunctionOrMethod;
3970 return;
3971 }
3972
3973 uint64_t ArgumentIdx;
3974 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3975 Attr.getLoc(), 2,
3976 Attr.getArg(0), ArgumentIdx))
3977 return;
3978
3979 uint64_t TypeTagIdx;
3980 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3981 Attr.getLoc(), 3,
3982 Attr.getArg(1), TypeTagIdx))
3983 return;
3984
3985 bool IsPointer = (AttrName == "pointer_with_type_tag");
3986 if (IsPointer) {
3987 // Ensure that buffer has a pointer type.
3988 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3989 if (!BufferTy->isPointerType()) {
3990 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3991 << AttrName;
3992 }
3993 }
3994
Michael Han51d8c522013-01-24 16:46:58 +00003995 D->addAttr(::new (S.Context)
3996 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3997 ArgumentIdx, TypeTagIdx, IsPointer,
3998 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003999}
4000
4001static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4002 const AttributeList &Attr) {
4003 IdentifierInfo *PointerKind = Attr.getParameterName();
4004 if (!PointerKind) {
4005 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4006 << "type_tag_for_datatype" << 1;
4007 return;
4008 }
4009
4010 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4011
Michael Han51d8c522013-01-24 16:46:58 +00004012 D->addAttr(::new (S.Context)
4013 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4014 MatchingCType,
4015 Attr.getLayoutCompatible(),
4016 Attr.getMustBeNull(),
4017 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004018}
4019
Chris Lattner0744e5f2008-06-29 00:23:49 +00004020//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004021// Checker-specific attribute handlers.
4022//===----------------------------------------------------------------------===//
4023
John McCallc7ad3812011-01-25 03:31:58 +00004024static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004025 return type->isDependentType() ||
4026 type->isObjCObjectPointerType() ||
4027 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004028}
4029static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004030 return type->isDependentType() ||
4031 type->isPointerType() ||
4032 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004033}
4034
Chandler Carruth1b03c872011-07-02 00:01:44 +00004035static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004036 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004037 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004038 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004039 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004040 return;
4041 }
4042
4043 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004044 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004045 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4046 cf = false;
4047 } else {
4048 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4049 cf = true;
4050 }
4051
4052 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004053 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004054 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004055 return;
4056 }
4057
4058 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004059 param->addAttr(::new (S.Context)
4060 CFConsumedAttr(Attr.getRange(), S.Context,
4061 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004062 else
Michael Han51d8c522013-01-24 16:46:58 +00004063 param->addAttr(::new (S.Context)
4064 NSConsumedAttr(Attr.getRange(), S.Context,
4065 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004066}
4067
Chandler Carruth1b03c872011-07-02 00:01:44 +00004068static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4069 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004070 if (!isa<ObjCMethodDecl>(D)) {
4071 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004072 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004073 return;
4074 }
4075
Michael Han51d8c522013-01-24 16:46:58 +00004076 D->addAttr(::new (S.Context)
4077 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4078 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004079}
4080
Chandler Carruth1b03c872011-07-02 00:01:44 +00004081static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4082 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004083
John McCallc7ad3812011-01-25 03:31:58 +00004084 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004085
Chandler Carruth87c44602011-07-01 23:49:12 +00004086 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004087 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004088 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004089 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004090 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004091 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4092 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004093 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004094 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004095 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004096 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004097 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004098 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004099 return;
4100 }
Mike Stumpbf916502009-07-24 19:02:52 +00004101
John McCallc7ad3812011-01-25 03:31:58 +00004102 bool typeOK;
4103 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004104 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004105 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004106 case AttributeList::AT_NSReturnsAutoreleased:
4107 case AttributeList::AT_NSReturnsRetained:
4108 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004109 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4110 cf = false;
4111 break;
4112
Sean Hunt8e083e72012-06-19 23:57:03 +00004113 case AttributeList::AT_CFReturnsRetained:
4114 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004115 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4116 cf = true;
4117 break;
4118 }
4119
4120 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004121 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004122 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004123 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004124 }
Mike Stumpbf916502009-07-24 19:02:52 +00004125
Chandler Carruth87c44602011-07-01 23:49:12 +00004126 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004127 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004128 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004129 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004130 D->addAttr(::new (S.Context)
4131 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4132 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004133 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004134 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004135 D->addAttr(::new (S.Context)
4136 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4137 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004138 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004139 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004140 D->addAttr(::new (S.Context)
4141 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4142 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004143 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004144 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004145 D->addAttr(::new (S.Context)
4146 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4147 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004148 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004149 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004150 D->addAttr(::new (S.Context)
4151 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4152 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004153 return;
4154 };
4155}
4156
John McCalldc7c5ad2011-07-22 08:53:00 +00004157static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4158 const AttributeList &attr) {
4159 SourceLocation loc = attr.getLoc();
4160
4161 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4162
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004163 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004164 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004165 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004166 return;
4167 }
4168
4169 // Check that the method returns a normal pointer.
4170 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004171
4172 if (!resultType->isReferenceType() &&
4173 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004174 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4175 << SourceRange(loc)
4176 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4177
4178 // Drop the attribute.
4179 return;
4180 }
4181
Michael Han51d8c522013-01-24 16:46:58 +00004182 method->addAttr(::new (S.Context)
4183 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4184 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004185}
4186
Fariborz Jahanian84101132012-09-07 23:46:23 +00004187static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4188 const AttributeList &attr) {
4189 SourceLocation loc = attr.getLoc();
4190 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4191
4192 if (!method) {
4193 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4194 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4195 return;
4196 }
4197 DeclContext *DC = method->getDeclContext();
4198 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4199 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4200 << attr.getName() << 0;
4201 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4202 return;
4203 }
4204 if (method->getMethodFamily() == OMF_dealloc) {
4205 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4206 << attr.getName() << 1;
4207 return;
4208 }
4209
Michael Han51d8c522013-01-24 16:46:58 +00004210 method->addAttr(::new (S.Context)
4211 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4212 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004213}
4214
John McCall8dfac0b2011-09-30 05:12:12 +00004215/// Handle cf_audited_transfer and cf_unknown_transfer.
4216static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4217 if (!isa<FunctionDecl>(D)) {
4218 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004219 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004220 return;
4221 }
4222
Sean Hunt8e083e72012-06-19 23:57:03 +00004223 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004224
4225 // Check whether there's a conflicting attribute already present.
4226 Attr *Existing;
4227 if (IsAudited) {
4228 Existing = D->getAttr<CFUnknownTransferAttr>();
4229 } else {
4230 Existing = D->getAttr<CFAuditedTransferAttr>();
4231 }
4232 if (Existing) {
4233 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4234 << A.getName()
4235 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4236 << A.getRange() << Existing->getRange();
4237 return;
4238 }
4239
4240 // All clear; add the attribute.
4241 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004242 D->addAttr(::new (S.Context)
4243 CFAuditedTransferAttr(A.getRange(), S.Context,
4244 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004245 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004246 D->addAttr(::new (S.Context)
4247 CFUnknownTransferAttr(A.getRange(), S.Context,
4248 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004249 }
4250}
4251
John McCallfe98da02011-09-29 07:17:38 +00004252static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4253 const AttributeList &Attr) {
4254 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4255 if (!RD || RD->isUnion()) {
4256 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004257 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004258 }
4259
4260 IdentifierInfo *ParmName = Attr.getParameterName();
4261
4262 // In Objective-C, verify that the type names an Objective-C type.
4263 // We don't want to check this outside of ObjC because people sometimes
4264 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004265 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004266 // Check for an existing type with this name.
4267 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4268 Sema::LookupOrdinaryName);
4269 if (S.LookupName(R, Sc)) {
4270 NamedDecl *Target = R.getFoundDecl();
4271 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4272 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4273 S.Diag(Target->getLocStart(), diag::note_declared_at);
4274 }
4275 }
4276 }
4277
Michael Han51d8c522013-01-24 16:46:58 +00004278 D->addAttr(::new (S.Context)
4279 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4280 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004281}
4282
Chandler Carruth1b03c872011-07-02 00:01:44 +00004283static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4284 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004285 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004286
Chandler Carruth87c44602011-07-01 23:49:12 +00004287 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004288 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004289}
4290
Chandler Carruth1b03c872011-07-02 00:01:44 +00004291static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4292 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004293 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004294 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004295 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004296 return;
4297 }
4298
Chandler Carruth87c44602011-07-01 23:49:12 +00004299 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004300 QualType type = vd->getType();
4301
4302 if (!type->isDependentType() &&
4303 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004304 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004305 << type;
4306 return;
4307 }
4308
4309 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4310
4311 // If we have no lifetime yet, check the lifetime we're presumably
4312 // going to infer.
4313 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4314 lifetime = type->getObjCARCImplicitLifetime();
4315
4316 switch (lifetime) {
4317 case Qualifiers::OCL_None:
4318 assert(type->isDependentType() &&
4319 "didn't infer lifetime for non-dependent type?");
4320 break;
4321
4322 case Qualifiers::OCL_Weak: // meaningful
4323 case Qualifiers::OCL_Strong: // meaningful
4324 break;
4325
4326 case Qualifiers::OCL_ExplicitNone:
4327 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004328 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004329 << (lifetime == Qualifiers::OCL_Autoreleasing);
4330 break;
4331 }
4332
Chandler Carruth87c44602011-07-01 23:49:12 +00004333 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004334 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4335 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004336}
4337
Francois Pichet11542142010-12-19 06:50:37 +00004338//===----------------------------------------------------------------------===//
4339// Microsoft specific attribute handlers.
4340//===----------------------------------------------------------------------===//
4341
Chandler Carruth1b03c872011-07-02 00:01:44 +00004342static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004343 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004344 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004345 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004346 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004347
Francois Pichet11542142010-12-19 06:50:37 +00004348 Expr *Arg = Attr.getArg(0);
4349 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004350 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4352 << "uuid" << 1;
4353 return;
4354 }
4355
Chris Lattner5f9e2722011-07-23 10:55:15 +00004356 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004357
4358 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4359 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004360
Francois Pichetd3d3be92010-12-20 01:41:49 +00004361 // Validate GUID length.
4362 if (IsCurly && StrRef.size() != 38) {
4363 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4364 return;
4365 }
4366 if (!IsCurly && StrRef.size() != 36) {
4367 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4368 return;
4369 }
4370
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004371 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004372 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004373 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004374 if (IsCurly) // Skip the optional '{'
4375 ++I;
4376
4377 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004378 if (i == 8 || i == 13 || i == 18 || i == 23) {
4379 if (*I != '-') {
4380 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4381 return;
4382 }
4383 } else if (!isxdigit(*I)) {
4384 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4385 return;
4386 }
4387 I++;
4388 }
Francois Pichet11542142010-12-19 06:50:37 +00004389
Michael Han51d8c522013-01-24 16:46:58 +00004390 D->addAttr(::new (S.Context)
4391 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4392 Attr.getAttributeSpellingListIndex()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004393 } else
Francois Pichet11542142010-12-19 06:50:37 +00004394 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004395}
4396
John McCallc052dbb2012-05-22 21:28:12 +00004397static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004398 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004399 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004400 return;
4401 }
4402
4403 AttributeList::Kind Kind = Attr.getKind();
4404 if (Kind == AttributeList::AT_SingleInheritance)
4405 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004406 ::new (S.Context)
4407 SingleInheritanceAttr(Attr.getRange(), S.Context,
4408 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004409 else if (Kind == AttributeList::AT_MultipleInheritance)
4410 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004411 ::new (S.Context)
4412 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4413 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004414 else if (Kind == AttributeList::AT_VirtualInheritance)
4415 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004416 ::new (S.Context)
4417 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4418 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004419}
4420
4421static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4422 if (S.LangOpts.MicrosoftExt) {
4423 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004424 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004425 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004426 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4427 Attr.getAttributeSpellingListIndex()));
Sean Hunt8e083e72012-06-19 23:57:03 +00004428 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004429 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004430 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4431 Attr.getAttributeSpellingListIndex()));
Sean Hunt8e083e72012-06-19 23:57:03 +00004432 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004433 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004434 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4435 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004436 } else
4437 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4438}
4439
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004440static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4441 if (S.LangOpts.MicrosoftExt)
Michael Han51d8c522013-01-24 16:46:58 +00004442 D->addAttr(::new (S.Context)
4443 ForceInlineAttr(Attr.getRange(), S.Context,
4444 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004445 else
4446 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4447}
4448
Ted Kremenekb71368d2009-05-09 02:44:38 +00004449//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004450// Top Level Sema Entry Points
4451//===----------------------------------------------------------------------===//
4452
Chandler Carruth1b03c872011-07-02 00:01:44 +00004453static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4454 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004455 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004456 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4457 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4458 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004459 default:
4460 break;
4461 }
4462}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004463
Chandler Carruth1b03c872011-07-02 00:01:44 +00004464static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4465 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004466 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004467 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4468 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4469 case AttributeList::AT_IBOutletCollection:
4470 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004471 case AttributeList::AT_AddressSpace:
4472 case AttributeList::AT_OpenCLImageAccess:
4473 case AttributeList::AT_ObjCGC:
4474 case AttributeList::AT_VectorSize:
4475 case AttributeList::AT_NeonVectorType:
4476 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004477 // Ignore these, these are type attributes, handled by
4478 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004479 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004480 case AttributeList::AT_CUDADevice:
4481 case AttributeList::AT_CUDAHost:
4482 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004483 // Ignore, this is a non-inheritable attribute, handled
4484 // by ProcessNonInheritableDeclAttr.
4485 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004486 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4487 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4488 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4489 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004490 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004491 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004492 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004493 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004494 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4495 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4496 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004497 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004498 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4499 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4500 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004501 case AttributeList::AT_CXX11NoReturn:
4502 handleCXX11NoReturnAttr(S, D, Attr);
4503 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004504 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004505 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4506 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004507 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4508 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004509 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004510 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004511 case AttributeList::AT_MinSize:
4512 handleMinSizeAttr(S, D, Attr);
4513 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004514 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4515 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4516 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4517 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4518 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004519 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004520 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004521 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4522 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4523 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4524 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4525 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004526 case AttributeList::AT_ownership_returns:
4527 case AttributeList::AT_ownership_takes:
4528 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004529 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004530 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4531 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4532 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4533 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4534 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4535 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4536 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004537
Sean Hunt8e083e72012-06-19 23:57:03 +00004538 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004539 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004540 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004541 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004542
Sean Hunt8e083e72012-06-19 23:57:03 +00004543 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004544 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4545
Fariborz Jahanian84101132012-09-07 23:46:23 +00004546 case AttributeList::AT_ObjCRequiresSuper:
4547 handleObjCRequiresSuperAttr(S, D, Attr); break;
4548
Sean Hunt8e083e72012-06-19 23:57:03 +00004549 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004550 handleNSBridgedAttr(S, scope, D, Attr); break;
4551
Sean Hunt8e083e72012-06-19 23:57:03 +00004552 case AttributeList::AT_CFAuditedTransfer:
4553 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004554 handleCFTransferAttr(S, D, Attr); break;
4555
Ted Kremenekb71368d2009-05-09 02:44:38 +00004556 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004557 case AttributeList::AT_CFConsumed:
4558 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4559 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004560 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004561
Sean Hunt8e083e72012-06-19 23:57:03 +00004562 case AttributeList::AT_NSReturnsAutoreleased:
4563 case AttributeList::AT_NSReturnsNotRetained:
4564 case AttributeList::AT_CFReturnsNotRetained:
4565 case AttributeList::AT_NSReturnsRetained:
4566 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004567 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004568
Tanya Lattner0df579e2012-07-09 22:06:01 +00004569 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004570 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004571 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004572
Sean Hunt8e083e72012-06-19 23:57:03 +00004573 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004574 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004575
Sean Hunt8e083e72012-06-19 23:57:03 +00004576 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4577 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4578 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004579 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4580 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004581 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004582 handleArcWeakrefUnavailableAttr (S, D, Attr);
4583 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004584 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004585 handleObjCRootClassAttr(S, D, Attr);
4586 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004587 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004588 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004589 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004590 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4591 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004592 handleReturnsTwiceAttr(S, D, Attr);
4593 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004594 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4595 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4596 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004597 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004598 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4599 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4600 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4601 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004602 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004603 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004604 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004605 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004606 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004607 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004608 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004609 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004610 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4611 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4612 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4613 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4614 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4615 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4616 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4617 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4618 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004619 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004620 // Just ignore
4621 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004622 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004623 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004624 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004625 case AttributeList::AT_StdCall:
4626 case AttributeList::AT_CDecl:
4627 case AttributeList::AT_FastCall:
4628 case AttributeList::AT_ThisCall:
4629 case AttributeList::AT_Pascal:
4630 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004631 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004632 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004633 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004634 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004635 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004636 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004637 break;
John McCallc052dbb2012-05-22 21:28:12 +00004638
4639 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004640 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004641 handleMsStructAttr(S, D, Attr);
4642 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004643 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004644 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004645 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004646 case AttributeList::AT_SingleInheritance:
4647 case AttributeList::AT_MultipleInheritance:
4648 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004649 handleInheritanceAttr(S, D, Attr);
4650 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004651 case AttributeList::AT_Win64:
4652 case AttributeList::AT_Ptr32:
4653 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004654 handlePortabilityAttr(S, D, Attr);
4655 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004656 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004657 handleForceInlineAttr(S, D, Attr);
4658 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004659
4660 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004661 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004662 handleGuardedVarAttr(S, D, Attr);
4663 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004664 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004665 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004666 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004667 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004668 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004669 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004670 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004671 handleNoAddressSafetyAttr(S, D, Attr);
4672 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004673 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004674 handleNoThreadSafetyAttr(S, D, Attr);
4675 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004676 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004677 handleLockableAttr(S, D, Attr);
4678 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004679 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004680 handleGuardedByAttr(S, D, Attr);
4681 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004683 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004684 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004685 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004686 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004687 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004688 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004689 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004690 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004691 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004692 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004693 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004694 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004695 handleLockReturnedAttr(S, D, Attr);
4696 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004697 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004698 handleLocksExcludedAttr(S, D, Attr);
4699 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004700 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004701 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004702 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004703 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004704 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004705 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004706 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004707 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004708 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004709 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004710 handleUnlockFunAttr(S, D, Attr);
4711 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004712 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004713 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004714 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004715 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004716 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004717 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004718
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004719 // Type safety attributes.
4720 case AttributeList::AT_ArgumentWithTypeTag:
4721 handleArgumentWithTypeTagAttr(S, D, Attr);
4722 break;
4723 case AttributeList::AT_TypeTagForDatatype:
4724 handleTypeTagForDatatypeAttr(S, D, Attr);
4725 break;
4726
Chris Lattner803d0802008-06-29 00:43:07 +00004727 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004728 // Ask target about the attribute.
4729 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4730 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004731 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4732 diag::warn_unhandled_ms_attribute_ignored :
4733 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004734 break;
4735 }
4736}
4737
Peter Collingbourne60700392011-01-21 02:08:45 +00004738/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4739/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00004740/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004741static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4742 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00004743 bool NonInheritable, bool Inheritable,
4744 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004745 if (Attr.isInvalid())
4746 return;
4747
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004748 // Type attributes are still treated as declaration attributes by
4749 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4750 // want to process them, however, because we will simply warn about ignoring
4751 // them. So instead, we will bail out early.
4752 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004753 return;
4754
Richard Smithcd8ab512013-01-17 01:30:42 +00004755 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4756 // instead.
4757 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4758 return;
4759
Peter Collingbourne60700392011-01-21 02:08:45 +00004760 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004761 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004762
4763 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004764 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004765}
4766
Chris Lattner803d0802008-06-29 00:43:07 +00004767/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4768/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004769void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004770 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004771 bool NonInheritable, bool Inheritable,
4772 bool IncludeCXX11Attributes) {
4773 for (const AttributeList* l = AttrList; l; l = l->getNext())
4774 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
4775 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004776
4777 // GCC accepts
4778 // static int a9 __attribute__((weakref));
4779 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004780 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004781 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004782 cast<NamedDecl>(D)->getNameAsString();
4783 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004784 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004785 }
4786}
4787
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004788// Annotation attributes are the only attributes allowed after an access
4789// specifier.
4790bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4791 const AttributeList *AttrList) {
4792 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004793 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004794 handleAnnotateAttr(*this, ASDecl, *l);
4795 } else {
4796 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4797 return true;
4798 }
4799 }
4800
4801 return false;
4802}
4803
John McCalle82247a2011-10-01 05:17:03 +00004804/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4805/// contains any decl attributes that we should warn about.
4806static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4807 for ( ; A; A = A->getNext()) {
4808 // Only warn if the attribute is an unignored, non-type attribute.
4809 if (A->isUsedAsTypeAttr()) continue;
4810 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4811
4812 if (A->getKind() == AttributeList::UnknownAttribute) {
4813 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4814 << A->getName() << A->getRange();
4815 } else {
4816 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4817 << A->getName() << A->getRange();
4818 }
4819 }
4820}
4821
4822/// checkUnusedDeclAttributes - Given a declarator which is not being
4823/// used to build a declaration, complain about any decl attributes
4824/// which might be lying around on it.
4825void Sema::checkUnusedDeclAttributes(Declarator &D) {
4826 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4827 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4828 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4829 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4830}
4831
Ryan Flynne25ff832009-07-30 03:15:39 +00004832/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004833/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004834NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4835 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004836 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004837 NamedDecl *NewD = 0;
4838 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004839 FunctionDecl *NewFD;
4840 // FIXME: Missing call to CheckFunctionDeclaration().
4841 // FIXME: Mangling?
4842 // FIXME: Is the qualifier info correct?
4843 // FIXME: Is the DeclContext correct?
4844 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4845 Loc, Loc, DeclarationName(II),
4846 FD->getType(), FD->getTypeSourceInfo(),
4847 SC_None, SC_None,
4848 false/*isInlineSpecified*/,
4849 FD->hasPrototype(),
4850 false/*isConstexprSpecified*/);
4851 NewD = NewFD;
4852
4853 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004854 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004855
4856 // Fake up parameter variables; they are declared as if this were
4857 // a typedef.
4858 QualType FDTy = FD->getType();
4859 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4860 SmallVector<ParmVarDecl*, 16> Params;
4861 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4862 AE = FT->arg_type_end(); AI != AE; ++AI) {
4863 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4864 Param->setScopeInfo(0, Params.size());
4865 Params.push_back(Param);
4866 }
David Blaikie4278c652011-09-21 18:16:56 +00004867 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004868 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004869 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4870 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004871 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004872 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004873 VD->getStorageClass(),
4874 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004875 if (VD->getQualifier()) {
4876 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004877 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004878 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004879 }
4880 return NewD;
4881}
4882
James Dennett1dfbd922012-06-14 21:40:34 +00004883/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004884/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004885void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004886 if (W.getUsed()) return; // only do this once
4887 W.setUsed(true);
4888 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4889 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004890 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004891 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4892 NDId->getName()));
4893 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004894 WeakTopLevelDecl.push_back(NewD);
4895 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4896 // to insert Decl at TU scope, sorry.
4897 DeclContext *SavedContext = CurContext;
4898 CurContext = Context.getTranslationUnitDecl();
4899 PushOnScopeChains(NewD, S);
4900 CurContext = SavedContext;
4901 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004902 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004903 }
4904}
4905
Chris Lattner0744e5f2008-06-29 00:23:49 +00004906/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4907/// it, apply them to D. This is a bit tricky because PD can have attributes
4908/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004909void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4910 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004911 // It's valid to "forward-declare" #pragma weak, in which case we
4912 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004913 if (Inheritable) {
4914 LoadExternalWeakUndeclaredIdentifiers();
4915 if (!WeakUndeclaredIdentifiers.empty()) {
4916 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4917 if (IdentifierInfo *Id = ND->getIdentifier()) {
4918 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4919 = WeakUndeclaredIdentifiers.find(Id);
4920 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4921 WeakInfo W = I->second;
4922 DeclApplyPragmaWeak(S, ND, W);
4923 WeakUndeclaredIdentifiers[Id] = W;
4924 }
John McCalld4aff0e2010-10-27 00:59:00 +00004925 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004926 }
4927 }
4928 }
4929
Chris Lattner0744e5f2008-06-29 00:23:49 +00004930 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004931 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004932 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004933
Chris Lattner0744e5f2008-06-29 00:23:49 +00004934 // Walk the declarator structure, applying decl attributes that were in a type
4935 // position to the decl itself. This handles cases like:
4936 // int *__attr__(x)** D;
4937 // when X is a decl attribute.
4938 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4939 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00004940 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
4941 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00004942
Chris Lattner0744e5f2008-06-29 00:23:49 +00004943 // Finally, apply any attributes on the decl itself.
4944 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004945 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004946}
John McCall54abf7d2009-11-04 02:18:39 +00004947
John McCallf85e1932011-06-15 23:02:42 +00004948/// Is the given declaration allowed to use a forbidden type?
4949static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4950 // Private ivars are always okay. Unfortunately, people don't
4951 // always properly make their ivars private, even in system headers.
4952 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004953 // Function declarations in sys headers will be marked unavailable.
4954 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4955 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004956 return false;
4957
4958 // Require it to be declared in a system header.
4959 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4960}
4961
4962/// Handle a delayed forbidden-type diagnostic.
4963static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4964 Decl *decl) {
4965 if (decl && isForbiddenTypeAllowed(S, decl)) {
4966 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4967 "this system declaration uses an unsupported type"));
4968 return;
4969 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004970 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004971 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004972 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004973 // kind of forbidden type messages on unavailable functions.
4974 if (FD->hasAttr<UnavailableAttr>() &&
4975 diag.getForbiddenTypeDiagnostic() ==
4976 diag::err_arc_array_param_no_ownership) {
4977 diag.Triggered = true;
4978 return;
4979 }
4980 }
John McCallf85e1932011-06-15 23:02:42 +00004981
4982 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4983 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4984 diag.Triggered = true;
4985}
4986
John McCall92576642012-05-07 06:16:41 +00004987void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4988 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004989 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004990 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004991
John McCall92576642012-05-07 06:16:41 +00004992 // When delaying diagnostics to run in the context of a parsed
4993 // declaration, we only want to actually emit anything if parsing
4994 // succeeds.
4995 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004996
John McCall92576642012-05-07 06:16:41 +00004997 // We emit all the active diagnostics in this pool or any of its
4998 // parents. In general, we'll get one pool for the decl spec
4999 // and a child pool for each declarator; in a decl group like:
5000 // deprecated_typedef foo, *bar, baz();
5001 // only the declarator pops will be passed decls. This is correct;
5002 // we really do need to consider delayed diagnostics from the decl spec
5003 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005004 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005005 do {
John McCall13489672012-05-07 06:16:58 +00005006 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005007 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5008 // This const_cast is a bit lame. Really, Triggered should be mutable.
5009 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005010 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005011 continue;
5012
John McCalleee1d542011-02-14 07:13:47 +00005013 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005014 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005015 // Don't bother giving deprecation diagnostics if the decl is invalid.
5016 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005017 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005018 break;
5019
5020 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005021 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005022 break;
John McCallf85e1932011-06-15 23:02:42 +00005023
5024 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005025 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005026 break;
John McCall2f514482010-01-27 03:50:35 +00005027 }
5028 }
John McCall92576642012-05-07 06:16:41 +00005029 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005030}
5031
John McCall13489672012-05-07 06:16:58 +00005032/// Given a set of delayed diagnostics, re-emit them as if they had
5033/// been delayed in the current context instead of in the given pool.
5034/// Essentially, this just moves them to the current pool.
5035void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5036 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5037 assert(curPool && "re-emitting in undelayed context not supported");
5038 curPool->steal(pool);
5039}
5040
John McCall54abf7d2009-11-04 02:18:39 +00005041static bool isDeclDeprecated(Decl *D) {
5042 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005043 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005044 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005045 // A category implicitly has the availability of the interface.
5046 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5047 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005048 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5049 return false;
5050}
5051
Eli Friedmanc3b23082012-08-08 21:52:41 +00005052static void
5053DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5054 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005055 const ObjCInterfaceDecl *UnknownObjCClass,
5056 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005057 DeclarationName Name = D->getDeclName();
5058 if (!Message.empty()) {
5059 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5060 S.Diag(D->getLocation(),
5061 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5062 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005063 if (ObjCPropery)
5064 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5065 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005066 } else if (!UnknownObjCClass) {
5067 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5068 S.Diag(D->getLocation(),
5069 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5070 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005071 if (ObjCPropery)
5072 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5073 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005074 } else {
5075 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5076 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5077 }
5078}
5079
John McCall9c3087b2010-08-26 02:13:20 +00005080void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005081 Decl *Ctx) {
5082 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005083 return;
5084
John McCall2f514482010-01-27 03:50:35 +00005085 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005086 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5087 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005088 DD.getUnknownObjCClass(),
5089 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005090}
5091
Chris Lattner5f9e2722011-07-23 10:55:15 +00005092void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005093 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005094 const ObjCInterfaceDecl *UnknownObjCClass,
5095 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005096 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005097 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005098 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5099 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005100 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005101 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005102 return;
5103 }
5104
5105 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005106 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005107 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005108 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005109}