blob: 74a6bc5546edccf3c0d94bca7f917f87a14fa1db [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);
1138 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
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001143 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1144 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001145
1146
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001147 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001148 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001149 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001150 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1151 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1153 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001154 return;
1155 }
Mike Stumpbf916502009-07-24 19:02:52 +00001156
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001157 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001158
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001159 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001161 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001162 return;
1163 }
Mike Stumpbf916502009-07-24 19:02:52 +00001164
Ted Kremenek465172f2008-07-21 22:09:15 +00001165 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001166 if (HasImplicitThisParam) {
1167 if (x == 0) {
1168 S.Diag(Attr.getLoc(),
1169 diag::err_attribute_invalid_implicit_this_argument)
1170 << "nonnull" << Ex->getSourceRange();
1171 return;
1172 }
1173 --x;
1174 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001175
1176 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001177 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001178 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001179
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001180 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001181 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001182 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001183 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001184 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001185 }
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001187 NonNullArgs.push_back(x);
1188 }
Mike Stumpbf916502009-07-24 19:02:52 +00001189
1190 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1191 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001192 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001193 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1194 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001195 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001196 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001197 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001198 }
Mike Stumpbf916502009-07-24 19:02:52 +00001199
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001200 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001201 if (NonNullArgs.empty()) {
1202 // Warn the trivial case only if attribute is not coming from a
1203 // macro instantiation.
1204 if (Attr.getLoc().isFileID())
1205 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001206 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001207 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001208 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001209
1210 unsigned* start = &NonNullArgs[0];
1211 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001212 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001213 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001214 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001215}
1216
Chandler Carruth1b03c872011-07-02 00:01:44 +00001217static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001218 // This attribute must be applied to a function declaration.
1219 // The first argument to the attribute must be a string,
1220 // the name of the resource, for example "malloc".
1221 // The following arguments must be argument indexes, the arguments must be
1222 // of integer type for Returns, otherwise of pointer type.
1223 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001224 // after being held. free() should be __attribute((ownership_takes)), whereas
1225 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001226
1227 if (!AL.getParameterName()) {
1228 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1229 << AL.getName()->getName() << 1;
1230 return;
1231 }
1232 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001233 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001234 switch (AL.getKind()) {
1235 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001236 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001237 if (AL.getNumArgs() < 1) {
1238 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1239 return;
1240 }
Jordy Rose2a479922010-08-12 08:54:03 +00001241 break;
1242 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001243 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001244 if (AL.getNumArgs() < 1) {
1245 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1246 return;
1247 }
Jordy Rose2a479922010-08-12 08:54:03 +00001248 break;
1249 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001250 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001251 if (AL.getNumArgs() > 1) {
1252 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1253 << AL.getNumArgs() + 1;
1254 return;
1255 }
Jordy Rose2a479922010-08-12 08:54:03 +00001256 break;
1257 default:
1258 // This should never happen given how we are called.
1259 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001260 }
1261
Chandler Carruth87c44602011-07-01 23:49:12 +00001262 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001263 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1264 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001265 return;
1266 }
1267
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001268 // In C++ the implicit 'this' function parameter also counts, and they are
1269 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001270 bool HasImplicitThisParam = isInstanceMethod(D);
1271 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001272
Chris Lattner5f9e2722011-07-23 10:55:15 +00001273 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001274
1275 // Normalize the argument, __foo__ becomes foo.
1276 if (Module.startswith("__") && Module.endswith("__"))
1277 Module = Module.substr(2, Module.size() - 4);
1278
Chris Lattner5f9e2722011-07-23 10:55:15 +00001279 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001280
Jordy Rose2a479922010-08-12 08:54:03 +00001281 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1282 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001283
Peter Collingbourne7a730022010-11-23 20:45:58 +00001284 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001285 llvm::APSInt ArgNum(32);
1286 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1287 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1288 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1289 << AL.getName()->getName() << IdxExpr->getSourceRange();
1290 continue;
1291 }
1292
1293 unsigned x = (unsigned) ArgNum.getZExtValue();
1294
1295 if (x > NumArgs || x < 1) {
1296 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1297 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1298 continue;
1299 }
1300 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001301 if (HasImplicitThisParam) {
1302 if (x == 0) {
1303 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1304 << "ownership" << IdxExpr->getSourceRange();
1305 return;
1306 }
1307 --x;
1308 }
1309
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001310 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001311 case OwnershipAttr::Takes:
1312 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001313 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001314 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001315 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1316 // FIXME: Should also highlight argument in decl.
1317 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001318 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001319 << "pointer"
1320 << IdxExpr->getSourceRange();
1321 continue;
1322 }
1323 break;
1324 }
Sean Huntcf807c42010-08-18 23:23:40 +00001325 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001326 if (AL.getNumArgs() > 1) {
1327 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001328 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001329 llvm::APSInt ArgNum(32);
1330 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1331 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1332 S.Diag(AL.getLoc(), diag::err_ownership_type)
1333 << "ownership_returns" << "integer"
1334 << IdxExpr->getSourceRange();
1335 return;
1336 }
1337 }
1338 break;
1339 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001340 } // switch
1341
1342 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001343 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001344 i = D->specific_attr_begin<OwnershipAttr>(),
1345 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001346 i != e; ++i) {
1347 if ((*i)->getOwnKind() != K) {
1348 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1349 I!=E; ++I) {
1350 if (x == *I) {
1351 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1352 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001353 }
1354 }
1355 }
1356 }
1357 OwnershipArgs.push_back(x);
1358 }
1359
1360 unsigned* start = OwnershipArgs.data();
1361 unsigned size = OwnershipArgs.size();
1362 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001363
1364 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1365 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1366 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001367 }
Sean Huntcf807c42010-08-18 23:23:40 +00001368
Chandler Carruth87c44602011-07-01 23:49:12 +00001369 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001370 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001371}
1372
John McCall332bb2a2011-02-08 22:35:49 +00001373/// Whether this declaration has internal linkage for the purposes of
1374/// things that want to complain about things not have internal linkage.
1375static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1376 switch (D->getLinkage()) {
1377 case NoLinkage:
1378 case InternalLinkage:
1379 return true;
1380
1381 // Template instantiations that go from external to unique-external
1382 // shouldn't get diagnosed.
1383 case UniqueExternalLinkage:
1384 return true;
1385
1386 case ExternalLinkage:
1387 return false;
1388 }
1389 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001390}
1391
Chandler Carruth1b03c872011-07-02 00:01:44 +00001392static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001393 // Check the attribute arguments.
1394 if (Attr.getNumArgs() > 1) {
1395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1396 return;
1397 }
1398
Chandler Carruth87c44602011-07-01 23:49:12 +00001399 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001401 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001402 return;
1403 }
1404
Chandler Carruth87c44602011-07-01 23:49:12 +00001405 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001406
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001407 // gcc rejects
1408 // class c {
1409 // static int a __attribute__((weakref ("v2")));
1410 // static int b() __attribute__((weakref ("f3")));
1411 // };
1412 // and ignores the attributes of
1413 // void f(void) {
1414 // static int a __attribute__((weakref ("v2")));
1415 // }
1416 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001417 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001418 if (!Ctx->isFileContext()) {
1419 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001420 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001421 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001422 }
1423
1424 // The GCC manual says
1425 //
1426 // At present, a declaration to which `weakref' is attached can only
1427 // be `static'.
1428 //
1429 // It also says
1430 //
1431 // Without a TARGET,
1432 // given as an argument to `weakref' or to `alias', `weakref' is
1433 // equivalent to `weak'.
1434 //
1435 // gcc 4.4.1 will accept
1436 // int a7 __attribute__((weakref));
1437 // as
1438 // int a7 __attribute__((weak));
1439 // This looks like a bug in gcc. We reject that for now. We should revisit
1440 // it if this behaviour is actually used.
1441
John McCall332bb2a2011-02-08 22:35:49 +00001442 if (!hasEffectivelyInternalLinkage(nd)) {
1443 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001444 return;
1445 }
1446
1447 // GCC rejects
1448 // static ((alias ("y"), weakref)).
1449 // Should we? How to check that weakref is before or after alias?
1450
1451 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001452 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001453 Arg = Arg->IgnoreParenCasts();
1454 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1455
Douglas Gregor5cee1192011-07-27 05:40:30 +00001456 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1458 << "weakref" << 1;
1459 return;
1460 }
1461 // GCC will accept anything as the argument of weakref. Should we
1462 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001463 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001464 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001465 }
1466
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001467 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001468}
1469
Chandler Carruth1b03c872011-07-02 00:01:44 +00001470static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001471 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001472 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001474 return;
1475 }
Mike Stumpbf916502009-07-24 19:02:52 +00001476
Peter Collingbourne7a730022010-11-23 20:45:58 +00001477 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001478 Arg = Arg->IgnoreParenCasts();
1479 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001480
Douglas Gregor5cee1192011-07-27 05:40:30 +00001481 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001483 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001484 return;
1485 }
Mike Stumpbf916502009-07-24 19:02:52 +00001486
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001487 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001488 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1489 return;
1490 }
1491
Chris Lattner6b6b5372008-06-26 18:38:35 +00001492 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001493
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001494 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001495 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001496}
1497
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001498static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1499 // Check the attribute arguments.
1500 if (!checkAttributeNumArgs(S, Attr, 0))
1501 return;
1502
1503 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1505 << Attr.getName() << ExpectedFunctionOrMethod;
1506 return;
1507 }
1508
1509 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1510}
1511
Benjamin Krameree409a92012-05-12 21:10:52 +00001512static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1513 // Check the attribute arguments.
1514 if (!checkAttributeNumArgs(S, Attr, 0))
1515 return;
1516
1517 if (!isa<FunctionDecl>(D)) {
1518 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1519 << Attr.getName() << ExpectedFunction;
1520 return;
1521 }
1522
1523 if (D->hasAttr<HotAttr>()) {
1524 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1525 << Attr.getName() << "hot";
1526 return;
1527 }
1528
1529 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1530}
1531
1532static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1533 // Check the attribute arguments.
1534 if (!checkAttributeNumArgs(S, Attr, 0))
1535 return;
1536
1537 if (!isa<FunctionDecl>(D)) {
1538 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1539 << Attr.getName() << ExpectedFunction;
1540 return;
1541 }
1542
1543 if (D->hasAttr<ColdAttr>()) {
1544 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1545 << Attr.getName() << "cold";
1546 return;
1547 }
1548
1549 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1550}
1551
Chandler Carruth1b03c872011-07-02 00:01:44 +00001552static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001553 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001554 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001555 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001556
Chandler Carruth87c44602011-07-01 23:49:12 +00001557 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001558 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001559 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001560 return;
1561 }
1562
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001563 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001564}
1565
Chandler Carruth1b03c872011-07-02 00:01:44 +00001566static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1567 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001568 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001569 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001570 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1571 return;
1572 }
1573
Chandler Carruth87c44602011-07-01 23:49:12 +00001574 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001575 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001576 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001577 return;
1578 }
Mike Stumpbf916502009-07-24 19:02:52 +00001579
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001580 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001581}
1582
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001583static void handleTLSModelAttr(Sema &S, Decl *D,
1584 const AttributeList &Attr) {
1585 // Check the attribute arguments.
1586 if (Attr.getNumArgs() != 1) {
1587 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1588 return;
1589 }
1590
1591 Expr *Arg = Attr.getArg(0);
1592 Arg = Arg->IgnoreParenCasts();
1593 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1594
1595 // Check that it is a string.
1596 if (!Str) {
1597 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1598 return;
1599 }
1600
1601 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1602 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1603 << Attr.getName() << ExpectedTLSVar;
1604 return;
1605 }
1606
1607 // Check that the value.
1608 StringRef Model = Str->getString();
1609 if (Model != "global-dynamic" && Model != "local-dynamic"
1610 && Model != "initial-exec" && Model != "local-exec") {
1611 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1612 return;
1613 }
1614
1615 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1616 Model));
1617}
1618
Chandler Carruth1b03c872011-07-02 00:01:44 +00001619static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001620 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001621 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001622 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1623 return;
1624 }
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Chandler Carruth87c44602011-07-01 23:49:12 +00001626 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001628 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001629 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001630 return;
1631 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001632 }
1633
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001634 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001635}
1636
Chandler Carruth1b03c872011-07-02 00:01:44 +00001637static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001638 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001639 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001640 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001641
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001642 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001643}
1644
Chandler Carruth1b03c872011-07-02 00:01:44 +00001645static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001646 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001647 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001648 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001649 else
1650 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001651 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001652}
1653
Chandler Carruth1b03c872011-07-02 00:01:44 +00001654static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001655 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001656 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001657 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001658 else
1659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001660 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001661}
1662
Chandler Carruth1b03c872011-07-02 00:01:44 +00001663static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001664 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001665
1666 if (S.CheckNoReturnAttr(attr)) return;
1667
Chandler Carruth87c44602011-07-01 23:49:12 +00001668 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001669 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001670 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001671 return;
1672 }
1673
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001674 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001675}
1676
1677bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001678 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001679 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1680 attr.setInvalid();
1681 return true;
1682 }
1683
1684 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001685}
1686
Chandler Carruth1b03c872011-07-02 00:01:44 +00001687static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1688 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001689
1690 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1691 // because 'analyzer_noreturn' does not impact the type.
1692
Chandler Carruth1731e202011-07-11 23:30:35 +00001693 if(!checkAttributeNumArgs(S, Attr, 0))
1694 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001695
Chandler Carruth87c44602011-07-01 23:49:12 +00001696 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1697 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001698 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1699 && !VD->getType()->isFunctionPointerType())) {
1700 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001701 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001702 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001703 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001704 return;
1705 }
1706 }
1707
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001708 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001709}
1710
John Thompson35cc9622010-08-09 21:53:52 +00001711// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001712static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001713/*
1714 Returning a Vector Class in Registers
1715
Eric Christopherf48f3672010-12-01 22:13:54 +00001716 According to the PPU ABI specifications, a class with a single member of
1717 vector type is returned in memory when used as the return value of a function.
1718 This results in inefficient code when implementing vector classes. To return
1719 the value in a single vector register, add the vecreturn attribute to the
1720 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001721
1722 Example:
1723
1724 struct Vector
1725 {
1726 __vector float xyzw;
1727 } __attribute__((vecreturn));
1728
1729 Vector Add(Vector lhs, Vector rhs)
1730 {
1731 Vector result;
1732 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1733 return result; // This will be returned in a register
1734 }
1735*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001736 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001737 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001738 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001739 return;
1740 }
1741
Chandler Carruth87c44602011-07-01 23:49:12 +00001742 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001743 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1744 return;
1745 }
1746
Chandler Carruth87c44602011-07-01 23:49:12 +00001747 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001748 int count = 0;
1749
1750 if (!isa<CXXRecordDecl>(record)) {
1751 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1752 return;
1753 }
1754
1755 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1756 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1757 return;
1758 }
1759
Eric Christopherf48f3672010-12-01 22:13:54 +00001760 for (RecordDecl::field_iterator iter = record->field_begin();
1761 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001762 if ((count == 1) || !iter->getType()->isVectorType()) {
1763 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1764 return;
1765 }
1766 count++;
1767 }
1768
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001769 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001770}
1771
Chandler Carruth1b03c872011-07-02 00:01:44 +00001772static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001773 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001775 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001776 return;
1777 }
1778 // FIXME: Actually store the attribute on the declaration
1779}
1780
Chandler Carruth1b03c872011-07-02 00:01:44 +00001781static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001782 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001783 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001784 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001785 return;
1786 }
Mike Stumpbf916502009-07-24 19:02:52 +00001787
Chandler Carruth87c44602011-07-01 23:49:12 +00001788 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001789 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001790 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001791 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001792 return;
1793 }
Mike Stumpbf916502009-07-24 19:02:52 +00001794
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001795 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001796}
1797
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001798static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1799 const AttributeList &Attr) {
1800 // check the attribute arguments.
1801 if (Attr.hasParameterOrArguments()) {
1802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1803 return;
1804 }
1805
1806 if (!isa<FunctionDecl>(D)) {
1807 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1808 << Attr.getName() << ExpectedFunction;
1809 return;
1810 }
1811
1812 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1813}
1814
Chandler Carruth1b03c872011-07-02 00:01:44 +00001815static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001816 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001817 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001818 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1819 return;
1820 }
Mike Stumpbf916502009-07-24 19:02:52 +00001821
Chandler Carruth87c44602011-07-01 23:49:12 +00001822 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001823 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001824 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1825 return;
1826 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001827 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001829 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001830 return;
1831 }
Mike Stumpbf916502009-07-24 19:02:52 +00001832
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001833 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001834}
1835
Chandler Carruth1b03c872011-07-02 00:01:44 +00001836static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001837 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001838 if (Attr.getNumArgs() > 1) {
1839 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001840 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001841 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001842
1843 int priority = 65535; // FIXME: Do not hardcode such constants.
1844 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001845 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001846 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001847 if (E->isTypeDependent() || E->isValueDependent() ||
1848 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001849 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001850 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001851 return;
1852 }
1853 priority = Idx.getZExtValue();
1854 }
Mike Stumpbf916502009-07-24 19:02:52 +00001855
Chandler Carruth87c44602011-07-01 23:49:12 +00001856 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001858 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001859 return;
1860 }
1861
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001862 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001863 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001864}
1865
Chandler Carruth1b03c872011-07-02 00:01:44 +00001866static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001867 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001868 if (Attr.getNumArgs() > 1) {
1869 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001870 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001871 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001872
1873 int priority = 65535; // FIXME: Do not hardcode such constants.
1874 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001875 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001876 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001877 if (E->isTypeDependent() || E->isValueDependent() ||
1878 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001880 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001881 return;
1882 }
1883 priority = Idx.getZExtValue();
1884 }
Mike Stumpbf916502009-07-24 19:02:52 +00001885
Chandler Carruth87c44602011-07-01 23:49:12 +00001886 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001887 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001888 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001889 return;
1890 }
1891
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001892 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001893 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001894}
1895
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001896template <typename AttrTy>
1897static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1898 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001899 unsigned NumArgs = Attr.getNumArgs();
1900 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001902 return;
1903 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001904
1905 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001906 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001907 if (NumArgs == 1) {
1908 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001909 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001910 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001911 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001912 return;
1913 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001914 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001915 }
Mike Stumpbf916502009-07-24 19:02:52 +00001916
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001917 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001918}
1919
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001920static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1921 const AttributeList &Attr) {
1922 unsigned NumArgs = Attr.getNumArgs();
1923 if (NumArgs > 0) {
1924 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1925 return;
1926 }
1927
1928 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001929 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001930}
1931
Patrick Beardb2f68202012-04-06 18:12:22 +00001932static void handleObjCRootClassAttr(Sema &S, Decl *D,
1933 const AttributeList &Attr) {
1934 if (!isa<ObjCInterfaceDecl>(D)) {
1935 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1936 return;
1937 }
1938
1939 unsigned NumArgs = Attr.getNumArgs();
1940 if (NumArgs > 0) {
1941 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1942 return;
1943 }
1944
1945 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1946}
1947
Ted Kremenek71207fc2012-01-05 22:47:47 +00001948static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001949 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001950 if (!isa<ObjCInterfaceDecl>(D)) {
1951 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1952 return;
1953 }
1954
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001955 unsigned NumArgs = Attr.getNumArgs();
1956 if (NumArgs > 0) {
1957 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1958 return;
1959 }
1960
Ted Kremenek71207fc2012-01-05 22:47:47 +00001961 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001962 Attr.getRange(), S.Context));
1963}
1964
Jordy Rosefad5de92012-05-08 03:27:22 +00001965static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1966 IdentifierInfo *Platform,
1967 VersionTuple Introduced,
1968 VersionTuple Deprecated,
1969 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001970 StringRef PlatformName
1971 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1972 if (PlatformName.empty())
1973 PlatformName = Platform->getName();
1974
1975 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1976 // of these steps are needed).
1977 if (!Introduced.empty() && !Deprecated.empty() &&
1978 !(Introduced <= Deprecated)) {
1979 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1980 << 1 << PlatformName << Deprecated.getAsString()
1981 << 0 << Introduced.getAsString();
1982 return true;
1983 }
1984
1985 if (!Introduced.empty() && !Obsoleted.empty() &&
1986 !(Introduced <= Obsoleted)) {
1987 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1988 << 2 << PlatformName << Obsoleted.getAsString()
1989 << 0 << Introduced.getAsString();
1990 return true;
1991 }
1992
1993 if (!Deprecated.empty() && !Obsoleted.empty() &&
1994 !(Deprecated <= Obsoleted)) {
1995 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1996 << 2 << PlatformName << Obsoleted.getAsString()
1997 << 1 << Deprecated.getAsString();
1998 return true;
1999 }
2000
2001 return false;
2002}
2003
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002004/// \brief Check whether the two versions match.
2005///
2006/// If either version tuple is empty, then they are assumed to match. If
2007/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2008static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2009 bool BeforeIsOkay) {
2010 if (X.empty() || Y.empty())
2011 return true;
2012
2013 if (X == Y)
2014 return true;
2015
2016 if (BeforeIsOkay && X < Y)
2017 return true;
2018
2019 return false;
2020}
2021
Rafael Espindola51be6e32013-01-08 22:04:34 +00002022AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002023 IdentifierInfo *Platform,
2024 VersionTuple Introduced,
2025 VersionTuple Deprecated,
2026 VersionTuple Obsoleted,
2027 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002028 StringRef Message,
2029 bool Override) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002030 VersionTuple MergedIntroduced = Introduced;
2031 VersionTuple MergedDeprecated = Deprecated;
2032 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002033 bool FoundAny = false;
2034
Rafael Espindola98ae8342012-05-10 02:50:16 +00002035 if (D->hasAttrs()) {
2036 AttrVec &Attrs = D->getAttrs();
2037 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2038 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2039 if (!OldAA) {
2040 ++i;
2041 continue;
2042 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002043
Rafael Espindola98ae8342012-05-10 02:50:16 +00002044 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2045 if (OldPlatform != Platform) {
2046 ++i;
2047 continue;
2048 }
2049
2050 FoundAny = true;
2051 VersionTuple OldIntroduced = OldAA->getIntroduced();
2052 VersionTuple OldDeprecated = OldAA->getDeprecated();
2053 VersionTuple OldObsoleted = OldAA->getObsoleted();
2054 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002055
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002056 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2057 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2058 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2059 !(OldIsUnavailable == IsUnavailable ||
2060 (Override && OldIsUnavailable && !IsUnavailable))) {
2061 if (Override) {
2062 int Which = -1;
2063 VersionTuple FirstVersion;
2064 VersionTuple SecondVersion;
2065 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2066 Which = 0;
2067 FirstVersion = OldIntroduced;
2068 SecondVersion = Introduced;
2069 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2070 Which = 1;
2071 FirstVersion = Deprecated;
2072 SecondVersion = OldDeprecated;
2073 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2074 Which = 2;
2075 FirstVersion = Obsoleted;
2076 SecondVersion = OldObsoleted;
2077 }
2078
2079 if (Which == -1) {
2080 Diag(OldAA->getLocation(),
2081 diag::warn_mismatched_availability_override_unavail)
2082 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2083 } else {
2084 Diag(OldAA->getLocation(),
2085 diag::warn_mismatched_availability_override)
2086 << Which
2087 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2088 << FirstVersion.getAsString() << SecondVersion.getAsString();
2089 }
2090 Diag(Range.getBegin(), diag::note_overridden_method);
2091 } else {
2092 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2093 Diag(Range.getBegin(), diag::note_previous_attribute);
2094 }
2095
Rafael Espindola98ae8342012-05-10 02:50:16 +00002096 Attrs.erase(Attrs.begin() + i);
2097 --e;
2098 continue;
2099 }
2100
2101 VersionTuple MergedIntroduced2 = MergedIntroduced;
2102 VersionTuple MergedDeprecated2 = MergedDeprecated;
2103 VersionTuple MergedObsoleted2 = MergedObsoleted;
2104
2105 if (MergedIntroduced2.empty())
2106 MergedIntroduced2 = OldIntroduced;
2107 if (MergedDeprecated2.empty())
2108 MergedDeprecated2 = OldDeprecated;
2109 if (MergedObsoleted2.empty())
2110 MergedObsoleted2 = OldObsoleted;
2111
2112 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2113 MergedIntroduced2, MergedDeprecated2,
2114 MergedObsoleted2)) {
2115 Attrs.erase(Attrs.begin() + i);
2116 --e;
2117 continue;
2118 }
2119
2120 MergedIntroduced = MergedIntroduced2;
2121 MergedDeprecated = MergedDeprecated2;
2122 MergedObsoleted = MergedObsoleted2;
2123 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002124 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002125 }
2126
2127 if (FoundAny &&
2128 MergedIntroduced == Introduced &&
2129 MergedDeprecated == Deprecated &&
2130 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002131 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002132
Rafael Espindola98ae8342012-05-10 02:50:16 +00002133 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002134 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002135 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2136 Introduced, Deprecated,
2137 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002138 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002139 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002140}
2141
Chandler Carruth1b03c872011-07-02 00:01:44 +00002142static void handleAvailabilityAttr(Sema &S, Decl *D,
2143 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002144 IdentifierInfo *Platform = Attr.getParameterName();
2145 SourceLocation PlatformLoc = Attr.getParameterLoc();
2146
Rafael Espindola3b294362012-05-06 19:56:25 +00002147 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002148 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2149 << Platform;
2150
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002151 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2152 if (!ND) {
2153 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2154 return;
2155 }
2156
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002157 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2158 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2159 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002160 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002161 StringRef Str;
2162 const StringLiteral *SE =
2163 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2164 if (SE)
2165 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002166
Rafael Espindola51be6e32013-01-08 22:04:34 +00002167 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002168 Platform,
2169 Introduced.Version,
2170 Deprecated.Version,
2171 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002172 IsUnavailable, Str,
2173 /*Override=*/false);
Rafael Espindola838dc592013-01-12 06:42:30 +00002174 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002175 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002176}
2177
Rafael Espindola599f1b72012-05-13 03:25:18 +00002178VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2179 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002180 if (isa<TypedefNameDecl>(D)) {
2181 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002182 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002183 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002184 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2185 if (ExistingAttr) {
2186 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2187 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002188 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002189 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2190 Diag(Range.getBegin(), diag::note_previous_attribute);
2191 D->dropAttr<VisibilityAttr>();
2192 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002193 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002194}
2195
Chandler Carruth1b03c872011-07-02 00:01:44 +00002196static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002197 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002198 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002199 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002200
Peter Collingbourne7a730022010-11-23 20:45:58 +00002201 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002202 Arg = Arg->IgnoreParenCasts();
2203 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002204
Douglas Gregor5cee1192011-07-27 05:40:30 +00002205 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002207 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002208 return;
2209 }
Mike Stumpbf916502009-07-24 19:02:52 +00002210
Chris Lattner5f9e2722011-07-23 10:55:15 +00002211 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002212 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002213
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002214 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002215 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002216 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002217 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002218 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002219 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002220 else if (TypeStr == "protected") {
2221 // Complain about attempts to use protected visibility on targets
2222 // (like Darwin) that don't support it.
2223 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2224 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2225 type = VisibilityAttr::Default;
2226 } else {
2227 type = VisibilityAttr::Protected;
2228 }
2229 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002230 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002231 return;
2232 }
Mike Stumpbf916502009-07-24 19:02:52 +00002233
Rafael Espindola599f1b72012-05-13 03:25:18 +00002234 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
Rafael Espindola838dc592013-01-12 06:42:30 +00002235 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002236 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002237}
2238
Chandler Carruth1b03c872011-07-02 00:01:44 +00002239static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2240 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002241 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2242 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002243 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002244 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002245 return;
2246 }
2247
Chandler Carruth87c44602011-07-01 23:49:12 +00002248 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2249 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002251 << "objc_method_family" << 1;
2252 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002254 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002255 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002256 return;
2257 }
2258
Chris Lattner5f9e2722011-07-23 10:55:15 +00002259 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002260 ObjCMethodFamilyAttr::FamilyKind family;
2261 if (param == "none")
2262 family = ObjCMethodFamilyAttr::OMF_None;
2263 else if (param == "alloc")
2264 family = ObjCMethodFamilyAttr::OMF_alloc;
2265 else if (param == "copy")
2266 family = ObjCMethodFamilyAttr::OMF_copy;
2267 else if (param == "init")
2268 family = ObjCMethodFamilyAttr::OMF_init;
2269 else if (param == "mutableCopy")
2270 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2271 else if (param == "new")
2272 family = ObjCMethodFamilyAttr::OMF_new;
2273 else {
2274 // Just warn and ignore it. This is future-proof against new
2275 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002276 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002277 return;
2278 }
2279
John McCallf85e1932011-06-15 23:02:42 +00002280 if (family == ObjCMethodFamilyAttr::OMF_init &&
2281 !method->getResultType()->isObjCObjectPointerType()) {
2282 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2283 << method->getResultType();
2284 // Ignore the attribute.
2285 return;
2286 }
2287
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002288 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002289 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002290}
2291
Chandler Carruth1b03c872011-07-02 00:01:44 +00002292static void handleObjCExceptionAttr(Sema &S, Decl *D,
2293 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002294 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002295 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002296
Chris Lattner0db29ec2009-02-14 08:09:34 +00002297 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2298 if (OCI == 0) {
2299 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2300 return;
2301 }
Mike Stumpbf916502009-07-24 19:02:52 +00002302
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002303 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002304}
2305
Chandler Carruth1b03c872011-07-02 00:01:44 +00002306static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002307 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002308 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002309 return;
2310 }
Richard Smith162e1c12011-04-15 14:24:37 +00002311 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002312 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002313 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002314 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2315 return;
2316 }
2317 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002318 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2319 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002320 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002321 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2322 return;
2323 }
2324 }
2325 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002326 // It is okay to include this attribute on properties, e.g.:
2327 //
2328 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2329 //
2330 // In this case it follows tradition and suppresses an error in the above
2331 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002332 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002333 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002334 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002335}
2336
Mike Stumpbf916502009-07-24 19:02:52 +00002337static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002338handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002339 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002341 return;
2342 }
2343
2344 if (!isa<FunctionDecl>(D)) {
2345 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2346 return;
2347 }
2348
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002349 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002350}
2351
Chandler Carruth1b03c872011-07-02 00:01:44 +00002352static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002353 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002354 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002355 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002356 return;
2357 }
Mike Stumpbf916502009-07-24 19:02:52 +00002358
Steve Naroff9eae5762008-09-18 16:44:58 +00002359 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002361 return;
2362 }
Mike Stumpbf916502009-07-24 19:02:52 +00002363
Sean Huntcf807c42010-08-18 23:23:40 +00002364 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002365 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002366 type = BlocksAttr::ByRef;
2367 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002368 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002369 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002370 return;
2371 }
Mike Stumpbf916502009-07-24 19:02:52 +00002372
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002373 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002374}
2375
Chandler Carruth1b03c872011-07-02 00:01:44 +00002376static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002377 // check the attribute arguments.
2378 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002379 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002380 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002381 }
2382
John McCall3323fad2011-09-09 07:56:05 +00002383 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002384 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002385 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002386 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002387 if (E->isTypeDependent() || E->isValueDependent() ||
2388 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002389 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002390 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002391 return;
2392 }
Mike Stumpbf916502009-07-24 19:02:52 +00002393
John McCall3323fad2011-09-09 07:56:05 +00002394 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002395 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2396 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002397 return;
2398 }
John McCall3323fad2011-09-09 07:56:05 +00002399
2400 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002401 }
2402
John McCall3323fad2011-09-09 07:56:05 +00002403 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002404 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002405 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002406 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002407 if (E->isTypeDependent() || E->isValueDependent() ||
2408 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002409 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002410 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002411 return;
2412 }
2413 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002414
John McCall3323fad2011-09-09 07:56:05 +00002415 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002416 // FIXME: This error message could be improved, it would be nice
2417 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002418 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2419 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002420 return;
2421 }
2422 }
2423
Chandler Carruth87c44602011-07-01 23:49:12 +00002424 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002425 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002426 if (isa<FunctionNoProtoType>(FT)) {
2427 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2428 return;
2429 }
Mike Stumpbf916502009-07-24 19:02:52 +00002430
Chris Lattner897cd902009-03-17 23:03:47 +00002431 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002432 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002433 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002434 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002435 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002436 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002437 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002438 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002439 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002440 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2441 if (!BD->isVariadic()) {
2442 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2443 return;
2444 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002445 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002446 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002447 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002448 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002449 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002450 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002451 int m = Ty->isFunctionPointerType() ? 0 : 1;
2452 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002453 return;
2454 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002455 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002457 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002458 return;
2459 }
Anders Carlsson77091822008-10-05 18:05:59 +00002460 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002461 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002462 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002463 return;
2464 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002465 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002466 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002467}
2468
Chandler Carruth1b03c872011-07-02 00:01:44 +00002469static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002470 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002471 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002472 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002473
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002474 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002475 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002476 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002477 return;
2478 }
Mike Stumpbf916502009-07-24 19:02:52 +00002479
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002480 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2481 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2482 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002483 return;
2484 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002485 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2486 if (MD->getResultType()->isVoidType()) {
2487 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2488 << Attr.getName() << 1;
2489 return;
2490 }
2491
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002492 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002493}
2494
Chandler Carruth1b03c872011-07-02 00:01:44 +00002495static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002496 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002497 if (Attr.hasParameterOrArguments()) {
2498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002499 return;
2500 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002501
Chandler Carruth87c44602011-07-01 23:49:12 +00002502 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002503 if (isa<CXXRecordDecl>(D)) {
2504 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2505 return;
2506 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002507 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2508 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002509 return;
2510 }
2511
Chandler Carruth87c44602011-07-01 23:49:12 +00002512 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002513
2514 // 'weak' only applies to declarations with external linkage.
2515 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002516 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002517 return;
2518 }
Mike Stumpbf916502009-07-24 19:02:52 +00002519
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002520 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002521}
2522
Chandler Carruth1b03c872011-07-02 00:01:44 +00002523static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002524 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002525 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002526 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002527
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002528
2529 // weak_import only applies to variable & function declarations.
2530 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002531 if (!D->canBeWeakImported(isDef)) {
2532 if (isDef)
2533 S.Diag(Attr.getLoc(),
2534 diag::warn_attribute_weak_import_invalid_on_definition)
2535 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002536 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002537 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002538 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002539 // Nothing to warn about here.
2540 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002541 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002542 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002543
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002544 return;
2545 }
2546
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002547 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002548}
2549
Tanya Lattner0df579e2012-07-09 22:06:01 +00002550// Handles reqd_work_group_size and work_group_size_hint.
2551static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002552 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002553 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2554 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2555
Nate Begeman6f3d8382009-06-26 06:32:41 +00002556 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002557 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002558
2559 unsigned WGSize[3];
2560 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002561 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002562 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002563 if (E->isTypeDependent() || E->isValueDependent() ||
2564 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002566 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002567 return;
2568 }
2569 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2570 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002571
2572 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2573 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2574 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2575 if (!(A->getXDim() == WGSize[0] &&
2576 A->getYDim() == WGSize[1] &&
2577 A->getZDim() == WGSize[2])) {
2578 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2579 Attr.getName();
2580 }
2581 }
2582
2583 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2584 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2585 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2586 if (!(A->getXDim() == WGSize[0] &&
2587 A->getYDim() == WGSize[1] &&
2588 A->getZDim() == WGSize[2])) {
2589 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2590 Attr.getName();
2591 }
2592 }
2593
2594 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2595 D->addAttr(::new (S.Context)
2596 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2597 WGSize[0], WGSize[1], WGSize[2]));
2598 else
2599 D->addAttr(::new (S.Context)
2600 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2601 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002602}
2603
Rafael Espindola599f1b72012-05-13 03:25:18 +00002604SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2605 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002606 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2607 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002608 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002609 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2610 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002611 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002612 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002613 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002614}
2615
Chandler Carruth1b03c872011-07-02 00:01:44 +00002616static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002617 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002618 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002619 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002620
2621 // Make sure that there is a string literal as the sections's single
2622 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002623 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002624 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002625 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002626 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002627 return;
2628 }
Mike Stump1eb44332009-09-09 15:08:12 +00002629
Chris Lattner797c3c42009-08-10 19:03:04 +00002630 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002631 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002632 if (!Error.empty()) {
2633 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2634 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002635 return;
2636 }
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002638 // This attribute cannot be applied to local variables.
2639 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2640 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2641 return;
2642 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002643 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2644 SE->getString());
2645 if (NewAttr)
2646 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002647}
2648
Chris Lattner6b6b5372008-06-26 18:38:35 +00002649
Chandler Carruth1b03c872011-07-02 00:01:44 +00002650static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002651 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002652 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002654 return;
2655 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002656
Chandler Carruth87c44602011-07-01 23:49:12 +00002657 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002658 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002659 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002660 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002661 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002662 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002663}
2664
Chandler Carruth1b03c872011-07-02 00:01:44 +00002665static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002666 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002667 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002669 return;
2670 }
Mike Stumpbf916502009-07-24 19:02:52 +00002671
Chandler Carruth87c44602011-07-01 23:49:12 +00002672 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002673 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002674 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002675 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002676 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002677 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002678}
2679
Chandler Carruth1b03c872011-07-02 00:01:44 +00002680static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002681 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002682 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002683 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002684
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002685 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002686}
2687
Chandler Carruth1b03c872011-07-02 00:01:44 +00002688static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002689 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2691 return;
2692 }
Mike Stumpbf916502009-07-24 19:02:52 +00002693
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002694 if (Attr.getNumArgs() != 0) {
2695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2696 return;
2697 }
Mike Stumpbf916502009-07-24 19:02:52 +00002698
Chandler Carruth87c44602011-07-01 23:49:12 +00002699 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002700
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002701 if (!VD || !VD->hasLocalStorage()) {
2702 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2703 return;
2704 }
Mike Stumpbf916502009-07-24 19:02:52 +00002705
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002706 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002707 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002708 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002709 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2710 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002711 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002712 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002713 Attr.getParameterName();
2714 return;
2715 }
Mike Stumpbf916502009-07-24 19:02:52 +00002716
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002717 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2718 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002719 S.Diag(Attr.getParameterLoc(),
2720 diag::err_attribute_cleanup_arg_not_function)
2721 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002722 return;
2723 }
2724
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002725 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002726 S.Diag(Attr.getParameterLoc(),
2727 diag::err_attribute_cleanup_func_must_take_one_arg)
2728 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002729 return;
2730 }
Mike Stumpbf916502009-07-24 19:02:52 +00002731
Anders Carlsson89941c12009-02-07 23:16:50 +00002732 // We're currently more strict than GCC about what function types we accept.
2733 // If this ever proves to be a problem it should be easy to fix.
2734 QualType Ty = S.Context.getPointerType(VD->getType());
2735 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002736 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2737 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002738 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002739 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2740 Attr.getParameterName() << ParamTy << Ty;
2741 return;
2742 }
Mike Stumpbf916502009-07-24 19:02:52 +00002743
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002744 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002745 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002746}
2747
Mike Stumpbf916502009-07-24 19:02:52 +00002748/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002749/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002750static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002751 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002752 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002753
Chandler Carruth87c44602011-07-01 23:49:12 +00002754 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002755 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002756 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002757 return;
2758 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002759
2760 // In C++ the implicit 'this' function parameter also counts, and they are
2761 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002762 bool HasImplicitThisParam = isInstanceMethod(D);
2763 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002764 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002765
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002766 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002767 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002768 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002769 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2770 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002771 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2772 << "format" << 2 << IdxExpr->getSourceRange();
2773 return;
2774 }
Mike Stumpbf916502009-07-24 19:02:52 +00002775
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002776 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2778 << "format" << 2 << IdxExpr->getSourceRange();
2779 return;
2780 }
Mike Stumpbf916502009-07-24 19:02:52 +00002781
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002782 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002783
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002784 if (HasImplicitThisParam) {
2785 if (ArgIdx == 0) {
2786 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2787 << "format_arg" << IdxExpr->getSourceRange();
2788 return;
2789 }
2790 ArgIdx--;
2791 }
2792
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002793 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002794 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002795
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002796 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2797 if (not_nsstring_type &&
2798 !isCFStringType(Ty, S.Context) &&
2799 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002800 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002801 // FIXME: Should highlight the actual expression that has the wrong type.
2802 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002803 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002804 << IdxExpr->getSourceRange();
2805 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002806 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002807 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002808 if (!isNSStringType(Ty, S.Context) &&
2809 !isCFStringType(Ty, S.Context) &&
2810 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002811 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002812 // FIXME: Should highlight the actual expression that has the wrong type.
2813 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002814 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002815 << IdxExpr->getSourceRange();
2816 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002817 }
2818
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002819 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002820 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002821}
2822
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002823enum FormatAttrKind {
2824 CFStringFormat,
2825 NSStringFormat,
2826 StrftimeFormat,
2827 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002828 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002829 InvalidFormat
2830};
2831
2832/// getFormatAttrKind - Map from format attribute names to supported format
2833/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002834static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002835 return llvm::StringSwitch<FormatAttrKind>(Format)
2836 // Check for formats that get handled specially.
2837 .Case("NSString", NSStringFormat)
2838 .Case("CFString", CFStringFormat)
2839 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002840
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002841 // Otherwise, check for supported formats.
2842 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2843 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2844 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002845
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002846 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2847 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002848}
2849
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002850/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002851/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002852static void handleInitPriorityAttr(Sema &S, Decl *D,
2853 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002854 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002855 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2856 return;
2857 }
2858
Chandler Carruth87c44602011-07-01 23:49:12 +00002859 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002860 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2861 Attr.setInvalid();
2862 return;
2863 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002864 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002865 if (S.Context.getAsArrayType(T))
2866 T = S.Context.getBaseElementType(T);
2867 if (!T->getAs<RecordType>()) {
2868 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2869 Attr.setInvalid();
2870 return;
2871 }
2872
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002873 if (Attr.getNumArgs() != 1) {
2874 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2875 Attr.setInvalid();
2876 return;
2877 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002878 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002879
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002880 llvm::APSInt priority(32);
2881 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2882 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2883 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2884 << "init_priority" << priorityExpr->getSourceRange();
2885 Attr.setInvalid();
2886 return;
2887 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002888 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002889 if (prioritynum < 101 || prioritynum > 65535) {
2890 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2891 << priorityExpr->getSourceRange();
2892 Attr.setInvalid();
2893 return;
2894 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002895 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002896 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002897}
2898
Rafael Espindola599f1b72012-05-13 03:25:18 +00002899FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2900 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002901 // Check whether we already have an equivalent format attribute.
2902 for (specific_attr_iterator<FormatAttr>
2903 i = D->specific_attr_begin<FormatAttr>(),
2904 e = D->specific_attr_end<FormatAttr>();
2905 i != e ; ++i) {
2906 FormatAttr *f = *i;
2907 if (f->getType() == Format &&
2908 f->getFormatIdx() == FormatIdx &&
2909 f->getFirstArg() == FirstArg) {
2910 // If we don't have a valid location for this attribute, adopt the
2911 // location.
2912 if (f->getLocation().isInvalid())
2913 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002914 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002915 }
2916 }
2917
Rafael Espindola599f1b72012-05-13 03:25:18 +00002918 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2919 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002920}
2921
Mike Stumpbf916502009-07-24 19:02:52 +00002922/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002923/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002924static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002925
Chris Lattner545dd342008-06-28 23:36:30 +00002926 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002927 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002928 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002929 return;
2930 }
2931
Chris Lattner545dd342008-06-28 23:36:30 +00002932 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002934 return;
2935 }
2936
Chandler Carruth87c44602011-07-01 23:49:12 +00002937 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002939 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002940 return;
2941 }
2942
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002943 // In C++ the implicit 'this' function parameter also counts, and they are
2944 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002945 bool HasImplicitThisParam = isInstanceMethod(D);
2946 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002947 unsigned FirstIdx = 1;
2948
Chris Lattner5f9e2722011-07-23 10:55:15 +00002949 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002950
2951 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002952 if (Format.startswith("__") && Format.endswith("__"))
2953 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002954
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002955 // Check for supported formats.
2956 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002957
2958 if (Kind == IgnoredFormat)
2959 return;
2960
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002961 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002962 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002963 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002964 return;
2965 }
2966
2967 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002968 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002969 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002970 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2971 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002972 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002973 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002974 return;
2975 }
2976
2977 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002978 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002979 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002980 return;
2981 }
2982
2983 // FIXME: Do we need to bounds check?
2984 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002985
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002986 if (HasImplicitThisParam) {
2987 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002988 S.Diag(Attr.getLoc(),
2989 diag::err_format_attribute_implicit_this_format_string)
2990 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002991 return;
2992 }
2993 ArgIdx--;
2994 }
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Chris Lattner6b6b5372008-06-26 18:38:35 +00002996 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002997 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002998
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002999 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003000 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003001 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3002 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003003 return;
3004 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003005 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003006 // FIXME: do we need to check if the type is NSString*? What are the
3007 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003008 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003009 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003010 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3011 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003012 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003013 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003014 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003015 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003016 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003017 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3018 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003019 return;
3020 }
3021
3022 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003023 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003024 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003025 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3026 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003027 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003028 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003029 return;
3030 }
3031
3032 // check if the function is variadic if the 3rd argument non-zero
3033 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003034 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003035 ++NumArgs; // +1 for ...
3036 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003037 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003038 return;
3039 }
3040 }
3041
Chris Lattner3c73c412008-11-19 08:23:25 +00003042 // strftime requires FirstArg to be 0 because it doesn't read from any
3043 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003044 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003045 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003046 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3047 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003048 return;
3049 }
3050 // if 0 it disables parameter checking (to use with e.g. va_list)
3051 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003053 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003054 return;
3055 }
3056
Rafael Espindola599f1b72012-05-13 03:25:18 +00003057 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3058 Idx.getZExtValue(),
3059 FirstArg.getZExtValue());
3060 if (NewAttr)
3061 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003062}
3063
Chandler Carruth1b03c872011-07-02 00:01:44 +00003064static void handleTransparentUnionAttr(Sema &S, Decl *D,
3065 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003066 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003067 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003068 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003069
Chris Lattner6b6b5372008-06-26 18:38:35 +00003070
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003071 // Try to find the underlying union declaration.
3072 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003073 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003074 if (TD && TD->getUnderlyingType()->isUnionType())
3075 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3076 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003077 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003078
3079 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003080 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003081 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003082 return;
3083 }
3084
John McCall5e1cdac2011-10-07 06:10:15 +00003085 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003086 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003087 diag::warn_transparent_union_attribute_not_definition);
3088 return;
3089 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003090
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003091 RecordDecl::field_iterator Field = RD->field_begin(),
3092 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003093 if (Field == FieldEnd) {
3094 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3095 return;
3096 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003097
David Blaikie581deb32012-06-06 20:45:41 +00003098 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003099 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003100 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003101 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003102 diag::warn_transparent_union_attribute_floating)
3103 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003104 return;
3105 }
3106
3107 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3108 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3109 for (; Field != FieldEnd; ++Field) {
3110 QualType FieldType = Field->getType();
3111 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3112 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3113 // Warn if we drop the attribute.
3114 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003115 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003116 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003117 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003118 diag::warn_transparent_union_attribute_field_size_align)
3119 << isSize << Field->getDeclName() << FieldBits;
3120 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003121 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003122 diag::note_transparent_union_first_field_size_align)
3123 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003124 return;
3125 }
3126 }
3127
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003128 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003129}
3130
Chandler Carruth1b03c872011-07-02 00:01:44 +00003131static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003132 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003133 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003135
Peter Collingbourne7a730022010-11-23 20:45:58 +00003136 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003137 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003138
Chris Lattner6b6b5372008-06-26 18:38:35 +00003139 // Make sure that there is a string literal as the annotation's single
3140 // argument.
3141 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003142 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003143 return;
3144 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003145
3146 // Don't duplicate annotations that are already set.
3147 for (specific_attr_iterator<AnnotateAttr>
3148 i = D->specific_attr_begin<AnnotateAttr>(),
3149 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3150 if ((*i)->getAnnotation() == SE->getString())
3151 return;
3152 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003153 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003154 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003155}
3156
Chandler Carruth1b03c872011-07-02 00:01:44 +00003157static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003158 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003159 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003160 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003161 return;
3162 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003163
Sean Huntbbd37c62009-11-21 08:43:09 +00003164 //FIXME: The C++0x version of this attribute has more limited applicabilty
3165 // than GNU's, and should error out when it is used to specify a
3166 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003167
Chris Lattner545dd342008-06-28 23:36:30 +00003168 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003169 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3170 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003171 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003172 }
Mike Stumpbf916502009-07-24 19:02:52 +00003173
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003174 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3175 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003176}
3177
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003178void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3179 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003180 // FIXME: Handle pack-expansions here.
3181 if (DiagnoseUnexpandedParameterPack(E))
3182 return;
3183
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003184 if (E->isTypeDependent() || E->isValueDependent()) {
3185 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003186 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3187 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003188 return;
3189 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003190
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003191 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003192 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003193 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003194 ExprResult ICE
3195 = VerifyIntegerConstantExpression(E, &Alignment,
3196 diag::err_aligned_attribute_argument_not_int,
3197 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003198 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003199 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003200 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003201 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3202 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003203 return;
3204 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003205 if (isDeclSpec) {
3206 // We've already verified it's a power of 2, now let's make sure it's
3207 // 8192 or less.
3208 if (Alignment.getZExtValue() > 8192) {
3209 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3210 << E->getSourceRange();
3211 return;
3212 }
3213 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003214
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003215 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3216 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003217}
3218
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003219void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3220 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003221 // FIXME: Cache the number on the Attr object if non-dependent?
3222 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003223 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3224 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003225 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003226}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003227
Chandler Carruthd309c812011-07-01 23:49:16 +00003228/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003229/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003230///
Mike Stumpbf916502009-07-24 19:02:52 +00003231/// Despite what would be logical, the mode attribute is a decl attribute, not a
3232/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3233/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003234static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003235 // This attribute isn't documented, but glibc uses it. It changes
3236 // the width of an int or unsigned int to the specified size.
3237
3238 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003239 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003240 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003241
Chris Lattnerfbf13472008-06-27 22:18:37 +00003242
3243 IdentifierInfo *Name = Attr.getParameterName();
3244 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003245 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003246 return;
3247 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003248
Chris Lattner5f9e2722011-07-23 10:55:15 +00003249 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003250
3251 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003252 if (Str.startswith("__") && Str.endswith("__"))
3253 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003254
3255 unsigned DestWidth = 0;
3256 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003257 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003258 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003259 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003260 switch (Str[0]) {
3261 case 'Q': DestWidth = 8; break;
3262 case 'H': DestWidth = 16; break;
3263 case 'S': DestWidth = 32; break;
3264 case 'D': DestWidth = 64; break;
3265 case 'X': DestWidth = 96; break;
3266 case 'T': DestWidth = 128; break;
3267 }
3268 if (Str[1] == 'F') {
3269 IntegerMode = false;
3270 } else if (Str[1] == 'C') {
3271 IntegerMode = false;
3272 ComplexMode = true;
3273 } else if (Str[1] != 'I') {
3274 DestWidth = 0;
3275 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003276 break;
3277 case 4:
3278 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3279 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003280 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003281 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003282 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003283 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003284 break;
3285 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003286 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003287 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003288 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003289 case 11:
3290 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003291 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003292 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003293 }
3294
3295 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003296 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003297 OldTy = TD->getUnderlyingType();
3298 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3299 OldTy = VD->getType();
3300 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003301 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003302 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003303 return;
3304 }
Eli Friedman73397492009-03-03 06:41:03 +00003305
John McCall183700f2009-09-21 23:43:11 +00003306 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003307 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3308 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003309 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003310 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3311 } else if (ComplexMode) {
3312 if (!OldTy->isComplexType())
3313 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3314 } else {
3315 if (!OldTy->isFloatingType())
3316 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3317 }
3318
Mike Stump390b4cc2009-05-16 07:39:55 +00003319 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3320 // and friends, at least with glibc.
3321 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3322 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003323 // FIXME: Make sure floating-point mappings are accurate
3324 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003325 QualType NewTy;
3326 switch (DestWidth) {
3327 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003328 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003329 return;
3330 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003331 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003332 return;
3333 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003334 if (!IntegerMode) {
3335 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3336 return;
3337 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003338 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003339 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003340 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003341 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003342 break;
3343 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003344 if (!IntegerMode) {
3345 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3346 return;
3347 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003348 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003349 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003350 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003351 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003352 break;
3353 case 32:
3354 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003355 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003356 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003357 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003358 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003359 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003360 break;
3361 case 64:
3362 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003363 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003364 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003365 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003366 NewTy = S.Context.LongTy;
3367 else
3368 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003369 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003370 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003371 NewTy = S.Context.UnsignedLongTy;
3372 else
3373 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003374 break;
Eli Friedman73397492009-03-03 06:41:03 +00003375 case 96:
3376 NewTy = S.Context.LongDoubleTy;
3377 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003378 case 128:
3379 if (!IntegerMode) {
3380 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3381 return;
3382 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003383 if (OldTy->isSignedIntegerType())
3384 NewTy = S.Context.Int128Ty;
3385 else
3386 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003387 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003388 }
3389
Eli Friedman73397492009-03-03 06:41:03 +00003390 if (ComplexMode) {
3391 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003392 }
3393
3394 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003395 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003396 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003397 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003398 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003399 cast<ValueDecl>(D)->setType(NewTy);
3400}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003401
Chandler Carruth1b03c872011-07-02 00:01:44 +00003402static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003403 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003404 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003405 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003406
Nick Lewycky78d1a102012-07-24 01:40:49 +00003407 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3408 if (!VD->hasGlobalStorage())
3409 S.Diag(Attr.getLoc(),
3410 diag::warn_attribute_requires_functions_or_static_globals)
3411 << Attr.getName();
3412 } else if (!isFunctionOrMethod(D)) {
3413 S.Diag(Attr.getLoc(),
3414 diag::warn_attribute_requires_functions_or_static_globals)
3415 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003416 return;
3417 }
Mike Stumpbf916502009-07-24 19:02:52 +00003418
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003419 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003420}
3421
Chandler Carruth1b03c872011-07-02 00:01:44 +00003422static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003423 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003424 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003425 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003426
Mike Stumpbf916502009-07-24 19:02:52 +00003427
Chandler Carruth87c44602011-07-01 23:49:12 +00003428 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003430 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003431 return;
3432 }
Mike Stumpbf916502009-07-24 19:02:52 +00003433
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003434 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003435}
3436
Chandler Carruth1b03c872011-07-02 00:01:44 +00003437static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3438 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003439 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003440 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003441 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003442
Chris Lattner7255a2d2010-06-22 00:03:40 +00003443
Chandler Carruth87c44602011-07-01 23:49:12 +00003444 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003445 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003446 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003447 return;
3448 }
3449
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003450 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003451 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003452}
3453
Chandler Carruth1b03c872011-07-02 00:01:44 +00003454static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003455 if (S.LangOpts.CUDA) {
3456 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003457 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3459 return;
3460 }
3461
Chandler Carruth87c44602011-07-01 23:49:12 +00003462 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003464 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003465 return;
3466 }
3467
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003468 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003469 } else {
3470 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3471 }
3472}
3473
Chandler Carruth1b03c872011-07-02 00:01:44 +00003474static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003475 if (S.LangOpts.CUDA) {
3476 // check the attribute arguments.
3477 if (Attr.getNumArgs() != 0) {
3478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3479 return;
3480 }
3481
Chandler Carruth87c44602011-07-01 23:49:12 +00003482 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003484 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003485 return;
3486 }
3487
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003488 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003489 } else {
3490 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3491 }
3492}
3493
Chandler Carruth1b03c872011-07-02 00:01:44 +00003494static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003495 if (S.LangOpts.CUDA) {
3496 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003497 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003498 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003499
Chandler Carruth87c44602011-07-01 23:49:12 +00003500 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003502 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003503 return;
3504 }
3505
Chandler Carruth87c44602011-07-01 23:49:12 +00003506 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003507 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003508 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003509 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3510 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3511 << FD->getType()
3512 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3513 "void");
3514 } else {
3515 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3516 << FD->getType();
3517 }
3518 return;
3519 }
3520
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003521 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003522 } else {
3523 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3524 }
3525}
3526
Chandler Carruth1b03c872011-07-02 00:01:44 +00003527static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003528 if (S.LangOpts.CUDA) {
3529 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003530 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003531 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003532
Peter Collingbourneced76712010-12-01 03:15:31 +00003533
Chandler Carruth87c44602011-07-01 23:49:12 +00003534 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003535 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003536 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003537 return;
3538 }
3539
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003540 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003541 } else {
3542 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3543 }
3544}
3545
Chandler Carruth1b03c872011-07-02 00:01:44 +00003546static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003547 if (S.LangOpts.CUDA) {
3548 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003549 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003550 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003551
Peter Collingbourneced76712010-12-01 03:15:31 +00003552
Chandler Carruth87c44602011-07-01 23:49:12 +00003553 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003555 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003556 return;
3557 }
3558
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003559 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003560 } else {
3561 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3562 }
3563}
3564
Chandler Carruth1b03c872011-07-02 00:01:44 +00003565static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003566 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003567 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003568 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003569
Chandler Carruth87c44602011-07-01 23:49:12 +00003570 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003571 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003573 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003574 return;
3575 }
Mike Stumpbf916502009-07-24 19:02:52 +00003576
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003577 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003578 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003579 return;
3580 }
Mike Stumpbf916502009-07-24 19:02:52 +00003581
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003582 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003583}
3584
Chandler Carruth1b03c872011-07-02 00:01:44 +00003585static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003586 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003587
Aaron Ballmanfff32482012-12-09 17:45:41 +00003588 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003589 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003590 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3591 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003592 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003593 return;
3594
Chandler Carruth87c44602011-07-01 23:49:12 +00003595 if (!isa<ObjCMethodDecl>(D)) {
3596 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3597 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003598 return;
3599 }
3600
Chandler Carruth87c44602011-07-01 23:49:12 +00003601 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003602 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003603 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003604 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003605 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003606 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003607 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003608 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003609 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003610 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003611 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003612 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003613 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003614 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003615 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003616 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003617 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003618 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003619 switch (CC) {
3620 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003621 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003622 break;
3623 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003624 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003625 break;
3626 default:
3627 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003628 }
3629
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003630 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuff263366f2012-10-16 22:30:41 +00003631 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003632 }
Derek Schuff263366f2012-10-16 22:30:41 +00003633 case AttributeList::AT_PnaclCall:
3634 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3635 return;
Guy Benyei38980082012-12-25 08:53:55 +00003636 case AttributeList::AT_IntelOclBicc:
3637 D->addAttr(::new (S.Context) IntelOclBiccAttr(Attr.getRange(), S.Context));
3638 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003639
Abramo Bagnarae215f722010-04-30 13:10:51 +00003640 default:
3641 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003642 }
3643}
3644
Chandler Carruth1b03c872011-07-02 00:01:44 +00003645static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003646 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003647 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003648}
3649
Aaron Ballmanfff32482012-12-09 17:45:41 +00003650bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3651 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003652 if (attr.isInvalid())
3653 return true;
3654
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003655 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3656 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3657 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003658 attr.setInvalid();
3659 return true;
3660 }
3661
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003662 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3663 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003664 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003665 case AttributeList::AT_CDecl: CC = CC_C; break;
3666 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3667 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3668 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3669 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3670 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003671 Expr *Arg = attr.getArg(0);
3672 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003673 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003674 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3675 << "pcs" << 1;
3676 attr.setInvalid();
3677 return true;
3678 }
3679
Chris Lattner5f9e2722011-07-23 10:55:15 +00003680 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003681 if (StrRef == "aapcs") {
3682 CC = CC_AAPCS;
3683 break;
3684 } else if (StrRef == "aapcs-vfp") {
3685 CC = CC_AAPCS_VFP;
3686 break;
3687 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003688
3689 attr.setInvalid();
3690 Diag(attr.getLoc(), diag::err_invalid_pcs);
3691 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003692 }
Derek Schuff263366f2012-10-16 22:30:41 +00003693 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003694 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003695 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003696 }
3697
Aaron Ballman82bfa192012-10-02 14:26:08 +00003698 const TargetInfo &TI = Context.getTargetInfo();
3699 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3700 if (A == TargetInfo::CCCR_Warning) {
3701 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003702
3703 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3704 if (FD)
3705 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3706 TargetInfo::CCMT_NonMember;
3707 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003708 }
3709
John McCall711c52b2011-01-05 12:14:39 +00003710 return false;
3711}
3712
Chandler Carruth1b03c872011-07-02 00:01:44 +00003713static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003714 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003715
3716 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003717 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003718 return;
3719
Chandler Carruth87c44602011-07-01 23:49:12 +00003720 if (!isa<ObjCMethodDecl>(D)) {
3721 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3722 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003723 return;
3724 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003725
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003726 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003727}
3728
3729/// Checks a regparm attribute, returning true if it is ill-formed and
3730/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003731bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3732 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003733 return true;
3734
Chandler Carruth87c44602011-07-01 23:49:12 +00003735 if (Attr.getNumArgs() != 1) {
3736 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3737 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003738 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003739 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003740
Chandler Carruth87c44602011-07-01 23:49:12 +00003741 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003742 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003743 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003744 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003745 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003746 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003747 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003748 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003749 }
3750
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003751 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003752 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003753 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003754 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003755 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003756 }
3757
John McCall711c52b2011-01-05 12:14:39 +00003758 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003759 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003760 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003761 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003762 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003763 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003764 }
3765
John McCall711c52b2011-01-05 12:14:39 +00003766 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003767}
3768
Chandler Carruth1b03c872011-07-02 00:01:44 +00003769static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003770 if (S.LangOpts.CUDA) {
3771 // check the attribute arguments.
3772 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003773 // FIXME: 0 is not okay.
3774 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003775 return;
3776 }
3777
Chandler Carruth87c44602011-07-01 23:49:12 +00003778 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003780 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003781 return;
3782 }
3783
3784 Expr *MaxThreadsExpr = Attr.getArg(0);
3785 llvm::APSInt MaxThreads(32);
3786 if (MaxThreadsExpr->isTypeDependent() ||
3787 MaxThreadsExpr->isValueDependent() ||
3788 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3789 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3790 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3791 return;
3792 }
3793
3794 llvm::APSInt MinBlocks(32);
3795 if (Attr.getNumArgs() > 1) {
3796 Expr *MinBlocksExpr = Attr.getArg(1);
3797 if (MinBlocksExpr->isTypeDependent() ||
3798 MinBlocksExpr->isValueDependent() ||
3799 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3800 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3801 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3802 return;
3803 }
3804 }
3805
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003806 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003807 MaxThreads.getZExtValue(),
3808 MinBlocks.getZExtValue()));
3809 } else {
3810 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3811 }
3812}
3813
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003814static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3815 const AttributeList &Attr) {
3816 StringRef AttrName = Attr.getName()->getName();
3817 if (!Attr.getParameterName()) {
3818 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3819 << Attr.getName() << /* arg num = */ 1;
3820 return;
3821 }
3822
3823 if (Attr.getNumArgs() != 2) {
3824 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3825 << /* required args = */ 3;
3826 return;
3827 }
3828
3829 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3830
3831 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3832 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3833 << Attr.getName() << ExpectedFunctionOrMethod;
3834 return;
3835 }
3836
3837 uint64_t ArgumentIdx;
3838 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3839 Attr.getLoc(), 2,
3840 Attr.getArg(0), ArgumentIdx))
3841 return;
3842
3843 uint64_t TypeTagIdx;
3844 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3845 Attr.getLoc(), 3,
3846 Attr.getArg(1), TypeTagIdx))
3847 return;
3848
3849 bool IsPointer = (AttrName == "pointer_with_type_tag");
3850 if (IsPointer) {
3851 // Ensure that buffer has a pointer type.
3852 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3853 if (!BufferTy->isPointerType()) {
3854 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3855 << AttrName;
3856 }
3857 }
3858
3859 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3860 S.Context,
3861 ArgumentKind,
3862 ArgumentIdx,
3863 TypeTagIdx,
3864 IsPointer));
3865}
3866
3867static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3868 const AttributeList &Attr) {
3869 IdentifierInfo *PointerKind = Attr.getParameterName();
3870 if (!PointerKind) {
3871 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3872 << "type_tag_for_datatype" << 1;
3873 return;
3874 }
3875
3876 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3877
3878 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3879 Attr.getRange(),
3880 S.Context,
3881 PointerKind,
3882 MatchingCType,
3883 Attr.getLayoutCompatible(),
3884 Attr.getMustBeNull()));
3885}
3886
Chris Lattner0744e5f2008-06-29 00:23:49 +00003887//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003888// Checker-specific attribute handlers.
3889//===----------------------------------------------------------------------===//
3890
John McCallc7ad3812011-01-25 03:31:58 +00003891static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003892 return type->isDependentType() ||
3893 type->isObjCObjectPointerType() ||
3894 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003895}
3896static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003897 return type->isDependentType() ||
3898 type->isPointerType() ||
3899 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003900}
3901
Chandler Carruth1b03c872011-07-02 00:01:44 +00003902static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003903 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003904 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003905 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003906 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003907 return;
3908 }
3909
3910 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003911 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003912 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3913 cf = false;
3914 } else {
3915 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3916 cf = true;
3917 }
3918
3919 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003920 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003921 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003922 return;
3923 }
3924
3925 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003926 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003927 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003928 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003929}
3930
Chandler Carruth1b03c872011-07-02 00:01:44 +00003931static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3932 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003933 if (!isa<ObjCMethodDecl>(D)) {
3934 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003935 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003936 return;
3937 }
3938
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003939 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003940}
3941
Chandler Carruth1b03c872011-07-02 00:01:44 +00003942static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3943 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003944
John McCallc7ad3812011-01-25 03:31:58 +00003945 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003946
Chandler Carruth87c44602011-07-01 23:49:12 +00003947 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003948 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003949 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003950 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003951 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003952 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3953 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003954 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003955 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003956 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003957 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003958 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003959 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003960 return;
3961 }
Mike Stumpbf916502009-07-24 19:02:52 +00003962
John McCallc7ad3812011-01-25 03:31:58 +00003963 bool typeOK;
3964 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003965 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003966 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003967 case AttributeList::AT_NSReturnsAutoreleased:
3968 case AttributeList::AT_NSReturnsRetained:
3969 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003970 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3971 cf = false;
3972 break;
3973
Sean Hunt8e083e72012-06-19 23:57:03 +00003974 case AttributeList::AT_CFReturnsRetained:
3975 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003976 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3977 cf = true;
3978 break;
3979 }
3980
3981 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003982 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003983 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003984 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003985 }
Mike Stumpbf916502009-07-24 19:02:52 +00003986
Chandler Carruth87c44602011-07-01 23:49:12 +00003987 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003988 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003989 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003990 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003991 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003992 S.Context));
3993 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003994 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003995 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003996 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003997 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003998 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003999 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00004000 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004001 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004002 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004003 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00004004 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004005 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004006 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004007 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00004008 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004009 return;
4010 };
4011}
4012
John McCalldc7c5ad2011-07-22 08:53:00 +00004013static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4014 const AttributeList &attr) {
4015 SourceLocation loc = attr.getLoc();
4016
4017 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4018
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004019 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004020 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004021 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004022 return;
4023 }
4024
4025 // Check that the method returns a normal pointer.
4026 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004027
4028 if (!resultType->isReferenceType() &&
4029 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004030 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4031 << SourceRange(loc)
4032 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4033
4034 // Drop the attribute.
4035 return;
4036 }
4037
4038 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004039 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00004040}
4041
Fariborz Jahanian84101132012-09-07 23:46:23 +00004042static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4043 const AttributeList &attr) {
4044 SourceLocation loc = attr.getLoc();
4045 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4046
4047 if (!method) {
4048 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4049 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4050 return;
4051 }
4052 DeclContext *DC = method->getDeclContext();
4053 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4054 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4055 << attr.getName() << 0;
4056 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4057 return;
4058 }
4059 if (method->getMethodFamily() == OMF_dealloc) {
4060 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4061 << attr.getName() << 1;
4062 return;
4063 }
4064
4065 method->addAttr(
4066 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4067}
4068
John McCall8dfac0b2011-09-30 05:12:12 +00004069/// Handle cf_audited_transfer and cf_unknown_transfer.
4070static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4071 if (!isa<FunctionDecl>(D)) {
4072 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004073 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004074 return;
4075 }
4076
Sean Hunt8e083e72012-06-19 23:57:03 +00004077 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004078
4079 // Check whether there's a conflicting attribute already present.
4080 Attr *Existing;
4081 if (IsAudited) {
4082 Existing = D->getAttr<CFUnknownTransferAttr>();
4083 } else {
4084 Existing = D->getAttr<CFAuditedTransferAttr>();
4085 }
4086 if (Existing) {
4087 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4088 << A.getName()
4089 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4090 << A.getRange() << Existing->getRange();
4091 return;
4092 }
4093
4094 // All clear; add the attribute.
4095 if (IsAudited) {
4096 D->addAttr(
4097 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4098 } else {
4099 D->addAttr(
4100 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4101 }
4102}
4103
John McCallfe98da02011-09-29 07:17:38 +00004104static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4105 const AttributeList &Attr) {
4106 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4107 if (!RD || RD->isUnion()) {
4108 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004109 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004110 }
4111
4112 IdentifierInfo *ParmName = Attr.getParameterName();
4113
4114 // In Objective-C, verify that the type names an Objective-C type.
4115 // We don't want to check this outside of ObjC because people sometimes
4116 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004117 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004118 // Check for an existing type with this name.
4119 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4120 Sema::LookupOrdinaryName);
4121 if (S.LookupName(R, Sc)) {
4122 NamedDecl *Target = R.getFoundDecl();
4123 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4124 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4125 S.Diag(Target->getLocStart(), diag::note_declared_at);
4126 }
4127 }
4128 }
4129
4130 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4131 ParmName));
4132}
4133
Chandler Carruth1b03c872011-07-02 00:01:44 +00004134static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4135 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004136 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004137
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004139 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004140}
4141
Chandler Carruth1b03c872011-07-02 00:01:44 +00004142static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4143 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004144 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004145 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004146 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004147 return;
4148 }
4149
Chandler Carruth87c44602011-07-01 23:49:12 +00004150 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004151 QualType type = vd->getType();
4152
4153 if (!type->isDependentType() &&
4154 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004155 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004156 << type;
4157 return;
4158 }
4159
4160 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4161
4162 // If we have no lifetime yet, check the lifetime we're presumably
4163 // going to infer.
4164 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4165 lifetime = type->getObjCARCImplicitLifetime();
4166
4167 switch (lifetime) {
4168 case Qualifiers::OCL_None:
4169 assert(type->isDependentType() &&
4170 "didn't infer lifetime for non-dependent type?");
4171 break;
4172
4173 case Qualifiers::OCL_Weak: // meaningful
4174 case Qualifiers::OCL_Strong: // meaningful
4175 break;
4176
4177 case Qualifiers::OCL_ExplicitNone:
4178 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004179 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004180 << (lifetime == Qualifiers::OCL_Autoreleasing);
4181 break;
4182 }
4183
Chandler Carruth87c44602011-07-01 23:49:12 +00004184 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004185 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004186}
4187
Francois Pichet11542142010-12-19 06:50:37 +00004188//===----------------------------------------------------------------------===//
4189// Microsoft specific attribute handlers.
4190//===----------------------------------------------------------------------===//
4191
Chandler Carruth1b03c872011-07-02 00:01:44 +00004192static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004193 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004194 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004195 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004196 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004197
Francois Pichet11542142010-12-19 06:50:37 +00004198 Expr *Arg = Attr.getArg(0);
4199 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004200 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4202 << "uuid" << 1;
4203 return;
4204 }
4205
Chris Lattner5f9e2722011-07-23 10:55:15 +00004206 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004207
4208 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4209 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004210
Francois Pichetd3d3be92010-12-20 01:41:49 +00004211 // Validate GUID length.
4212 if (IsCurly && StrRef.size() != 38) {
4213 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4214 return;
4215 }
4216 if (!IsCurly && StrRef.size() != 36) {
4217 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4218 return;
4219 }
4220
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004221 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004222 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004223 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004224 if (IsCurly) // Skip the optional '{'
4225 ++I;
4226
4227 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004228 if (i == 8 || i == 13 || i == 18 || i == 23) {
4229 if (*I != '-') {
4230 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4231 return;
4232 }
4233 } else if (!isxdigit(*I)) {
4234 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4235 return;
4236 }
4237 I++;
4238 }
Francois Pichet11542142010-12-19 06:50:37 +00004239
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004240 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004241 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004242 } else
Francois Pichet11542142010-12-19 06:50:37 +00004243 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004244}
4245
John McCallc052dbb2012-05-22 21:28:12 +00004246static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004247 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004248 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004249 return;
4250 }
4251
4252 AttributeList::Kind Kind = Attr.getKind();
4253 if (Kind == AttributeList::AT_SingleInheritance)
4254 D->addAttr(
4255 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4256 else if (Kind == AttributeList::AT_MultipleInheritance)
4257 D->addAttr(
4258 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4259 else if (Kind == AttributeList::AT_VirtualInheritance)
4260 D->addAttr(
4261 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
John McCallc052dbb2012-05-22 21:28:12 +00004262}
4263
4264static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4265 if (S.LangOpts.MicrosoftExt) {
4266 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004267 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004268 D->addAttr(
4269 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004270 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004271 D->addAttr(
4272 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004273 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004274 D->addAttr(
4275 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4276 } else
4277 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4278}
4279
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004280static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4281 if (S.LangOpts.MicrosoftExt)
4282 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4283 else
4284 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4285}
4286
Ted Kremenekb71368d2009-05-09 02:44:38 +00004287//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004288// Top Level Sema Entry Points
4289//===----------------------------------------------------------------------===//
4290
Chandler Carruth1b03c872011-07-02 00:01:44 +00004291static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4292 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004293 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004294 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4295 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4296 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004297 default:
4298 break;
4299 }
4300}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004301
Chandler Carruth1b03c872011-07-02 00:01:44 +00004302static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4303 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004304 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004305 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4306 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4307 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004308 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004309 case AttributeList::AT_AddressSpace:
4310 case AttributeList::AT_OpenCLImageAccess:
4311 case AttributeList::AT_ObjCGC:
4312 case AttributeList::AT_VectorSize:
4313 case AttributeList::AT_NeonVectorType:
4314 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004315 // Ignore these, these are type attributes, handled by
4316 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004317 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004318 case AttributeList::AT_CUDADevice:
4319 case AttributeList::AT_CUDAHost:
4320 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004321 // Ignore, this is a non-inheritable attribute, handled
4322 // by ProcessNonInheritableDeclAttr.
4323 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004324 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4325 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4326 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4327 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004328 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004329 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004330 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004331 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004332 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4333 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4334 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004335 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004336 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4337 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4338 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4339 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004340 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4341 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004342 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4343 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004344 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004345 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004346 case AttributeList::AT_MinSize:
4347 handleMinSizeAttr(S, D, Attr);
4348 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004349 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4350 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4351 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4352 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4353 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004354 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004355 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004356 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4357 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4358 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4359 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4360 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004361 case AttributeList::AT_ownership_returns:
4362 case AttributeList::AT_ownership_takes:
4363 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004364 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004365 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4366 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4367 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4368 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4369 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4370 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4371 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004372
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004374 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004375 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004376 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004377
Sean Hunt8e083e72012-06-19 23:57:03 +00004378 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004379 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4380
Fariborz Jahanian84101132012-09-07 23:46:23 +00004381 case AttributeList::AT_ObjCRequiresSuper:
4382 handleObjCRequiresSuperAttr(S, D, Attr); break;
4383
Sean Hunt8e083e72012-06-19 23:57:03 +00004384 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004385 handleNSBridgedAttr(S, scope, D, Attr); break;
4386
Sean Hunt8e083e72012-06-19 23:57:03 +00004387 case AttributeList::AT_CFAuditedTransfer:
4388 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004389 handleCFTransferAttr(S, D, Attr); break;
4390
Ted Kremenekb71368d2009-05-09 02:44:38 +00004391 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004392 case AttributeList::AT_CFConsumed:
4393 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4394 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004395 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004396
Sean Hunt8e083e72012-06-19 23:57:03 +00004397 case AttributeList::AT_NSReturnsAutoreleased:
4398 case AttributeList::AT_NSReturnsNotRetained:
4399 case AttributeList::AT_CFReturnsNotRetained:
4400 case AttributeList::AT_NSReturnsRetained:
4401 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004402 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004403
Tanya Lattner0df579e2012-07-09 22:06:01 +00004404 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004405 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004406 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004407
Sean Hunt8e083e72012-06-19 23:57:03 +00004408 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004409 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004410
Sean Hunt8e083e72012-06-19 23:57:03 +00004411 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4412 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4413 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004414 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4415 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004416 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004417 handleArcWeakrefUnavailableAttr (S, D, Attr);
4418 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004419 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004420 handleObjCRootClassAttr(S, D, Attr);
4421 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004422 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004423 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004424 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004425 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4426 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004427 handleReturnsTwiceAttr(S, D, Attr);
4428 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004429 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4430 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4431 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004432 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004433 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4434 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4435 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4436 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004437 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004438 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004439 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004440 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004441 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004442 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004443 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004444 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004445 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4446 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4447 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4448 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4449 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4450 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4451 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4452 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4453 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004454 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004455 // Just ignore
4456 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004457 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004458 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004459 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004460 case AttributeList::AT_StdCall:
4461 case AttributeList::AT_CDecl:
4462 case AttributeList::AT_FastCall:
4463 case AttributeList::AT_ThisCall:
4464 case AttributeList::AT_Pascal:
4465 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004466 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004467 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004468 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004469 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004470 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004471 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004472 break;
John McCallc052dbb2012-05-22 21:28:12 +00004473
4474 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004475 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004476 handleMsStructAttr(S, D, Attr);
4477 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004478 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004479 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004480 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004481 case AttributeList::AT_SingleInheritance:
4482 case AttributeList::AT_MultipleInheritance:
4483 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004484 handleInheritanceAttr(S, D, Attr);
4485 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004486 case AttributeList::AT_Win64:
4487 case AttributeList::AT_Ptr32:
4488 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004489 handlePortabilityAttr(S, D, Attr);
4490 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004491 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004492 handleForceInlineAttr(S, D, Attr);
4493 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004494
4495 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004496 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004497 handleGuardedVarAttr(S, D, Attr);
4498 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004499 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004500 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004501 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004502 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004503 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004504 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004505 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004506 handleNoAddressSafetyAttr(S, D, Attr);
4507 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004508 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004509 handleNoThreadSafetyAttr(S, D, Attr);
4510 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004511 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004512 handleLockableAttr(S, D, Attr);
4513 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004514 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004515 handleGuardedByAttr(S, D, Attr);
4516 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004517 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004518 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004519 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004520 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004521 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004522 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004523 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004524 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004525 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004526 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004527 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004528 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004529 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004530 handleLockReturnedAttr(S, D, Attr);
4531 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004532 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004533 handleLocksExcludedAttr(S, D, Attr);
4534 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004535 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004536 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004537 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004538 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004539 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004540 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004541 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004542 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004543 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004544 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004545 handleUnlockFunAttr(S, D, Attr);
4546 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004547 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004548 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004549 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004550 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004551 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004552 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004553
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004554 // Type safety attributes.
4555 case AttributeList::AT_ArgumentWithTypeTag:
4556 handleArgumentWithTypeTagAttr(S, D, Attr);
4557 break;
4558 case AttributeList::AT_TypeTagForDatatype:
4559 handleTypeTagForDatatypeAttr(S, D, Attr);
4560 break;
4561
Chris Lattner803d0802008-06-29 00:43:07 +00004562 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004563 // Ask target about the attribute.
4564 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4565 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004566 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4567 diag::warn_unhandled_ms_attribute_ignored :
4568 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004569 break;
4570 }
4571}
4572
Peter Collingbourne60700392011-01-21 02:08:45 +00004573/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4574/// the attribute applies to decls. If the attribute is a type attribute, just
4575/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4576/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004577static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4578 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004579 bool NonInheritable, bool Inheritable) {
4580 if (Attr.isInvalid())
4581 return;
4582
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004583 // Type attributes are still treated as declaration attributes by
4584 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4585 // want to process them, however, because we will simply warn about ignoring
4586 // them. So instead, we will bail out early.
4587 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004588 return;
4589
4590 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004591 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004592
4593 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004594 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004595}
4596
Chris Lattner803d0802008-06-29 00:43:07 +00004597/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4598/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004599void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004600 const AttributeList *AttrList,
4601 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004602 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004603 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004604 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004605
4606 // GCC accepts
4607 // static int a9 __attribute__((weakref));
4608 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004609 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004610 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004611 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004612 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004613 }
4614}
4615
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004616// Annotation attributes are the only attributes allowed after an access
4617// specifier.
4618bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4619 const AttributeList *AttrList) {
4620 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004621 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004622 handleAnnotateAttr(*this, ASDecl, *l);
4623 } else {
4624 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4625 return true;
4626 }
4627 }
4628
4629 return false;
4630}
4631
John McCalle82247a2011-10-01 05:17:03 +00004632/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4633/// contains any decl attributes that we should warn about.
4634static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4635 for ( ; A; A = A->getNext()) {
4636 // Only warn if the attribute is an unignored, non-type attribute.
4637 if (A->isUsedAsTypeAttr()) continue;
4638 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4639
4640 if (A->getKind() == AttributeList::UnknownAttribute) {
4641 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4642 << A->getName() << A->getRange();
4643 } else {
4644 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4645 << A->getName() << A->getRange();
4646 }
4647 }
4648}
4649
4650/// checkUnusedDeclAttributes - Given a declarator which is not being
4651/// used to build a declaration, complain about any decl attributes
4652/// which might be lying around on it.
4653void Sema::checkUnusedDeclAttributes(Declarator &D) {
4654 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4655 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4656 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4657 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4658}
4659
Ryan Flynne25ff832009-07-30 03:15:39 +00004660/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004661/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004662NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4663 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004664 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004665 NamedDecl *NewD = 0;
4666 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004667 FunctionDecl *NewFD;
4668 // FIXME: Missing call to CheckFunctionDeclaration().
4669 // FIXME: Mangling?
4670 // FIXME: Is the qualifier info correct?
4671 // FIXME: Is the DeclContext correct?
4672 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4673 Loc, Loc, DeclarationName(II),
4674 FD->getType(), FD->getTypeSourceInfo(),
4675 SC_None, SC_None,
4676 false/*isInlineSpecified*/,
4677 FD->hasPrototype(),
4678 false/*isConstexprSpecified*/);
4679 NewD = NewFD;
4680
4681 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004682 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004683
4684 // Fake up parameter variables; they are declared as if this were
4685 // a typedef.
4686 QualType FDTy = FD->getType();
4687 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4688 SmallVector<ParmVarDecl*, 16> Params;
4689 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4690 AE = FT->arg_type_end(); AI != AE; ++AI) {
4691 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4692 Param->setScopeInfo(0, Params.size());
4693 Params.push_back(Param);
4694 }
David Blaikie4278c652011-09-21 18:16:56 +00004695 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004696 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004697 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4698 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004699 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004700 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004701 VD->getStorageClass(),
4702 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004703 if (VD->getQualifier()) {
4704 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004705 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004706 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004707 }
4708 return NewD;
4709}
4710
James Dennett1dfbd922012-06-14 21:40:34 +00004711/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004712/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004713void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004714 if (W.getUsed()) return; // only do this once
4715 W.setUsed(true);
4716 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4717 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004718 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004719 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4720 NDId->getName()));
4721 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004722 WeakTopLevelDecl.push_back(NewD);
4723 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4724 // to insert Decl at TU scope, sorry.
4725 DeclContext *SavedContext = CurContext;
4726 CurContext = Context.getTranslationUnitDecl();
4727 PushOnScopeChains(NewD, S);
4728 CurContext = SavedContext;
4729 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004730 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004731 }
4732}
4733
Chris Lattner0744e5f2008-06-29 00:23:49 +00004734/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4735/// it, apply them to D. This is a bit tricky because PD can have attributes
4736/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004737void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4738 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004739 // It's valid to "forward-declare" #pragma weak, in which case we
4740 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004741 if (Inheritable) {
4742 LoadExternalWeakUndeclaredIdentifiers();
4743 if (!WeakUndeclaredIdentifiers.empty()) {
4744 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4745 if (IdentifierInfo *Id = ND->getIdentifier()) {
4746 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4747 = WeakUndeclaredIdentifiers.find(Id);
4748 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4749 WeakInfo W = I->second;
4750 DeclApplyPragmaWeak(S, ND, W);
4751 WeakUndeclaredIdentifiers[Id] = W;
4752 }
John McCalld4aff0e2010-10-27 00:59:00 +00004753 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004754 }
4755 }
4756 }
4757
Chris Lattner0744e5f2008-06-29 00:23:49 +00004758 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004759 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004760 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004761
Chris Lattner0744e5f2008-06-29 00:23:49 +00004762 // Walk the declarator structure, applying decl attributes that were in a type
4763 // position to the decl itself. This handles cases like:
4764 // int *__attr__(x)** D;
4765 // when X is a decl attribute.
4766 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4767 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004768 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004769
Chris Lattner0744e5f2008-06-29 00:23:49 +00004770 // Finally, apply any attributes on the decl itself.
4771 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004772 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004773}
John McCall54abf7d2009-11-04 02:18:39 +00004774
John McCallf85e1932011-06-15 23:02:42 +00004775/// Is the given declaration allowed to use a forbidden type?
4776static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4777 // Private ivars are always okay. Unfortunately, people don't
4778 // always properly make their ivars private, even in system headers.
4779 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004780 // Function declarations in sys headers will be marked unavailable.
4781 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4782 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004783 return false;
4784
4785 // Require it to be declared in a system header.
4786 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4787}
4788
4789/// Handle a delayed forbidden-type diagnostic.
4790static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4791 Decl *decl) {
4792 if (decl && isForbiddenTypeAllowed(S, decl)) {
4793 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4794 "this system declaration uses an unsupported type"));
4795 return;
4796 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004797 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004798 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004799 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004800 // kind of forbidden type messages on unavailable functions.
4801 if (FD->hasAttr<UnavailableAttr>() &&
4802 diag.getForbiddenTypeDiagnostic() ==
4803 diag::err_arc_array_param_no_ownership) {
4804 diag.Triggered = true;
4805 return;
4806 }
4807 }
John McCallf85e1932011-06-15 23:02:42 +00004808
4809 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4810 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4811 diag.Triggered = true;
4812}
4813
John McCall92576642012-05-07 06:16:41 +00004814void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4815 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004816 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004817 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004818
John McCall92576642012-05-07 06:16:41 +00004819 // When delaying diagnostics to run in the context of a parsed
4820 // declaration, we only want to actually emit anything if parsing
4821 // succeeds.
4822 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004823
John McCall92576642012-05-07 06:16:41 +00004824 // We emit all the active diagnostics in this pool or any of its
4825 // parents. In general, we'll get one pool for the decl spec
4826 // and a child pool for each declarator; in a decl group like:
4827 // deprecated_typedef foo, *bar, baz();
4828 // only the declarator pops will be passed decls. This is correct;
4829 // we really do need to consider delayed diagnostics from the decl spec
4830 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004831 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004832 do {
John McCall13489672012-05-07 06:16:58 +00004833 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004834 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4835 // This const_cast is a bit lame. Really, Triggered should be mutable.
4836 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004837 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004838 continue;
4839
John McCalleee1d542011-02-14 07:13:47 +00004840 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004841 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004842 // Don't bother giving deprecation diagnostics if the decl is invalid.
4843 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004844 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004845 break;
4846
4847 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004848 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004849 break;
John McCallf85e1932011-06-15 23:02:42 +00004850
4851 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004852 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004853 break;
John McCall2f514482010-01-27 03:50:35 +00004854 }
4855 }
John McCall92576642012-05-07 06:16:41 +00004856 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004857}
4858
John McCall13489672012-05-07 06:16:58 +00004859/// Given a set of delayed diagnostics, re-emit them as if they had
4860/// been delayed in the current context instead of in the given pool.
4861/// Essentially, this just moves them to the current pool.
4862void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4863 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4864 assert(curPool && "re-emitting in undelayed context not supported");
4865 curPool->steal(pool);
4866}
4867
John McCall54abf7d2009-11-04 02:18:39 +00004868static bool isDeclDeprecated(Decl *D) {
4869 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004870 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004871 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004872 // A category implicitly has the availability of the interface.
4873 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4874 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004875 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4876 return false;
4877}
4878
Eli Friedmanc3b23082012-08-08 21:52:41 +00004879static void
4880DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4881 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004882 const ObjCInterfaceDecl *UnknownObjCClass,
4883 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00004884 DeclarationName Name = D->getDeclName();
4885 if (!Message.empty()) {
4886 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4887 S.Diag(D->getLocation(),
4888 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4889 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004890 if (ObjCPropery)
4891 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4892 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004893 } else if (!UnknownObjCClass) {
4894 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4895 S.Diag(D->getLocation(),
4896 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4897 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004898 if (ObjCPropery)
4899 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4900 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004901 } else {
4902 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4903 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4904 }
4905}
4906
John McCall9c3087b2010-08-26 02:13:20 +00004907void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004908 Decl *Ctx) {
4909 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004910 return;
4911
John McCall2f514482010-01-27 03:50:35 +00004912 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004913 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4914 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004915 DD.getUnknownObjCClass(),
4916 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00004917}
4918
Chris Lattner5f9e2722011-07-23 10:55:15 +00004919void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004920 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004921 const ObjCInterfaceDecl *UnknownObjCClass,
4922 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00004923 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004924 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004925 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4926 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004927 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004928 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004929 return;
4930 }
4931
4932 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004933 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004934 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004935 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00004936}