blob: 225ee24d0fce3e81fc503465bf7bd87b4e8ca523 [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 Handc691572012-07-23 18:48:41 +0000508 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
509}
510
Michael Hanf1aae3b2012-08-03 17:40:43 +0000511static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000512 const AttributeList &Attr) {
513 if (!checkGuardedVarAttrCommon(S, D, Attr))
514 return;
515
516 if (!threadSafetyCheckIsPointer(S, D, Attr))
517 return;
518
519 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
520}
521
Michael Hanf1aae3b2012-08-03 17:40:43 +0000522static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
523 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000524 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000525 assert(!Attr.isInvalid());
526
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000527 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000528 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000529
530 // D must be either a member field or global (potentially shared) variable.
531 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000532 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
533 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000534 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000535 }
536
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000537 SmallVector<Expr*, 1> Args;
538 // check that all arguments are lockable objects
539 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
540 unsigned Size = Args.size();
541 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000542 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000543
Michael Handc691572012-07-23 18:48:41 +0000544 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000545
Michael Handc691572012-07-23 18:48:41 +0000546 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000547}
548
Michael Handc691572012-07-23 18:48:41 +0000549static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
550 Expr *Arg = 0;
551 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
552 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000553
Michael Handc691572012-07-23 18:48:41 +0000554 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
555}
556
Michael Hanf1aae3b2012-08-03 17:40:43 +0000557static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000558 const AttributeList &Attr) {
559 Expr *Arg = 0;
560 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
561 return;
562
563 if (!threadSafetyCheckIsPointer(S, D, Attr))
564 return;
565
566 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
567 S.Context, Arg));
568}
569
Michael Hanf1aae3b2012-08-03 17:40:43 +0000570static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000571 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000572 assert(!Attr.isInvalid());
573
574 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000575 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000576
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000577 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000578 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000579 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
580 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000581 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000582 }
583
Michael Handc691572012-07-23 18:48:41 +0000584 return true;
585}
586
587static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
588 if (!checkLockableAttrCommon(S, D, Attr))
589 return;
590
591 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
592}
593
Michael Hanf1aae3b2012-08-03 17:40:43 +0000594static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000595 const AttributeList &Attr) {
596 if (!checkLockableAttrCommon(S, D, Attr))
597 return;
598
599 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000600}
601
602static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
603 const AttributeList &Attr) {
604 assert(!Attr.isInvalid());
605
606 if (!checkAttributeNumArgs(S, Attr, 0))
607 return;
608
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000609 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000610 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
611 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000612 return;
613 }
614
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000615 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000616 S.Context));
617}
618
Kostya Serebryany71efba02012-01-24 19:25:38 +0000619static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000620 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000621 assert(!Attr.isInvalid());
622
623 if (!checkAttributeNumArgs(S, Attr, 0))
624 return;
625
626 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
627 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
628 << Attr.getName() << ExpectedFunctionOrMethod;
629 return;
630 }
631
632 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000633 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000634}
635
Michael Hanf1aae3b2012-08-03 17:40:43 +0000636static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
637 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000638 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000639 assert(!Attr.isInvalid());
640
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000641 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000642 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000643
644 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000645 ValueDecl *VD = dyn_cast<ValueDecl>(D);
646 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000647 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
648 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000649 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000650 }
651
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000652 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000653 QualType QT = VD->getType();
654 if (!QT->isDependentType()) {
655 const RecordType *RT = getRecordType(QT);
656 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000657 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000658 << Attr.getName();
659 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000660 }
661 }
662
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000663 // Check that all arguments are lockable objects.
664 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000665 if (Args.size() == 0)
666 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000667
Michael Handc691572012-07-23 18:48:41 +0000668 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000669}
670
Michael Hanf1aae3b2012-08-03 17:40:43 +0000671static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000672 const AttributeList &Attr) {
673 SmallVector<Expr*, 1> Args;
674 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
675 return;
676
677 Expr **StartArg = &Args[0];
678 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000679 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000680}
681
Michael Hanf1aae3b2012-08-03 17:40:43 +0000682static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000683 const AttributeList &Attr) {
684 SmallVector<Expr*, 1> Args;
685 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
686 return;
687
688 Expr **StartArg = &Args[0];
689 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000690 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000691}
692
Michael Hanf1aae3b2012-08-03 17:40:43 +0000693static bool checkLockFunAttrCommon(Sema &S, Decl *D,
694 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000695 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000696 assert(!Attr.isInvalid());
697
698 // zero or more arguments ok
699
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000700 // check that the attribute is applied to a function
701 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000702 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
703 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000705 }
706
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000707 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000708 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000709
Michael Handc691572012-07-23 18:48:41 +0000710 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000711}
712
Michael Hanf1aae3b2012-08-03 17:40:43 +0000713static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000714 const AttributeList &Attr) {
715 SmallVector<Expr*, 1> Args;
716 if (!checkLockFunAttrCommon(S, D, Attr, Args))
717 return;
718
719 unsigned Size = Args.size();
720 Expr **StartArg = Size == 0 ? 0 : &Args[0];
721 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000722 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000723 StartArg, Size));
724}
725
Michael Hanf1aae3b2012-08-03 17:40:43 +0000726static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000727 const AttributeList &Attr) {
728 SmallVector<Expr*, 1> Args;
729 if (!checkLockFunAttrCommon(S, D, Attr, Args))
730 return;
731
732 unsigned Size = Args.size();
733 Expr **StartArg = Size == 0 ? 0 : &Args[0];
734 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000735 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000736 StartArg, Size));
737}
738
Michael Hanf1aae3b2012-08-03 17:40:43 +0000739static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
740 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000741 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000742 assert(!Attr.isInvalid());
743
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000744 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000745 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000746
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000747 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000748 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
749 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000750 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000751 }
752
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000753 if (!isIntOrBool(Attr.getArg(0))) {
754 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000755 << Attr.getName();
756 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000757 }
758
759 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000761
Michael Handc691572012-07-23 18:48:41 +0000762 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000763}
764
Michael Hanf1aae3b2012-08-03 17:40:43 +0000765static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000766 const AttributeList &Attr) {
767 SmallVector<Expr*, 2> Args;
768 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
769 return;
770
771 unsigned Size = Args.size();
772 Expr **StartArg = Size == 0 ? 0 : &Args[0];
773 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000774 S.Context,
775 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000776 StartArg, Size));
777}
778
Michael Hanf1aae3b2012-08-03 17:40:43 +0000779static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000780 const AttributeList &Attr) {
781 SmallVector<Expr*, 2> Args;
782 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
783 return;
784
785 unsigned Size = Args.size();
786 Expr **StartArg = Size == 0 ? 0 : &Args[0];
787 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000788 S.Context,
789 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000790 StartArg, Size));
791}
792
Michael Hanf1aae3b2012-08-03 17:40:43 +0000793static bool checkLocksRequiredCommon(Sema &S, Decl *D,
794 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000795 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000796 assert(!Attr.isInvalid());
797
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000798 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000799 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000800
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000801 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000802 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
803 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000804 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000805 }
806
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000807 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000808 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000809 if (Args.size() == 0)
810 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000811
Michael Handc691572012-07-23 18:48:41 +0000812 return true;
813}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000814
Michael Hanf1aae3b2012-08-03 17:40:43 +0000815static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000816 const AttributeList &Attr) {
817 SmallVector<Expr*, 1> Args;
818 if (!checkLocksRequiredCommon(S, D, Attr, Args))
819 return;
820
821 Expr **StartArg = &Args[0];
822 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000823 S.Context,
824 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000825 Args.size()));
826}
827
Michael Hanf1aae3b2012-08-03 17:40:43 +0000828static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000829 const AttributeList &Attr) {
830 SmallVector<Expr*, 1> Args;
831 if (!checkLocksRequiredCommon(S, D, Attr, Args))
832 return;
833
834 Expr **StartArg = &Args[0];
835 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000836 S.Context,
837 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000838 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839}
840
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000841static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000843 assert(!Attr.isInvalid());
844
845 // zero or more arguments ok
846
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000847 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000848 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
849 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000850 return;
851 }
852
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000853 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000855 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000856 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000857 Expr **StartArg = Size == 0 ? 0 : &Args[0];
858
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000859 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000860 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000861}
862
863static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000864 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000865 assert(!Attr.isInvalid());
866
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000867 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000868 return;
869
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000870 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000871 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
872 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000873 return;
874 }
875
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000876 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000877 SmallVector<Expr*, 1> Args;
878 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
879 unsigned Size = Args.size();
880 if (Size == 0)
881 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000882
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000883 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
884 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000885}
886
887static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000888 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889 assert(!Attr.isInvalid());
890
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000891 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000892 return;
893
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000894 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000895 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
896 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000897 return;
898 }
899
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000900 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000901 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000902 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000903 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000904 if (Size == 0)
905 return;
906 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000907
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000908 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000909 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000910}
911
912
Chandler Carruth1b03c872011-07-02 00:01:44 +0000913static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
914 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000915 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000916 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000917 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000918 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919 }
Mike Stumpbf916502009-07-24 19:02:52 +0000920
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000922
923 Expr *sizeExpr;
924
925 // Special case where the argument is a template id.
926 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000927 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000928 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000929 UnqualifiedId id;
930 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000931
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000932 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
933 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000934 if (Size.isInvalid())
935 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000936
Douglas Gregor4ac01402011-06-15 16:02:29 +0000937 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000938 } else {
939 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000940 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000941 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000942
Peter Collingbourne7a730022010-11-23 20:45:58 +0000943 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000944 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000945
946 // Instantiate/Install the vector type, and let Sema build the type for us.
947 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000948 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000949 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000950 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000951 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000952
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000953 // Remember this typedef decl, we will need it later for diagnostics.
954 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000955 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000956}
957
Chandler Carruth1b03c872011-07-02 00:01:44 +0000958static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000960 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000962
Chandler Carruth87c44602011-07-01 23:49:12 +0000963 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000964 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000965 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000966 // If the alignment is less than or equal to 8 bits, the packed attribute
967 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000968 if (!FD->getType()->isDependentType() &&
969 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000970 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000972 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000974 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000975 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000977}
978
Chandler Carruth1b03c872011-07-02 00:01:44 +0000979static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000980 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
981 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000982 else
983 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
984}
985
Chandler Carruth1b03c872011-07-02 00:01:44 +0000986static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000987 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000988 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000989 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000990
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000991 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000992 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000993 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000994 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000995 return;
996 }
997
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000998 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000999}
1000
Ted Kremenek2f041d02011-09-29 07:02:25 +00001001static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1002 // The IBOutlet/IBOutletCollection attributes only apply to instance
1003 // variables or properties of Objective-C classes. The outlet must also
1004 // have an object reference type.
1005 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1006 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001007 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001008 << Attr.getName() << VD->getType() << 0;
1009 return false;
1010 }
1011 }
1012 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1013 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001014 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001015 << Attr.getName() << PD->getType() << 1;
1016 return false;
1017 }
1018 }
1019 else {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1021 return false;
1022 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001023
Ted Kremenek2f041d02011-09-29 07:02:25 +00001024 return true;
1025}
1026
Chandler Carruth1b03c872011-07-02 00:01:44 +00001027static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001028 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001029 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001030 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001031
1032 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001033 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001034
Ted Kremenek2f041d02011-09-29 07:02:25 +00001035 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001036}
1037
Chandler Carruth1b03c872011-07-02 00:01:44 +00001038static void handleIBOutletCollection(Sema &S, Decl *D,
1039 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001040
1041 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001042 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1044 return;
1045 }
1046
Ted Kremenek2f041d02011-09-29 07:02:25 +00001047 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001048 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001049
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001050 IdentifierInfo *II = Attr.getParameterName();
1051 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001052 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001053
John McCallb3d87482010-08-24 05:47:05 +00001054 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001055 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001056 if (!TypeRep) {
1057 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1058 return;
1059 }
John McCallb3d87482010-08-24 05:47:05 +00001060 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001061 // Diagnose use of non-object type in iboutletcollection attribute.
1062 // FIXME. Gnu attribute extension ignores use of builtin types in
1063 // attributes. So, __attribute__((iboutletcollection(char))) will be
1064 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001065 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001066 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1067 return;
1068 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001069 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1070 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001071}
1072
Chandler Carruthd309c812011-07-01 23:49:16 +00001073static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001074 if (const RecordType *UT = T->getAsUnionType())
1075 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1076 RecordDecl *UD = UT->getDecl();
1077 for (RecordDecl::field_iterator it = UD->field_begin(),
1078 itend = UD->field_end(); it != itend; ++it) {
1079 QualType QT = it->getType();
1080 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1081 T = QT;
1082 return;
1083 }
1084 }
1085 }
1086}
1087
Nuno Lopes587de5b2012-05-24 00:22:00 +00001088static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001089 if (!isFunctionOrMethod(D)) {
1090 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1091 << "alloc_size" << ExpectedFunctionOrMethod;
1092 return;
1093 }
1094
Nuno Lopes587de5b2012-05-24 00:22:00 +00001095 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1096 return;
1097
1098 // In C++ the implicit 'this' function parameter also counts, and they are
1099 // counted from one.
1100 bool HasImplicitThisParam = isInstanceMethod(D);
1101 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1102
1103 SmallVector<unsigned, 8> SizeArgs;
1104
1105 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1106 E = Attr.arg_end(); I!=E; ++I) {
1107 // The argument must be an integer constant expression.
1108 Expr *Ex = *I;
1109 llvm::APSInt ArgNum;
1110 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1111 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1112 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1113 << "alloc_size" << Ex->getSourceRange();
1114 return;
1115 }
1116
1117 uint64_t x = ArgNum.getZExtValue();
1118
1119 if (x < 1 || x > NumArgs) {
1120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1121 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1122 return;
1123 }
1124
1125 --x;
1126 if (HasImplicitThisParam) {
1127 if (x == 0) {
1128 S.Diag(Attr.getLoc(),
1129 diag::err_attribute_invalid_implicit_this_argument)
1130 << "alloc_size" << Ex->getSourceRange();
1131 return;
1132 }
1133 --x;
1134 }
1135
1136 // check if the function argument is of an integer type
1137 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1138 if (!T->isIntegerType()) {
1139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1140 << "alloc_size" << Ex->getSourceRange();
1141 return;
1142 }
1143
Nuno Lopes587de5b2012-05-24 00:22:00 +00001144 SizeArgs.push_back(x);
1145 }
1146
1147 // check if the function returns a pointer
1148 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1149 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1150 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1151 }
1152
Nuno Lopes96c67d12012-06-18 16:27:56 +00001153 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1154 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001155}
1156
Chandler Carruth1b03c872011-07-02 00:01:44 +00001157static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001158 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1159 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001160 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001162 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001163 return;
1164 }
Mike Stumpbf916502009-07-24 19:02:52 +00001165
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001166 // In C++ the implicit 'this' function parameter also counts, and they are
1167 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001168 bool HasImplicitThisParam = isInstanceMethod(D);
1169 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001170
1171 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001172 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001173
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001174 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1175 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001176
1177
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001178 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001179 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001180 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001181 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1182 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1184 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001185 return;
1186 }
Mike Stumpbf916502009-07-24 19:02:52 +00001187
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001188 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001189
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001190 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001192 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001193 return;
1194 }
Mike Stumpbf916502009-07-24 19:02:52 +00001195
Ted Kremenek465172f2008-07-21 22:09:15 +00001196 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001197 if (HasImplicitThisParam) {
1198 if (x == 0) {
1199 S.Diag(Attr.getLoc(),
1200 diag::err_attribute_invalid_implicit_this_argument)
1201 << "nonnull" << Ex->getSourceRange();
1202 return;
1203 }
1204 --x;
1205 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001206
1207 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001208 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001209 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001210
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001211 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001212 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001213 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001214 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001215 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001216 }
Mike Stumpbf916502009-07-24 19:02:52 +00001217
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001218 NonNullArgs.push_back(x);
1219 }
Mike Stumpbf916502009-07-24 19:02:52 +00001220
1221 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1222 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001223 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001224 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1225 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001226 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001227 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001228 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001229 }
Mike Stumpbf916502009-07-24 19:02:52 +00001230
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001231 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001232 if (NonNullArgs.empty()) {
1233 // Warn the trivial case only if attribute is not coming from a
1234 // macro instantiation.
1235 if (Attr.getLoc().isFileID())
1236 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001237 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001238 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001239 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001240
1241 unsigned* start = &NonNullArgs[0];
1242 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001243 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001244 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001245 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001246}
1247
Chandler Carruth1b03c872011-07-02 00:01:44 +00001248static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001249 // This attribute must be applied to a function declaration.
1250 // The first argument to the attribute must be a string,
1251 // the name of the resource, for example "malloc".
1252 // The following arguments must be argument indexes, the arguments must be
1253 // of integer type for Returns, otherwise of pointer type.
1254 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001255 // after being held. free() should be __attribute((ownership_takes)), whereas
1256 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001257
1258 if (!AL.getParameterName()) {
1259 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1260 << AL.getName()->getName() << 1;
1261 return;
1262 }
1263 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001264 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001265 switch (AL.getKind()) {
1266 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001267 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001268 if (AL.getNumArgs() < 1) {
1269 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1270 return;
1271 }
Jordy Rose2a479922010-08-12 08:54:03 +00001272 break;
1273 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001274 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001275 if (AL.getNumArgs() < 1) {
1276 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1277 return;
1278 }
Jordy Rose2a479922010-08-12 08:54:03 +00001279 break;
1280 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001281 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001282 if (AL.getNumArgs() > 1) {
1283 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1284 << AL.getNumArgs() + 1;
1285 return;
1286 }
Jordy Rose2a479922010-08-12 08:54:03 +00001287 break;
1288 default:
1289 // This should never happen given how we are called.
1290 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001291 }
1292
Chandler Carruth87c44602011-07-01 23:49:12 +00001293 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001294 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1295 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001296 return;
1297 }
1298
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001299 // In C++ the implicit 'this' function parameter also counts, and they are
1300 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001301 bool HasImplicitThisParam = isInstanceMethod(D);
1302 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001303
Chris Lattner5f9e2722011-07-23 10:55:15 +00001304 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001305
1306 // Normalize the argument, __foo__ becomes foo.
1307 if (Module.startswith("__") && Module.endswith("__"))
1308 Module = Module.substr(2, Module.size() - 4);
1309
Chris Lattner5f9e2722011-07-23 10:55:15 +00001310 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001311
Jordy Rose2a479922010-08-12 08:54:03 +00001312 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1313 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001314
Peter Collingbourne7a730022010-11-23 20:45:58 +00001315 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001316 llvm::APSInt ArgNum(32);
1317 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1318 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1319 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1320 << AL.getName()->getName() << IdxExpr->getSourceRange();
1321 continue;
1322 }
1323
1324 unsigned x = (unsigned) ArgNum.getZExtValue();
1325
1326 if (x > NumArgs || x < 1) {
1327 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1328 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1329 continue;
1330 }
1331 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001332 if (HasImplicitThisParam) {
1333 if (x == 0) {
1334 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1335 << "ownership" << IdxExpr->getSourceRange();
1336 return;
1337 }
1338 --x;
1339 }
1340
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001342 case OwnershipAttr::Takes:
1343 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001344 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001345 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001346 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1347 // FIXME: Should also highlight argument in decl.
1348 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001349 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001350 << "pointer"
1351 << IdxExpr->getSourceRange();
1352 continue;
1353 }
1354 break;
1355 }
Sean Huntcf807c42010-08-18 23:23:40 +00001356 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001357 if (AL.getNumArgs() > 1) {
1358 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001359 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001360 llvm::APSInt ArgNum(32);
1361 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1362 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1363 S.Diag(AL.getLoc(), diag::err_ownership_type)
1364 << "ownership_returns" << "integer"
1365 << IdxExpr->getSourceRange();
1366 return;
1367 }
1368 }
1369 break;
1370 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001371 } // switch
1372
1373 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001374 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001375 i = D->specific_attr_begin<OwnershipAttr>(),
1376 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001377 i != e; ++i) {
1378 if ((*i)->getOwnKind() != K) {
1379 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1380 I!=E; ++I) {
1381 if (x == *I) {
1382 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1383 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001384 }
1385 }
1386 }
1387 }
1388 OwnershipArgs.push_back(x);
1389 }
1390
1391 unsigned* start = OwnershipArgs.data();
1392 unsigned size = OwnershipArgs.size();
1393 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001394
1395 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1396 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1397 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398 }
Sean Huntcf807c42010-08-18 23:23:40 +00001399
Chandler Carruth87c44602011-07-01 23:49:12 +00001400 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001401 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001402}
1403
John McCall332bb2a2011-02-08 22:35:49 +00001404/// Whether this declaration has internal linkage for the purposes of
1405/// things that want to complain about things not have internal linkage.
1406static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1407 switch (D->getLinkage()) {
1408 case NoLinkage:
1409 case InternalLinkage:
1410 return true;
1411
1412 // Template instantiations that go from external to unique-external
1413 // shouldn't get diagnosed.
1414 case UniqueExternalLinkage:
1415 return true;
1416
1417 case ExternalLinkage:
1418 return false;
1419 }
1420 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001421}
1422
Chandler Carruth1b03c872011-07-02 00:01:44 +00001423static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001424 // Check the attribute arguments.
1425 if (Attr.getNumArgs() > 1) {
1426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1427 return;
1428 }
1429
Chandler Carruth87c44602011-07-01 23:49:12 +00001430 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001432 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001433 return;
1434 }
1435
Chandler Carruth87c44602011-07-01 23:49:12 +00001436 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001437
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001438 // gcc rejects
1439 // class c {
1440 // static int a __attribute__((weakref ("v2")));
1441 // static int b() __attribute__((weakref ("f3")));
1442 // };
1443 // and ignores the attributes of
1444 // void f(void) {
1445 // static int a __attribute__((weakref ("v2")));
1446 // }
1447 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001448 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001449 if (!Ctx->isFileContext()) {
1450 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001451 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001452 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001453 }
1454
1455 // The GCC manual says
1456 //
1457 // At present, a declaration to which `weakref' is attached can only
1458 // be `static'.
1459 //
1460 // It also says
1461 //
1462 // Without a TARGET,
1463 // given as an argument to `weakref' or to `alias', `weakref' is
1464 // equivalent to `weak'.
1465 //
1466 // gcc 4.4.1 will accept
1467 // int a7 __attribute__((weakref));
1468 // as
1469 // int a7 __attribute__((weak));
1470 // This looks like a bug in gcc. We reject that for now. We should revisit
1471 // it if this behaviour is actually used.
1472
John McCall332bb2a2011-02-08 22:35:49 +00001473 if (!hasEffectivelyInternalLinkage(nd)) {
1474 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001475 return;
1476 }
1477
1478 // GCC rejects
1479 // static ((alias ("y"), weakref)).
1480 // Should we? How to check that weakref is before or after alias?
1481
1482 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001483 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001484 Arg = Arg->IgnoreParenCasts();
1485 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1486
Douglas Gregor5cee1192011-07-27 05:40:30 +00001487 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1489 << "weakref" << 1;
1490 return;
1491 }
1492 // GCC will accept anything as the argument of weakref. Should we
1493 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001494 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001495 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001496 }
1497
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001498 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001499}
1500
Chandler Carruth1b03c872011-07-02 00:01:44 +00001501static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001502 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001503 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001505 return;
1506 }
Mike Stumpbf916502009-07-24 19:02:52 +00001507
Peter Collingbourne7a730022010-11-23 20:45:58 +00001508 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001509 Arg = Arg->IgnoreParenCasts();
1510 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001511
Douglas Gregor5cee1192011-07-27 05:40:30 +00001512 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001514 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001515 return;
1516 }
Mike Stumpbf916502009-07-24 19:02:52 +00001517
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001518 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001519 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1520 return;
1521 }
1522
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001524
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001525 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001526 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001527}
1528
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001529static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1530 // Check the attribute arguments.
1531 if (!checkAttributeNumArgs(S, Attr, 0))
1532 return;
1533
1534 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1536 << Attr.getName() << ExpectedFunctionOrMethod;
1537 return;
1538 }
1539
1540 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1541}
1542
Benjamin Krameree409a92012-05-12 21:10:52 +00001543static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1544 // Check the attribute arguments.
1545 if (!checkAttributeNumArgs(S, Attr, 0))
1546 return;
1547
1548 if (!isa<FunctionDecl>(D)) {
1549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1550 << Attr.getName() << ExpectedFunction;
1551 return;
1552 }
1553
1554 if (D->hasAttr<HotAttr>()) {
1555 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1556 << Attr.getName() << "hot";
1557 return;
1558 }
1559
1560 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1561}
1562
1563static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1564 // Check the attribute arguments.
1565 if (!checkAttributeNumArgs(S, Attr, 0))
1566 return;
1567
1568 if (!isa<FunctionDecl>(D)) {
1569 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1570 << Attr.getName() << ExpectedFunction;
1571 return;
1572 }
1573
1574 if (D->hasAttr<ColdAttr>()) {
1575 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1576 << Attr.getName() << "cold";
1577 return;
1578 }
1579
1580 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1581}
1582
Chandler Carruth1b03c872011-07-02 00:01:44 +00001583static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001584 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001585 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001586 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001587
Chandler Carruth87c44602011-07-01 23:49:12 +00001588 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001589 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001590 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001591 return;
1592 }
1593
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001594 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001595}
1596
Chandler Carruth1b03c872011-07-02 00:01:44 +00001597static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001599 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001600 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1602 return;
1603 }
1604
Chandler Carruth87c44602011-07-01 23:49:12 +00001605 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001607 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001608 return;
1609 }
Mike Stumpbf916502009-07-24 19:02:52 +00001610
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001611 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001612}
1613
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001614static void handleTLSModelAttr(Sema &S, Decl *D,
1615 const AttributeList &Attr) {
1616 // Check the attribute arguments.
1617 if (Attr.getNumArgs() != 1) {
1618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1619 return;
1620 }
1621
1622 Expr *Arg = Attr.getArg(0);
1623 Arg = Arg->IgnoreParenCasts();
1624 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1625
1626 // Check that it is a string.
1627 if (!Str) {
1628 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1629 return;
1630 }
1631
1632 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1634 << Attr.getName() << ExpectedTLSVar;
1635 return;
1636 }
1637
1638 // Check that the value.
1639 StringRef Model = Str->getString();
1640 if (Model != "global-dynamic" && Model != "local-dynamic"
1641 && Model != "initial-exec" && Model != "local-exec") {
1642 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1643 return;
1644 }
1645
1646 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1647 Model));
1648}
1649
Chandler Carruth1b03c872011-07-02 00:01:44 +00001650static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001651 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001652 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 return;
1655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Chandler Carruth87c44602011-07-01 23:49:12 +00001657 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001658 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001659 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001660 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001661 return;
1662 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001663 }
1664
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001665 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001666}
1667
Chandler Carruth1b03c872011-07-02 00:01:44 +00001668static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001669 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001670 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001671 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001672
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001673 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001674}
1675
Chandler Carruth1b03c872011-07-02 00:01:44 +00001676static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001677 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001678 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001679 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001680 else
1681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001682 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001683}
1684
Chandler Carruth1b03c872011-07-02 00:01:44 +00001685static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001686 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001687 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001688 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001689 else
1690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001692}
1693
Chandler Carruth1b03c872011-07-02 00:01:44 +00001694static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001695 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001696
1697 if (S.CheckNoReturnAttr(attr)) return;
1698
Chandler Carruth87c44602011-07-01 23:49:12 +00001699 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001700 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001701 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001702 return;
1703 }
1704
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001705 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001706}
1707
1708bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001709 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001710 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1711 attr.setInvalid();
1712 return true;
1713 }
1714
1715 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001716}
1717
Chandler Carruth1b03c872011-07-02 00:01:44 +00001718static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1719 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001720
1721 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1722 // because 'analyzer_noreturn' does not impact the type.
1723
Chandler Carruth1731e202011-07-11 23:30:35 +00001724 if(!checkAttributeNumArgs(S, Attr, 0))
1725 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001726
Chandler Carruth87c44602011-07-01 23:49:12 +00001727 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1728 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001729 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1730 && !VD->getType()->isFunctionPointerType())) {
1731 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001732 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001733 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001734 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001735 return;
1736 }
1737 }
1738
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001739 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001740}
1741
John Thompson35cc9622010-08-09 21:53:52 +00001742// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001743static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001744/*
1745 Returning a Vector Class in Registers
1746
Eric Christopherf48f3672010-12-01 22:13:54 +00001747 According to the PPU ABI specifications, a class with a single member of
1748 vector type is returned in memory when used as the return value of a function.
1749 This results in inefficient code when implementing vector classes. To return
1750 the value in a single vector register, add the vecreturn attribute to the
1751 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001752
1753 Example:
1754
1755 struct Vector
1756 {
1757 __vector float xyzw;
1758 } __attribute__((vecreturn));
1759
1760 Vector Add(Vector lhs, Vector rhs)
1761 {
1762 Vector result;
1763 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1764 return result; // This will be returned in a register
1765 }
1766*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001767 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001769 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001770 return;
1771 }
1772
Chandler Carruth87c44602011-07-01 23:49:12 +00001773 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001774 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1775 return;
1776 }
1777
Chandler Carruth87c44602011-07-01 23:49:12 +00001778 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001779 int count = 0;
1780
1781 if (!isa<CXXRecordDecl>(record)) {
1782 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1783 return;
1784 }
1785
1786 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1787 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1788 return;
1789 }
1790
Eric Christopherf48f3672010-12-01 22:13:54 +00001791 for (RecordDecl::field_iterator iter = record->field_begin();
1792 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001793 if ((count == 1) || !iter->getType()->isVectorType()) {
1794 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1795 return;
1796 }
1797 count++;
1798 }
1799
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001800 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001801}
1802
Chandler Carruth1b03c872011-07-02 00:01:44 +00001803static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001804 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001806 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001807 return;
1808 }
1809 // FIXME: Actually store the attribute on the declaration
1810}
1811
Chandler Carruth1b03c872011-07-02 00:01:44 +00001812static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001813 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001814 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001816 return;
1817 }
Mike Stumpbf916502009-07-24 19:02:52 +00001818
Chandler Carruth87c44602011-07-01 23:49:12 +00001819 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001820 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001822 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001823 return;
1824 }
Mike Stumpbf916502009-07-24 19:02:52 +00001825
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001826 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001827}
1828
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001829static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1830 const AttributeList &Attr) {
1831 // check the attribute arguments.
1832 if (Attr.hasParameterOrArguments()) {
1833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1834 return;
1835 }
1836
1837 if (!isa<FunctionDecl>(D)) {
1838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1839 << Attr.getName() << ExpectedFunction;
1840 return;
1841 }
1842
1843 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1844}
1845
Chandler Carruth1b03c872011-07-02 00:01:44 +00001846static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001847 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001848 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1850 return;
1851 }
Mike Stumpbf916502009-07-24 19:02:52 +00001852
Chandler Carruth87c44602011-07-01 23:49:12 +00001853 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001854 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001855 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1856 return;
1857 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001858 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001860 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001861 return;
1862 }
Mike Stumpbf916502009-07-24 19:02:52 +00001863
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001864 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001865}
1866
Chandler Carruth1b03c872011-07-02 00:01:44 +00001867static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001868 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001869 if (Attr.getNumArgs() > 1) {
1870 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001871 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001872 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001873
1874 int priority = 65535; // FIXME: Do not hardcode such constants.
1875 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001876 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001877 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001878 if (E->isTypeDependent() || E->isValueDependent() ||
1879 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001880 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001881 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001882 return;
1883 }
1884 priority = Idx.getZExtValue();
1885 }
Mike Stumpbf916502009-07-24 19:02:52 +00001886
Chandler Carruth87c44602011-07-01 23:49:12 +00001887 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001888 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001889 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001890 return;
1891 }
1892
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001893 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001894 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001895}
1896
Chandler Carruth1b03c872011-07-02 00:01:44 +00001897static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001898 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001899 if (Attr.getNumArgs() > 1) {
1900 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001901 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001902 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001903
1904 int priority = 65535; // FIXME: Do not hardcode such constants.
1905 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001906 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001907 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001908 if (E->isTypeDependent() || E->isValueDependent() ||
1909 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001911 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001912 return;
1913 }
1914 priority = Idx.getZExtValue();
1915 }
Mike Stumpbf916502009-07-24 19:02:52 +00001916
Chandler Carruth87c44602011-07-01 23:49:12 +00001917 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001918 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001919 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001920 return;
1921 }
1922
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001923 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001924 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001925}
1926
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001927template <typename AttrTy>
1928static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1929 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001930 unsigned NumArgs = Attr.getNumArgs();
1931 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001932 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001933 return;
1934 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001935
1936 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001937 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001938 if (NumArgs == 1) {
1939 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001940 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001941 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001942 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001943 return;
1944 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001945 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001948 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001949}
1950
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001951static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1952 const AttributeList &Attr) {
1953 unsigned NumArgs = Attr.getNumArgs();
1954 if (NumArgs > 0) {
1955 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1956 return;
1957 }
1958
1959 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001960 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001961}
1962
Patrick Beardb2f68202012-04-06 18:12:22 +00001963static void handleObjCRootClassAttr(Sema &S, Decl *D,
1964 const AttributeList &Attr) {
1965 if (!isa<ObjCInterfaceDecl>(D)) {
1966 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1967 return;
1968 }
1969
1970 unsigned NumArgs = Attr.getNumArgs();
1971 if (NumArgs > 0) {
1972 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1973 return;
1974 }
1975
1976 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1977}
1978
Ted Kremenek71207fc2012-01-05 22:47:47 +00001979static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001980 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001981 if (!isa<ObjCInterfaceDecl>(D)) {
1982 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1983 return;
1984 }
1985
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001986 unsigned NumArgs = Attr.getNumArgs();
1987 if (NumArgs > 0) {
1988 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1989 return;
1990 }
1991
Ted Kremenek71207fc2012-01-05 22:47:47 +00001992 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001993 Attr.getRange(), S.Context));
1994}
1995
Jordy Rosefad5de92012-05-08 03:27:22 +00001996static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1997 IdentifierInfo *Platform,
1998 VersionTuple Introduced,
1999 VersionTuple Deprecated,
2000 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002001 StringRef PlatformName
2002 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2003 if (PlatformName.empty())
2004 PlatformName = Platform->getName();
2005
2006 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2007 // of these steps are needed).
2008 if (!Introduced.empty() && !Deprecated.empty() &&
2009 !(Introduced <= Deprecated)) {
2010 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2011 << 1 << PlatformName << Deprecated.getAsString()
2012 << 0 << Introduced.getAsString();
2013 return true;
2014 }
2015
2016 if (!Introduced.empty() && !Obsoleted.empty() &&
2017 !(Introduced <= Obsoleted)) {
2018 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2019 << 2 << PlatformName << Obsoleted.getAsString()
2020 << 0 << Introduced.getAsString();
2021 return true;
2022 }
2023
2024 if (!Deprecated.empty() && !Obsoleted.empty() &&
2025 !(Deprecated <= Obsoleted)) {
2026 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2027 << 2 << PlatformName << Obsoleted.getAsString()
2028 << 1 << Deprecated.getAsString();
2029 return true;
2030 }
2031
2032 return false;
2033}
2034
Rafael Espindola51be6e32013-01-08 22:04:34 +00002035AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002036 IdentifierInfo *Platform,
2037 VersionTuple Introduced,
2038 VersionTuple Deprecated,
2039 VersionTuple Obsoleted,
2040 bool IsUnavailable,
2041 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002042 VersionTuple MergedIntroduced = Introduced;
2043 VersionTuple MergedDeprecated = Deprecated;
2044 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002045 bool FoundAny = false;
Rafael Espindolad130fd22013-01-08 22:31:36 +00002046 bool DroppedAny = false;
Rafael Espindola3b294362012-05-06 19:56:25 +00002047
Rafael Espindola98ae8342012-05-10 02:50:16 +00002048 if (D->hasAttrs()) {
2049 AttrVec &Attrs = D->getAttrs();
2050 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2051 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2052 if (!OldAA) {
2053 ++i;
2054 continue;
2055 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002056
Rafael Espindola98ae8342012-05-10 02:50:16 +00002057 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2058 if (OldPlatform != Platform) {
2059 ++i;
2060 continue;
2061 }
2062
2063 FoundAny = true;
2064 VersionTuple OldIntroduced = OldAA->getIntroduced();
2065 VersionTuple OldDeprecated = OldAA->getDeprecated();
2066 VersionTuple OldObsoleted = OldAA->getObsoleted();
2067 bool OldIsUnavailable = OldAA->getUnavailable();
2068 StringRef OldMessage = OldAA->getMessage();
2069
2070 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2071 OldIntroduced != Introduced) ||
2072 (!OldDeprecated.empty() && !Deprecated.empty() &&
2073 OldDeprecated != Deprecated) ||
2074 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2075 OldObsoleted != Obsoleted) ||
2076 (OldIsUnavailable != IsUnavailable) ||
2077 (OldMessage != Message)) {
2078 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2079 Diag(Range.getBegin(), diag::note_previous_attribute);
2080 Attrs.erase(Attrs.begin() + i);
Rafael Espindolad130fd22013-01-08 22:31:36 +00002081 DroppedAny = true;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002082 --e;
2083 continue;
2084 }
2085
2086 VersionTuple MergedIntroduced2 = MergedIntroduced;
2087 VersionTuple MergedDeprecated2 = MergedDeprecated;
2088 VersionTuple MergedObsoleted2 = MergedObsoleted;
2089
2090 if (MergedIntroduced2.empty())
2091 MergedIntroduced2 = OldIntroduced;
2092 if (MergedDeprecated2.empty())
2093 MergedDeprecated2 = OldDeprecated;
2094 if (MergedObsoleted2.empty())
2095 MergedObsoleted2 = OldObsoleted;
2096
2097 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2098 MergedIntroduced2, MergedDeprecated2,
2099 MergedObsoleted2)) {
2100 Attrs.erase(Attrs.begin() + i);
Rafael Espindolad130fd22013-01-08 22:31:36 +00002101 DroppedAny = true;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002102 --e;
2103 continue;
2104 }
2105
2106 MergedIntroduced = MergedIntroduced2;
2107 MergedDeprecated = MergedDeprecated2;
2108 MergedObsoleted = MergedObsoleted2;
2109 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002110 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002111 }
2112
Rafael Espindolad130fd22013-01-08 22:31:36 +00002113 if (DroppedAny)
2114 D->ClearLVCache();
2115
Rafael Espindola3b294362012-05-06 19:56:25 +00002116 if (FoundAny &&
2117 MergedIntroduced == Introduced &&
2118 MergedDeprecated == Deprecated &&
2119 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002120 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002121
Rafael Espindola98ae8342012-05-10 02:50:16 +00002122 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002123 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002124 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2125 Introduced, Deprecated,
2126 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002127 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002128 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002129}
2130
Chandler Carruth1b03c872011-07-02 00:01:44 +00002131static void handleAvailabilityAttr(Sema &S, Decl *D,
2132 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002133 IdentifierInfo *Platform = Attr.getParameterName();
2134 SourceLocation PlatformLoc = Attr.getParameterLoc();
2135
Rafael Espindola3b294362012-05-06 19:56:25 +00002136 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002137 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2138 << Platform;
2139
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002140 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2141 if (!ND) {
2142 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2143 return;
2144 }
2145
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002146 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2147 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2148 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002149 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002150 StringRef Str;
2151 const StringLiteral *SE =
2152 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2153 if (SE)
2154 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002155
Rafael Espindola51be6e32013-01-08 22:04:34 +00002156 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002157 Platform,
2158 Introduced.Version,
2159 Deprecated.Version,
2160 Obsoleted.Version,
2161 IsUnavailable, Str);
Rafael Espindola140aadf2012-12-25 07:31:49 +00002162 if (NewAttr) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002163 D->addAttr(NewAttr);
Rafael Espindola140aadf2012-12-25 07:31:49 +00002164 ND->ClearLVCache();
2165 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002166}
2167
Rafael Espindola599f1b72012-05-13 03:25:18 +00002168VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2169 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002170 if (isa<TypedefNameDecl>(D)) {
2171 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002172 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002173 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002174 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2175 if (ExistingAttr) {
2176 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2177 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002178 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002179 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2180 Diag(Range.getBegin(), diag::note_previous_attribute);
2181 D->dropAttr<VisibilityAttr>();
Rafael Espindola140aadf2012-12-25 07:31:49 +00002182 NamedDecl *ND = cast<NamedDecl>(D);
2183 ND->ClearLVCache();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002184 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002185 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002186}
2187
Chandler Carruth1b03c872011-07-02 00:01:44 +00002188static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002189 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002190 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002191 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002192
Peter Collingbourne7a730022010-11-23 20:45:58 +00002193 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002194 Arg = Arg->IgnoreParenCasts();
2195 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002196
Douglas Gregor5cee1192011-07-27 05:40:30 +00002197 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002198 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002199 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002200 return;
2201 }
Mike Stumpbf916502009-07-24 19:02:52 +00002202
Chris Lattner5f9e2722011-07-23 10:55:15 +00002203 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002204 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002205
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002206 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002207 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002208 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002209 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002210 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002211 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002212 else if (TypeStr == "protected") {
2213 // Complain about attempts to use protected visibility on targets
2214 // (like Darwin) that don't support it.
2215 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2216 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2217 type = VisibilityAttr::Default;
2218 } else {
2219 type = VisibilityAttr::Protected;
2220 }
2221 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002222 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002223 return;
2224 }
Mike Stumpbf916502009-07-24 19:02:52 +00002225
Rafael Espindola599f1b72012-05-13 03:25:18 +00002226 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
Rafael Espindola140aadf2012-12-25 07:31:49 +00002227 if (NewAttr) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002228 D->addAttr(NewAttr);
Rafael Espindola140aadf2012-12-25 07:31:49 +00002229 NamedDecl *ND = cast<NamedDecl>(D);
2230 ND->ClearLVCache();
2231 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002232}
2233
Chandler Carruth1b03c872011-07-02 00:01:44 +00002234static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2235 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002236 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2237 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002238 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002239 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002240 return;
2241 }
2242
Chandler Carruth87c44602011-07-01 23:49:12 +00002243 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2244 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002246 << "objc_method_family" << 1;
2247 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002249 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002250 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002251 return;
2252 }
2253
Chris Lattner5f9e2722011-07-23 10:55:15 +00002254 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002255 ObjCMethodFamilyAttr::FamilyKind family;
2256 if (param == "none")
2257 family = ObjCMethodFamilyAttr::OMF_None;
2258 else if (param == "alloc")
2259 family = ObjCMethodFamilyAttr::OMF_alloc;
2260 else if (param == "copy")
2261 family = ObjCMethodFamilyAttr::OMF_copy;
2262 else if (param == "init")
2263 family = ObjCMethodFamilyAttr::OMF_init;
2264 else if (param == "mutableCopy")
2265 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2266 else if (param == "new")
2267 family = ObjCMethodFamilyAttr::OMF_new;
2268 else {
2269 // Just warn and ignore it. This is future-proof against new
2270 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002271 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002272 return;
2273 }
2274
John McCallf85e1932011-06-15 23:02:42 +00002275 if (family == ObjCMethodFamilyAttr::OMF_init &&
2276 !method->getResultType()->isObjCObjectPointerType()) {
2277 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2278 << method->getResultType();
2279 // Ignore the attribute.
2280 return;
2281 }
2282
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002283 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002284 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002285}
2286
Chandler Carruth1b03c872011-07-02 00:01:44 +00002287static void handleObjCExceptionAttr(Sema &S, Decl *D,
2288 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002289 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002290 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002291
Chris Lattner0db29ec2009-02-14 08:09:34 +00002292 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2293 if (OCI == 0) {
2294 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2295 return;
2296 }
Mike Stumpbf916502009-07-24 19:02:52 +00002297
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002298 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002299}
2300
Chandler Carruth1b03c872011-07-02 00:01:44 +00002301static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002302 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002304 return;
2305 }
Richard Smith162e1c12011-04-15 14:24:37 +00002306 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002307 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002308 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002309 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2310 return;
2311 }
2312 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002313 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2314 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002315 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002316 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2317 return;
2318 }
2319 }
2320 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002321 // It is okay to include this attribute on properties, e.g.:
2322 //
2323 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2324 //
2325 // In this case it follows tradition and suppresses an error in the above
2326 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002327 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002328 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002329 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002330}
2331
Mike Stumpbf916502009-07-24 19:02:52 +00002332static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002333handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002334 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002336 return;
2337 }
2338
2339 if (!isa<FunctionDecl>(D)) {
2340 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2341 return;
2342 }
2343
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002344 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002345}
2346
Chandler Carruth1b03c872011-07-02 00:01:44 +00002347static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002348 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002350 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002351 return;
2352 }
Mike Stumpbf916502009-07-24 19:02:52 +00002353
Steve Naroff9eae5762008-09-18 16:44:58 +00002354 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002356 return;
2357 }
Mike Stumpbf916502009-07-24 19:02:52 +00002358
Sean Huntcf807c42010-08-18 23:23:40 +00002359 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002360 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002361 type = BlocksAttr::ByRef;
2362 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002363 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002364 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002365 return;
2366 }
Mike Stumpbf916502009-07-24 19:02:52 +00002367
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002368 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002369}
2370
Chandler Carruth1b03c872011-07-02 00:01:44 +00002371static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002372 // check the attribute arguments.
2373 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002374 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002375 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002376 }
2377
John McCall3323fad2011-09-09 07:56:05 +00002378 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002379 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002380 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002381 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002382 if (E->isTypeDependent() || E->isValueDependent() ||
2383 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002384 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002385 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002386 return;
2387 }
Mike Stumpbf916502009-07-24 19:02:52 +00002388
John McCall3323fad2011-09-09 07:56:05 +00002389 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002390 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2391 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002392 return;
2393 }
John McCall3323fad2011-09-09 07:56:05 +00002394
2395 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002396 }
2397
John McCall3323fad2011-09-09 07:56:05 +00002398 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002399 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002400 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002401 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002402 if (E->isTypeDependent() || E->isValueDependent() ||
2403 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002404 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002405 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002406 return;
2407 }
2408 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002409
John McCall3323fad2011-09-09 07:56:05 +00002410 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002411 // FIXME: This error message could be improved, it would be nice
2412 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002413 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2414 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002415 return;
2416 }
2417 }
2418
Chandler Carruth87c44602011-07-01 23:49:12 +00002419 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002420 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002421 if (isa<FunctionNoProtoType>(FT)) {
2422 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2423 return;
2424 }
Mike Stumpbf916502009-07-24 19:02:52 +00002425
Chris Lattner897cd902009-03-17 23:03:47 +00002426 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002427 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002428 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002429 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002430 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002431 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002432 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002433 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002434 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002435 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2436 if (!BD->isVariadic()) {
2437 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2438 return;
2439 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002440 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002441 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002442 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002443 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002444 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002445 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002446 int m = Ty->isFunctionPointerType() ? 0 : 1;
2447 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002448 return;
2449 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002450 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002451 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002452 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002453 return;
2454 }
Anders Carlsson77091822008-10-05 18:05:59 +00002455 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002457 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002458 return;
2459 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002460 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002461 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002462}
2463
Chandler Carruth1b03c872011-07-02 00:01:44 +00002464static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002465 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002466 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002467 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002468
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002469 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002471 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002472 return;
2473 }
Mike Stumpbf916502009-07-24 19:02:52 +00002474
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002475 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2476 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2477 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002478 return;
2479 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002480 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2481 if (MD->getResultType()->isVoidType()) {
2482 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2483 << Attr.getName() << 1;
2484 return;
2485 }
2486
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002487 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002488}
2489
Chandler Carruth1b03c872011-07-02 00:01:44 +00002490static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002491 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002492 if (Attr.hasParameterOrArguments()) {
2493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002494 return;
2495 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002496
Chandler Carruth87c44602011-07-01 23:49:12 +00002497 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002498 if (isa<CXXRecordDecl>(D)) {
2499 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2500 return;
2501 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002502 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2503 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002504 return;
2505 }
2506
Chandler Carruth87c44602011-07-01 23:49:12 +00002507 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002508
2509 // 'weak' only applies to declarations with external linkage.
2510 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002511 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002512 return;
2513 }
Mike Stumpbf916502009-07-24 19:02:52 +00002514
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002515 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002516}
2517
Chandler Carruth1b03c872011-07-02 00:01:44 +00002518static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002519 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002520 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002521 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002522
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002523
2524 // weak_import only applies to variable & function declarations.
2525 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002526 if (!D->canBeWeakImported(isDef)) {
2527 if (isDef)
2528 S.Diag(Attr.getLoc(),
2529 diag::warn_attribute_weak_import_invalid_on_definition)
2530 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002531 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002532 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002533 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002534 // Nothing to warn about here.
2535 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002536 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002537 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002538
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002539 return;
2540 }
2541
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002542 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002543}
2544
Tanya Lattner0df579e2012-07-09 22:06:01 +00002545// Handles reqd_work_group_size and work_group_size_hint.
2546static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002547 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002548 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2549 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2550
Nate Begeman6f3d8382009-06-26 06:32:41 +00002551 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002552 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002553
2554 unsigned WGSize[3];
2555 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002556 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002557 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002558 if (E->isTypeDependent() || E->isValueDependent() ||
2559 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002561 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002562 return;
2563 }
2564 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2565 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002566
2567 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2568 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2569 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2570 if (!(A->getXDim() == WGSize[0] &&
2571 A->getYDim() == WGSize[1] &&
2572 A->getZDim() == WGSize[2])) {
2573 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2574 Attr.getName();
2575 }
2576 }
2577
2578 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2579 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2580 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2581 if (!(A->getXDim() == WGSize[0] &&
2582 A->getYDim() == WGSize[1] &&
2583 A->getZDim() == WGSize[2])) {
2584 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2585 Attr.getName();
2586 }
2587 }
2588
2589 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2590 D->addAttr(::new (S.Context)
2591 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2592 WGSize[0], WGSize[1], WGSize[2]));
2593 else
2594 D->addAttr(::new (S.Context)
2595 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2596 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002597}
2598
Rafael Espindola599f1b72012-05-13 03:25:18 +00002599SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2600 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002601 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2602 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002603 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002604 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2605 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002606 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002607 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002608 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002609}
2610
Chandler Carruth1b03c872011-07-02 00:01:44 +00002611static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002612 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002613 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002614 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002615
2616 // Make sure that there is a string literal as the sections's single
2617 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002618 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002619 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002620 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002621 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002622 return;
2623 }
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Chris Lattner797c3c42009-08-10 19:03:04 +00002625 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002626 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002627 if (!Error.empty()) {
2628 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2629 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002630 return;
2631 }
Mike Stump1eb44332009-09-09 15:08:12 +00002632
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002633 // This attribute cannot be applied to local variables.
2634 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2635 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2636 return;
2637 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002638 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2639 SE->getString());
2640 if (NewAttr)
2641 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002642}
2643
Chris Lattner6b6b5372008-06-26 18:38:35 +00002644
Chandler Carruth1b03c872011-07-02 00:01:44 +00002645static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002646 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002647 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002649 return;
2650 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002651
Chandler Carruth87c44602011-07-01 23:49:12 +00002652 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002653 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002654 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002655 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002656 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002657 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002658}
2659
Chandler Carruth1b03c872011-07-02 00:01:44 +00002660static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002661 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002662 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002664 return;
2665 }
Mike Stumpbf916502009-07-24 19:02:52 +00002666
Chandler Carruth87c44602011-07-01 23:49:12 +00002667 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002668 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002669 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002670 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002671 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002672 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002673}
2674
Chandler Carruth1b03c872011-07-02 00:01:44 +00002675static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002676 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002677 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002678 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002679
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002680 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002681}
2682
Chandler Carruth1b03c872011-07-02 00:01:44 +00002683static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002684 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2686 return;
2687 }
Mike Stumpbf916502009-07-24 19:02:52 +00002688
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002689 if (Attr.getNumArgs() != 0) {
2690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2691 return;
2692 }
Mike Stumpbf916502009-07-24 19:02:52 +00002693
Chandler Carruth87c44602011-07-01 23:49:12 +00002694 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002695
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002696 if (!VD || !VD->hasLocalStorage()) {
2697 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2698 return;
2699 }
Mike Stumpbf916502009-07-24 19:02:52 +00002700
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002701 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002702 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002703 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002704 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2705 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002706 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002707 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002708 Attr.getParameterName();
2709 return;
2710 }
Mike Stumpbf916502009-07-24 19:02:52 +00002711
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002712 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2713 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002714 S.Diag(Attr.getParameterLoc(),
2715 diag::err_attribute_cleanup_arg_not_function)
2716 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002717 return;
2718 }
2719
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002720 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002721 S.Diag(Attr.getParameterLoc(),
2722 diag::err_attribute_cleanup_func_must_take_one_arg)
2723 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002724 return;
2725 }
Mike Stumpbf916502009-07-24 19:02:52 +00002726
Anders Carlsson89941c12009-02-07 23:16:50 +00002727 // We're currently more strict than GCC about what function types we accept.
2728 // If this ever proves to be a problem it should be easy to fix.
2729 QualType Ty = S.Context.getPointerType(VD->getType());
2730 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002731 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2732 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002733 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002734 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2735 Attr.getParameterName() << ParamTy << Ty;
2736 return;
2737 }
Mike Stumpbf916502009-07-24 19:02:52 +00002738
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002739 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002740 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002741}
2742
Mike Stumpbf916502009-07-24 19:02:52 +00002743/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002744/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002745static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002746 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002747 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002748
Chandler Carruth87c44602011-07-01 23:49:12 +00002749 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002750 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002751 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002752 return;
2753 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002754
2755 // In C++ the implicit 'this' function parameter also counts, and they are
2756 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002757 bool HasImplicitThisParam = isInstanceMethod(D);
2758 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002759 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002760
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002761 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002762 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002763 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002764 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2765 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002766 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2767 << "format" << 2 << IdxExpr->getSourceRange();
2768 return;
2769 }
Mike Stumpbf916502009-07-24 19:02:52 +00002770
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002771 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2772 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2773 << "format" << 2 << IdxExpr->getSourceRange();
2774 return;
2775 }
Mike Stumpbf916502009-07-24 19:02:52 +00002776
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002777 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002778
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002779 if (HasImplicitThisParam) {
2780 if (ArgIdx == 0) {
2781 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2782 << "format_arg" << IdxExpr->getSourceRange();
2783 return;
2784 }
2785 ArgIdx--;
2786 }
2787
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002788 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002789 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002790
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002791 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2792 if (not_nsstring_type &&
2793 !isCFStringType(Ty, S.Context) &&
2794 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002795 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002796 // FIXME: Should highlight the actual expression that has the wrong type.
2797 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002798 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002799 << IdxExpr->getSourceRange();
2800 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002801 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002802 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002803 if (!isNSStringType(Ty, S.Context) &&
2804 !isCFStringType(Ty, S.Context) &&
2805 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002806 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002807 // FIXME: Should highlight the actual expression that has the wrong type.
2808 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002809 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002810 << IdxExpr->getSourceRange();
2811 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002812 }
2813
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002814 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002815 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002816}
2817
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002818enum FormatAttrKind {
2819 CFStringFormat,
2820 NSStringFormat,
2821 StrftimeFormat,
2822 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002823 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002824 InvalidFormat
2825};
2826
2827/// getFormatAttrKind - Map from format attribute names to supported format
2828/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002829static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002830 return llvm::StringSwitch<FormatAttrKind>(Format)
2831 // Check for formats that get handled specially.
2832 .Case("NSString", NSStringFormat)
2833 .Case("CFString", CFStringFormat)
2834 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002835
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002836 // Otherwise, check for supported formats.
2837 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2838 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2839 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002840
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002841 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2842 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002843}
2844
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002845/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002846/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002847static void handleInitPriorityAttr(Sema &S, Decl *D,
2848 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002849 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2851 return;
2852 }
2853
Chandler Carruth87c44602011-07-01 23:49:12 +00002854 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002855 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2856 Attr.setInvalid();
2857 return;
2858 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002859 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002860 if (S.Context.getAsArrayType(T))
2861 T = S.Context.getBaseElementType(T);
2862 if (!T->getAs<RecordType>()) {
2863 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2864 Attr.setInvalid();
2865 return;
2866 }
2867
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002868 if (Attr.getNumArgs() != 1) {
2869 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2870 Attr.setInvalid();
2871 return;
2872 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002873 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002874
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002875 llvm::APSInt priority(32);
2876 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2877 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2879 << "init_priority" << priorityExpr->getSourceRange();
2880 Attr.setInvalid();
2881 return;
2882 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002883 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002884 if (prioritynum < 101 || prioritynum > 65535) {
2885 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2886 << priorityExpr->getSourceRange();
2887 Attr.setInvalid();
2888 return;
2889 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002890 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002891 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002892}
2893
Rafael Espindola599f1b72012-05-13 03:25:18 +00002894FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2895 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002896 // Check whether we already have an equivalent format attribute.
2897 for (specific_attr_iterator<FormatAttr>
2898 i = D->specific_attr_begin<FormatAttr>(),
2899 e = D->specific_attr_end<FormatAttr>();
2900 i != e ; ++i) {
2901 FormatAttr *f = *i;
2902 if (f->getType() == Format &&
2903 f->getFormatIdx() == FormatIdx &&
2904 f->getFirstArg() == FirstArg) {
2905 // If we don't have a valid location for this attribute, adopt the
2906 // location.
2907 if (f->getLocation().isInvalid())
2908 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002909 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002910 }
2911 }
2912
Rafael Espindola599f1b72012-05-13 03:25:18 +00002913 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2914 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002915}
2916
Mike Stumpbf916502009-07-24 19:02:52 +00002917/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002918/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002919static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002920
Chris Lattner545dd342008-06-28 23:36:30 +00002921 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002923 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002924 return;
2925 }
2926
Chris Lattner545dd342008-06-28 23:36:30 +00002927 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002928 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002929 return;
2930 }
2931
Chandler Carruth87c44602011-07-01 23:49:12 +00002932 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002933 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002934 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002935 return;
2936 }
2937
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002938 // In C++ the implicit 'this' function parameter also counts, and they are
2939 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002940 bool HasImplicitThisParam = isInstanceMethod(D);
2941 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002942 unsigned FirstIdx = 1;
2943
Chris Lattner5f9e2722011-07-23 10:55:15 +00002944 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002945
2946 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002947 if (Format.startswith("__") && Format.endswith("__"))
2948 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002949
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002950 // Check for supported formats.
2951 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002952
2953 if (Kind == IgnoredFormat)
2954 return;
2955
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002956 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002957 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002958 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002959 return;
2960 }
2961
2962 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002963 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002964 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002965 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2966 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002968 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002969 return;
2970 }
2971
2972 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002973 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002974 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002975 return;
2976 }
2977
2978 // FIXME: Do we need to bounds check?
2979 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002980
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002981 if (HasImplicitThisParam) {
2982 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002983 S.Diag(Attr.getLoc(),
2984 diag::err_format_attribute_implicit_this_format_string)
2985 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002986 return;
2987 }
2988 ArgIdx--;
2989 }
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Chris Lattner6b6b5372008-06-26 18:38:35 +00002991 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002992 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002993
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002994 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002995 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002996 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2997 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002998 return;
2999 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003000 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003001 // FIXME: do we need to check if the type is NSString*? What are the
3002 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003003 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003004 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003005 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3006 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003007 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003008 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003009 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003010 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003011 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003012 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3013 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003014 return;
3015 }
3016
3017 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003018 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003019 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003020 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3021 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003023 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003024 return;
3025 }
3026
3027 // check if the function is variadic if the 3rd argument non-zero
3028 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003029 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003030 ++NumArgs; // +1 for ...
3031 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003032 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003033 return;
3034 }
3035 }
3036
Chris Lattner3c73c412008-11-19 08:23:25 +00003037 // strftime requires FirstArg to be 0 because it doesn't read from any
3038 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003039 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003040 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003041 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3042 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003043 return;
3044 }
3045 // if 0 it disables parameter checking (to use with e.g. va_list)
3046 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003047 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003048 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003049 return;
3050 }
3051
Rafael Espindola599f1b72012-05-13 03:25:18 +00003052 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3053 Idx.getZExtValue(),
3054 FirstArg.getZExtValue());
3055 if (NewAttr)
3056 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003057}
3058
Chandler Carruth1b03c872011-07-02 00:01:44 +00003059static void handleTransparentUnionAttr(Sema &S, Decl *D,
3060 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003061 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003062 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003063 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003064
Chris Lattner6b6b5372008-06-26 18:38:35 +00003065
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003066 // Try to find the underlying union declaration.
3067 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003068 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003069 if (TD && TD->getUnderlyingType()->isUnionType())
3070 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3071 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003072 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003073
3074 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003076 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003077 return;
3078 }
3079
John McCall5e1cdac2011-10-07 06:10:15 +00003080 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003081 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003082 diag::warn_transparent_union_attribute_not_definition);
3083 return;
3084 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003085
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003086 RecordDecl::field_iterator Field = RD->field_begin(),
3087 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003088 if (Field == FieldEnd) {
3089 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3090 return;
3091 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003092
David Blaikie581deb32012-06-06 20:45:41 +00003093 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003094 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003095 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003096 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003097 diag::warn_transparent_union_attribute_floating)
3098 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003099 return;
3100 }
3101
3102 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3103 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3104 for (; Field != FieldEnd; ++Field) {
3105 QualType FieldType = Field->getType();
3106 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3107 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3108 // Warn if we drop the attribute.
3109 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003110 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003111 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003112 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003113 diag::warn_transparent_union_attribute_field_size_align)
3114 << isSize << Field->getDeclName() << FieldBits;
3115 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003116 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003117 diag::note_transparent_union_first_field_size_align)
3118 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003119 return;
3120 }
3121 }
3122
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003123 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003124}
3125
Chandler Carruth1b03c872011-07-02 00:01:44 +00003126static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003127 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003128 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003129 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003130
Peter Collingbourne7a730022010-11-23 20:45:58 +00003131 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003132 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003133
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 // Make sure that there is a string literal as the annotation's single
3135 // argument.
3136 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003137 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003138 return;
3139 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003140
3141 // Don't duplicate annotations that are already set.
3142 for (specific_attr_iterator<AnnotateAttr>
3143 i = D->specific_attr_begin<AnnotateAttr>(),
3144 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3145 if ((*i)->getAnnotation() == SE->getString())
3146 return;
3147 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003148 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003149 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003150}
3151
Chandler Carruth1b03c872011-07-02 00:01:44 +00003152static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003153 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003154 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003155 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003156 return;
3157 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003158
Sean Huntbbd37c62009-11-21 08:43:09 +00003159 //FIXME: The C++0x version of this attribute has more limited applicabilty
3160 // than GNU's, and should error out when it is used to specify a
3161 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003162
Chris Lattner545dd342008-06-28 23:36:30 +00003163 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003164 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3165 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003166 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003167 }
Mike Stumpbf916502009-07-24 19:02:52 +00003168
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003169 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3170 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003171}
3172
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003173void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3174 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003175 // FIXME: Handle pack-expansions here.
3176 if (DiagnoseUnexpandedParameterPack(E))
3177 return;
3178
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003179 if (E->isTypeDependent() || E->isValueDependent()) {
3180 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003181 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3182 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003183 return;
3184 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003185
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003186 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003187 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003188 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003189 ExprResult ICE
3190 = VerifyIntegerConstantExpression(E, &Alignment,
3191 diag::err_aligned_attribute_argument_not_int,
3192 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003193 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003194 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003195 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003196 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3197 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003198 return;
3199 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003200 if (isDeclSpec) {
3201 // We've already verified it's a power of 2, now let's make sure it's
3202 // 8192 or less.
3203 if (Alignment.getZExtValue() > 8192) {
3204 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3205 << E->getSourceRange();
3206 return;
3207 }
3208 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003209
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003210 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3211 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003212}
3213
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003214void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3215 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003216 // FIXME: Cache the number on the Attr object if non-dependent?
3217 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003218 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3219 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003220 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003221}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003222
Chandler Carruthd309c812011-07-01 23:49:16 +00003223/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003224/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003225///
Mike Stumpbf916502009-07-24 19:02:52 +00003226/// Despite what would be logical, the mode attribute is a decl attribute, not a
3227/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3228/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003229static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003230 // This attribute isn't documented, but glibc uses it. It changes
3231 // the width of an int or unsigned int to the specified size.
3232
3233 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003234 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003235 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003236
Chris Lattnerfbf13472008-06-27 22:18:37 +00003237
3238 IdentifierInfo *Name = Attr.getParameterName();
3239 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003240 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003241 return;
3242 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003243
Chris Lattner5f9e2722011-07-23 10:55:15 +00003244 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003245
3246 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003247 if (Str.startswith("__") && Str.endswith("__"))
3248 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003249
3250 unsigned DestWidth = 0;
3251 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003252 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003253 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003254 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003255 switch (Str[0]) {
3256 case 'Q': DestWidth = 8; break;
3257 case 'H': DestWidth = 16; break;
3258 case 'S': DestWidth = 32; break;
3259 case 'D': DestWidth = 64; break;
3260 case 'X': DestWidth = 96; break;
3261 case 'T': DestWidth = 128; break;
3262 }
3263 if (Str[1] == 'F') {
3264 IntegerMode = false;
3265 } else if (Str[1] == 'C') {
3266 IntegerMode = false;
3267 ComplexMode = true;
3268 } else if (Str[1] != 'I') {
3269 DestWidth = 0;
3270 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003271 break;
3272 case 4:
3273 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3274 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003275 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003276 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003277 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003278 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003279 break;
3280 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003281 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003282 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003283 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003284 case 11:
3285 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003286 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003287 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003288 }
3289
3290 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003291 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003292 OldTy = TD->getUnderlyingType();
3293 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3294 OldTy = VD->getType();
3295 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003296 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003297 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003298 return;
3299 }
Eli Friedman73397492009-03-03 06:41:03 +00003300
John McCall183700f2009-09-21 23:43:11 +00003301 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003302 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3303 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003304 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003305 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3306 } else if (ComplexMode) {
3307 if (!OldTy->isComplexType())
3308 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3309 } else {
3310 if (!OldTy->isFloatingType())
3311 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3312 }
3313
Mike Stump390b4cc2009-05-16 07:39:55 +00003314 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3315 // and friends, at least with glibc.
3316 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3317 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003318 // FIXME: Make sure floating-point mappings are accurate
3319 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003320 QualType NewTy;
3321 switch (DestWidth) {
3322 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003323 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003324 return;
3325 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003326 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003327 return;
3328 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003329 if (!IntegerMode) {
3330 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3331 return;
3332 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003333 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003334 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003335 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003336 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003337 break;
3338 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003339 if (!IntegerMode) {
3340 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3341 return;
3342 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003343 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003344 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003345 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003346 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003347 break;
3348 case 32:
3349 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003350 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003351 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003352 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003353 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003354 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003355 break;
3356 case 64:
3357 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003358 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003359 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003360 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003361 NewTy = S.Context.LongTy;
3362 else
3363 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003364 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003365 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003366 NewTy = S.Context.UnsignedLongTy;
3367 else
3368 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003369 break;
Eli Friedman73397492009-03-03 06:41:03 +00003370 case 96:
3371 NewTy = S.Context.LongDoubleTy;
3372 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003373 case 128:
3374 if (!IntegerMode) {
3375 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3376 return;
3377 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003378 if (OldTy->isSignedIntegerType())
3379 NewTy = S.Context.Int128Ty;
3380 else
3381 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003382 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003383 }
3384
Eli Friedman73397492009-03-03 06:41:03 +00003385 if (ComplexMode) {
3386 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003387 }
3388
3389 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003390 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003391 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003392 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003393 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003394 cast<ValueDecl>(D)->setType(NewTy);
3395}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003396
Chandler Carruth1b03c872011-07-02 00:01:44 +00003397static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003398 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003399 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003400 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003401
Nick Lewycky78d1a102012-07-24 01:40:49 +00003402 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3403 if (!VD->hasGlobalStorage())
3404 S.Diag(Attr.getLoc(),
3405 diag::warn_attribute_requires_functions_or_static_globals)
3406 << Attr.getName();
3407 } else if (!isFunctionOrMethod(D)) {
3408 S.Diag(Attr.getLoc(),
3409 diag::warn_attribute_requires_functions_or_static_globals)
3410 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003411 return;
3412 }
Mike Stumpbf916502009-07-24 19:02:52 +00003413
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003414 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003415}
3416
Chandler Carruth1b03c872011-07-02 00:01:44 +00003417static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003418 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003419 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003420 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003421
Mike Stumpbf916502009-07-24 19:02:52 +00003422
Chandler Carruth87c44602011-07-01 23:49:12 +00003423 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003424 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003425 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003426 return;
3427 }
Mike Stumpbf916502009-07-24 19:02:52 +00003428
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003429 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003430}
3431
Chandler Carruth1b03c872011-07-02 00:01:44 +00003432static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3433 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003434 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003435 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003436 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003437
Chris Lattner7255a2d2010-06-22 00:03:40 +00003438
Chandler Carruth87c44602011-07-01 23:49:12 +00003439 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003440 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003441 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003442 return;
3443 }
3444
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003445 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003446 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003447}
3448
Chandler Carruth1b03c872011-07-02 00:01:44 +00003449static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003450 if (S.LangOpts.CUDA) {
3451 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003452 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3454 return;
3455 }
3456
Chandler Carruth87c44602011-07-01 23:49:12 +00003457 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003458 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003459 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003460 return;
3461 }
3462
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003463 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003464 } else {
3465 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3466 }
3467}
3468
Chandler Carruth1b03c872011-07-02 00:01:44 +00003469static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003470 if (S.LangOpts.CUDA) {
3471 // check the attribute arguments.
3472 if (Attr.getNumArgs() != 0) {
3473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3474 return;
3475 }
3476
Chandler Carruth87c44602011-07-01 23:49:12 +00003477 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003478 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003479 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003480 return;
3481 }
3482
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003483 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003484 } else {
3485 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3486 }
3487}
3488
Chandler Carruth1b03c872011-07-02 00:01:44 +00003489static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003490 if (S.LangOpts.CUDA) {
3491 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003492 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003493 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003494
Chandler Carruth87c44602011-07-01 23:49:12 +00003495 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003496 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003497 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003498 return;
3499 }
3500
Chandler Carruth87c44602011-07-01 23:49:12 +00003501 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003502 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003503 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003504 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3505 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3506 << FD->getType()
3507 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3508 "void");
3509 } else {
3510 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3511 << FD->getType();
3512 }
3513 return;
3514 }
3515
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003516 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003517 } else {
3518 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3519 }
3520}
3521
Chandler Carruth1b03c872011-07-02 00:01:44 +00003522static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003523 if (S.LangOpts.CUDA) {
3524 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003525 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003526 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003527
Peter Collingbourneced76712010-12-01 03:15:31 +00003528
Chandler Carruth87c44602011-07-01 23:49:12 +00003529 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003531 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003532 return;
3533 }
3534
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003535 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003536 } else {
3537 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3538 }
3539}
3540
Chandler Carruth1b03c872011-07-02 00:01:44 +00003541static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003542 if (S.LangOpts.CUDA) {
3543 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003544 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003545 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003546
Peter Collingbourneced76712010-12-01 03:15:31 +00003547
Chandler Carruth87c44602011-07-01 23:49:12 +00003548 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003550 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003551 return;
3552 }
3553
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003554 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003555 } else {
3556 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3557 }
3558}
3559
Chandler Carruth1b03c872011-07-02 00:01:44 +00003560static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003561 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003562 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003563 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003564
Chandler Carruth87c44602011-07-01 23:49:12 +00003565 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003566 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003568 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003569 return;
3570 }
Mike Stumpbf916502009-07-24 19:02:52 +00003571
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003572 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003573 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003574 return;
3575 }
Mike Stumpbf916502009-07-24 19:02:52 +00003576
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003577 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003578}
3579
Chandler Carruth1b03c872011-07-02 00:01:44 +00003580static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003581 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003582
Aaron Ballmanfff32482012-12-09 17:45:41 +00003583 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003584 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003585 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3586 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003587 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003588 return;
3589
Chandler Carruth87c44602011-07-01 23:49:12 +00003590 if (!isa<ObjCMethodDecl>(D)) {
3591 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3592 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003593 return;
3594 }
3595
Chandler Carruth87c44602011-07-01 23:49:12 +00003596 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003597 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003598 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003599 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003600 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003601 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003602 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003603 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003604 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003605 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003606 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003607 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003608 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003609 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003610 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003611 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003612 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003613 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003614 switch (CC) {
3615 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003616 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003617 break;
3618 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003619 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003620 break;
3621 default:
3622 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003623 }
3624
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003625 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuff263366f2012-10-16 22:30:41 +00003626 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003627 }
Derek Schuff263366f2012-10-16 22:30:41 +00003628 case AttributeList::AT_PnaclCall:
3629 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3630 return;
Guy Benyei38980082012-12-25 08:53:55 +00003631 case AttributeList::AT_IntelOclBicc:
3632 D->addAttr(::new (S.Context) IntelOclBiccAttr(Attr.getRange(), S.Context));
3633 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003634
Abramo Bagnarae215f722010-04-30 13:10:51 +00003635 default:
3636 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003637 }
3638}
3639
Chandler Carruth1b03c872011-07-02 00:01:44 +00003640static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003641 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003642 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003643}
3644
Aaron Ballmanfff32482012-12-09 17:45:41 +00003645bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3646 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003647 if (attr.isInvalid())
3648 return true;
3649
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003650 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3651 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3652 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003653 attr.setInvalid();
3654 return true;
3655 }
3656
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003657 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3658 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003659 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003660 case AttributeList::AT_CDecl: CC = CC_C; break;
3661 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3662 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3663 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3664 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3665 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003666 Expr *Arg = attr.getArg(0);
3667 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003668 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003669 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3670 << "pcs" << 1;
3671 attr.setInvalid();
3672 return true;
3673 }
3674
Chris Lattner5f9e2722011-07-23 10:55:15 +00003675 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003676 if (StrRef == "aapcs") {
3677 CC = CC_AAPCS;
3678 break;
3679 } else if (StrRef == "aapcs-vfp") {
3680 CC = CC_AAPCS_VFP;
3681 break;
3682 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003683
3684 attr.setInvalid();
3685 Diag(attr.getLoc(), diag::err_invalid_pcs);
3686 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003687 }
Derek Schuff263366f2012-10-16 22:30:41 +00003688 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003689 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003690 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003691 }
3692
Aaron Ballman82bfa192012-10-02 14:26:08 +00003693 const TargetInfo &TI = Context.getTargetInfo();
3694 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3695 if (A == TargetInfo::CCCR_Warning) {
3696 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003697
3698 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3699 if (FD)
3700 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3701 TargetInfo::CCMT_NonMember;
3702 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003703 }
3704
John McCall711c52b2011-01-05 12:14:39 +00003705 return false;
3706}
3707
Chandler Carruth1b03c872011-07-02 00:01:44 +00003708static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003709 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003710
3711 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003712 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003713 return;
3714
Chandler Carruth87c44602011-07-01 23:49:12 +00003715 if (!isa<ObjCMethodDecl>(D)) {
3716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3717 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003718 return;
3719 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003720
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003721 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003722}
3723
3724/// Checks a regparm attribute, returning true if it is ill-formed and
3725/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003726bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3727 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003728 return true;
3729
Chandler Carruth87c44602011-07-01 23:49:12 +00003730 if (Attr.getNumArgs() != 1) {
3731 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3732 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003733 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003734 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003735
Chandler Carruth87c44602011-07-01 23:49:12 +00003736 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003737 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003738 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003739 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003740 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003741 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003742 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003743 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003744 }
3745
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003746 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003747 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003748 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003749 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003750 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003751 }
3752
John McCall711c52b2011-01-05 12:14:39 +00003753 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003754 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003755 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003756 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003757 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003758 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003759 }
3760
John McCall711c52b2011-01-05 12:14:39 +00003761 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003762}
3763
Chandler Carruth1b03c872011-07-02 00:01:44 +00003764static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003765 if (S.LangOpts.CUDA) {
3766 // check the attribute arguments.
3767 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003768 // FIXME: 0 is not okay.
3769 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003770 return;
3771 }
3772
Chandler Carruth87c44602011-07-01 23:49:12 +00003773 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003774 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003775 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003776 return;
3777 }
3778
3779 Expr *MaxThreadsExpr = Attr.getArg(0);
3780 llvm::APSInt MaxThreads(32);
3781 if (MaxThreadsExpr->isTypeDependent() ||
3782 MaxThreadsExpr->isValueDependent() ||
3783 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3784 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3785 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3786 return;
3787 }
3788
3789 llvm::APSInt MinBlocks(32);
3790 if (Attr.getNumArgs() > 1) {
3791 Expr *MinBlocksExpr = Attr.getArg(1);
3792 if (MinBlocksExpr->isTypeDependent() ||
3793 MinBlocksExpr->isValueDependent() ||
3794 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3795 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3796 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3797 return;
3798 }
3799 }
3800
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003801 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003802 MaxThreads.getZExtValue(),
3803 MinBlocks.getZExtValue()));
3804 } else {
3805 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3806 }
3807}
3808
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003809static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3810 const AttributeList &Attr) {
3811 StringRef AttrName = Attr.getName()->getName();
3812 if (!Attr.getParameterName()) {
3813 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3814 << Attr.getName() << /* arg num = */ 1;
3815 return;
3816 }
3817
3818 if (Attr.getNumArgs() != 2) {
3819 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3820 << /* required args = */ 3;
3821 return;
3822 }
3823
3824 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3825
3826 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3827 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3828 << Attr.getName() << ExpectedFunctionOrMethod;
3829 return;
3830 }
3831
3832 uint64_t ArgumentIdx;
3833 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3834 Attr.getLoc(), 2,
3835 Attr.getArg(0), ArgumentIdx))
3836 return;
3837
3838 uint64_t TypeTagIdx;
3839 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3840 Attr.getLoc(), 3,
3841 Attr.getArg(1), TypeTagIdx))
3842 return;
3843
3844 bool IsPointer = (AttrName == "pointer_with_type_tag");
3845 if (IsPointer) {
3846 // Ensure that buffer has a pointer type.
3847 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3848 if (!BufferTy->isPointerType()) {
3849 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3850 << AttrName;
3851 }
3852 }
3853
3854 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3855 S.Context,
3856 ArgumentKind,
3857 ArgumentIdx,
3858 TypeTagIdx,
3859 IsPointer));
3860}
3861
3862static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3863 const AttributeList &Attr) {
3864 IdentifierInfo *PointerKind = Attr.getParameterName();
3865 if (!PointerKind) {
3866 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3867 << "type_tag_for_datatype" << 1;
3868 return;
3869 }
3870
3871 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3872
3873 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3874 Attr.getRange(),
3875 S.Context,
3876 PointerKind,
3877 MatchingCType,
3878 Attr.getLayoutCompatible(),
3879 Attr.getMustBeNull()));
3880}
3881
Chris Lattner0744e5f2008-06-29 00:23:49 +00003882//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003883// Checker-specific attribute handlers.
3884//===----------------------------------------------------------------------===//
3885
John McCallc7ad3812011-01-25 03:31:58 +00003886static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003887 return type->isDependentType() ||
3888 type->isObjCObjectPointerType() ||
3889 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003890}
3891static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003892 return type->isDependentType() ||
3893 type->isPointerType() ||
3894 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003895}
3896
Chandler Carruth1b03c872011-07-02 00:01:44 +00003897static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003898 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003899 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003900 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003901 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003902 return;
3903 }
3904
3905 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003906 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003907 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3908 cf = false;
3909 } else {
3910 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3911 cf = true;
3912 }
3913
3914 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003915 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003916 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003917 return;
3918 }
3919
3920 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003921 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003922 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003923 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003924}
3925
Chandler Carruth1b03c872011-07-02 00:01:44 +00003926static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3927 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003928 if (!isa<ObjCMethodDecl>(D)) {
3929 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003930 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003931 return;
3932 }
3933
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003934 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003935}
3936
Chandler Carruth1b03c872011-07-02 00:01:44 +00003937static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3938 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003939
John McCallc7ad3812011-01-25 03:31:58 +00003940 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003941
Chandler Carruth87c44602011-07-01 23:49:12 +00003942 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003943 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003944 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003945 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003946 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003947 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3948 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003949 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003950 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003951 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003952 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003953 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003954 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003955 return;
3956 }
Mike Stumpbf916502009-07-24 19:02:52 +00003957
John McCallc7ad3812011-01-25 03:31:58 +00003958 bool typeOK;
3959 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003960 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003961 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003962 case AttributeList::AT_NSReturnsAutoreleased:
3963 case AttributeList::AT_NSReturnsRetained:
3964 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003965 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3966 cf = false;
3967 break;
3968
Sean Hunt8e083e72012-06-19 23:57:03 +00003969 case AttributeList::AT_CFReturnsRetained:
3970 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003971 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3972 cf = true;
3973 break;
3974 }
3975
3976 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003977 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003978 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003979 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003980 }
Mike Stumpbf916502009-07-24 19:02:52 +00003981
Chandler Carruth87c44602011-07-01 23:49:12 +00003982 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003983 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003984 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003985 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003986 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003987 S.Context));
3988 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003989 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003990 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003991 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003992 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003993 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003994 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003995 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003996 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003997 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003998 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003999 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004000 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004001 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004002 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00004003 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004004 return;
4005 };
4006}
4007
John McCalldc7c5ad2011-07-22 08:53:00 +00004008static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4009 const AttributeList &attr) {
4010 SourceLocation loc = attr.getLoc();
4011
4012 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4013
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004014 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004015 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004016 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004017 return;
4018 }
4019
4020 // Check that the method returns a normal pointer.
4021 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004022
4023 if (!resultType->isReferenceType() &&
4024 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004025 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4026 << SourceRange(loc)
4027 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4028
4029 // Drop the attribute.
4030 return;
4031 }
4032
4033 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004034 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00004035}
4036
Fariborz Jahanian84101132012-09-07 23:46:23 +00004037static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4038 const AttributeList &attr) {
4039 SourceLocation loc = attr.getLoc();
4040 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4041
4042 if (!method) {
4043 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4044 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4045 return;
4046 }
4047 DeclContext *DC = method->getDeclContext();
4048 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4049 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4050 << attr.getName() << 0;
4051 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4052 return;
4053 }
4054 if (method->getMethodFamily() == OMF_dealloc) {
4055 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4056 << attr.getName() << 1;
4057 return;
4058 }
4059
4060 method->addAttr(
4061 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4062}
4063
John McCall8dfac0b2011-09-30 05:12:12 +00004064/// Handle cf_audited_transfer and cf_unknown_transfer.
4065static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4066 if (!isa<FunctionDecl>(D)) {
4067 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004068 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004069 return;
4070 }
4071
Sean Hunt8e083e72012-06-19 23:57:03 +00004072 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004073
4074 // Check whether there's a conflicting attribute already present.
4075 Attr *Existing;
4076 if (IsAudited) {
4077 Existing = D->getAttr<CFUnknownTransferAttr>();
4078 } else {
4079 Existing = D->getAttr<CFAuditedTransferAttr>();
4080 }
4081 if (Existing) {
4082 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4083 << A.getName()
4084 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4085 << A.getRange() << Existing->getRange();
4086 return;
4087 }
4088
4089 // All clear; add the attribute.
4090 if (IsAudited) {
4091 D->addAttr(
4092 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4093 } else {
4094 D->addAttr(
4095 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4096 }
4097}
4098
John McCallfe98da02011-09-29 07:17:38 +00004099static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4100 const AttributeList &Attr) {
4101 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4102 if (!RD || RD->isUnion()) {
4103 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004104 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004105 }
4106
4107 IdentifierInfo *ParmName = Attr.getParameterName();
4108
4109 // In Objective-C, verify that the type names an Objective-C type.
4110 // We don't want to check this outside of ObjC because people sometimes
4111 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004112 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004113 // Check for an existing type with this name.
4114 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4115 Sema::LookupOrdinaryName);
4116 if (S.LookupName(R, Sc)) {
4117 NamedDecl *Target = R.getFoundDecl();
4118 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4119 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4120 S.Diag(Target->getLocStart(), diag::note_declared_at);
4121 }
4122 }
4123 }
4124
4125 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4126 ParmName));
4127}
4128
Chandler Carruth1b03c872011-07-02 00:01:44 +00004129static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4130 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004131 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004132
Chandler Carruth87c44602011-07-01 23:49:12 +00004133 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004134 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004135}
4136
Chandler Carruth1b03c872011-07-02 00:01:44 +00004137static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4138 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004139 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004140 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004141 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004142 return;
4143 }
4144
Chandler Carruth87c44602011-07-01 23:49:12 +00004145 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004146 QualType type = vd->getType();
4147
4148 if (!type->isDependentType() &&
4149 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004150 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004151 << type;
4152 return;
4153 }
4154
4155 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4156
4157 // If we have no lifetime yet, check the lifetime we're presumably
4158 // going to infer.
4159 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4160 lifetime = type->getObjCARCImplicitLifetime();
4161
4162 switch (lifetime) {
4163 case Qualifiers::OCL_None:
4164 assert(type->isDependentType() &&
4165 "didn't infer lifetime for non-dependent type?");
4166 break;
4167
4168 case Qualifiers::OCL_Weak: // meaningful
4169 case Qualifiers::OCL_Strong: // meaningful
4170 break;
4171
4172 case Qualifiers::OCL_ExplicitNone:
4173 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004174 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004175 << (lifetime == Qualifiers::OCL_Autoreleasing);
4176 break;
4177 }
4178
Chandler Carruth87c44602011-07-01 23:49:12 +00004179 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004180 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004181}
4182
Francois Pichet11542142010-12-19 06:50:37 +00004183//===----------------------------------------------------------------------===//
4184// Microsoft specific attribute handlers.
4185//===----------------------------------------------------------------------===//
4186
Chandler Carruth1b03c872011-07-02 00:01:44 +00004187static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004188 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004189 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004190 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004191 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004192
Francois Pichet11542142010-12-19 06:50:37 +00004193 Expr *Arg = Attr.getArg(0);
4194 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004195 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004196 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4197 << "uuid" << 1;
4198 return;
4199 }
4200
Chris Lattner5f9e2722011-07-23 10:55:15 +00004201 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004202
4203 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4204 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004205
Francois Pichetd3d3be92010-12-20 01:41:49 +00004206 // Validate GUID length.
4207 if (IsCurly && StrRef.size() != 38) {
4208 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4209 return;
4210 }
4211 if (!IsCurly && StrRef.size() != 36) {
4212 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4213 return;
4214 }
4215
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004216 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004217 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004218 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004219 if (IsCurly) // Skip the optional '{'
4220 ++I;
4221
4222 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004223 if (i == 8 || i == 13 || i == 18 || i == 23) {
4224 if (*I != '-') {
4225 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4226 return;
4227 }
4228 } else if (!isxdigit(*I)) {
4229 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4230 return;
4231 }
4232 I++;
4233 }
Francois Pichet11542142010-12-19 06:50:37 +00004234
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004235 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004236 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004237 } else
Francois Pichet11542142010-12-19 06:50:37 +00004238 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004239}
4240
John McCallc052dbb2012-05-22 21:28:12 +00004241static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004242 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004243 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004244 return;
4245 }
4246
4247 AttributeList::Kind Kind = Attr.getKind();
4248 if (Kind == AttributeList::AT_SingleInheritance)
4249 D->addAttr(
4250 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4251 else if (Kind == AttributeList::AT_MultipleInheritance)
4252 D->addAttr(
4253 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4254 else if (Kind == AttributeList::AT_VirtualInheritance)
4255 D->addAttr(
4256 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
John McCallc052dbb2012-05-22 21:28:12 +00004257}
4258
4259static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4260 if (S.LangOpts.MicrosoftExt) {
4261 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004262 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004263 D->addAttr(
4264 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004265 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004266 D->addAttr(
4267 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004268 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004269 D->addAttr(
4270 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4271 } else
4272 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4273}
4274
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004275static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4276 if (S.LangOpts.MicrosoftExt)
4277 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4278 else
4279 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4280}
4281
Ted Kremenekb71368d2009-05-09 02:44:38 +00004282//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004283// Top Level Sema Entry Points
4284//===----------------------------------------------------------------------===//
4285
Chandler Carruth1b03c872011-07-02 00:01:44 +00004286static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4287 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004288 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004289 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4290 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4291 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004292 default:
4293 break;
4294 }
4295}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004296
Chandler Carruth1b03c872011-07-02 00:01:44 +00004297static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4298 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004299 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004300 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4301 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4302 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004303 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004304 case AttributeList::AT_AddressSpace:
4305 case AttributeList::AT_OpenCLImageAccess:
4306 case AttributeList::AT_ObjCGC:
4307 case AttributeList::AT_VectorSize:
4308 case AttributeList::AT_NeonVectorType:
4309 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004310 // Ignore these, these are type attributes, handled by
4311 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004312 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004313 case AttributeList::AT_CUDADevice:
4314 case AttributeList::AT_CUDAHost:
4315 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004316 // Ignore, this is a non-inheritable attribute, handled
4317 // by ProcessNonInheritableDeclAttr.
4318 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004319 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4320 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4321 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4322 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004323 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004324 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004325 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004326 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004327 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4328 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4329 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004330 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004331 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4332 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4333 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4334 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004335 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4336 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004337 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4338 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004339 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004340 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004341 case AttributeList::AT_MinSize:
4342 handleMinSizeAttr(S, D, Attr);
4343 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004344 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4345 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4346 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4347 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4348 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004349 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004350 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004351 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4352 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4353 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4354 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4355 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004356 case AttributeList::AT_ownership_returns:
4357 case AttributeList::AT_ownership_takes:
4358 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004359 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004360 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4361 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4362 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4363 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4364 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4365 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4366 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004367
Sean Hunt8e083e72012-06-19 23:57:03 +00004368 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004369 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004370 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004371 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004372
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004374 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4375
Fariborz Jahanian84101132012-09-07 23:46:23 +00004376 case AttributeList::AT_ObjCRequiresSuper:
4377 handleObjCRequiresSuperAttr(S, D, Attr); break;
4378
Sean Hunt8e083e72012-06-19 23:57:03 +00004379 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004380 handleNSBridgedAttr(S, scope, D, Attr); break;
4381
Sean Hunt8e083e72012-06-19 23:57:03 +00004382 case AttributeList::AT_CFAuditedTransfer:
4383 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004384 handleCFTransferAttr(S, D, Attr); break;
4385
Ted Kremenekb71368d2009-05-09 02:44:38 +00004386 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004387 case AttributeList::AT_CFConsumed:
4388 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4389 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004390 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004391
Sean Hunt8e083e72012-06-19 23:57:03 +00004392 case AttributeList::AT_NSReturnsAutoreleased:
4393 case AttributeList::AT_NSReturnsNotRetained:
4394 case AttributeList::AT_CFReturnsNotRetained:
4395 case AttributeList::AT_NSReturnsRetained:
4396 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004397 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004398
Tanya Lattner0df579e2012-07-09 22:06:01 +00004399 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004400 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004401 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004402
Sean Hunt8e083e72012-06-19 23:57:03 +00004403 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004404 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004405
Sean Hunt8e083e72012-06-19 23:57:03 +00004406 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4407 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4408 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004409 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4410 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004411 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004412 handleArcWeakrefUnavailableAttr (S, D, Attr);
4413 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004414 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004415 handleObjCRootClassAttr(S, D, Attr);
4416 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004417 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004418 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004419 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004420 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4421 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004422 handleReturnsTwiceAttr(S, D, Attr);
4423 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004424 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4425 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4426 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004427 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004428 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4429 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4430 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4431 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004432 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004433 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004434 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004435 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004436 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004437 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004438 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004439 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004440 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4441 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4442 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4443 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4444 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4445 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4446 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4447 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4448 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004449 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004450 // Just ignore
4451 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004452 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004453 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004454 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004455 case AttributeList::AT_StdCall:
4456 case AttributeList::AT_CDecl:
4457 case AttributeList::AT_FastCall:
4458 case AttributeList::AT_ThisCall:
4459 case AttributeList::AT_Pascal:
4460 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004461 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004462 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004463 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004464 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004465 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004466 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004467 break;
John McCallc052dbb2012-05-22 21:28:12 +00004468
4469 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004470 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004471 handleMsStructAttr(S, D, Attr);
4472 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004473 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004474 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004475 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004476 case AttributeList::AT_SingleInheritance:
4477 case AttributeList::AT_MultipleInheritance:
4478 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004479 handleInheritanceAttr(S, D, Attr);
4480 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004481 case AttributeList::AT_Win64:
4482 case AttributeList::AT_Ptr32:
4483 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004484 handlePortabilityAttr(S, D, Attr);
4485 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004486 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004487 handleForceInlineAttr(S, D, Attr);
4488 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004489
4490 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004491 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004492 handleGuardedVarAttr(S, D, Attr);
4493 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004494 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004495 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004496 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004497 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004498 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004499 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004500 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004501 handleNoAddressSafetyAttr(S, D, Attr);
4502 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004503 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004504 handleNoThreadSafetyAttr(S, D, Attr);
4505 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004506 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004507 handleLockableAttr(S, D, Attr);
4508 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004509 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004510 handleGuardedByAttr(S, D, Attr);
4511 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004512 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004513 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004514 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004515 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004516 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004517 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004518 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004519 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004520 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004521 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004522 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004523 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004524 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004525 handleLockReturnedAttr(S, D, Attr);
4526 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004527 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004528 handleLocksExcludedAttr(S, D, Attr);
4529 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004530 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004531 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004532 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004533 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004534 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004535 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004536 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004537 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004538 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004539 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004540 handleUnlockFunAttr(S, D, Attr);
4541 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004542 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004543 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004544 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004545 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004546 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004547 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004548
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004549 // Type safety attributes.
4550 case AttributeList::AT_ArgumentWithTypeTag:
4551 handleArgumentWithTypeTagAttr(S, D, Attr);
4552 break;
4553 case AttributeList::AT_TypeTagForDatatype:
4554 handleTypeTagForDatatypeAttr(S, D, Attr);
4555 break;
4556
Chris Lattner803d0802008-06-29 00:43:07 +00004557 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004558 // Ask target about the attribute.
4559 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4560 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004561 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4562 diag::warn_unhandled_ms_attribute_ignored :
4563 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004564 break;
4565 }
4566}
4567
Peter Collingbourne60700392011-01-21 02:08:45 +00004568/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4569/// the attribute applies to decls. If the attribute is a type attribute, just
4570/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4571/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004572static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4573 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004574 bool NonInheritable, bool Inheritable) {
4575 if (Attr.isInvalid())
4576 return;
4577
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004578 // Type attributes are still treated as declaration attributes by
4579 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4580 // want to process them, however, because we will simply warn about ignoring
4581 // them. So instead, we will bail out early.
4582 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004583 return;
4584
4585 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004586 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004587
4588 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004589 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004590}
4591
Chris Lattner803d0802008-06-29 00:43:07 +00004592/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4593/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004594void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004595 const AttributeList *AttrList,
4596 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004597 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004598 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004599 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004600
4601 // GCC accepts
4602 // static int a9 __attribute__((weakref));
4603 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004604 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004605 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004606 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004607 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004608 }
4609}
4610
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004611// Annotation attributes are the only attributes allowed after an access
4612// specifier.
4613bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4614 const AttributeList *AttrList) {
4615 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004616 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004617 handleAnnotateAttr(*this, ASDecl, *l);
4618 } else {
4619 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4620 return true;
4621 }
4622 }
4623
4624 return false;
4625}
4626
John McCalle82247a2011-10-01 05:17:03 +00004627/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4628/// contains any decl attributes that we should warn about.
4629static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4630 for ( ; A; A = A->getNext()) {
4631 // Only warn if the attribute is an unignored, non-type attribute.
4632 if (A->isUsedAsTypeAttr()) continue;
4633 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4634
4635 if (A->getKind() == AttributeList::UnknownAttribute) {
4636 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4637 << A->getName() << A->getRange();
4638 } else {
4639 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4640 << A->getName() << A->getRange();
4641 }
4642 }
4643}
4644
4645/// checkUnusedDeclAttributes - Given a declarator which is not being
4646/// used to build a declaration, complain about any decl attributes
4647/// which might be lying around on it.
4648void Sema::checkUnusedDeclAttributes(Declarator &D) {
4649 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4650 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4651 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4652 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4653}
4654
Ryan Flynne25ff832009-07-30 03:15:39 +00004655/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004656/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004657NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4658 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004659 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004660 NamedDecl *NewD = 0;
4661 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004662 FunctionDecl *NewFD;
4663 // FIXME: Missing call to CheckFunctionDeclaration().
4664 // FIXME: Mangling?
4665 // FIXME: Is the qualifier info correct?
4666 // FIXME: Is the DeclContext correct?
4667 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4668 Loc, Loc, DeclarationName(II),
4669 FD->getType(), FD->getTypeSourceInfo(),
4670 SC_None, SC_None,
4671 false/*isInlineSpecified*/,
4672 FD->hasPrototype(),
4673 false/*isConstexprSpecified*/);
4674 NewD = NewFD;
4675
4676 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004677 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004678
4679 // Fake up parameter variables; they are declared as if this were
4680 // a typedef.
4681 QualType FDTy = FD->getType();
4682 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4683 SmallVector<ParmVarDecl*, 16> Params;
4684 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4685 AE = FT->arg_type_end(); AI != AE; ++AI) {
4686 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4687 Param->setScopeInfo(0, Params.size());
4688 Params.push_back(Param);
4689 }
David Blaikie4278c652011-09-21 18:16:56 +00004690 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004691 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004692 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4693 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004694 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004695 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004696 VD->getStorageClass(),
4697 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004698 if (VD->getQualifier()) {
4699 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004700 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004701 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004702 }
4703 return NewD;
4704}
4705
James Dennett1dfbd922012-06-14 21:40:34 +00004706/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004707/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004708void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004709 if (W.getUsed()) return; // only do this once
4710 W.setUsed(true);
4711 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4712 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004713 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004714 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4715 NDId->getName()));
4716 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004717 WeakTopLevelDecl.push_back(NewD);
4718 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4719 // to insert Decl at TU scope, sorry.
4720 DeclContext *SavedContext = CurContext;
4721 CurContext = Context.getTranslationUnitDecl();
4722 PushOnScopeChains(NewD, S);
4723 CurContext = SavedContext;
4724 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004725 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004726 }
4727}
4728
Chris Lattner0744e5f2008-06-29 00:23:49 +00004729/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4730/// it, apply them to D. This is a bit tricky because PD can have attributes
4731/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004732void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4733 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004734 // It's valid to "forward-declare" #pragma weak, in which case we
4735 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004736 if (Inheritable) {
4737 LoadExternalWeakUndeclaredIdentifiers();
4738 if (!WeakUndeclaredIdentifiers.empty()) {
4739 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4740 if (IdentifierInfo *Id = ND->getIdentifier()) {
4741 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4742 = WeakUndeclaredIdentifiers.find(Id);
4743 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4744 WeakInfo W = I->second;
4745 DeclApplyPragmaWeak(S, ND, W);
4746 WeakUndeclaredIdentifiers[Id] = W;
4747 }
John McCalld4aff0e2010-10-27 00:59:00 +00004748 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004749 }
4750 }
4751 }
4752
Chris Lattner0744e5f2008-06-29 00:23:49 +00004753 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004754 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004755 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004756
Chris Lattner0744e5f2008-06-29 00:23:49 +00004757 // Walk the declarator structure, applying decl attributes that were in a type
4758 // position to the decl itself. This handles cases like:
4759 // int *__attr__(x)** D;
4760 // when X is a decl attribute.
4761 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4762 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004763 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004764
Chris Lattner0744e5f2008-06-29 00:23:49 +00004765 // Finally, apply any attributes on the decl itself.
4766 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004767 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004768}
John McCall54abf7d2009-11-04 02:18:39 +00004769
John McCallf85e1932011-06-15 23:02:42 +00004770/// Is the given declaration allowed to use a forbidden type?
4771static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4772 // Private ivars are always okay. Unfortunately, people don't
4773 // always properly make their ivars private, even in system headers.
4774 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004775 // Function declarations in sys headers will be marked unavailable.
4776 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4777 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004778 return false;
4779
4780 // Require it to be declared in a system header.
4781 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4782}
4783
4784/// Handle a delayed forbidden-type diagnostic.
4785static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4786 Decl *decl) {
4787 if (decl && isForbiddenTypeAllowed(S, decl)) {
4788 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4789 "this system declaration uses an unsupported type"));
4790 return;
4791 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004792 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004793 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004794 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004795 // kind of forbidden type messages on unavailable functions.
4796 if (FD->hasAttr<UnavailableAttr>() &&
4797 diag.getForbiddenTypeDiagnostic() ==
4798 diag::err_arc_array_param_no_ownership) {
4799 diag.Triggered = true;
4800 return;
4801 }
4802 }
John McCallf85e1932011-06-15 23:02:42 +00004803
4804 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4805 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4806 diag.Triggered = true;
4807}
4808
John McCall92576642012-05-07 06:16:41 +00004809void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4810 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004811 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004812 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004813
John McCall92576642012-05-07 06:16:41 +00004814 // When delaying diagnostics to run in the context of a parsed
4815 // declaration, we only want to actually emit anything if parsing
4816 // succeeds.
4817 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004818
John McCall92576642012-05-07 06:16:41 +00004819 // We emit all the active diagnostics in this pool or any of its
4820 // parents. In general, we'll get one pool for the decl spec
4821 // and a child pool for each declarator; in a decl group like:
4822 // deprecated_typedef foo, *bar, baz();
4823 // only the declarator pops will be passed decls. This is correct;
4824 // we really do need to consider delayed diagnostics from the decl spec
4825 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004826 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004827 do {
John McCall13489672012-05-07 06:16:58 +00004828 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004829 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4830 // This const_cast is a bit lame. Really, Triggered should be mutable.
4831 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004832 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004833 continue;
4834
John McCalleee1d542011-02-14 07:13:47 +00004835 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004836 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004837 // Don't bother giving deprecation diagnostics if the decl is invalid.
4838 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004839 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004840 break;
4841
4842 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004843 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004844 break;
John McCallf85e1932011-06-15 23:02:42 +00004845
4846 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004847 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004848 break;
John McCall2f514482010-01-27 03:50:35 +00004849 }
4850 }
John McCall92576642012-05-07 06:16:41 +00004851 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004852}
4853
John McCall13489672012-05-07 06:16:58 +00004854/// Given a set of delayed diagnostics, re-emit them as if they had
4855/// been delayed in the current context instead of in the given pool.
4856/// Essentially, this just moves them to the current pool.
4857void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4858 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4859 assert(curPool && "re-emitting in undelayed context not supported");
4860 curPool->steal(pool);
4861}
4862
John McCall54abf7d2009-11-04 02:18:39 +00004863static bool isDeclDeprecated(Decl *D) {
4864 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004865 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004866 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004867 // A category implicitly has the availability of the interface.
4868 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4869 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004870 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4871 return false;
4872}
4873
Eli Friedmanc3b23082012-08-08 21:52:41 +00004874static void
4875DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4876 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004877 const ObjCInterfaceDecl *UnknownObjCClass,
4878 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00004879 DeclarationName Name = D->getDeclName();
4880 if (!Message.empty()) {
4881 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4882 S.Diag(D->getLocation(),
4883 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4884 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004885 if (ObjCPropery)
4886 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4887 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004888 } else if (!UnknownObjCClass) {
4889 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4890 S.Diag(D->getLocation(),
4891 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4892 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004893 if (ObjCPropery)
4894 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4895 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004896 } else {
4897 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4898 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4899 }
4900}
4901
John McCall9c3087b2010-08-26 02:13:20 +00004902void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004903 Decl *Ctx) {
4904 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004905 return;
4906
John McCall2f514482010-01-27 03:50:35 +00004907 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004908 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4909 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004910 DD.getUnknownObjCClass(),
4911 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00004912}
4913
Chris Lattner5f9e2722011-07-23 10:55:15 +00004914void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004915 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004916 const ObjCInterfaceDecl *UnknownObjCClass,
4917 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00004918 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004919 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004920 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4921 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004922 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004923 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004924 return;
4925 }
4926
4927 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004928 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004929 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004930 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00004931}