blob: beea474a7e1f4c07fb94da0b9cba99adb1d2d5b7 [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) {
Richard Smitha4fa9002013-01-13 02:11:23 +0000915 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
916 if (TD == 0) {
917 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
918 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +0000919 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000920 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921 }
Mike Stumpbf916502009-07-24 19:02:52 +0000922
Richard Smitha4fa9002013-01-13 02:11:23 +0000923 // Remember this typedef decl, we will need it later for diagnostics.
924 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000925}
926
Chandler Carruth1b03c872011-07-02 00:01:44 +0000927static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000928 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000929 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000930 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000931
Chandler Carruth87c44602011-07-01 23:49:12 +0000932 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000933 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000934 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000935 // If the alignment is less than or equal to 8 bits, the packed attribute
936 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000937 if (!FD->getType()->isDependentType() &&
938 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000939 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000940 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000941 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000942 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000943 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000944 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000945 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000946}
947
Chandler Carruth1b03c872011-07-02 00:01:44 +0000948static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000949 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
950 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000951 else
952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
953}
954
Chandler Carruth1b03c872011-07-02 00:01:44 +0000955static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000956 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000957 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000958 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000959
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000960 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000961 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000962 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000963 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000964 return;
965 }
966
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000967 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000968}
969
Ted Kremenek2f041d02011-09-29 07:02:25 +0000970static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
971 // The IBOutlet/IBOutletCollection attributes only apply to instance
972 // variables or properties of Objective-C classes. The outlet must also
973 // have an object reference type.
974 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
975 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000976 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000977 << Attr.getName() << VD->getType() << 0;
978 return false;
979 }
980 }
981 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
982 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000983 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000984 << Attr.getName() << PD->getType() << 1;
985 return false;
986 }
987 }
988 else {
989 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
990 return false;
991 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000992
Ted Kremenek2f041d02011-09-29 07:02:25 +0000993 return true;
994}
995
Chandler Carruth1b03c872011-07-02 00:01:44 +0000996static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000997 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000998 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000999 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001000
1001 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001002 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001003
Ted Kremenek2f041d02011-09-29 07:02:25 +00001004 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001005}
1006
Chandler Carruth1b03c872011-07-02 00:01:44 +00001007static void handleIBOutletCollection(Sema &S, Decl *D,
1008 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001009
1010 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001011 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001012 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1013 return;
1014 }
1015
Ted Kremenek2f041d02011-09-29 07:02:25 +00001016 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001017 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001018
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001019 IdentifierInfo *II = Attr.getParameterName();
1020 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001021 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001022
John McCallb3d87482010-08-24 05:47:05 +00001023 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001024 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001025 if (!TypeRep) {
1026 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1027 return;
1028 }
John McCallb3d87482010-08-24 05:47:05 +00001029 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001030 // Diagnose use of non-object type in iboutletcollection attribute.
1031 // FIXME. Gnu attribute extension ignores use of builtin types in
1032 // attributes. So, __attribute__((iboutletcollection(char))) will be
1033 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001034 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001035 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1036 return;
1037 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001038 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1039 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001040}
1041
Chandler Carruthd309c812011-07-01 23:49:16 +00001042static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001043 if (const RecordType *UT = T->getAsUnionType())
1044 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1045 RecordDecl *UD = UT->getDecl();
1046 for (RecordDecl::field_iterator it = UD->field_begin(),
1047 itend = UD->field_end(); it != itend; ++it) {
1048 QualType QT = it->getType();
1049 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1050 T = QT;
1051 return;
1052 }
1053 }
1054 }
1055}
1056
Nuno Lopes587de5b2012-05-24 00:22:00 +00001057static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001058 if (!isFunctionOrMethod(D)) {
1059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1060 << "alloc_size" << ExpectedFunctionOrMethod;
1061 return;
1062 }
1063
Nuno Lopes587de5b2012-05-24 00:22:00 +00001064 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1065 return;
1066
1067 // In C++ the implicit 'this' function parameter also counts, and they are
1068 // counted from one.
1069 bool HasImplicitThisParam = isInstanceMethod(D);
1070 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1071
1072 SmallVector<unsigned, 8> SizeArgs;
1073
1074 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1075 E = Attr.arg_end(); I!=E; ++I) {
1076 // The argument must be an integer constant expression.
1077 Expr *Ex = *I;
1078 llvm::APSInt ArgNum;
1079 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1080 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1082 << "alloc_size" << Ex->getSourceRange();
1083 return;
1084 }
1085
1086 uint64_t x = ArgNum.getZExtValue();
1087
1088 if (x < 1 || x > NumArgs) {
1089 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1090 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1091 return;
1092 }
1093
1094 --x;
1095 if (HasImplicitThisParam) {
1096 if (x == 0) {
1097 S.Diag(Attr.getLoc(),
1098 diag::err_attribute_invalid_implicit_this_argument)
1099 << "alloc_size" << Ex->getSourceRange();
1100 return;
1101 }
1102 --x;
1103 }
1104
1105 // check if the function argument is of an integer type
1106 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1107 if (!T->isIntegerType()) {
1108 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1109 << "alloc_size" << Ex->getSourceRange();
1110 return;
1111 }
1112
Nuno Lopes587de5b2012-05-24 00:22:00 +00001113 SizeArgs.push_back(x);
1114 }
1115
1116 // check if the function returns a pointer
1117 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1118 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1119 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1120 }
1121
Nuno Lopes96c67d12012-06-18 16:27:56 +00001122 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1123 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001124}
1125
Chandler Carruth1b03c872011-07-02 00:01:44 +00001126static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001127 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1128 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001129 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001130 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001131 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001132 return;
1133 }
Mike Stumpbf916502009-07-24 19:02:52 +00001134
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001135 // In C++ the implicit 'this' function parameter also counts, and they are
1136 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001137 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001138 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001139
1140 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001141 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001142
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001143 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1144 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001145 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001146 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001147 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001148 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1149 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1151 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001152 return;
1153 }
Mike Stumpbf916502009-07-24 19:02:52 +00001154
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001155 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001156
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001157 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001159 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001160 return;
1161 }
Mike Stumpbf916502009-07-24 19:02:52 +00001162
Ted Kremenek465172f2008-07-21 22:09:15 +00001163 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001164 if (HasImplicitThisParam) {
1165 if (x == 0) {
1166 S.Diag(Attr.getLoc(),
1167 diag::err_attribute_invalid_implicit_this_argument)
1168 << "nonnull" << Ex->getSourceRange();
1169 return;
1170 }
1171 --x;
1172 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001173
1174 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001175 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001176 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001177
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001178 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001179 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001180 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001181 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001182 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001183 }
Mike Stumpbf916502009-07-24 19:02:52 +00001184
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001185 NonNullArgs.push_back(x);
1186 }
Mike Stumpbf916502009-07-24 19:02:52 +00001187
1188 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1189 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001190 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001191 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1192 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001193 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001194 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001195 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001196 }
Mike Stumpbf916502009-07-24 19:02:52 +00001197
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001198 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001199 if (NonNullArgs.empty()) {
1200 // Warn the trivial case only if attribute is not coming from a
1201 // macro instantiation.
1202 if (Attr.getLoc().isFileID())
1203 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001204 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001205 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001206 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001207
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001208 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001209 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001210 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001211 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001212 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001213}
1214
Chandler Carruth1b03c872011-07-02 00:01:44 +00001215static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001216 // This attribute must be applied to a function declaration.
1217 // The first argument to the attribute must be a string,
1218 // the name of the resource, for example "malloc".
1219 // The following arguments must be argument indexes, the arguments must be
1220 // of integer type for Returns, otherwise of pointer type.
1221 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001222 // after being held. free() should be __attribute((ownership_takes)), whereas
1223 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001224
1225 if (!AL.getParameterName()) {
1226 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1227 << AL.getName()->getName() << 1;
1228 return;
1229 }
1230 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001231 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001232 switch (AL.getKind()) {
1233 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001234 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001235 if (AL.getNumArgs() < 1) {
1236 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1237 return;
1238 }
Jordy Rose2a479922010-08-12 08:54:03 +00001239 break;
1240 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001241 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001242 if (AL.getNumArgs() < 1) {
1243 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1244 return;
1245 }
Jordy Rose2a479922010-08-12 08:54:03 +00001246 break;
1247 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001248 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001249 if (AL.getNumArgs() > 1) {
1250 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1251 << AL.getNumArgs() + 1;
1252 return;
1253 }
Jordy Rose2a479922010-08-12 08:54:03 +00001254 break;
1255 default:
1256 // This should never happen given how we are called.
1257 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001258 }
1259
Chandler Carruth87c44602011-07-01 23:49:12 +00001260 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001261 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1262 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001263 return;
1264 }
1265
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001266 // In C++ the implicit 'this' function parameter also counts, and they are
1267 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001268 bool HasImplicitThisParam = isInstanceMethod(D);
1269 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001270
Chris Lattner5f9e2722011-07-23 10:55:15 +00001271 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001272
1273 // Normalize the argument, __foo__ becomes foo.
1274 if (Module.startswith("__") && Module.endswith("__"))
1275 Module = Module.substr(2, Module.size() - 4);
1276
Chris Lattner5f9e2722011-07-23 10:55:15 +00001277 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001278
Jordy Rose2a479922010-08-12 08:54:03 +00001279 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1280 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001281
Peter Collingbourne7a730022010-11-23 20:45:58 +00001282 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001283 llvm::APSInt ArgNum(32);
1284 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1285 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1286 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1287 << AL.getName()->getName() << IdxExpr->getSourceRange();
1288 continue;
1289 }
1290
1291 unsigned x = (unsigned) ArgNum.getZExtValue();
1292
1293 if (x > NumArgs || x < 1) {
1294 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1295 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1296 continue;
1297 }
1298 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001299 if (HasImplicitThisParam) {
1300 if (x == 0) {
1301 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1302 << "ownership" << IdxExpr->getSourceRange();
1303 return;
1304 }
1305 --x;
1306 }
1307
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001308 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001309 case OwnershipAttr::Takes:
1310 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001311 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001312 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001313 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1314 // FIXME: Should also highlight argument in decl.
1315 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001316 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001317 << "pointer"
1318 << IdxExpr->getSourceRange();
1319 continue;
1320 }
1321 break;
1322 }
Sean Huntcf807c42010-08-18 23:23:40 +00001323 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001324 if (AL.getNumArgs() > 1) {
1325 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001326 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001327 llvm::APSInt ArgNum(32);
1328 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1329 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1330 S.Diag(AL.getLoc(), diag::err_ownership_type)
1331 << "ownership_returns" << "integer"
1332 << IdxExpr->getSourceRange();
1333 return;
1334 }
1335 }
1336 break;
1337 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001338 } // switch
1339
1340 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001341 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001342 i = D->specific_attr_begin<OwnershipAttr>(),
1343 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001344 i != e; ++i) {
1345 if ((*i)->getOwnKind() != K) {
1346 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1347 I!=E; ++I) {
1348 if (x == *I) {
1349 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1350 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001351 }
1352 }
1353 }
1354 }
1355 OwnershipArgs.push_back(x);
1356 }
1357
1358 unsigned* start = OwnershipArgs.data();
1359 unsigned size = OwnershipArgs.size();
1360 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001361
1362 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1363 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1364 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001365 }
Sean Huntcf807c42010-08-18 23:23:40 +00001366
Chandler Carruth87c44602011-07-01 23:49:12 +00001367 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001368 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001369}
1370
Chandler Carruth1b03c872011-07-02 00:01:44 +00001371static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001372 // Check the attribute arguments.
1373 if (Attr.getNumArgs() > 1) {
1374 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1375 return;
1376 }
1377
Chandler Carruth87c44602011-07-01 23:49:12 +00001378 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001379 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001380 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001381 return;
1382 }
1383
Chandler Carruth87c44602011-07-01 23:49:12 +00001384 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001385
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001386 // gcc rejects
1387 // class c {
1388 // static int a __attribute__((weakref ("v2")));
1389 // static int b() __attribute__((weakref ("f3")));
1390 // };
1391 // and ignores the attributes of
1392 // void f(void) {
1393 // static int a __attribute__((weakref ("v2")));
1394 // }
1395 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001396 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001397 if (!Ctx->isFileContext()) {
1398 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001399 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001400 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001401 }
1402
1403 // The GCC manual says
1404 //
1405 // At present, a declaration to which `weakref' is attached can only
1406 // be `static'.
1407 //
1408 // It also says
1409 //
1410 // Without a TARGET,
1411 // given as an argument to `weakref' or to `alias', `weakref' is
1412 // equivalent to `weak'.
1413 //
1414 // gcc 4.4.1 will accept
1415 // int a7 __attribute__((weakref));
1416 // as
1417 // int a7 __attribute__((weak));
1418 // This looks like a bug in gcc. We reject that for now. We should revisit
1419 // it if this behaviour is actually used.
1420
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001421 // GCC rejects
1422 // static ((alias ("y"), weakref)).
1423 // Should we? How to check that weakref is before or after alias?
1424
1425 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001426 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001427 Arg = Arg->IgnoreParenCasts();
1428 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1429
Douglas Gregor5cee1192011-07-27 05:40:30 +00001430 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001431 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1432 << "weakref" << 1;
1433 return;
1434 }
1435 // GCC will accept anything as the argument of weakref. Should we
1436 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001437 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001438 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001439 }
1440
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001441 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001442}
1443
Chandler Carruth1b03c872011-07-02 00:01:44 +00001444static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001445 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001446 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001448 return;
1449 }
Mike Stumpbf916502009-07-24 19:02:52 +00001450
Peter Collingbourne7a730022010-11-23 20:45:58 +00001451 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001452 Arg = Arg->IgnoreParenCasts();
1453 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001454
Douglas Gregor5cee1192011-07-27 05:40:30 +00001455 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001456 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001457 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001458 return;
1459 }
Mike Stumpbf916502009-07-24 19:02:52 +00001460
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001461 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001462 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1463 return;
1464 }
1465
Chris Lattner6b6b5372008-06-26 18:38:35 +00001466 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001467
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001468 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001469 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001470}
1471
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001472static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1473 // Check the attribute arguments.
1474 if (!checkAttributeNumArgs(S, Attr, 0))
1475 return;
1476
1477 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1479 << Attr.getName() << ExpectedFunctionOrMethod;
1480 return;
1481 }
1482
1483 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1484}
1485
Benjamin Krameree409a92012-05-12 21:10:52 +00001486static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1487 // Check the attribute arguments.
1488 if (!checkAttributeNumArgs(S, Attr, 0))
1489 return;
1490
1491 if (!isa<FunctionDecl>(D)) {
1492 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1493 << Attr.getName() << ExpectedFunction;
1494 return;
1495 }
1496
1497 if (D->hasAttr<HotAttr>()) {
1498 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1499 << Attr.getName() << "hot";
1500 return;
1501 }
1502
1503 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1504}
1505
1506static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1507 // Check the attribute arguments.
1508 if (!checkAttributeNumArgs(S, Attr, 0))
1509 return;
1510
1511 if (!isa<FunctionDecl>(D)) {
1512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1513 << Attr.getName() << ExpectedFunction;
1514 return;
1515 }
1516
1517 if (D->hasAttr<ColdAttr>()) {
1518 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1519 << Attr.getName() << "cold";
1520 return;
1521 }
1522
1523 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1524}
1525
Chandler Carruth1b03c872011-07-02 00:01:44 +00001526static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001527 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001528 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001529 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001530
Chandler Carruth87c44602011-07-01 23:49:12 +00001531 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001533 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001534 return;
1535 }
1536
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001537 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001538}
1539
Chandler Carruth1b03c872011-07-02 00:01:44 +00001540static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1541 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001542 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001543 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001544 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1545 return;
1546 }
1547
Chandler Carruth87c44602011-07-01 23:49:12 +00001548 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001550 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001551 return;
1552 }
Mike Stumpbf916502009-07-24 19:02:52 +00001553
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001554 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001555}
1556
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001557static void handleTLSModelAttr(Sema &S, Decl *D,
1558 const AttributeList &Attr) {
1559 // Check the attribute arguments.
1560 if (Attr.getNumArgs() != 1) {
1561 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1562 return;
1563 }
1564
1565 Expr *Arg = Attr.getArg(0);
1566 Arg = Arg->IgnoreParenCasts();
1567 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1568
1569 // Check that it is a string.
1570 if (!Str) {
1571 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1572 return;
1573 }
1574
1575 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1576 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1577 << Attr.getName() << ExpectedTLSVar;
1578 return;
1579 }
1580
1581 // Check that the value.
1582 StringRef Model = Str->getString();
1583 if (Model != "global-dynamic" && Model != "local-dynamic"
1584 && Model != "initial-exec" && Model != "local-exec") {
1585 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1586 return;
1587 }
1588
1589 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1590 Model));
1591}
1592
Chandler Carruth1b03c872011-07-02 00:01:44 +00001593static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001594 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001595 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1597 return;
1598 }
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Chandler Carruth87c44602011-07-01 23:49:12 +00001600 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001601 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001602 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001603 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001604 return;
1605 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001606 }
1607
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001608 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001609}
1610
Chandler Carruth1b03c872011-07-02 00:01:44 +00001611static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001612 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001613 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001614 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001615
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001616 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001617}
1618
Chandler Carruth1b03c872011-07-02 00:01:44 +00001619static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001620 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001621 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001622 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001623 else
1624 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001625 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001626}
1627
Chandler Carruth1b03c872011-07-02 00:01:44 +00001628static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001629 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001630 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001631 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001632 else
1633 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001634 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001635}
1636
Chandler Carruth1b03c872011-07-02 00:01:44 +00001637static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001638 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001639
1640 if (S.CheckNoReturnAttr(attr)) return;
1641
Chandler Carruth87c44602011-07-01 23:49:12 +00001642 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001643 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001644 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001645 return;
1646 }
1647
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001648 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001649}
1650
1651bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001652 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001653 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 attr.setInvalid();
1655 return true;
1656 }
1657
1658 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001659}
1660
Chandler Carruth1b03c872011-07-02 00:01:44 +00001661static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1662 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001663
1664 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1665 // because 'analyzer_noreturn' does not impact the type.
1666
Chandler Carruth1731e202011-07-11 23:30:35 +00001667 if(!checkAttributeNumArgs(S, Attr, 0))
1668 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001669
Chandler Carruth87c44602011-07-01 23:49:12 +00001670 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1671 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001672 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1673 && !VD->getType()->isFunctionPointerType())) {
1674 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001675 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001676 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001677 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001678 return;
1679 }
1680 }
1681
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001682 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001683}
1684
Richard Smithcd8ab512013-01-17 01:30:42 +00001685static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1686 const AttributeList &Attr) {
1687 // C++11 [dcl.attr.noreturn]p1:
1688 // The attribute may be applied to the declarator-id in a function
1689 // declaration.
1690 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1691 if (!FD) {
1692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1693 << Attr.getName() << ExpectedFunctionOrMethod;
1694 return;
1695 }
1696
1697 D->addAttr(::new (S.Context) CXX11NoReturnAttr(Attr.getRange(), S.Context));
1698}
1699
John Thompson35cc9622010-08-09 21:53:52 +00001700// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001701static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001702/*
1703 Returning a Vector Class in Registers
1704
Eric Christopherf48f3672010-12-01 22:13:54 +00001705 According to the PPU ABI specifications, a class with a single member of
1706 vector type is returned in memory when used as the return value of a function.
1707 This results in inefficient code when implementing vector classes. To return
1708 the value in a single vector register, add the vecreturn attribute to the
1709 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001710
1711 Example:
1712
1713 struct Vector
1714 {
1715 __vector float xyzw;
1716 } __attribute__((vecreturn));
1717
1718 Vector Add(Vector lhs, Vector rhs)
1719 {
1720 Vector result;
1721 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1722 return result; // This will be returned in a register
1723 }
1724*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001725 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001727 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001728 return;
1729 }
1730
Chandler Carruth87c44602011-07-01 23:49:12 +00001731 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001732 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1733 return;
1734 }
1735
Chandler Carruth87c44602011-07-01 23:49:12 +00001736 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001737 int count = 0;
1738
1739 if (!isa<CXXRecordDecl>(record)) {
1740 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1741 return;
1742 }
1743
1744 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1745 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1746 return;
1747 }
1748
Eric Christopherf48f3672010-12-01 22:13:54 +00001749 for (RecordDecl::field_iterator iter = record->field_begin();
1750 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001751 if ((count == 1) || !iter->getType()->isVectorType()) {
1752 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1753 return;
1754 }
1755 count++;
1756 }
1757
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001758 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001759}
1760
Chandler Carruth1b03c872011-07-02 00:01:44 +00001761static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001762 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001763 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001764 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001765 return;
1766 }
1767 // FIXME: Actually store the attribute on the declaration
1768}
1769
Chandler Carruth1b03c872011-07-02 00:01:44 +00001770static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001771 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001772 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001773 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001774 return;
1775 }
Mike Stumpbf916502009-07-24 19:02:52 +00001776
Chandler Carruth87c44602011-07-01 23:49:12 +00001777 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001778 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001780 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001781 return;
1782 }
Mike Stumpbf916502009-07-24 19:02:52 +00001783
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001784 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001785}
1786
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001787static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1788 const AttributeList &Attr) {
1789 // check the attribute arguments.
1790 if (Attr.hasParameterOrArguments()) {
1791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1792 return;
1793 }
1794
1795 if (!isa<FunctionDecl>(D)) {
1796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1797 << Attr.getName() << ExpectedFunction;
1798 return;
1799 }
1800
1801 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1802}
1803
Chandler Carruth1b03c872011-07-02 00:01:44 +00001804static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001805 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001806 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1808 return;
1809 }
Mike Stumpbf916502009-07-24 19:02:52 +00001810
Chandler Carruth87c44602011-07-01 23:49:12 +00001811 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001812 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001813 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1814 return;
1815 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001816 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001817 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001818 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001819 return;
1820 }
Mike Stumpbf916502009-07-24 19:02:52 +00001821
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001822 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001823}
1824
Chandler Carruth1b03c872011-07-02 00:01:44 +00001825static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001826 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001827 if (Attr.getNumArgs() > 1) {
1828 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001829 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001830 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001831
1832 int priority = 65535; // FIXME: Do not hardcode such constants.
1833 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001834 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001835 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001836 if (E->isTypeDependent() || E->isValueDependent() ||
1837 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001838 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001839 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001840 return;
1841 }
1842 priority = Idx.getZExtValue();
1843 }
Mike Stumpbf916502009-07-24 19:02:52 +00001844
Chandler Carruth87c44602011-07-01 23:49:12 +00001845 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001846 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001847 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001848 return;
1849 }
1850
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001851 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001852 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001853}
1854
Chandler Carruth1b03c872011-07-02 00:01:44 +00001855static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001856 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001857 if (Attr.getNumArgs() > 1) {
1858 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001859 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001860 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001861
1862 int priority = 65535; // FIXME: Do not hardcode such constants.
1863 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001864 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001865 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001866 if (E->isTypeDependent() || E->isValueDependent() ||
1867 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001869 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001870 return;
1871 }
1872 priority = Idx.getZExtValue();
1873 }
Mike Stumpbf916502009-07-24 19:02:52 +00001874
Chandler Carruth87c44602011-07-01 23:49:12 +00001875 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001877 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001878 return;
1879 }
1880
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001881 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001882 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001883}
1884
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001885template <typename AttrTy>
1886static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1887 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001888 unsigned NumArgs = Attr.getNumArgs();
1889 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001890 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001891 return;
1892 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001893
1894 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001895 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001896 if (NumArgs == 1) {
1897 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001898 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001899 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001900 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001901 return;
1902 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001903 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001904 }
Mike Stumpbf916502009-07-24 19:02:52 +00001905
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001906 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001907}
1908
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001909static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1910 const AttributeList &Attr) {
1911 unsigned NumArgs = Attr.getNumArgs();
1912 if (NumArgs > 0) {
1913 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1914 return;
1915 }
1916
1917 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001918 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001919}
1920
Patrick Beardb2f68202012-04-06 18:12:22 +00001921static void handleObjCRootClassAttr(Sema &S, Decl *D,
1922 const AttributeList &Attr) {
1923 if (!isa<ObjCInterfaceDecl>(D)) {
1924 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1925 return;
1926 }
1927
1928 unsigned NumArgs = Attr.getNumArgs();
1929 if (NumArgs > 0) {
1930 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1931 return;
1932 }
1933
1934 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1935}
1936
Ted Kremenek71207fc2012-01-05 22:47:47 +00001937static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001938 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001939 if (!isa<ObjCInterfaceDecl>(D)) {
1940 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1941 return;
1942 }
1943
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001944 unsigned NumArgs = Attr.getNumArgs();
1945 if (NumArgs > 0) {
1946 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1947 return;
1948 }
1949
Ted Kremenek71207fc2012-01-05 22:47:47 +00001950 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001951 Attr.getRange(), S.Context));
1952}
1953
Jordy Rosefad5de92012-05-08 03:27:22 +00001954static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1955 IdentifierInfo *Platform,
1956 VersionTuple Introduced,
1957 VersionTuple Deprecated,
1958 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001959 StringRef PlatformName
1960 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1961 if (PlatformName.empty())
1962 PlatformName = Platform->getName();
1963
1964 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1965 // of these steps are needed).
1966 if (!Introduced.empty() && !Deprecated.empty() &&
1967 !(Introduced <= Deprecated)) {
1968 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1969 << 1 << PlatformName << Deprecated.getAsString()
1970 << 0 << Introduced.getAsString();
1971 return true;
1972 }
1973
1974 if (!Introduced.empty() && !Obsoleted.empty() &&
1975 !(Introduced <= Obsoleted)) {
1976 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1977 << 2 << PlatformName << Obsoleted.getAsString()
1978 << 0 << Introduced.getAsString();
1979 return true;
1980 }
1981
1982 if (!Deprecated.empty() && !Obsoleted.empty() &&
1983 !(Deprecated <= Obsoleted)) {
1984 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1985 << 2 << PlatformName << Obsoleted.getAsString()
1986 << 1 << Deprecated.getAsString();
1987 return true;
1988 }
1989
1990 return false;
1991}
1992
Douglas Gregorf4d918f2013-01-15 22:43:08 +00001993/// \brief Check whether the two versions match.
1994///
1995/// If either version tuple is empty, then they are assumed to match. If
1996/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1997static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1998 bool BeforeIsOkay) {
1999 if (X.empty() || Y.empty())
2000 return true;
2001
2002 if (X == Y)
2003 return true;
2004
2005 if (BeforeIsOkay && X < Y)
2006 return true;
2007
2008 return false;
2009}
2010
Rafael Espindola51be6e32013-01-08 22:04:34 +00002011AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002012 IdentifierInfo *Platform,
2013 VersionTuple Introduced,
2014 VersionTuple Deprecated,
2015 VersionTuple Obsoleted,
2016 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002017 StringRef Message,
2018 bool Override) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002019 VersionTuple MergedIntroduced = Introduced;
2020 VersionTuple MergedDeprecated = Deprecated;
2021 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002022 bool FoundAny = false;
2023
Rafael Espindola98ae8342012-05-10 02:50:16 +00002024 if (D->hasAttrs()) {
2025 AttrVec &Attrs = D->getAttrs();
2026 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2027 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2028 if (!OldAA) {
2029 ++i;
2030 continue;
2031 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002032
Rafael Espindola98ae8342012-05-10 02:50:16 +00002033 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2034 if (OldPlatform != Platform) {
2035 ++i;
2036 continue;
2037 }
2038
2039 FoundAny = true;
2040 VersionTuple OldIntroduced = OldAA->getIntroduced();
2041 VersionTuple OldDeprecated = OldAA->getDeprecated();
2042 VersionTuple OldObsoleted = OldAA->getObsoleted();
2043 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002044
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002045 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2046 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2047 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2048 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002049 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002050 if (Override) {
2051 int Which = -1;
2052 VersionTuple FirstVersion;
2053 VersionTuple SecondVersion;
2054 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2055 Which = 0;
2056 FirstVersion = OldIntroduced;
2057 SecondVersion = Introduced;
2058 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2059 Which = 1;
2060 FirstVersion = Deprecated;
2061 SecondVersion = OldDeprecated;
2062 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2063 Which = 2;
2064 FirstVersion = Obsoleted;
2065 SecondVersion = OldObsoleted;
2066 }
2067
2068 if (Which == -1) {
2069 Diag(OldAA->getLocation(),
2070 diag::warn_mismatched_availability_override_unavail)
2071 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2072 } else {
2073 Diag(OldAA->getLocation(),
2074 diag::warn_mismatched_availability_override)
2075 << Which
2076 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2077 << FirstVersion.getAsString() << SecondVersion.getAsString();
2078 }
2079 Diag(Range.getBegin(), diag::note_overridden_method);
2080 } else {
2081 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2082 Diag(Range.getBegin(), diag::note_previous_attribute);
2083 }
2084
Rafael Espindola98ae8342012-05-10 02:50:16 +00002085 Attrs.erase(Attrs.begin() + i);
2086 --e;
2087 continue;
2088 }
2089
2090 VersionTuple MergedIntroduced2 = MergedIntroduced;
2091 VersionTuple MergedDeprecated2 = MergedDeprecated;
2092 VersionTuple MergedObsoleted2 = MergedObsoleted;
2093
2094 if (MergedIntroduced2.empty())
2095 MergedIntroduced2 = OldIntroduced;
2096 if (MergedDeprecated2.empty())
2097 MergedDeprecated2 = OldDeprecated;
2098 if (MergedObsoleted2.empty())
2099 MergedObsoleted2 = OldObsoleted;
2100
2101 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2102 MergedIntroduced2, MergedDeprecated2,
2103 MergedObsoleted2)) {
2104 Attrs.erase(Attrs.begin() + i);
2105 --e;
2106 continue;
2107 }
2108
2109 MergedIntroduced = MergedIntroduced2;
2110 MergedDeprecated = MergedDeprecated2;
2111 MergedObsoleted = MergedObsoleted2;
2112 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002113 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002114 }
2115
2116 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,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002161 IsUnavailable, Str,
2162 /*Override=*/false);
Rafael Espindola838dc592013-01-12 06:42:30 +00002163 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002164 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002165}
2166
Rafael Espindola599f1b72012-05-13 03:25:18 +00002167VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2168 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002169 if (isa<TypedefNameDecl>(D)) {
2170 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002171 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002172 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002173 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2174 if (ExistingAttr) {
2175 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2176 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002177 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002178 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2179 Diag(Range.getBegin(), diag::note_previous_attribute);
2180 D->dropAttr<VisibilityAttr>();
2181 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002182 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002183}
2184
Chandler Carruth1b03c872011-07-02 00:01:44 +00002185static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002186 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002187 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002188 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002189
Peter Collingbourne7a730022010-11-23 20:45:58 +00002190 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002191 Arg = Arg->IgnoreParenCasts();
2192 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002193
Douglas Gregor5cee1192011-07-27 05:40:30 +00002194 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002195 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002196 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002197 return;
2198 }
Mike Stumpbf916502009-07-24 19:02:52 +00002199
Chris Lattner5f9e2722011-07-23 10:55:15 +00002200 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002201 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002202
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002203 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002204 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002205 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002206 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002207 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002208 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002209 else if (TypeStr == "protected") {
2210 // Complain about attempts to use protected visibility on targets
2211 // (like Darwin) that don't support it.
2212 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2213 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2214 type = VisibilityAttr::Default;
2215 } else {
2216 type = VisibilityAttr::Protected;
2217 }
2218 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002219 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002220 return;
2221 }
Mike Stumpbf916502009-07-24 19:02:52 +00002222
Rafael Espindola599f1b72012-05-13 03:25:18 +00002223 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
Rafael Espindola838dc592013-01-12 06:42:30 +00002224 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002225 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002226}
2227
Chandler Carruth1b03c872011-07-02 00:01:44 +00002228static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2229 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002230 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2231 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002233 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002234 return;
2235 }
2236
Chandler Carruth87c44602011-07-01 23:49:12 +00002237 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2238 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002240 << "objc_method_family" << 1;
2241 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002243 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002244 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002245 return;
2246 }
2247
Chris Lattner5f9e2722011-07-23 10:55:15 +00002248 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002249 ObjCMethodFamilyAttr::FamilyKind family;
2250 if (param == "none")
2251 family = ObjCMethodFamilyAttr::OMF_None;
2252 else if (param == "alloc")
2253 family = ObjCMethodFamilyAttr::OMF_alloc;
2254 else if (param == "copy")
2255 family = ObjCMethodFamilyAttr::OMF_copy;
2256 else if (param == "init")
2257 family = ObjCMethodFamilyAttr::OMF_init;
2258 else if (param == "mutableCopy")
2259 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2260 else if (param == "new")
2261 family = ObjCMethodFamilyAttr::OMF_new;
2262 else {
2263 // Just warn and ignore it. This is future-proof against new
2264 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002265 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002266 return;
2267 }
2268
John McCallf85e1932011-06-15 23:02:42 +00002269 if (family == ObjCMethodFamilyAttr::OMF_init &&
2270 !method->getResultType()->isObjCObjectPointerType()) {
2271 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2272 << method->getResultType();
2273 // Ignore the attribute.
2274 return;
2275 }
2276
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002277 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002278 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002279}
2280
Chandler Carruth1b03c872011-07-02 00:01:44 +00002281static void handleObjCExceptionAttr(Sema &S, Decl *D,
2282 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002283 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002284 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002285
Chris Lattner0db29ec2009-02-14 08:09:34 +00002286 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2287 if (OCI == 0) {
2288 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2289 return;
2290 }
Mike Stumpbf916502009-07-24 19:02:52 +00002291
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002292 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002293}
2294
Chandler Carruth1b03c872011-07-02 00:01:44 +00002295static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002296 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002297 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002298 return;
2299 }
Richard Smith162e1c12011-04-15 14:24:37 +00002300 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002301 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002302 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002303 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2304 return;
2305 }
2306 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002307 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2308 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002309 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002310 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2311 return;
2312 }
2313 }
2314 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002315 // It is okay to include this attribute on properties, e.g.:
2316 //
2317 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2318 //
2319 // In this case it follows tradition and suppresses an error in the above
2320 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002321 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002322 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002323 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002324}
2325
Mike Stumpbf916502009-07-24 19:02:52 +00002326static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002327handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002328 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002329 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002330 return;
2331 }
2332
2333 if (!isa<FunctionDecl>(D)) {
2334 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2335 return;
2336 }
2337
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002338 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002339}
2340
Chandler Carruth1b03c872011-07-02 00:01:44 +00002341static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002342 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002343 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002344 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002345 return;
2346 }
Mike Stumpbf916502009-07-24 19:02:52 +00002347
Steve Naroff9eae5762008-09-18 16:44:58 +00002348 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002350 return;
2351 }
Mike Stumpbf916502009-07-24 19:02:52 +00002352
Sean Huntcf807c42010-08-18 23:23:40 +00002353 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002354 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002355 type = BlocksAttr::ByRef;
2356 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002357 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002358 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002359 return;
2360 }
Mike Stumpbf916502009-07-24 19:02:52 +00002361
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002362 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002363}
2364
Chandler Carruth1b03c872011-07-02 00:01:44 +00002365static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002366 // check the attribute arguments.
2367 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002368 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002369 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002370 }
2371
John McCall3323fad2011-09-09 07:56:05 +00002372 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002373 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002374 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002375 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002376 if (E->isTypeDependent() || E->isValueDependent() ||
2377 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002378 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002379 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002380 return;
2381 }
Mike Stumpbf916502009-07-24 19:02:52 +00002382
John McCall3323fad2011-09-09 07:56:05 +00002383 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002384 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2385 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002386 return;
2387 }
John McCall3323fad2011-09-09 07:56:05 +00002388
2389 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002390 }
2391
John McCall3323fad2011-09-09 07:56:05 +00002392 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002393 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002394 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002395 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002396 if (E->isTypeDependent() || E->isValueDependent() ||
2397 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002398 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002399 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002400 return;
2401 }
2402 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002403
John McCall3323fad2011-09-09 07:56:05 +00002404 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002405 // FIXME: This error message could be improved, it would be nice
2406 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002407 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2408 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002409 return;
2410 }
2411 }
2412
Chandler Carruth87c44602011-07-01 23:49:12 +00002413 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002414 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002415 if (isa<FunctionNoProtoType>(FT)) {
2416 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2417 return;
2418 }
Mike Stumpbf916502009-07-24 19:02:52 +00002419
Chris Lattner897cd902009-03-17 23:03:47 +00002420 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002421 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002422 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002423 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002424 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002425 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002426 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002427 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002428 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002429 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2430 if (!BD->isVariadic()) {
2431 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2432 return;
2433 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002434 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002435 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002436 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002437 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002438 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002439 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002440 int m = Ty->isFunctionPointerType() ? 0 : 1;
2441 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002442 return;
2443 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002444 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002445 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002446 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002447 return;
2448 }
Anders Carlsson77091822008-10-05 18:05:59 +00002449 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002450 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002451 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002452 return;
2453 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002454 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002455 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002456}
2457
Chandler Carruth1b03c872011-07-02 00:01:44 +00002458static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002459 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002460 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002461 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002462
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002463 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002465 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002466 return;
2467 }
Mike Stumpbf916502009-07-24 19:02:52 +00002468
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002469 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2470 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2471 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002472 return;
2473 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002474 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2475 if (MD->getResultType()->isVoidType()) {
2476 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2477 << Attr.getName() << 1;
2478 return;
2479 }
2480
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002481 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002482}
2483
Chandler Carruth1b03c872011-07-02 00:01:44 +00002484static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002485 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002486 if (Attr.hasParameterOrArguments()) {
2487 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002488 return;
2489 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002490
Chandler Carruth87c44602011-07-01 23:49:12 +00002491 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002492 if (isa<CXXRecordDecl>(D)) {
2493 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2494 return;
2495 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002496 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2497 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002498 return;
2499 }
2500
Chandler Carruth87c44602011-07-01 23:49:12 +00002501 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002502
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002503 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002504}
2505
Chandler Carruth1b03c872011-07-02 00:01:44 +00002506static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002507 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002508 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002509 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002510
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002511
2512 // weak_import only applies to variable & function declarations.
2513 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002514 if (!D->canBeWeakImported(isDef)) {
2515 if (isDef)
2516 S.Diag(Attr.getLoc(),
2517 diag::warn_attribute_weak_import_invalid_on_definition)
2518 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002519 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002520 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002521 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002522 // Nothing to warn about here.
2523 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002525 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002526
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002527 return;
2528 }
2529
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002530 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002531}
2532
Tanya Lattner0df579e2012-07-09 22:06:01 +00002533// Handles reqd_work_group_size and work_group_size_hint.
2534static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002535 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002536 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2537 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2538
Nate Begeman6f3d8382009-06-26 06:32:41 +00002539 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002540 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002541
2542 unsigned WGSize[3];
2543 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002544 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002545 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002546 if (E->isTypeDependent() || E->isValueDependent() ||
2547 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002549 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002550 return;
2551 }
2552 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2553 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002554
2555 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2556 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2557 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2558 if (!(A->getXDim() == WGSize[0] &&
2559 A->getYDim() == WGSize[1] &&
2560 A->getZDim() == WGSize[2])) {
2561 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2562 Attr.getName();
2563 }
2564 }
2565
2566 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2567 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2568 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2569 if (!(A->getXDim() == WGSize[0] &&
2570 A->getYDim() == WGSize[1] &&
2571 A->getZDim() == WGSize[2])) {
2572 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2573 Attr.getName();
2574 }
2575 }
2576
2577 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2578 D->addAttr(::new (S.Context)
2579 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2580 WGSize[0], WGSize[1], WGSize[2]));
2581 else
2582 D->addAttr(::new (S.Context)
2583 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2584 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002585}
2586
Rafael Espindola599f1b72012-05-13 03:25:18 +00002587SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2588 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002589 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2590 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002591 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002592 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2593 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002594 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002595 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002596 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002597}
2598
Chandler Carruth1b03c872011-07-02 00:01:44 +00002599static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002600 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002601 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002602 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002603
2604 // Make sure that there is a string literal as the sections's single
2605 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002606 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002607 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002608 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002609 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002610 return;
2611 }
Mike Stump1eb44332009-09-09 15:08:12 +00002612
Chris Lattner797c3c42009-08-10 19:03:04 +00002613 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002614 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002615 if (!Error.empty()) {
2616 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2617 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002618 return;
2619 }
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002621 // This attribute cannot be applied to local variables.
2622 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2623 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2624 return;
2625 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002626 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2627 SE->getString());
2628 if (NewAttr)
2629 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002630}
2631
Chris Lattner6b6b5372008-06-26 18:38:35 +00002632
Chandler Carruth1b03c872011-07-02 00:01:44 +00002633static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002634 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002635 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002637 return;
2638 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002639
Chandler Carruth87c44602011-07-01 23:49:12 +00002640 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002641 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002642 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002643 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002644 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002645 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002646}
2647
Chandler Carruth1b03c872011-07-02 00:01:44 +00002648static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002649 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002650 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002652 return;
2653 }
Mike Stumpbf916502009-07-24 19:02:52 +00002654
Chandler Carruth87c44602011-07-01 23:49:12 +00002655 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002656 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002657 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002658 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002659 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002660 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002661}
2662
Chandler Carruth1b03c872011-07-02 00:01:44 +00002663static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002664 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002665 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002666 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002667
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002668 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002669}
2670
Chandler Carruth1b03c872011-07-02 00:01:44 +00002671static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002672 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2674 return;
2675 }
Mike Stumpbf916502009-07-24 19:02:52 +00002676
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002677 if (Attr.getNumArgs() != 0) {
2678 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2679 return;
2680 }
Mike Stumpbf916502009-07-24 19:02:52 +00002681
Chandler Carruth87c44602011-07-01 23:49:12 +00002682 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002683
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002684 if (!VD || !VD->hasLocalStorage()) {
2685 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2686 return;
2687 }
Mike Stumpbf916502009-07-24 19:02:52 +00002688
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002689 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002690 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002691 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002692 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2693 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002694 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002695 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002696 Attr.getParameterName();
2697 return;
2698 }
Mike Stumpbf916502009-07-24 19:02:52 +00002699
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002700 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2701 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002702 S.Diag(Attr.getParameterLoc(),
2703 diag::err_attribute_cleanup_arg_not_function)
2704 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002705 return;
2706 }
2707
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002708 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002709 S.Diag(Attr.getParameterLoc(),
2710 diag::err_attribute_cleanup_func_must_take_one_arg)
2711 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002712 return;
2713 }
Mike Stumpbf916502009-07-24 19:02:52 +00002714
Anders Carlsson89941c12009-02-07 23:16:50 +00002715 // We're currently more strict than GCC about what function types we accept.
2716 // If this ever proves to be a problem it should be easy to fix.
2717 QualType Ty = S.Context.getPointerType(VD->getType());
2718 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002719 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2720 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002721 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002722 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2723 Attr.getParameterName() << ParamTy << Ty;
2724 return;
2725 }
Mike Stumpbf916502009-07-24 19:02:52 +00002726
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002727 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002728 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002729}
2730
Mike Stumpbf916502009-07-24 19:02:52 +00002731/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002732/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002733static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002734 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002735 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002736
Chandler Carruth87c44602011-07-01 23:49:12 +00002737 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002739 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002740 return;
2741 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002742
2743 // In C++ the implicit 'this' function parameter also counts, and they are
2744 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002745 bool HasImplicitThisParam = isInstanceMethod(D);
2746 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002747 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002748
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002749 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002750 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002751 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002752 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2753 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002754 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2755 << "format" << 2 << IdxExpr->getSourceRange();
2756 return;
2757 }
Mike Stumpbf916502009-07-24 19:02:52 +00002758
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002759 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2760 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2761 << "format" << 2 << IdxExpr->getSourceRange();
2762 return;
2763 }
Mike Stumpbf916502009-07-24 19:02:52 +00002764
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002765 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002766
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002767 if (HasImplicitThisParam) {
2768 if (ArgIdx == 0) {
2769 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2770 << "format_arg" << IdxExpr->getSourceRange();
2771 return;
2772 }
2773 ArgIdx--;
2774 }
2775
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002776 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002777 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002778
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002779 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2780 if (not_nsstring_type &&
2781 !isCFStringType(Ty, S.Context) &&
2782 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002783 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002784 // FIXME: Should highlight the actual expression that has the wrong type.
2785 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002786 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002787 << IdxExpr->getSourceRange();
2788 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002789 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002790 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002791 if (!isNSStringType(Ty, S.Context) &&
2792 !isCFStringType(Ty, S.Context) &&
2793 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002794 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002795 // FIXME: Should highlight the actual expression that has the wrong type.
2796 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002797 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002798 << IdxExpr->getSourceRange();
2799 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002800 }
2801
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002802 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002803 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002804}
2805
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002806enum FormatAttrKind {
2807 CFStringFormat,
2808 NSStringFormat,
2809 StrftimeFormat,
2810 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002811 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002812 InvalidFormat
2813};
2814
2815/// getFormatAttrKind - Map from format attribute names to supported format
2816/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002817static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002818 return llvm::StringSwitch<FormatAttrKind>(Format)
2819 // Check for formats that get handled specially.
2820 .Case("NSString", NSStringFormat)
2821 .Case("CFString", CFStringFormat)
2822 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002823
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002824 // Otherwise, check for supported formats.
2825 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2826 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2827 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002828
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002829 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2830 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002831}
2832
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002833/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002834/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002835static void handleInitPriorityAttr(Sema &S, Decl *D,
2836 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002837 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002838 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2839 return;
2840 }
2841
Chandler Carruth87c44602011-07-01 23:49:12 +00002842 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002843 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2844 Attr.setInvalid();
2845 return;
2846 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002847 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002848 if (S.Context.getAsArrayType(T))
2849 T = S.Context.getBaseElementType(T);
2850 if (!T->getAs<RecordType>()) {
2851 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2852 Attr.setInvalid();
2853 return;
2854 }
2855
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002856 if (Attr.getNumArgs() != 1) {
2857 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2858 Attr.setInvalid();
2859 return;
2860 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002861 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002862
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002863 llvm::APSInt priority(32);
2864 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2865 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2866 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2867 << "init_priority" << priorityExpr->getSourceRange();
2868 Attr.setInvalid();
2869 return;
2870 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002871 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002872 if (prioritynum < 101 || prioritynum > 65535) {
2873 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2874 << priorityExpr->getSourceRange();
2875 Attr.setInvalid();
2876 return;
2877 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002878 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002879 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002880}
2881
Rafael Espindola599f1b72012-05-13 03:25:18 +00002882FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2883 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002884 // Check whether we already have an equivalent format attribute.
2885 for (specific_attr_iterator<FormatAttr>
2886 i = D->specific_attr_begin<FormatAttr>(),
2887 e = D->specific_attr_end<FormatAttr>();
2888 i != e ; ++i) {
2889 FormatAttr *f = *i;
2890 if (f->getType() == Format &&
2891 f->getFormatIdx() == FormatIdx &&
2892 f->getFirstArg() == FirstArg) {
2893 // If we don't have a valid location for this attribute, adopt the
2894 // location.
2895 if (f->getLocation().isInvalid())
2896 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002897 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002898 }
2899 }
2900
Rafael Espindola599f1b72012-05-13 03:25:18 +00002901 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2902 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002903}
2904
Mike Stumpbf916502009-07-24 19:02:52 +00002905/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002906/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002907static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002908
Chris Lattner545dd342008-06-28 23:36:30 +00002909 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002911 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002912 return;
2913 }
2914
Chris Lattner545dd342008-06-28 23:36:30 +00002915 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002917 return;
2918 }
2919
Chandler Carruth87c44602011-07-01 23:49:12 +00002920 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002921 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002922 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002923 return;
2924 }
2925
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002926 // In C++ the implicit 'this' function parameter also counts, and they are
2927 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002928 bool HasImplicitThisParam = isInstanceMethod(D);
2929 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002930 unsigned FirstIdx = 1;
2931
Chris Lattner5f9e2722011-07-23 10:55:15 +00002932 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002933
2934 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002935 if (Format.startswith("__") && Format.endswith("__"))
2936 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002937
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002938 // Check for supported formats.
2939 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002940
2941 if (Kind == IgnoredFormat)
2942 return;
2943
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002944 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002945 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002946 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002947 return;
2948 }
2949
2950 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002951 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002952 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002953 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2954 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002955 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002956 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002957 return;
2958 }
2959
2960 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002961 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002962 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002963 return;
2964 }
2965
2966 // FIXME: Do we need to bounds check?
2967 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002968
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002969 if (HasImplicitThisParam) {
2970 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002971 S.Diag(Attr.getLoc(),
2972 diag::err_format_attribute_implicit_this_format_string)
2973 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002974 return;
2975 }
2976 ArgIdx--;
2977 }
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Chris Lattner6b6b5372008-06-26 18:38:35 +00002979 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002980 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002981
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002982 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002983 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002984 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2985 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002986 return;
2987 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002988 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002989 // FIXME: do we need to check if the type is NSString*? What are the
2990 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002991 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002992 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002993 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2994 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002995 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002996 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002997 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002998 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002999 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003000 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3001 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003002 return;
3003 }
3004
3005 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003006 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003007 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003008 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3009 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003011 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003012 return;
3013 }
3014
3015 // check if the function is variadic if the 3rd argument non-zero
3016 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003017 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003018 ++NumArgs; // +1 for ...
3019 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003020 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003021 return;
3022 }
3023 }
3024
Chris Lattner3c73c412008-11-19 08:23:25 +00003025 // strftime requires FirstArg to be 0 because it doesn't read from any
3026 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003027 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003028 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003029 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3030 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003031 return;
3032 }
3033 // if 0 it disables parameter checking (to use with e.g. va_list)
3034 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003036 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003037 return;
3038 }
3039
Rafael Espindola599f1b72012-05-13 03:25:18 +00003040 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3041 Idx.getZExtValue(),
3042 FirstArg.getZExtValue());
3043 if (NewAttr)
3044 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003045}
3046
Chandler Carruth1b03c872011-07-02 00:01:44 +00003047static void handleTransparentUnionAttr(Sema &S, Decl *D,
3048 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003049 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003050 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003051 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003052
Chris Lattner6b6b5372008-06-26 18:38:35 +00003053
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003054 // Try to find the underlying union declaration.
3055 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003056 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003057 if (TD && TD->getUnderlyingType()->isUnionType())
3058 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3059 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003060 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003061
3062 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003063 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003064 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003065 return;
3066 }
3067
John McCall5e1cdac2011-10-07 06:10:15 +00003068 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003069 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003070 diag::warn_transparent_union_attribute_not_definition);
3071 return;
3072 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003073
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003074 RecordDecl::field_iterator Field = RD->field_begin(),
3075 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003076 if (Field == FieldEnd) {
3077 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3078 return;
3079 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003080
David Blaikie581deb32012-06-06 20:45:41 +00003081 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003082 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003083 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003084 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003085 diag::warn_transparent_union_attribute_floating)
3086 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003087 return;
3088 }
3089
3090 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3091 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3092 for (; Field != FieldEnd; ++Field) {
3093 QualType FieldType = Field->getType();
3094 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3095 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3096 // Warn if we drop the attribute.
3097 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003098 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003099 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003100 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003101 diag::warn_transparent_union_attribute_field_size_align)
3102 << isSize << Field->getDeclName() << FieldBits;
3103 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003104 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003105 diag::note_transparent_union_first_field_size_align)
3106 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003107 return;
3108 }
3109 }
3110
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003111 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003112}
3113
Chandler Carruth1b03c872011-07-02 00:01:44 +00003114static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003115 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003116 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003117 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003118
Peter Collingbourne7a730022010-11-23 20:45:58 +00003119 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003120 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003121
Chris Lattner6b6b5372008-06-26 18:38:35 +00003122 // Make sure that there is a string literal as the annotation's single
3123 // argument.
3124 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003125 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003126 return;
3127 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003128
3129 // Don't duplicate annotations that are already set.
3130 for (specific_attr_iterator<AnnotateAttr>
3131 i = D->specific_attr_begin<AnnotateAttr>(),
3132 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3133 if ((*i)->getAnnotation() == SE->getString())
3134 return;
3135 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003136 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003137 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003138}
3139
Chandler Carruth1b03c872011-07-02 00:01:44 +00003140static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003142 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003143 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003144 return;
3145 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003146
Sean Huntbbd37c62009-11-21 08:43:09 +00003147 //FIXME: The C++0x version of this attribute has more limited applicabilty
3148 // than GNU's, and should error out when it is used to specify a
3149 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003150
Chris Lattner545dd342008-06-28 23:36:30 +00003151 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003152 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3153 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003154 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003155 }
Mike Stumpbf916502009-07-24 19:02:52 +00003156
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003157 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3158 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003159}
3160
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003161void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3162 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003163 // FIXME: Handle pack-expansions here.
3164 if (DiagnoseUnexpandedParameterPack(E))
3165 return;
3166
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003167 if (E->isTypeDependent() || E->isValueDependent()) {
3168 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003169 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3170 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003171 return;
3172 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003173
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003174 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003175 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003176 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003177 ExprResult ICE
3178 = VerifyIntegerConstantExpression(E, &Alignment,
3179 diag::err_aligned_attribute_argument_not_int,
3180 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003181 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003182 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003183 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003184 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3185 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003186 return;
3187 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003188 if (isDeclSpec) {
3189 // We've already verified it's a power of 2, now let's make sure it's
3190 // 8192 or less.
3191 if (Alignment.getZExtValue() > 8192) {
3192 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3193 << E->getSourceRange();
3194 return;
3195 }
3196 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003197
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003198 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3199 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003200}
3201
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003202void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3203 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003204 // FIXME: Cache the number on the Attr object if non-dependent?
3205 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003206 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3207 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003208 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003209}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003210
Chandler Carruthd309c812011-07-01 23:49:16 +00003211/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003212/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003213///
Mike Stumpbf916502009-07-24 19:02:52 +00003214/// Despite what would be logical, the mode attribute is a decl attribute, not a
3215/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3216/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003217static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003218 // This attribute isn't documented, but glibc uses it. It changes
3219 // the width of an int or unsigned int to the specified size.
3220
3221 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003222 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003223 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003224
Chris Lattnerfbf13472008-06-27 22:18:37 +00003225
3226 IdentifierInfo *Name = Attr.getParameterName();
3227 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003228 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003229 return;
3230 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003231
Chris Lattner5f9e2722011-07-23 10:55:15 +00003232 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003233
3234 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003235 if (Str.startswith("__") && Str.endswith("__"))
3236 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003237
3238 unsigned DestWidth = 0;
3239 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003240 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003241 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003242 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003243 switch (Str[0]) {
3244 case 'Q': DestWidth = 8; break;
3245 case 'H': DestWidth = 16; break;
3246 case 'S': DestWidth = 32; break;
3247 case 'D': DestWidth = 64; break;
3248 case 'X': DestWidth = 96; break;
3249 case 'T': DestWidth = 128; break;
3250 }
3251 if (Str[1] == 'F') {
3252 IntegerMode = false;
3253 } else if (Str[1] == 'C') {
3254 IntegerMode = false;
3255 ComplexMode = true;
3256 } else if (Str[1] != 'I') {
3257 DestWidth = 0;
3258 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003259 break;
3260 case 4:
3261 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3262 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003263 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003264 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003265 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003266 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003267 break;
3268 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003269 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003270 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003271 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003272 case 11:
3273 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003274 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003275 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003276 }
3277
3278 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003279 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003280 OldTy = TD->getUnderlyingType();
3281 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3282 OldTy = VD->getType();
3283 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003284 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003285 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003286 return;
3287 }
Eli Friedman73397492009-03-03 06:41:03 +00003288
John McCall183700f2009-09-21 23:43:11 +00003289 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003290 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3291 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003292 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003293 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3294 } else if (ComplexMode) {
3295 if (!OldTy->isComplexType())
3296 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3297 } else {
3298 if (!OldTy->isFloatingType())
3299 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3300 }
3301
Mike Stump390b4cc2009-05-16 07:39:55 +00003302 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3303 // and friends, at least with glibc.
3304 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3305 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003306 // FIXME: Make sure floating-point mappings are accurate
3307 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003308 QualType NewTy;
3309 switch (DestWidth) {
3310 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003311 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003312 return;
3313 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003314 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003315 return;
3316 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003317 if (!IntegerMode) {
3318 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3319 return;
3320 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003321 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003322 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003323 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003324 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003325 break;
3326 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003327 if (!IntegerMode) {
3328 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3329 return;
3330 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003331 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003332 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003333 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003334 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003335 break;
3336 case 32:
3337 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003338 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003339 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003340 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003341 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003342 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003343 break;
3344 case 64:
3345 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003346 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003347 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003348 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003349 NewTy = S.Context.LongTy;
3350 else
3351 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003352 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003353 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003354 NewTy = S.Context.UnsignedLongTy;
3355 else
3356 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003357 break;
Eli Friedman73397492009-03-03 06:41:03 +00003358 case 96:
3359 NewTy = S.Context.LongDoubleTy;
3360 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003361 case 128:
3362 if (!IntegerMode) {
3363 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3364 return;
3365 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003366 if (OldTy->isSignedIntegerType())
3367 NewTy = S.Context.Int128Ty;
3368 else
3369 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003370 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003371 }
3372
Eli Friedman73397492009-03-03 06:41:03 +00003373 if (ComplexMode) {
3374 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003375 }
3376
3377 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003378 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003379 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003380 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003381 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003382 cast<ValueDecl>(D)->setType(NewTy);
3383}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003384
Chandler Carruth1b03c872011-07-02 00:01:44 +00003385static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003386 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003387 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003388 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003389
Nick Lewycky78d1a102012-07-24 01:40:49 +00003390 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3391 if (!VD->hasGlobalStorage())
3392 S.Diag(Attr.getLoc(),
3393 diag::warn_attribute_requires_functions_or_static_globals)
3394 << Attr.getName();
3395 } else if (!isFunctionOrMethod(D)) {
3396 S.Diag(Attr.getLoc(),
3397 diag::warn_attribute_requires_functions_or_static_globals)
3398 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003399 return;
3400 }
Mike Stumpbf916502009-07-24 19:02:52 +00003401
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003402 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003403}
3404
Chandler Carruth1b03c872011-07-02 00:01:44 +00003405static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003406 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003407 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003408 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003409
Mike Stumpbf916502009-07-24 19:02:52 +00003410
Chandler Carruth87c44602011-07-01 23:49:12 +00003411 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003413 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003414 return;
3415 }
Mike Stumpbf916502009-07-24 19:02:52 +00003416
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003417 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003418}
3419
Chandler Carruth1b03c872011-07-02 00:01:44 +00003420static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3421 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003422 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003423 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003424 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003425
Chris Lattner7255a2d2010-06-22 00:03:40 +00003426
Chandler Carruth87c44602011-07-01 23:49:12 +00003427 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003428 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003429 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003430 return;
3431 }
3432
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003433 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003434 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003435}
3436
Chandler Carruth1b03c872011-07-02 00:01:44 +00003437static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003438 if (S.LangOpts.CUDA) {
3439 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003440 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3442 return;
3443 }
3444
Chandler Carruth87c44602011-07-01 23:49:12 +00003445 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003446 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003447 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003448 return;
3449 }
3450
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003451 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003452 } else {
3453 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3454 }
3455}
3456
Chandler Carruth1b03c872011-07-02 00:01:44 +00003457static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003458 if (S.LangOpts.CUDA) {
3459 // check the attribute arguments.
3460 if (Attr.getNumArgs() != 0) {
3461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3462 return;
3463 }
3464
Chandler Carruth87c44602011-07-01 23:49:12 +00003465 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003467 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003468 return;
3469 }
3470
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003471 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003472 } else {
3473 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3474 }
3475}
3476
Chandler Carruth1b03c872011-07-02 00:01:44 +00003477static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003478 if (S.LangOpts.CUDA) {
3479 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003480 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003481 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003482
Chandler Carruth87c44602011-07-01 23:49:12 +00003483 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003484 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003485 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003486 return;
3487 }
3488
Chandler Carruth87c44602011-07-01 23:49:12 +00003489 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003490 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003491 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003492 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3493 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3494 << FD->getType()
3495 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3496 "void");
3497 } else {
3498 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3499 << FD->getType();
3500 }
3501 return;
3502 }
3503
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003504 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003505 } else {
3506 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3507 }
3508}
3509
Chandler Carruth1b03c872011-07-02 00:01:44 +00003510static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003511 if (S.LangOpts.CUDA) {
3512 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003513 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003514 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003515
Peter Collingbourneced76712010-12-01 03:15:31 +00003516
Chandler Carruth87c44602011-07-01 23:49:12 +00003517 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003518 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003519 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003520 return;
3521 }
3522
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003523 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003524 } else {
3525 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3526 }
3527}
3528
Chandler Carruth1b03c872011-07-02 00:01:44 +00003529static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003530 if (S.LangOpts.CUDA) {
3531 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003532 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003533 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003534
Peter Collingbourneced76712010-12-01 03:15:31 +00003535
Chandler Carruth87c44602011-07-01 23:49:12 +00003536 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003537 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003538 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003539 return;
3540 }
3541
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003542 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003543 } else {
3544 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3545 }
3546}
3547
Chandler Carruth1b03c872011-07-02 00:01:44 +00003548static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003549 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003550 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003551 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003552
Chandler Carruth87c44602011-07-01 23:49:12 +00003553 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003554 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003555 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003556 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003557 return;
3558 }
Mike Stumpbf916502009-07-24 19:02:52 +00003559
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003560 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003561 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003562 return;
3563 }
Mike Stumpbf916502009-07-24 19:02:52 +00003564
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003565 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003566}
3567
Chandler Carruth1b03c872011-07-02 00:01:44 +00003568static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003569 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003570
Aaron Ballmanfff32482012-12-09 17:45:41 +00003571 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003572 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003573 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3574 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003575 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003576 return;
3577
Chandler Carruth87c44602011-07-01 23:49:12 +00003578 if (!isa<ObjCMethodDecl>(D)) {
3579 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3580 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003581 return;
3582 }
3583
Chandler Carruth87c44602011-07-01 23:49:12 +00003584 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003585 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003586 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003587 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003588 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003589 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003590 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003591 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003592 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003593 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003594 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003595 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003596 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003597 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003598 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003599 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003600 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003601 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003602 switch (CC) {
3603 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003604 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003605 break;
3606 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003607 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003608 break;
3609 default:
3610 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003611 }
3612
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003613 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuff263366f2012-10-16 22:30:41 +00003614 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003615 }
Derek Schuff263366f2012-10-16 22:30:41 +00003616 case AttributeList::AT_PnaclCall:
3617 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3618 return;
Guy Benyei38980082012-12-25 08:53:55 +00003619 case AttributeList::AT_IntelOclBicc:
3620 D->addAttr(::new (S.Context) IntelOclBiccAttr(Attr.getRange(), S.Context));
3621 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003622
Abramo Bagnarae215f722010-04-30 13:10:51 +00003623 default:
3624 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003625 }
3626}
3627
Chandler Carruth1b03c872011-07-02 00:01:44 +00003628static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003629 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003630 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003631}
3632
Aaron Ballmanfff32482012-12-09 17:45:41 +00003633bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3634 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003635 if (attr.isInvalid())
3636 return true;
3637
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003638 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3639 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3640 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003641 attr.setInvalid();
3642 return true;
3643 }
3644
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003645 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3646 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003647 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003648 case AttributeList::AT_CDecl: CC = CC_C; break;
3649 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3650 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3651 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3652 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3653 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003654 Expr *Arg = attr.getArg(0);
3655 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003656 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003657 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3658 << "pcs" << 1;
3659 attr.setInvalid();
3660 return true;
3661 }
3662
Chris Lattner5f9e2722011-07-23 10:55:15 +00003663 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003664 if (StrRef == "aapcs") {
3665 CC = CC_AAPCS;
3666 break;
3667 } else if (StrRef == "aapcs-vfp") {
3668 CC = CC_AAPCS_VFP;
3669 break;
3670 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003671
3672 attr.setInvalid();
3673 Diag(attr.getLoc(), diag::err_invalid_pcs);
3674 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003675 }
Derek Schuff263366f2012-10-16 22:30:41 +00003676 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003677 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003678 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003679 }
3680
Aaron Ballman82bfa192012-10-02 14:26:08 +00003681 const TargetInfo &TI = Context.getTargetInfo();
3682 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3683 if (A == TargetInfo::CCCR_Warning) {
3684 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003685
3686 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3687 if (FD)
3688 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3689 TargetInfo::CCMT_NonMember;
3690 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003691 }
3692
John McCall711c52b2011-01-05 12:14:39 +00003693 return false;
3694}
3695
Chandler Carruth1b03c872011-07-02 00:01:44 +00003696static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003697 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003698
3699 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003700 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003701 return;
3702
Chandler Carruth87c44602011-07-01 23:49:12 +00003703 if (!isa<ObjCMethodDecl>(D)) {
3704 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3705 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003706 return;
3707 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003708
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003709 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003710}
3711
3712/// Checks a regparm attribute, returning true if it is ill-formed and
3713/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003714bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3715 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003716 return true;
3717
Chandler Carruth87c44602011-07-01 23:49:12 +00003718 if (Attr.getNumArgs() != 1) {
3719 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3720 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003721 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003722 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003723
Chandler Carruth87c44602011-07-01 23:49:12 +00003724 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003725 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003726 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003727 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003728 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003729 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003730 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003731 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003732 }
3733
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003734 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003735 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003736 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003737 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003738 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003739 }
3740
John McCall711c52b2011-01-05 12:14:39 +00003741 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003742 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003743 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003744 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003745 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003746 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003747 }
3748
John McCall711c52b2011-01-05 12:14:39 +00003749 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003750}
3751
Chandler Carruth1b03c872011-07-02 00:01:44 +00003752static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003753 if (S.LangOpts.CUDA) {
3754 // check the attribute arguments.
3755 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003756 // FIXME: 0 is not okay.
3757 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003758 return;
3759 }
3760
Chandler Carruth87c44602011-07-01 23:49:12 +00003761 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003763 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003764 return;
3765 }
3766
3767 Expr *MaxThreadsExpr = Attr.getArg(0);
3768 llvm::APSInt MaxThreads(32);
3769 if (MaxThreadsExpr->isTypeDependent() ||
3770 MaxThreadsExpr->isValueDependent() ||
3771 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3772 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3773 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3774 return;
3775 }
3776
3777 llvm::APSInt MinBlocks(32);
3778 if (Attr.getNumArgs() > 1) {
3779 Expr *MinBlocksExpr = Attr.getArg(1);
3780 if (MinBlocksExpr->isTypeDependent() ||
3781 MinBlocksExpr->isValueDependent() ||
3782 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3783 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3784 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3785 return;
3786 }
3787 }
3788
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003789 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003790 MaxThreads.getZExtValue(),
3791 MinBlocks.getZExtValue()));
3792 } else {
3793 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3794 }
3795}
3796
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003797static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3798 const AttributeList &Attr) {
3799 StringRef AttrName = Attr.getName()->getName();
3800 if (!Attr.getParameterName()) {
3801 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3802 << Attr.getName() << /* arg num = */ 1;
3803 return;
3804 }
3805
3806 if (Attr.getNumArgs() != 2) {
3807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3808 << /* required args = */ 3;
3809 return;
3810 }
3811
3812 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3813
3814 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3816 << Attr.getName() << ExpectedFunctionOrMethod;
3817 return;
3818 }
3819
3820 uint64_t ArgumentIdx;
3821 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3822 Attr.getLoc(), 2,
3823 Attr.getArg(0), ArgumentIdx))
3824 return;
3825
3826 uint64_t TypeTagIdx;
3827 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3828 Attr.getLoc(), 3,
3829 Attr.getArg(1), TypeTagIdx))
3830 return;
3831
3832 bool IsPointer = (AttrName == "pointer_with_type_tag");
3833 if (IsPointer) {
3834 // Ensure that buffer has a pointer type.
3835 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3836 if (!BufferTy->isPointerType()) {
3837 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3838 << AttrName;
3839 }
3840 }
3841
3842 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3843 S.Context,
3844 ArgumentKind,
3845 ArgumentIdx,
3846 TypeTagIdx,
3847 IsPointer));
3848}
3849
3850static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3851 const AttributeList &Attr) {
3852 IdentifierInfo *PointerKind = Attr.getParameterName();
3853 if (!PointerKind) {
3854 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3855 << "type_tag_for_datatype" << 1;
3856 return;
3857 }
3858
3859 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3860
3861 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3862 Attr.getRange(),
3863 S.Context,
3864 PointerKind,
3865 MatchingCType,
3866 Attr.getLayoutCompatible(),
3867 Attr.getMustBeNull()));
3868}
3869
Chris Lattner0744e5f2008-06-29 00:23:49 +00003870//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003871// Checker-specific attribute handlers.
3872//===----------------------------------------------------------------------===//
3873
John McCallc7ad3812011-01-25 03:31:58 +00003874static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003875 return type->isDependentType() ||
3876 type->isObjCObjectPointerType() ||
3877 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003878}
3879static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003880 return type->isDependentType() ||
3881 type->isPointerType() ||
3882 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003883}
3884
Chandler Carruth1b03c872011-07-02 00:01:44 +00003885static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003886 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003887 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003888 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003889 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003890 return;
3891 }
3892
3893 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003894 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003895 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3896 cf = false;
3897 } else {
3898 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3899 cf = true;
3900 }
3901
3902 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003903 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003904 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003905 return;
3906 }
3907
3908 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003909 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003910 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003911 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003912}
3913
Chandler Carruth1b03c872011-07-02 00:01:44 +00003914static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3915 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003916 if (!isa<ObjCMethodDecl>(D)) {
3917 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003918 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003919 return;
3920 }
3921
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003922 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003923}
3924
Chandler Carruth1b03c872011-07-02 00:01:44 +00003925static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3926 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003927
John McCallc7ad3812011-01-25 03:31:58 +00003928 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003929
Chandler Carruth87c44602011-07-01 23:49:12 +00003930 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003931 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003932 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003933 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003934 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003935 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3936 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003937 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003938 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003939 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003940 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003941 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003942 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003943 return;
3944 }
Mike Stumpbf916502009-07-24 19:02:52 +00003945
John McCallc7ad3812011-01-25 03:31:58 +00003946 bool typeOK;
3947 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003948 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003949 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003950 case AttributeList::AT_NSReturnsAutoreleased:
3951 case AttributeList::AT_NSReturnsRetained:
3952 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003953 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3954 cf = false;
3955 break;
3956
Sean Hunt8e083e72012-06-19 23:57:03 +00003957 case AttributeList::AT_CFReturnsRetained:
3958 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003959 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3960 cf = true;
3961 break;
3962 }
3963
3964 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003965 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003966 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003967 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003968 }
Mike Stumpbf916502009-07-24 19:02:52 +00003969
Chandler Carruth87c44602011-07-01 23:49:12 +00003970 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003971 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003972 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003973 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003974 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003975 S.Context));
3976 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003977 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003978 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003979 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003980 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003981 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003982 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003983 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003984 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003985 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003986 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003987 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003988 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003989 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003990 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003991 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003992 return;
3993 };
3994}
3995
John McCalldc7c5ad2011-07-22 08:53:00 +00003996static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3997 const AttributeList &attr) {
3998 SourceLocation loc = attr.getLoc();
3999
4000 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4001
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004002 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004003 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004004 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004005 return;
4006 }
4007
4008 // Check that the method returns a normal pointer.
4009 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004010
4011 if (!resultType->isReferenceType() &&
4012 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004013 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4014 << SourceRange(loc)
4015 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4016
4017 // Drop the attribute.
4018 return;
4019 }
4020
4021 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004022 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00004023}
4024
Fariborz Jahanian84101132012-09-07 23:46:23 +00004025static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4026 const AttributeList &attr) {
4027 SourceLocation loc = attr.getLoc();
4028 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4029
4030 if (!method) {
4031 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4032 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4033 return;
4034 }
4035 DeclContext *DC = method->getDeclContext();
4036 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4037 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4038 << attr.getName() << 0;
4039 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4040 return;
4041 }
4042 if (method->getMethodFamily() == OMF_dealloc) {
4043 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4044 << attr.getName() << 1;
4045 return;
4046 }
4047
4048 method->addAttr(
4049 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4050}
4051
John McCall8dfac0b2011-09-30 05:12:12 +00004052/// Handle cf_audited_transfer and cf_unknown_transfer.
4053static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4054 if (!isa<FunctionDecl>(D)) {
4055 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004056 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004057 return;
4058 }
4059
Sean Hunt8e083e72012-06-19 23:57:03 +00004060 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004061
4062 // Check whether there's a conflicting attribute already present.
4063 Attr *Existing;
4064 if (IsAudited) {
4065 Existing = D->getAttr<CFUnknownTransferAttr>();
4066 } else {
4067 Existing = D->getAttr<CFAuditedTransferAttr>();
4068 }
4069 if (Existing) {
4070 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4071 << A.getName()
4072 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4073 << A.getRange() << Existing->getRange();
4074 return;
4075 }
4076
4077 // All clear; add the attribute.
4078 if (IsAudited) {
4079 D->addAttr(
4080 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4081 } else {
4082 D->addAttr(
4083 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4084 }
4085}
4086
John McCallfe98da02011-09-29 07:17:38 +00004087static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4088 const AttributeList &Attr) {
4089 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4090 if (!RD || RD->isUnion()) {
4091 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004092 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004093 }
4094
4095 IdentifierInfo *ParmName = Attr.getParameterName();
4096
4097 // In Objective-C, verify that the type names an Objective-C type.
4098 // We don't want to check this outside of ObjC because people sometimes
4099 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004100 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004101 // Check for an existing type with this name.
4102 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4103 Sema::LookupOrdinaryName);
4104 if (S.LookupName(R, Sc)) {
4105 NamedDecl *Target = R.getFoundDecl();
4106 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4107 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4108 S.Diag(Target->getLocStart(), diag::note_declared_at);
4109 }
4110 }
4111 }
4112
4113 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4114 ParmName));
4115}
4116
Chandler Carruth1b03c872011-07-02 00:01:44 +00004117static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4118 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004119 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004120
Chandler Carruth87c44602011-07-01 23:49:12 +00004121 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004122 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004123}
4124
Chandler Carruth1b03c872011-07-02 00:01:44 +00004125static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4126 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004127 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004128 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004129 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004130 return;
4131 }
4132
Chandler Carruth87c44602011-07-01 23:49:12 +00004133 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004134 QualType type = vd->getType();
4135
4136 if (!type->isDependentType() &&
4137 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004139 << type;
4140 return;
4141 }
4142
4143 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4144
4145 // If we have no lifetime yet, check the lifetime we're presumably
4146 // going to infer.
4147 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4148 lifetime = type->getObjCARCImplicitLifetime();
4149
4150 switch (lifetime) {
4151 case Qualifiers::OCL_None:
4152 assert(type->isDependentType() &&
4153 "didn't infer lifetime for non-dependent type?");
4154 break;
4155
4156 case Qualifiers::OCL_Weak: // meaningful
4157 case Qualifiers::OCL_Strong: // meaningful
4158 break;
4159
4160 case Qualifiers::OCL_ExplicitNone:
4161 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004162 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004163 << (lifetime == Qualifiers::OCL_Autoreleasing);
4164 break;
4165 }
4166
Chandler Carruth87c44602011-07-01 23:49:12 +00004167 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004168 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004169}
4170
Francois Pichet11542142010-12-19 06:50:37 +00004171//===----------------------------------------------------------------------===//
4172// Microsoft specific attribute handlers.
4173//===----------------------------------------------------------------------===//
4174
Chandler Carruth1b03c872011-07-02 00:01:44 +00004175static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004176 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004177 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004178 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004179 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004180
Francois Pichet11542142010-12-19 06:50:37 +00004181 Expr *Arg = Attr.getArg(0);
4182 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004183 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004184 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4185 << "uuid" << 1;
4186 return;
4187 }
4188
Chris Lattner5f9e2722011-07-23 10:55:15 +00004189 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004190
4191 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4192 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004193
Francois Pichetd3d3be92010-12-20 01:41:49 +00004194 // Validate GUID length.
4195 if (IsCurly && StrRef.size() != 38) {
4196 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4197 return;
4198 }
4199 if (!IsCurly && StrRef.size() != 36) {
4200 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4201 return;
4202 }
4203
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004204 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004205 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004206 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004207 if (IsCurly) // Skip the optional '{'
4208 ++I;
4209
4210 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004211 if (i == 8 || i == 13 || i == 18 || i == 23) {
4212 if (*I != '-') {
4213 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4214 return;
4215 }
4216 } else if (!isxdigit(*I)) {
4217 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4218 return;
4219 }
4220 I++;
4221 }
Francois Pichet11542142010-12-19 06:50:37 +00004222
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004223 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004224 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004225 } else
Francois Pichet11542142010-12-19 06:50:37 +00004226 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004227}
4228
John McCallc052dbb2012-05-22 21:28:12 +00004229static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004230 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004231 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004232 return;
4233 }
4234
4235 AttributeList::Kind Kind = Attr.getKind();
4236 if (Kind == AttributeList::AT_SingleInheritance)
4237 D->addAttr(
4238 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4239 else if (Kind == AttributeList::AT_MultipleInheritance)
4240 D->addAttr(
4241 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4242 else if (Kind == AttributeList::AT_VirtualInheritance)
4243 D->addAttr(
4244 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
John McCallc052dbb2012-05-22 21:28:12 +00004245}
4246
4247static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4248 if (S.LangOpts.MicrosoftExt) {
4249 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004250 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004251 D->addAttr(
4252 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004253 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004254 D->addAttr(
4255 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004256 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004257 D->addAttr(
4258 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4259 } else
4260 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4261}
4262
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004263static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4264 if (S.LangOpts.MicrosoftExt)
4265 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4266 else
4267 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4268}
4269
Ted Kremenekb71368d2009-05-09 02:44:38 +00004270//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004271// Top Level Sema Entry Points
4272//===----------------------------------------------------------------------===//
4273
Chandler Carruth1b03c872011-07-02 00:01:44 +00004274static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4275 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004276 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004277 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4278 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4279 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004280 default:
4281 break;
4282 }
4283}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004284
Chandler Carruth1b03c872011-07-02 00:01:44 +00004285static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4286 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004287 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004288 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4289 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4290 case AttributeList::AT_IBOutletCollection:
4291 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004292 case AttributeList::AT_AddressSpace:
4293 case AttributeList::AT_OpenCLImageAccess:
4294 case AttributeList::AT_ObjCGC:
4295 case AttributeList::AT_VectorSize:
4296 case AttributeList::AT_NeonVectorType:
4297 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004298 // Ignore these, these are type attributes, handled by
4299 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004300 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004301 case AttributeList::AT_CUDADevice:
4302 case AttributeList::AT_CUDAHost:
4303 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004304 // Ignore, this is a non-inheritable attribute, handled
4305 // by ProcessNonInheritableDeclAttr.
4306 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004307 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4308 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4309 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4310 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004311 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004312 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004313 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004314 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004315 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4316 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4317 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004318 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004319 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4320 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4321 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004322 case AttributeList::AT_CXX11NoReturn:
4323 handleCXX11NoReturnAttr(S, D, Attr);
4324 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004325 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004326 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4327 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004328 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4329 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004330 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004331 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004332 case AttributeList::AT_MinSize:
4333 handleMinSizeAttr(S, D, Attr);
4334 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004335 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4336 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4337 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4338 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4339 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004340 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004341 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004342 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4343 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4344 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4345 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4346 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004347 case AttributeList::AT_ownership_returns:
4348 case AttributeList::AT_ownership_takes:
4349 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004350 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004351 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4352 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4353 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4354 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4355 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4356 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4357 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004358
Sean Hunt8e083e72012-06-19 23:57:03 +00004359 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004360 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004361 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004362 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004363
Sean Hunt8e083e72012-06-19 23:57:03 +00004364 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004365 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4366
Fariborz Jahanian84101132012-09-07 23:46:23 +00004367 case AttributeList::AT_ObjCRequiresSuper:
4368 handleObjCRequiresSuperAttr(S, D, Attr); break;
4369
Sean Hunt8e083e72012-06-19 23:57:03 +00004370 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004371 handleNSBridgedAttr(S, scope, D, Attr); break;
4372
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_CFAuditedTransfer:
4374 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004375 handleCFTransferAttr(S, D, Attr); break;
4376
Ted Kremenekb71368d2009-05-09 02:44:38 +00004377 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004378 case AttributeList::AT_CFConsumed:
4379 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4380 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004381 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004382
Sean Hunt8e083e72012-06-19 23:57:03 +00004383 case AttributeList::AT_NSReturnsAutoreleased:
4384 case AttributeList::AT_NSReturnsNotRetained:
4385 case AttributeList::AT_CFReturnsNotRetained:
4386 case AttributeList::AT_NSReturnsRetained:
4387 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004388 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004389
Tanya Lattner0df579e2012-07-09 22:06:01 +00004390 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004391 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004392 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004393
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004395 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004396
Sean Hunt8e083e72012-06-19 23:57:03 +00004397 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4398 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4399 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004400 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4401 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004402 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004403 handleArcWeakrefUnavailableAttr (S, D, Attr);
4404 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004405 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004406 handleObjCRootClassAttr(S, D, Attr);
4407 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004408 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004409 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004410 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004411 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4412 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004413 handleReturnsTwiceAttr(S, D, Attr);
4414 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004415 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4416 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4417 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004418 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004419 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4420 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4421 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4422 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004423 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004424 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004425 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004426 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004427 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004428 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004429 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004430 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004431 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4432 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4433 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4434 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4435 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4436 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4437 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4438 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4439 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004440 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004441 // Just ignore
4442 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004443 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004444 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004445 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004446 case AttributeList::AT_StdCall:
4447 case AttributeList::AT_CDecl:
4448 case AttributeList::AT_FastCall:
4449 case AttributeList::AT_ThisCall:
4450 case AttributeList::AT_Pascal:
4451 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004452 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004453 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004454 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004455 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004456 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004457 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004458 break;
John McCallc052dbb2012-05-22 21:28:12 +00004459
4460 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004461 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004462 handleMsStructAttr(S, D, Attr);
4463 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004464 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004465 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004466 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004467 case AttributeList::AT_SingleInheritance:
4468 case AttributeList::AT_MultipleInheritance:
4469 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004470 handleInheritanceAttr(S, D, Attr);
4471 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004472 case AttributeList::AT_Win64:
4473 case AttributeList::AT_Ptr32:
4474 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004475 handlePortabilityAttr(S, D, Attr);
4476 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004477 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004478 handleForceInlineAttr(S, D, Attr);
4479 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004480
4481 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004482 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004483 handleGuardedVarAttr(S, D, Attr);
4484 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004485 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004486 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004487 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004488 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004489 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004490 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004491 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004492 handleNoAddressSafetyAttr(S, D, Attr);
4493 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004494 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004495 handleNoThreadSafetyAttr(S, D, Attr);
4496 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004497 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004498 handleLockableAttr(S, D, Attr);
4499 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004500 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004501 handleGuardedByAttr(S, D, Attr);
4502 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004503 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004504 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004505 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004506 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004507 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004508 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004509 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004510 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004511 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004512 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004513 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004514 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004515 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004516 handleLockReturnedAttr(S, D, Attr);
4517 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004518 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004519 handleLocksExcludedAttr(S, D, Attr);
4520 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004521 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004522 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004523 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004524 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004525 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004526 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004527 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004528 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004529 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004530 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004531 handleUnlockFunAttr(S, D, Attr);
4532 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004533 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004534 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004535 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004536 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004537 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004538 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004539
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004540 // Type safety attributes.
4541 case AttributeList::AT_ArgumentWithTypeTag:
4542 handleArgumentWithTypeTagAttr(S, D, Attr);
4543 break;
4544 case AttributeList::AT_TypeTagForDatatype:
4545 handleTypeTagForDatatypeAttr(S, D, Attr);
4546 break;
4547
Chris Lattner803d0802008-06-29 00:43:07 +00004548 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004549 // Ask target about the attribute.
4550 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4551 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004552 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4553 diag::warn_unhandled_ms_attribute_ignored :
4554 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004555 break;
4556 }
4557}
4558
Peter Collingbourne60700392011-01-21 02:08:45 +00004559/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4560/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00004561/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004562static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4563 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00004564 bool NonInheritable, bool Inheritable,
4565 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004566 if (Attr.isInvalid())
4567 return;
4568
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004569 // Type attributes are still treated as declaration attributes by
4570 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4571 // want to process them, however, because we will simply warn about ignoring
4572 // them. So instead, we will bail out early.
4573 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004574 return;
4575
Richard Smithcd8ab512013-01-17 01:30:42 +00004576 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4577 // instead.
4578 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4579 return;
4580
Peter Collingbourne60700392011-01-21 02:08:45 +00004581 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004582 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004583
4584 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004585 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004586}
4587
Chris Lattner803d0802008-06-29 00:43:07 +00004588/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4589/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004590void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004591 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004592 bool NonInheritable, bool Inheritable,
4593 bool IncludeCXX11Attributes) {
4594 for (const AttributeList* l = AttrList; l; l = l->getNext())
4595 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
4596 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004597
4598 // GCC accepts
4599 // static int a9 __attribute__((weakref));
4600 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004601 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004602 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004603 cast<NamedDecl>(D)->getNameAsString();
4604 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004605 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004606 }
4607}
4608
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004609// Annotation attributes are the only attributes allowed after an access
4610// specifier.
4611bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4612 const AttributeList *AttrList) {
4613 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004614 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004615 handleAnnotateAttr(*this, ASDecl, *l);
4616 } else {
4617 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4618 return true;
4619 }
4620 }
4621
4622 return false;
4623}
4624
John McCalle82247a2011-10-01 05:17:03 +00004625/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4626/// contains any decl attributes that we should warn about.
4627static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4628 for ( ; A; A = A->getNext()) {
4629 // Only warn if the attribute is an unignored, non-type attribute.
4630 if (A->isUsedAsTypeAttr()) continue;
4631 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4632
4633 if (A->getKind() == AttributeList::UnknownAttribute) {
4634 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4635 << A->getName() << A->getRange();
4636 } else {
4637 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4638 << A->getName() << A->getRange();
4639 }
4640 }
4641}
4642
4643/// checkUnusedDeclAttributes - Given a declarator which is not being
4644/// used to build a declaration, complain about any decl attributes
4645/// which might be lying around on it.
4646void Sema::checkUnusedDeclAttributes(Declarator &D) {
4647 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4648 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4649 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4650 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4651}
4652
Ryan Flynne25ff832009-07-30 03:15:39 +00004653/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004654/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004655NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4656 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004657 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004658 NamedDecl *NewD = 0;
4659 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004660 FunctionDecl *NewFD;
4661 // FIXME: Missing call to CheckFunctionDeclaration().
4662 // FIXME: Mangling?
4663 // FIXME: Is the qualifier info correct?
4664 // FIXME: Is the DeclContext correct?
4665 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4666 Loc, Loc, DeclarationName(II),
4667 FD->getType(), FD->getTypeSourceInfo(),
4668 SC_None, SC_None,
4669 false/*isInlineSpecified*/,
4670 FD->hasPrototype(),
4671 false/*isConstexprSpecified*/);
4672 NewD = NewFD;
4673
4674 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004675 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004676
4677 // Fake up parameter variables; they are declared as if this were
4678 // a typedef.
4679 QualType FDTy = FD->getType();
4680 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4681 SmallVector<ParmVarDecl*, 16> Params;
4682 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4683 AE = FT->arg_type_end(); AI != AE; ++AI) {
4684 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4685 Param->setScopeInfo(0, Params.size());
4686 Params.push_back(Param);
4687 }
David Blaikie4278c652011-09-21 18:16:56 +00004688 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004689 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004690 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4691 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004692 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004693 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004694 VD->getStorageClass(),
4695 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004696 if (VD->getQualifier()) {
4697 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004698 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004699 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004700 }
4701 return NewD;
4702}
4703
James Dennett1dfbd922012-06-14 21:40:34 +00004704/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004705/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004706void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004707 if (W.getUsed()) return; // only do this once
4708 W.setUsed(true);
4709 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4710 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004711 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004712 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4713 NDId->getName()));
4714 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004715 WeakTopLevelDecl.push_back(NewD);
4716 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4717 // to insert Decl at TU scope, sorry.
4718 DeclContext *SavedContext = CurContext;
4719 CurContext = Context.getTranslationUnitDecl();
4720 PushOnScopeChains(NewD, S);
4721 CurContext = SavedContext;
4722 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004723 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004724 }
4725}
4726
Chris Lattner0744e5f2008-06-29 00:23:49 +00004727/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4728/// it, apply them to D. This is a bit tricky because PD can have attributes
4729/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004730void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4731 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004732 // It's valid to "forward-declare" #pragma weak, in which case we
4733 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004734 if (Inheritable) {
4735 LoadExternalWeakUndeclaredIdentifiers();
4736 if (!WeakUndeclaredIdentifiers.empty()) {
4737 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4738 if (IdentifierInfo *Id = ND->getIdentifier()) {
4739 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4740 = WeakUndeclaredIdentifiers.find(Id);
4741 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4742 WeakInfo W = I->second;
4743 DeclApplyPragmaWeak(S, ND, W);
4744 WeakUndeclaredIdentifiers[Id] = W;
4745 }
John McCalld4aff0e2010-10-27 00:59:00 +00004746 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004747 }
4748 }
4749 }
4750
Chris Lattner0744e5f2008-06-29 00:23:49 +00004751 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004752 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004753 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004754
Chris Lattner0744e5f2008-06-29 00:23:49 +00004755 // Walk the declarator structure, applying decl attributes that were in a type
4756 // position to the decl itself. This handles cases like:
4757 // int *__attr__(x)** D;
4758 // when X is a decl attribute.
4759 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4760 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00004761 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
4762 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00004763
Chris Lattner0744e5f2008-06-29 00:23:49 +00004764 // Finally, apply any attributes on the decl itself.
4765 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004766 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004767}
John McCall54abf7d2009-11-04 02:18:39 +00004768
John McCallf85e1932011-06-15 23:02:42 +00004769/// Is the given declaration allowed to use a forbidden type?
4770static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4771 // Private ivars are always okay. Unfortunately, people don't
4772 // always properly make their ivars private, even in system headers.
4773 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004774 // Function declarations in sys headers will be marked unavailable.
4775 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4776 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004777 return false;
4778
4779 // Require it to be declared in a system header.
4780 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4781}
4782
4783/// Handle a delayed forbidden-type diagnostic.
4784static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4785 Decl *decl) {
4786 if (decl && isForbiddenTypeAllowed(S, decl)) {
4787 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4788 "this system declaration uses an unsupported type"));
4789 return;
4790 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004791 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004792 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004793 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004794 // kind of forbidden type messages on unavailable functions.
4795 if (FD->hasAttr<UnavailableAttr>() &&
4796 diag.getForbiddenTypeDiagnostic() ==
4797 diag::err_arc_array_param_no_ownership) {
4798 diag.Triggered = true;
4799 return;
4800 }
4801 }
John McCallf85e1932011-06-15 23:02:42 +00004802
4803 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4804 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4805 diag.Triggered = true;
4806}
4807
John McCall92576642012-05-07 06:16:41 +00004808void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4809 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004810 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004811 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004812
John McCall92576642012-05-07 06:16:41 +00004813 // When delaying diagnostics to run in the context of a parsed
4814 // declaration, we only want to actually emit anything if parsing
4815 // succeeds.
4816 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004817
John McCall92576642012-05-07 06:16:41 +00004818 // We emit all the active diagnostics in this pool or any of its
4819 // parents. In general, we'll get one pool for the decl spec
4820 // and a child pool for each declarator; in a decl group like:
4821 // deprecated_typedef foo, *bar, baz();
4822 // only the declarator pops will be passed decls. This is correct;
4823 // we really do need to consider delayed diagnostics from the decl spec
4824 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004825 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004826 do {
John McCall13489672012-05-07 06:16:58 +00004827 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004828 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4829 // This const_cast is a bit lame. Really, Triggered should be mutable.
4830 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004831 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004832 continue;
4833
John McCalleee1d542011-02-14 07:13:47 +00004834 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004835 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004836 // Don't bother giving deprecation diagnostics if the decl is invalid.
4837 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004838 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004839 break;
4840
4841 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004842 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004843 break;
John McCallf85e1932011-06-15 23:02:42 +00004844
4845 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004846 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004847 break;
John McCall2f514482010-01-27 03:50:35 +00004848 }
4849 }
John McCall92576642012-05-07 06:16:41 +00004850 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004851}
4852
John McCall13489672012-05-07 06:16:58 +00004853/// Given a set of delayed diagnostics, re-emit them as if they had
4854/// been delayed in the current context instead of in the given pool.
4855/// Essentially, this just moves them to the current pool.
4856void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4857 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4858 assert(curPool && "re-emitting in undelayed context not supported");
4859 curPool->steal(pool);
4860}
4861
John McCall54abf7d2009-11-04 02:18:39 +00004862static bool isDeclDeprecated(Decl *D) {
4863 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004864 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004865 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004866 // A category implicitly has the availability of the interface.
4867 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4868 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004869 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4870 return false;
4871}
4872
Eli Friedmanc3b23082012-08-08 21:52:41 +00004873static void
4874DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4875 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004876 const ObjCInterfaceDecl *UnknownObjCClass,
4877 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00004878 DeclarationName Name = D->getDeclName();
4879 if (!Message.empty()) {
4880 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4881 S.Diag(D->getLocation(),
4882 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4883 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004884 if (ObjCPropery)
4885 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4886 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004887 } else if (!UnknownObjCClass) {
4888 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4889 S.Diag(D->getLocation(),
4890 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4891 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004892 if (ObjCPropery)
4893 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4894 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004895 } else {
4896 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4897 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4898 }
4899}
4900
John McCall9c3087b2010-08-26 02:13:20 +00004901void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004902 Decl *Ctx) {
4903 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004904 return;
4905
John McCall2f514482010-01-27 03:50:35 +00004906 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004907 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4908 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004909 DD.getUnknownObjCClass(),
4910 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00004911}
4912
Chris Lattner5f9e2722011-07-23 10:55:15 +00004913void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004914 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004915 const ObjCInterfaceDecl *UnknownObjCClass,
4916 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00004917 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004918 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004919 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4920 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004921 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004922 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004923 return;
4924 }
4925
4926 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004927 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004928 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004929 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00004930}