blob: 77413b0ef5e0710800d77d483a9e8471c874ec13 [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"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#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));
298 if (Res1.first == Res1.second)
299 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));
303 if (Res2.first == Res2.second)
304 return false;
305
306 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000307}
308
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000309/// \brief Check if passed in Decl is a pointer type.
310/// Note that this function may produce an error message.
311/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000312static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
313 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000314 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000315 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000316 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000317 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000318
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000319 if (const RecordType *RT = QT->getAs<RecordType>()) {
320 // If it's an incomplete type, it could be a smart pointer; skip it.
321 // (We don't want to force template instantiation if we can avoid it,
322 // since that would alter the order in which templates are instantiated.)
323 if (RT->isIncompleteType())
324 return true;
325
326 if (threadSafetyCheckIsSmartPointer(S, RT))
327 return true;
328 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000329
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000330 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000331 << Attr.getName()->getName() << QT;
332 } else {
333 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
334 << Attr.getName();
335 }
336 return false;
337}
338
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000339/// \brief Checks that the passed in QualType either is of RecordType or points
340/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000341static const RecordType *getRecordType(QualType QT) {
342 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000343 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000344
345 // Now check if we point to record type.
346 if (const PointerType *PT = QT->getAs<PointerType>())
347 return PT->getPointeeType()->getAs<RecordType>();
348
349 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350}
351
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000352
Jordy Rosefad5de92012-05-08 03:27:22 +0000353static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
354 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000355 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
356 if (RT->getDecl()->getAttr<LockableAttr>())
357 return true;
358 return false;
359}
360
361
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000362/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000363/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000364static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
365 QualType Ty) {
366 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000367
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000368 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000369 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000370 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000371 << Attr.getName() << Ty.getAsString();
372 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000373 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000374
Michael Hanf1aae3b2012-08-03 17:40:43 +0000375 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000376 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000377 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000378
379 // Allow smart pointers to be used as lockable objects.
380 // FIXME -- Check the type that the smart pointer points to.
381 if (threadSafetyCheckIsSmartPointer(S, RT))
382 return;
383
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000384 // Check if the type is lockable.
385 RecordDecl *RD = RT->getDecl();
386 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000387 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000388
389 // Else check if any base classes are lockable.
390 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
391 CXXBasePaths BPaths(false, false);
392 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
393 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000394 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000395
396 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
397 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000398}
399
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000400/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000401/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000402/// \param Sidx The attribute argument index to start checking with.
403/// \param ParamIdxOk Whether an argument can be indexing into a function
404/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000405static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000406 const AttributeList &Attr,
407 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000408 int Sidx = 0,
409 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000410 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000411 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000412
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000413 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000414 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000415 Args.push_back(ArgExp);
416 continue;
417 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000418
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000419 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000420 if (StrLit->getLength() == 0 ||
421 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000422 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000423 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000424 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000425 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000426 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000427
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000428 // We allow constant strings to be used as a placeholder for expressions
429 // that are not valid C++ syntax, but warn that they are ignored.
430 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
431 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000432 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000433 continue;
434 }
435
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000436 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000437
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000438 // A pointer to member expression of the form &MyClass::mu is treated
439 // specially -- we need to look at the type of the member.
440 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
441 if (UOp->getOpcode() == UO_AddrOf)
442 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
443 if (DRE->getDecl()->isCXXInstanceMember())
444 ArgTy = DRE->getDecl()->getType();
445
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000446 // First see if we can just cast to record type, or point to record type.
447 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000448
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000449 // Now check if we index into a record type function param.
450 if(!RT && ParamIdxOk) {
451 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000452 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
453 if(FD && IL) {
454 unsigned int NumParams = FD->getNumParams();
455 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
457 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
458 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
460 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000461 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000463 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000464 }
465 }
466
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000467 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000469 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000471}
472
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000473//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000474// Attribute Implementations
475//===----------------------------------------------------------------------===//
476
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000477// FIXME: All this manual attribute parsing code is gross. At the
478// least add some helper functions to check most argument patterns (#
479// and types of args).
480
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000481enum ThreadAttributeDeclKind {
482 ThreadExpectedFieldOrGlobalVar,
483 ThreadExpectedFunctionOrMethod,
484 ThreadExpectedClassOrStruct
485};
486
Michael Hanf1aae3b2012-08-03 17:40:43 +0000487static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000488 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000489 assert(!Attr.isInvalid());
490
491 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000492 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000493
494 // D must be either a member field or global (potentially shared) variable.
495 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000496 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
497 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000498 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000499 }
500
Michael Handc691572012-07-23 18:48:41 +0000501 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000502}
503
Michael Handc691572012-07-23 18:48:41 +0000504static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
505 if (!checkGuardedVarAttrCommon(S, D, Attr))
506 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000507
Michael Handc691572012-07-23 18:48:41 +0000508 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
509}
510
Michael Hanf1aae3b2012-08-03 17:40:43 +0000511static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000512 const AttributeList &Attr) {
513 if (!checkGuardedVarAttrCommon(S, D, Attr))
514 return;
515
516 if (!threadSafetyCheckIsPointer(S, D, Attr))
517 return;
518
519 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
520}
521
Michael Hanf1aae3b2012-08-03 17:40:43 +0000522static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
523 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000524 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000525 assert(!Attr.isInvalid());
526
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000527 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000528 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000529
530 // D must be either a member field or global (potentially shared) variable.
531 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000532 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
533 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000534 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000535 }
536
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000537 SmallVector<Expr*, 1> Args;
538 // check that all arguments are lockable objects
539 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
540 unsigned Size = Args.size();
541 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000542 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000543
Michael Handc691572012-07-23 18:48:41 +0000544 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000545
Michael Handc691572012-07-23 18:48:41 +0000546 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000547}
548
Michael Handc691572012-07-23 18:48:41 +0000549static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
550 Expr *Arg = 0;
551 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
552 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000553
Michael Handc691572012-07-23 18:48:41 +0000554 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
555}
556
Michael Hanf1aae3b2012-08-03 17:40:43 +0000557static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000558 const AttributeList &Attr) {
559 Expr *Arg = 0;
560 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
561 return;
562
563 if (!threadSafetyCheckIsPointer(S, D, Attr))
564 return;
565
566 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
567 S.Context, Arg));
568}
569
Michael Hanf1aae3b2012-08-03 17:40:43 +0000570static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000571 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000572 assert(!Attr.isInvalid());
573
574 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000575 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000576
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000577 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000578 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000579 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
580 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000581 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000582 }
583
Michael Handc691572012-07-23 18:48:41 +0000584 return true;
585}
586
587static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
588 if (!checkLockableAttrCommon(S, D, Attr))
589 return;
590
591 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
592}
593
Michael Hanf1aae3b2012-08-03 17:40:43 +0000594static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000595 const AttributeList &Attr) {
596 if (!checkLockableAttrCommon(S, D, Attr))
597 return;
598
599 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000600}
601
602static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
603 const AttributeList &Attr) {
604 assert(!Attr.isInvalid());
605
606 if (!checkAttributeNumArgs(S, Attr, 0))
607 return;
608
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000609 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000610 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
611 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000612 return;
613 }
614
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000615 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000616 S.Context));
617}
618
Kostya Serebryany71efba02012-01-24 19:25:38 +0000619static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000620 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000621 assert(!Attr.isInvalid());
622
623 if (!checkAttributeNumArgs(S, Attr, 0))
624 return;
625
626 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
627 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
628 << Attr.getName() << ExpectedFunctionOrMethod;
629 return;
630 }
631
632 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000633 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000634}
635
Michael Hanf1aae3b2012-08-03 17:40:43 +0000636static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
637 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000638 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000639 assert(!Attr.isInvalid());
640
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000641 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000642 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000643
644 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000645 ValueDecl *VD = dyn_cast<ValueDecl>(D);
646 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000647 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
648 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000649 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000650 }
651
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000652 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000653 QualType QT = VD->getType();
654 if (!QT->isDependentType()) {
655 const RecordType *RT = getRecordType(QT);
656 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000657 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000658 << Attr.getName();
659 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000660 }
661 }
662
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000663 // Check that all arguments are lockable objects.
664 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000665 if (Args.size() == 0)
666 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000667
Michael Handc691572012-07-23 18:48:41 +0000668 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000669}
670
Michael Hanf1aae3b2012-08-03 17:40:43 +0000671static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000672 const AttributeList &Attr) {
673 SmallVector<Expr*, 1> Args;
674 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
675 return;
676
677 Expr **StartArg = &Args[0];
678 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000679 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000680}
681
Michael Hanf1aae3b2012-08-03 17:40:43 +0000682static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000683 const AttributeList &Attr) {
684 SmallVector<Expr*, 1> Args;
685 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
686 return;
687
688 Expr **StartArg = &Args[0];
689 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000690 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000691}
692
Michael Hanf1aae3b2012-08-03 17:40:43 +0000693static bool checkLockFunAttrCommon(Sema &S, Decl *D,
694 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000695 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000696 assert(!Attr.isInvalid());
697
698 // zero or more arguments ok
699
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000700 // check that the attribute is applied to a function
701 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000702 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
703 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000705 }
706
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000707 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000708 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000709
Michael Handc691572012-07-23 18:48:41 +0000710 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000711}
712
Michael Hanf1aae3b2012-08-03 17:40:43 +0000713static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000714 const AttributeList &Attr) {
715 SmallVector<Expr*, 1> Args;
716 if (!checkLockFunAttrCommon(S, D, Attr, Args))
717 return;
718
719 unsigned Size = Args.size();
720 Expr **StartArg = Size == 0 ? 0 : &Args[0];
721 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000722 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000723 StartArg, Size));
724}
725
Michael Hanf1aae3b2012-08-03 17:40:43 +0000726static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000727 const AttributeList &Attr) {
728 SmallVector<Expr*, 1> Args;
729 if (!checkLockFunAttrCommon(S, D, Attr, Args))
730 return;
731
732 unsigned Size = Args.size();
733 Expr **StartArg = Size == 0 ? 0 : &Args[0];
734 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000735 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000736 StartArg, Size));
737}
738
Michael Hanf1aae3b2012-08-03 17:40:43 +0000739static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
740 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000741 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000742 assert(!Attr.isInvalid());
743
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000744 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000745 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000746
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000747 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000748 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
749 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000750 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000751 }
752
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000753 if (!isIntOrBool(Attr.getArg(0))) {
754 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000755 << Attr.getName();
756 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000757 }
758
759 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000761
Michael Handc691572012-07-23 18:48:41 +0000762 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000763}
764
Michael Hanf1aae3b2012-08-03 17:40:43 +0000765static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000766 const AttributeList &Attr) {
767 SmallVector<Expr*, 2> Args;
768 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
769 return;
770
771 unsigned Size = Args.size();
772 Expr **StartArg = Size == 0 ? 0 : &Args[0];
773 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000774 S.Context,
775 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000776 StartArg, Size));
777}
778
Michael Hanf1aae3b2012-08-03 17:40:43 +0000779static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000780 const AttributeList &Attr) {
781 SmallVector<Expr*, 2> Args;
782 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
783 return;
784
785 unsigned Size = Args.size();
786 Expr **StartArg = Size == 0 ? 0 : &Args[0];
787 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000788 S.Context,
789 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000790 StartArg, Size));
791}
792
Michael Hanf1aae3b2012-08-03 17:40:43 +0000793static bool checkLocksRequiredCommon(Sema &S, Decl *D,
794 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000795 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000796 assert(!Attr.isInvalid());
797
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000798 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000799 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000800
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000801 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000802 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
803 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000804 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000805 }
806
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000807 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000808 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000809 if (Args.size() == 0)
810 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000811
Michael Handc691572012-07-23 18:48:41 +0000812 return true;
813}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000814
Michael Hanf1aae3b2012-08-03 17:40:43 +0000815static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000816 const AttributeList &Attr) {
817 SmallVector<Expr*, 1> Args;
818 if (!checkLocksRequiredCommon(S, D, Attr, Args))
819 return;
820
821 Expr **StartArg = &Args[0];
822 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000823 S.Context,
824 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000825 Args.size()));
826}
827
Michael Hanf1aae3b2012-08-03 17:40:43 +0000828static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000829 const AttributeList &Attr) {
830 SmallVector<Expr*, 1> Args;
831 if (!checkLocksRequiredCommon(S, D, Attr, Args))
832 return;
833
834 Expr **StartArg = &Args[0];
835 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000836 S.Context,
837 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000838 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839}
840
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000841static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000843 assert(!Attr.isInvalid());
844
845 // zero or more arguments ok
846
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000847 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000848 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
849 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000850 return;
851 }
852
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000853 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000855 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000856 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000857 Expr **StartArg = Size == 0 ? 0 : &Args[0];
858
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000859 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000860 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000861}
862
863static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000864 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000865 assert(!Attr.isInvalid());
866
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000867 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000868 return;
869
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000870 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000871 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
872 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000873 return;
874 }
875
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000876 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000877 SmallVector<Expr*, 1> Args;
878 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
879 unsigned Size = Args.size();
880 if (Size == 0)
881 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000882
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000883 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
884 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000885}
886
887static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000888 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889 assert(!Attr.isInvalid());
890
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000891 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000892 return;
893
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000894 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000895 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
896 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000897 return;
898 }
899
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000900 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000901 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000902 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000903 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000904 if (Size == 0)
905 return;
906 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000907
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000908 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000909 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000910}
911
912
Chandler Carruth1b03c872011-07-02 00:01:44 +0000913static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
914 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000915 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000916 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000917 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000918 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919 }
Mike Stumpbf916502009-07-24 19:02:52 +0000920
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000922
923 Expr *sizeExpr;
924
925 // Special case where the argument is a template id.
926 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000927 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000928 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000929 UnqualifiedId id;
930 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000931
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000932 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
933 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000934 if (Size.isInvalid())
935 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000936
Douglas Gregor4ac01402011-06-15 16:02:29 +0000937 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000938 } else {
939 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000940 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000941 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000942
Peter Collingbourne7a730022010-11-23 20:45:58 +0000943 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000944 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000945
946 // Instantiate/Install the vector type, and let Sema build the type for us.
947 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000948 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000949 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000950 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000951 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000952
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000953 // Remember this typedef decl, we will need it later for diagnostics.
954 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000955 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000956}
957
Chandler Carruth1b03c872011-07-02 00:01:44 +0000958static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000960 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000962
Chandler Carruth87c44602011-07-01 23:49:12 +0000963 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000964 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000965 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000966 // If the alignment is less than or equal to 8 bits, the packed attribute
967 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000968 if (!FD->getType()->isDependentType() &&
969 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000970 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000972 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000974 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000975 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000977}
978
Chandler Carruth1b03c872011-07-02 00:01:44 +0000979static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000980 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
981 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000982 else
983 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
984}
985
Chandler Carruth1b03c872011-07-02 00:01:44 +0000986static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000987 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000988 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000989 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000990
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000991 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000992 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000993 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000994 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000995 return;
996 }
997
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000998 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000999}
1000
Ted Kremenek2f041d02011-09-29 07:02:25 +00001001static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1002 // The IBOutlet/IBOutletCollection attributes only apply to instance
1003 // variables or properties of Objective-C classes. The outlet must also
1004 // have an object reference type.
1005 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1006 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001007 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001008 << Attr.getName() << VD->getType() << 0;
1009 return false;
1010 }
1011 }
1012 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1013 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001014 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001015 << Attr.getName() << PD->getType() << 1;
1016 return false;
1017 }
1018 }
1019 else {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1021 return false;
1022 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001023
Ted Kremenek2f041d02011-09-29 07:02:25 +00001024 return true;
1025}
1026
Chandler Carruth1b03c872011-07-02 00:01:44 +00001027static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001028 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001029 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001030 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001031
1032 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001033 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001034
Ted Kremenek2f041d02011-09-29 07:02:25 +00001035 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001036}
1037
Chandler Carruth1b03c872011-07-02 00:01:44 +00001038static void handleIBOutletCollection(Sema &S, Decl *D,
1039 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001040
1041 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001042 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1044 return;
1045 }
1046
Ted Kremenek2f041d02011-09-29 07:02:25 +00001047 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001048 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001049
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001050 IdentifierInfo *II = Attr.getParameterName();
1051 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001052 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001053
John McCallb3d87482010-08-24 05:47:05 +00001054 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001055 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001056 if (!TypeRep) {
1057 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1058 return;
1059 }
John McCallb3d87482010-08-24 05:47:05 +00001060 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001061 // Diagnose use of non-object type in iboutletcollection attribute.
1062 // FIXME. Gnu attribute extension ignores use of builtin types in
1063 // attributes. So, __attribute__((iboutletcollection(char))) will be
1064 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001065 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001066 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1067 return;
1068 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001069 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1070 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001071}
1072
Chandler Carruthd309c812011-07-01 23:49:16 +00001073static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001074 if (const RecordType *UT = T->getAsUnionType())
1075 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1076 RecordDecl *UD = UT->getDecl();
1077 for (RecordDecl::field_iterator it = UD->field_begin(),
1078 itend = UD->field_end(); it != itend; ++it) {
1079 QualType QT = it->getType();
1080 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1081 T = QT;
1082 return;
1083 }
1084 }
1085 }
1086}
1087
Nuno Lopes587de5b2012-05-24 00:22:00 +00001088static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001089 if (!isFunctionOrMethod(D)) {
1090 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1091 << "alloc_size" << ExpectedFunctionOrMethod;
1092 return;
1093 }
1094
Nuno Lopes587de5b2012-05-24 00:22:00 +00001095 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1096 return;
1097
1098 // In C++ the implicit 'this' function parameter also counts, and they are
1099 // counted from one.
1100 bool HasImplicitThisParam = isInstanceMethod(D);
1101 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1102
1103 SmallVector<unsigned, 8> SizeArgs;
1104
1105 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1106 E = Attr.arg_end(); I!=E; ++I) {
1107 // The argument must be an integer constant expression.
1108 Expr *Ex = *I;
1109 llvm::APSInt ArgNum;
1110 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1111 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1112 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1113 << "alloc_size" << Ex->getSourceRange();
1114 return;
1115 }
1116
1117 uint64_t x = ArgNum.getZExtValue();
1118
1119 if (x < 1 || x > NumArgs) {
1120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1121 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1122 return;
1123 }
1124
1125 --x;
1126 if (HasImplicitThisParam) {
1127 if (x == 0) {
1128 S.Diag(Attr.getLoc(),
1129 diag::err_attribute_invalid_implicit_this_argument)
1130 << "alloc_size" << Ex->getSourceRange();
1131 return;
1132 }
1133 --x;
1134 }
1135
1136 // check if the function argument is of an integer type
1137 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1138 if (!T->isIntegerType()) {
1139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1140 << "alloc_size" << Ex->getSourceRange();
1141 return;
1142 }
1143
Nuno Lopes587de5b2012-05-24 00:22:00 +00001144 SizeArgs.push_back(x);
1145 }
1146
1147 // check if the function returns a pointer
1148 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1149 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1150 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1151 }
1152
Nuno Lopes96c67d12012-06-18 16:27:56 +00001153 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1154 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001155}
1156
Chandler Carruth1b03c872011-07-02 00:01:44 +00001157static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001158 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1159 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001160 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001162 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001163 return;
1164 }
Mike Stumpbf916502009-07-24 19:02:52 +00001165
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001166 // In C++ the implicit 'this' function parameter also counts, and they are
1167 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001168 bool HasImplicitThisParam = isInstanceMethod(D);
1169 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001170
1171 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001172 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001173
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001174 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1175 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001176
1177
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001178 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001179 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001180 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001181 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1182 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001183 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1184 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001185 return;
1186 }
Mike Stumpbf916502009-07-24 19:02:52 +00001187
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001188 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001189
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001190 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001192 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001193 return;
1194 }
Mike Stumpbf916502009-07-24 19:02:52 +00001195
Ted Kremenek465172f2008-07-21 22:09:15 +00001196 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001197 if (HasImplicitThisParam) {
1198 if (x == 0) {
1199 S.Diag(Attr.getLoc(),
1200 diag::err_attribute_invalid_implicit_this_argument)
1201 << "nonnull" << Ex->getSourceRange();
1202 return;
1203 }
1204 --x;
1205 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001206
1207 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001208 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001209 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001210
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001211 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001212 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001213 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001214 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001215 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001216 }
Mike Stumpbf916502009-07-24 19:02:52 +00001217
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001218 NonNullArgs.push_back(x);
1219 }
Mike Stumpbf916502009-07-24 19:02:52 +00001220
1221 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1222 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001223 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001224 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1225 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001226 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001227 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001228 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001229 }
Mike Stumpbf916502009-07-24 19:02:52 +00001230
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001231 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001232 if (NonNullArgs.empty()) {
1233 // Warn the trivial case only if attribute is not coming from a
1234 // macro instantiation.
1235 if (Attr.getLoc().isFileID())
1236 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001237 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001238 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001239 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001240
1241 unsigned* start = &NonNullArgs[0];
1242 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001243 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001244 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001245 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001246}
1247
Chandler Carruth1b03c872011-07-02 00:01:44 +00001248static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001249 // This attribute must be applied to a function declaration.
1250 // The first argument to the attribute must be a string,
1251 // the name of the resource, for example "malloc".
1252 // The following arguments must be argument indexes, the arguments must be
1253 // of integer type for Returns, otherwise of pointer type.
1254 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001255 // after being held. free() should be __attribute((ownership_takes)), whereas
1256 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001257
1258 if (!AL.getParameterName()) {
1259 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1260 << AL.getName()->getName() << 1;
1261 return;
1262 }
1263 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001264 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001265 switch (AL.getKind()) {
1266 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001267 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001268 if (AL.getNumArgs() < 1) {
1269 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1270 return;
1271 }
Jordy Rose2a479922010-08-12 08:54:03 +00001272 break;
1273 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001274 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001275 if (AL.getNumArgs() < 1) {
1276 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1277 return;
1278 }
Jordy Rose2a479922010-08-12 08:54:03 +00001279 break;
1280 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001281 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001282 if (AL.getNumArgs() > 1) {
1283 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1284 << AL.getNumArgs() + 1;
1285 return;
1286 }
Jordy Rose2a479922010-08-12 08:54:03 +00001287 break;
1288 default:
1289 // This should never happen given how we are called.
1290 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001291 }
1292
Chandler Carruth87c44602011-07-01 23:49:12 +00001293 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001294 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1295 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001296 return;
1297 }
1298
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001299 // In C++ the implicit 'this' function parameter also counts, and they are
1300 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001301 bool HasImplicitThisParam = isInstanceMethod(D);
1302 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001303
Chris Lattner5f9e2722011-07-23 10:55:15 +00001304 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001305
1306 // Normalize the argument, __foo__ becomes foo.
1307 if (Module.startswith("__") && Module.endswith("__"))
1308 Module = Module.substr(2, Module.size() - 4);
1309
Chris Lattner5f9e2722011-07-23 10:55:15 +00001310 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001311
Jordy Rose2a479922010-08-12 08:54:03 +00001312 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1313 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001314
Peter Collingbourne7a730022010-11-23 20:45:58 +00001315 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001316 llvm::APSInt ArgNum(32);
1317 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1318 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1319 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1320 << AL.getName()->getName() << IdxExpr->getSourceRange();
1321 continue;
1322 }
1323
1324 unsigned x = (unsigned) ArgNum.getZExtValue();
1325
1326 if (x > NumArgs || x < 1) {
1327 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1328 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1329 continue;
1330 }
1331 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001332 if (HasImplicitThisParam) {
1333 if (x == 0) {
1334 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1335 << "ownership" << IdxExpr->getSourceRange();
1336 return;
1337 }
1338 --x;
1339 }
1340
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001342 case OwnershipAttr::Takes:
1343 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001344 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001345 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001346 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1347 // FIXME: Should also highlight argument in decl.
1348 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001349 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001350 << "pointer"
1351 << IdxExpr->getSourceRange();
1352 continue;
1353 }
1354 break;
1355 }
Sean Huntcf807c42010-08-18 23:23:40 +00001356 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001357 if (AL.getNumArgs() > 1) {
1358 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001359 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001360 llvm::APSInt ArgNum(32);
1361 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1362 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1363 S.Diag(AL.getLoc(), diag::err_ownership_type)
1364 << "ownership_returns" << "integer"
1365 << IdxExpr->getSourceRange();
1366 return;
1367 }
1368 }
1369 break;
1370 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001371 } // switch
1372
1373 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001374 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001375 i = D->specific_attr_begin<OwnershipAttr>(),
1376 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001377 i != e; ++i) {
1378 if ((*i)->getOwnKind() != K) {
1379 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1380 I!=E; ++I) {
1381 if (x == *I) {
1382 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1383 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001384 }
1385 }
1386 }
1387 }
1388 OwnershipArgs.push_back(x);
1389 }
1390
1391 unsigned* start = OwnershipArgs.data();
1392 unsigned size = OwnershipArgs.size();
1393 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001394
1395 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1396 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1397 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398 }
Sean Huntcf807c42010-08-18 23:23:40 +00001399
Chandler Carruth87c44602011-07-01 23:49:12 +00001400 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001401 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001402}
1403
John McCall332bb2a2011-02-08 22:35:49 +00001404/// Whether this declaration has internal linkage for the purposes of
1405/// things that want to complain about things not have internal linkage.
1406static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1407 switch (D->getLinkage()) {
1408 case NoLinkage:
1409 case InternalLinkage:
1410 return true;
1411
1412 // Template instantiations that go from external to unique-external
1413 // shouldn't get diagnosed.
1414 case UniqueExternalLinkage:
1415 return true;
1416
1417 case ExternalLinkage:
1418 return false;
1419 }
1420 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001421}
1422
Chandler Carruth1b03c872011-07-02 00:01:44 +00001423static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001424 // Check the attribute arguments.
1425 if (Attr.getNumArgs() > 1) {
1426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1427 return;
1428 }
1429
Chandler Carruth87c44602011-07-01 23:49:12 +00001430 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001432 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001433 return;
1434 }
1435
Chandler Carruth87c44602011-07-01 23:49:12 +00001436 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001437
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001438 // gcc rejects
1439 // class c {
1440 // static int a __attribute__((weakref ("v2")));
1441 // static int b() __attribute__((weakref ("f3")));
1442 // };
1443 // and ignores the attributes of
1444 // void f(void) {
1445 // static int a __attribute__((weakref ("v2")));
1446 // }
1447 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001448 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001449 if (!Ctx->isFileContext()) {
1450 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001451 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001452 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001453 }
1454
1455 // The GCC manual says
1456 //
1457 // At present, a declaration to which `weakref' is attached can only
1458 // be `static'.
1459 //
1460 // It also says
1461 //
1462 // Without a TARGET,
1463 // given as an argument to `weakref' or to `alias', `weakref' is
1464 // equivalent to `weak'.
1465 //
1466 // gcc 4.4.1 will accept
1467 // int a7 __attribute__((weakref));
1468 // as
1469 // int a7 __attribute__((weak));
1470 // This looks like a bug in gcc. We reject that for now. We should revisit
1471 // it if this behaviour is actually used.
1472
John McCall332bb2a2011-02-08 22:35:49 +00001473 if (!hasEffectivelyInternalLinkage(nd)) {
1474 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001475 return;
1476 }
1477
1478 // GCC rejects
1479 // static ((alias ("y"), weakref)).
1480 // Should we? How to check that weakref is before or after alias?
1481
1482 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001483 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001484 Arg = Arg->IgnoreParenCasts();
1485 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1486
Douglas Gregor5cee1192011-07-27 05:40:30 +00001487 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1489 << "weakref" << 1;
1490 return;
1491 }
1492 // GCC will accept anything as the argument of weakref. Should we
1493 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001494 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001495 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001496 }
1497
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001498 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001499}
1500
Chandler Carruth1b03c872011-07-02 00:01:44 +00001501static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001502 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001503 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001505 return;
1506 }
Mike Stumpbf916502009-07-24 19:02:52 +00001507
Peter Collingbourne7a730022010-11-23 20:45:58 +00001508 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001509 Arg = Arg->IgnoreParenCasts();
1510 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001511
Douglas Gregor5cee1192011-07-27 05:40:30 +00001512 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001514 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001515 return;
1516 }
Mike Stumpbf916502009-07-24 19:02:52 +00001517
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001518 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001519 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1520 return;
1521 }
1522
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001524
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001525 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001526 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001527}
1528
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001529static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1530 // Check the attribute arguments.
1531 if (!checkAttributeNumArgs(S, Attr, 0))
1532 return;
1533
1534 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1536 << Attr.getName() << ExpectedFunctionOrMethod;
1537 return;
1538 }
1539
1540 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1541}
1542
Benjamin Krameree409a92012-05-12 21:10:52 +00001543static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1544 // Check the attribute arguments.
1545 if (!checkAttributeNumArgs(S, Attr, 0))
1546 return;
1547
1548 if (!isa<FunctionDecl>(D)) {
1549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1550 << Attr.getName() << ExpectedFunction;
1551 return;
1552 }
1553
1554 if (D->hasAttr<HotAttr>()) {
1555 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1556 << Attr.getName() << "hot";
1557 return;
1558 }
1559
1560 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1561}
1562
1563static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1564 // Check the attribute arguments.
1565 if (!checkAttributeNumArgs(S, Attr, 0))
1566 return;
1567
1568 if (!isa<FunctionDecl>(D)) {
1569 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1570 << Attr.getName() << ExpectedFunction;
1571 return;
1572 }
1573
1574 if (D->hasAttr<ColdAttr>()) {
1575 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1576 << Attr.getName() << "cold";
1577 return;
1578 }
1579
1580 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1581}
1582
Chandler Carruth1b03c872011-07-02 00:01:44 +00001583static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001584 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001585 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001586 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001587
Chandler Carruth87c44602011-07-01 23:49:12 +00001588 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001589 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001590 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001591 return;
1592 }
1593
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001594 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001595}
1596
Chandler Carruth1b03c872011-07-02 00:01:44 +00001597static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001599 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001600 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1602 return;
1603 }
1604
Chandler Carruth87c44602011-07-01 23:49:12 +00001605 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001607 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001608 return;
1609 }
Mike Stumpbf916502009-07-24 19:02:52 +00001610
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001611 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001612}
1613
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001614static void handleTLSModelAttr(Sema &S, Decl *D,
1615 const AttributeList &Attr) {
1616 // Check the attribute arguments.
1617 if (Attr.getNumArgs() != 1) {
1618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1619 return;
1620 }
1621
1622 Expr *Arg = Attr.getArg(0);
1623 Arg = Arg->IgnoreParenCasts();
1624 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1625
1626 // Check that it is a string.
1627 if (!Str) {
1628 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1629 return;
1630 }
1631
1632 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1634 << Attr.getName() << ExpectedTLSVar;
1635 return;
1636 }
1637
1638 // Check that the value.
1639 StringRef Model = Str->getString();
1640 if (Model != "global-dynamic" && Model != "local-dynamic"
1641 && Model != "initial-exec" && Model != "local-exec") {
1642 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1643 return;
1644 }
1645
1646 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1647 Model));
1648}
1649
Chandler Carruth1b03c872011-07-02 00:01:44 +00001650static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001651 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001652 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 return;
1655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Chandler Carruth87c44602011-07-01 23:49:12 +00001657 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001658 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001659 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001660 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001661 return;
1662 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001663 }
1664
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001665 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001666}
1667
Chandler Carruth1b03c872011-07-02 00:01:44 +00001668static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001669 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001670 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001671 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001672
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001673 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001674}
1675
Chandler Carruth1b03c872011-07-02 00:01:44 +00001676static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001677 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001678 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001679 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001680 else
1681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001682 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001683}
1684
Chandler Carruth1b03c872011-07-02 00:01:44 +00001685static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001686 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001687 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001688 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001689 else
1690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001692}
1693
Chandler Carruth1b03c872011-07-02 00:01:44 +00001694static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001695 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001696
1697 if (S.CheckNoReturnAttr(attr)) return;
1698
Chandler Carruth87c44602011-07-01 23:49:12 +00001699 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001700 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001701 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001702 return;
1703 }
1704
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001705 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001706}
1707
1708bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001709 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001710 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1711 attr.setInvalid();
1712 return true;
1713 }
1714
1715 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001716}
1717
Chandler Carruth1b03c872011-07-02 00:01:44 +00001718static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1719 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001720
1721 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1722 // because 'analyzer_noreturn' does not impact the type.
1723
Chandler Carruth1731e202011-07-11 23:30:35 +00001724 if(!checkAttributeNumArgs(S, Attr, 0))
1725 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001726
Chandler Carruth87c44602011-07-01 23:49:12 +00001727 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1728 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001729 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1730 && !VD->getType()->isFunctionPointerType())) {
1731 S.Diag(Attr.getLoc(),
1732 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1733 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001734 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001735 return;
1736 }
1737 }
1738
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001739 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001740}
1741
John Thompson35cc9622010-08-09 21:53:52 +00001742// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001743static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001744/*
1745 Returning a Vector Class in Registers
1746
Eric Christopherf48f3672010-12-01 22:13:54 +00001747 According to the PPU ABI specifications, a class with a single member of
1748 vector type is returned in memory when used as the return value of a function.
1749 This results in inefficient code when implementing vector classes. To return
1750 the value in a single vector register, add the vecreturn attribute to the
1751 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001752
1753 Example:
1754
1755 struct Vector
1756 {
1757 __vector float xyzw;
1758 } __attribute__((vecreturn));
1759
1760 Vector Add(Vector lhs, Vector rhs)
1761 {
1762 Vector result;
1763 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1764 return result; // This will be returned in a register
1765 }
1766*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001767 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001769 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001770 return;
1771 }
1772
Chandler Carruth87c44602011-07-01 23:49:12 +00001773 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001774 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1775 return;
1776 }
1777
Chandler Carruth87c44602011-07-01 23:49:12 +00001778 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001779 int count = 0;
1780
1781 if (!isa<CXXRecordDecl>(record)) {
1782 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1783 return;
1784 }
1785
1786 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1787 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1788 return;
1789 }
1790
Eric Christopherf48f3672010-12-01 22:13:54 +00001791 for (RecordDecl::field_iterator iter = record->field_begin();
1792 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001793 if ((count == 1) || !iter->getType()->isVectorType()) {
1794 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1795 return;
1796 }
1797 count++;
1798 }
1799
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001800 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001801}
1802
Chandler Carruth1b03c872011-07-02 00:01:44 +00001803static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001804 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001806 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001807 return;
1808 }
1809 // FIXME: Actually store the attribute on the declaration
1810}
1811
Chandler Carruth1b03c872011-07-02 00:01:44 +00001812static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001813 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001814 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001816 return;
1817 }
Mike Stumpbf916502009-07-24 19:02:52 +00001818
Chandler Carruth87c44602011-07-01 23:49:12 +00001819 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001820 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001822 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001823 return;
1824 }
Mike Stumpbf916502009-07-24 19:02:52 +00001825
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001826 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001827}
1828
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001829static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1830 const AttributeList &Attr) {
1831 // check the attribute arguments.
1832 if (Attr.hasParameterOrArguments()) {
1833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1834 return;
1835 }
1836
1837 if (!isa<FunctionDecl>(D)) {
1838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1839 << Attr.getName() << ExpectedFunction;
1840 return;
1841 }
1842
1843 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1844}
1845
Chandler Carruth1b03c872011-07-02 00:01:44 +00001846static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001847 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001848 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1850 return;
1851 }
Mike Stumpbf916502009-07-24 19:02:52 +00001852
Chandler Carruth87c44602011-07-01 23:49:12 +00001853 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001854 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001855 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1856 return;
1857 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001858 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001860 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001861 return;
1862 }
Mike Stumpbf916502009-07-24 19:02:52 +00001863
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001864 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001865}
1866
Chandler Carruth1b03c872011-07-02 00:01:44 +00001867static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001868 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001869 if (Attr.getNumArgs() > 1) {
1870 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001871 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001872 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001873
1874 int priority = 65535; // FIXME: Do not hardcode such constants.
1875 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001876 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001877 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001878 if (E->isTypeDependent() || E->isValueDependent() ||
1879 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001880 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001881 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001882 return;
1883 }
1884 priority = Idx.getZExtValue();
1885 }
Mike Stumpbf916502009-07-24 19:02:52 +00001886
Chandler Carruth87c44602011-07-01 23:49:12 +00001887 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001888 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001889 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001890 return;
1891 }
1892
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001893 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001894 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001895}
1896
Chandler Carruth1b03c872011-07-02 00:01:44 +00001897static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001898 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001899 if (Attr.getNumArgs() > 1) {
1900 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001901 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001902 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001903
1904 int priority = 65535; // FIXME: Do not hardcode such constants.
1905 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001906 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001907 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001908 if (E->isTypeDependent() || E->isValueDependent() ||
1909 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001911 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001912 return;
1913 }
1914 priority = Idx.getZExtValue();
1915 }
Mike Stumpbf916502009-07-24 19:02:52 +00001916
Chandler Carruth87c44602011-07-01 23:49:12 +00001917 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001918 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001919 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001920 return;
1921 }
1922
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001923 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001924 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001925}
1926
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001927template <typename AttrTy>
1928static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1929 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001930 unsigned NumArgs = Attr.getNumArgs();
1931 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001932 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001933 return;
1934 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001935
1936 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001937 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001938 if (NumArgs == 1) {
1939 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001940 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001941 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001942 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001943 return;
1944 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001945 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001948 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001949}
1950
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001951static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1952 const AttributeList &Attr) {
1953 unsigned NumArgs = Attr.getNumArgs();
1954 if (NumArgs > 0) {
1955 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1956 return;
1957 }
1958
1959 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001960 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001961}
1962
Patrick Beardb2f68202012-04-06 18:12:22 +00001963static void handleObjCRootClassAttr(Sema &S, Decl *D,
1964 const AttributeList &Attr) {
1965 if (!isa<ObjCInterfaceDecl>(D)) {
1966 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1967 return;
1968 }
1969
1970 unsigned NumArgs = Attr.getNumArgs();
1971 if (NumArgs > 0) {
1972 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1973 return;
1974 }
1975
1976 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1977}
1978
Ted Kremenek71207fc2012-01-05 22:47:47 +00001979static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001980 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001981 if (!isa<ObjCInterfaceDecl>(D)) {
1982 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1983 return;
1984 }
1985
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001986 unsigned NumArgs = Attr.getNumArgs();
1987 if (NumArgs > 0) {
1988 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1989 return;
1990 }
1991
Ted Kremenek71207fc2012-01-05 22:47:47 +00001992 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001993 Attr.getRange(), S.Context));
1994}
1995
Jordy Rosefad5de92012-05-08 03:27:22 +00001996static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1997 IdentifierInfo *Platform,
1998 VersionTuple Introduced,
1999 VersionTuple Deprecated,
2000 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002001 StringRef PlatformName
2002 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2003 if (PlatformName.empty())
2004 PlatformName = Platform->getName();
2005
2006 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2007 // of these steps are needed).
2008 if (!Introduced.empty() && !Deprecated.empty() &&
2009 !(Introduced <= Deprecated)) {
2010 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2011 << 1 << PlatformName << Deprecated.getAsString()
2012 << 0 << Introduced.getAsString();
2013 return true;
2014 }
2015
2016 if (!Introduced.empty() && !Obsoleted.empty() &&
2017 !(Introduced <= Obsoleted)) {
2018 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2019 << 2 << PlatformName << Obsoleted.getAsString()
2020 << 0 << Introduced.getAsString();
2021 return true;
2022 }
2023
2024 if (!Deprecated.empty() && !Obsoleted.empty() &&
2025 !(Deprecated <= Obsoleted)) {
2026 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2027 << 2 << PlatformName << Obsoleted.getAsString()
2028 << 1 << Deprecated.getAsString();
2029 return true;
2030 }
2031
2032 return false;
2033}
2034
Rafael Espindola599f1b72012-05-13 03:25:18 +00002035AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2036 IdentifierInfo *Platform,
2037 VersionTuple Introduced,
2038 VersionTuple Deprecated,
2039 VersionTuple Obsoleted,
2040 bool IsUnavailable,
2041 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002042 VersionTuple MergedIntroduced = Introduced;
2043 VersionTuple MergedDeprecated = Deprecated;
2044 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002045 bool FoundAny = false;
2046
Rafael Espindola98ae8342012-05-10 02:50:16 +00002047 if (D->hasAttrs()) {
2048 AttrVec &Attrs = D->getAttrs();
2049 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2050 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2051 if (!OldAA) {
2052 ++i;
2053 continue;
2054 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002055
Rafael Espindola98ae8342012-05-10 02:50:16 +00002056 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2057 if (OldPlatform != Platform) {
2058 ++i;
2059 continue;
2060 }
2061
2062 FoundAny = true;
2063 VersionTuple OldIntroduced = OldAA->getIntroduced();
2064 VersionTuple OldDeprecated = OldAA->getDeprecated();
2065 VersionTuple OldObsoleted = OldAA->getObsoleted();
2066 bool OldIsUnavailable = OldAA->getUnavailable();
2067 StringRef OldMessage = OldAA->getMessage();
2068
2069 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2070 OldIntroduced != Introduced) ||
2071 (!OldDeprecated.empty() && !Deprecated.empty() &&
2072 OldDeprecated != Deprecated) ||
2073 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2074 OldObsoleted != Obsoleted) ||
2075 (OldIsUnavailable != IsUnavailable) ||
2076 (OldMessage != Message)) {
2077 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2078 Diag(Range.getBegin(), diag::note_previous_attribute);
2079 Attrs.erase(Attrs.begin() + i);
2080 --e;
2081 continue;
2082 }
2083
2084 VersionTuple MergedIntroduced2 = MergedIntroduced;
2085 VersionTuple MergedDeprecated2 = MergedDeprecated;
2086 VersionTuple MergedObsoleted2 = MergedObsoleted;
2087
2088 if (MergedIntroduced2.empty())
2089 MergedIntroduced2 = OldIntroduced;
2090 if (MergedDeprecated2.empty())
2091 MergedDeprecated2 = OldDeprecated;
2092 if (MergedObsoleted2.empty())
2093 MergedObsoleted2 = OldObsoleted;
2094
2095 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2096 MergedIntroduced2, MergedDeprecated2,
2097 MergedObsoleted2)) {
2098 Attrs.erase(Attrs.begin() + i);
2099 --e;
2100 continue;
2101 }
2102
2103 MergedIntroduced = MergedIntroduced2;
2104 MergedDeprecated = MergedDeprecated2;
2105 MergedObsoleted = MergedObsoleted2;
2106 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002107 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002108 }
2109
2110 if (FoundAny &&
2111 MergedIntroduced == Introduced &&
2112 MergedDeprecated == Deprecated &&
2113 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002114 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002115
Rafael Espindola98ae8342012-05-10 02:50:16 +00002116 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002117 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002118 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2119 Introduced, Deprecated,
2120 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002121 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002122 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002123}
2124
Chandler Carruth1b03c872011-07-02 00:01:44 +00002125static void handleAvailabilityAttr(Sema &S, Decl *D,
2126 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002127 IdentifierInfo *Platform = Attr.getParameterName();
2128 SourceLocation PlatformLoc = Attr.getParameterLoc();
2129
Rafael Espindola3b294362012-05-06 19:56:25 +00002130 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002131 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2132 << Platform;
2133
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002134 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2135 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2136 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002137 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002138 StringRef Str;
2139 const StringLiteral *SE =
2140 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2141 if (SE)
2142 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002143
Rafael Espindola599f1b72012-05-13 03:25:18 +00002144 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2145 Platform,
2146 Introduced.Version,
2147 Deprecated.Version,
2148 Obsoleted.Version,
2149 IsUnavailable, Str);
2150 if (NewAttr)
2151 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002152}
2153
Rafael Espindola599f1b72012-05-13 03:25:18 +00002154VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2155 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002156 if (isa<TypedefNameDecl>(D)) {
2157 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002158 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002159 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002160 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2161 if (ExistingAttr) {
2162 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2163 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002164 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002165 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2166 Diag(Range.getBegin(), diag::note_previous_attribute);
2167 D->dropAttr<VisibilityAttr>();
2168 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002169 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002170}
2171
Chandler Carruth1b03c872011-07-02 00:01:44 +00002172static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002173 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002174 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002175 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002176
Peter Collingbourne7a730022010-11-23 20:45:58 +00002177 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002178 Arg = Arg->IgnoreParenCasts();
2179 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002180
Douglas Gregor5cee1192011-07-27 05:40:30 +00002181 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002182 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002183 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002184 return;
2185 }
Mike Stumpbf916502009-07-24 19:02:52 +00002186
Chris Lattner5f9e2722011-07-23 10:55:15 +00002187 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002188 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002189
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002190 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002191 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002192 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002193 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002194 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002195 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002196 else if (TypeStr == "protected") {
2197 // Complain about attempts to use protected visibility on targets
2198 // (like Darwin) that don't support it.
2199 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2200 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2201 type = VisibilityAttr::Default;
2202 } else {
2203 type = VisibilityAttr::Protected;
2204 }
2205 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002206 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002207 return;
2208 }
Mike Stumpbf916502009-07-24 19:02:52 +00002209
Rafael Espindola599f1b72012-05-13 03:25:18 +00002210 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2211 if (NewAttr)
2212 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002213}
2214
Chandler Carruth1b03c872011-07-02 00:01:44 +00002215static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2216 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002217 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2218 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002219 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002220 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002221 return;
2222 }
2223
Chandler Carruth87c44602011-07-01 23:49:12 +00002224 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2225 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002227 << "objc_method_family" << 1;
2228 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002229 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002230 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002231 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002232 return;
2233 }
2234
Chris Lattner5f9e2722011-07-23 10:55:15 +00002235 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002236 ObjCMethodFamilyAttr::FamilyKind family;
2237 if (param == "none")
2238 family = ObjCMethodFamilyAttr::OMF_None;
2239 else if (param == "alloc")
2240 family = ObjCMethodFamilyAttr::OMF_alloc;
2241 else if (param == "copy")
2242 family = ObjCMethodFamilyAttr::OMF_copy;
2243 else if (param == "init")
2244 family = ObjCMethodFamilyAttr::OMF_init;
2245 else if (param == "mutableCopy")
2246 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2247 else if (param == "new")
2248 family = ObjCMethodFamilyAttr::OMF_new;
2249 else {
2250 // Just warn and ignore it. This is future-proof against new
2251 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002252 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002253 return;
2254 }
2255
John McCallf85e1932011-06-15 23:02:42 +00002256 if (family == ObjCMethodFamilyAttr::OMF_init &&
2257 !method->getResultType()->isObjCObjectPointerType()) {
2258 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2259 << method->getResultType();
2260 // Ignore the attribute.
2261 return;
2262 }
2263
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002264 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002265 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002266}
2267
Chandler Carruth1b03c872011-07-02 00:01:44 +00002268static void handleObjCExceptionAttr(Sema &S, Decl *D,
2269 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002270 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002271 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002272
Chris Lattner0db29ec2009-02-14 08:09:34 +00002273 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2274 if (OCI == 0) {
2275 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2276 return;
2277 }
Mike Stumpbf916502009-07-24 19:02:52 +00002278
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002279 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002280}
2281
Chandler Carruth1b03c872011-07-02 00:01:44 +00002282static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002283 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002285 return;
2286 }
Richard Smith162e1c12011-04-15 14:24:37 +00002287 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002288 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002289 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002290 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2291 return;
2292 }
2293 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002294 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2295 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002296 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002297 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2298 return;
2299 }
2300 }
2301 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002302 // It is okay to include this attribute on properties, e.g.:
2303 //
2304 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2305 //
2306 // In this case it follows tradition and suppresses an error in the above
2307 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002308 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002309 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002310 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002311}
2312
Mike Stumpbf916502009-07-24 19:02:52 +00002313static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002314handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002315 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002317 return;
2318 }
2319
2320 if (!isa<FunctionDecl>(D)) {
2321 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2322 return;
2323 }
2324
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002325 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002326}
2327
Chandler Carruth1b03c872011-07-02 00:01:44 +00002328static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002329 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002331 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002332 return;
2333 }
Mike Stumpbf916502009-07-24 19:02:52 +00002334
Steve Naroff9eae5762008-09-18 16:44:58 +00002335 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002336 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002337 return;
2338 }
Mike Stumpbf916502009-07-24 19:02:52 +00002339
Sean Huntcf807c42010-08-18 23:23:40 +00002340 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002341 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002342 type = BlocksAttr::ByRef;
2343 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002344 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002345 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002346 return;
2347 }
Mike Stumpbf916502009-07-24 19:02:52 +00002348
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002349 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002350}
2351
Chandler Carruth1b03c872011-07-02 00:01:44 +00002352static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002353 // check the attribute arguments.
2354 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002355 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002356 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002357 }
2358
John McCall3323fad2011-09-09 07:56:05 +00002359 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002360 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002361 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002362 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002363 if (E->isTypeDependent() || E->isValueDependent() ||
2364 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002365 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002366 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002367 return;
2368 }
Mike Stumpbf916502009-07-24 19:02:52 +00002369
John McCall3323fad2011-09-09 07:56:05 +00002370 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002371 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2372 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002373 return;
2374 }
John McCall3323fad2011-09-09 07:56:05 +00002375
2376 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002377 }
2378
John McCall3323fad2011-09-09 07:56:05 +00002379 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002380 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002381 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002382 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002383 if (E->isTypeDependent() || E->isValueDependent() ||
2384 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002385 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002386 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002387 return;
2388 }
2389 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002390
John McCall3323fad2011-09-09 07:56:05 +00002391 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002392 // FIXME: This error message could be improved, it would be nice
2393 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002394 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2395 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002396 return;
2397 }
2398 }
2399
Chandler Carruth87c44602011-07-01 23:49:12 +00002400 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002401 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002402 if (isa<FunctionNoProtoType>(FT)) {
2403 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2404 return;
2405 }
Mike Stumpbf916502009-07-24 19:02:52 +00002406
Chris Lattner897cd902009-03-17 23:03:47 +00002407 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002408 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002409 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002410 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002411 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002412 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002413 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002414 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002415 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002416 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2417 if (!BD->isVariadic()) {
2418 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2419 return;
2420 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002421 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002422 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002423 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002424 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002425 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002426 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002427 int m = Ty->isFunctionPointerType() ? 0 : 1;
2428 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002429 return;
2430 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002431 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002432 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002433 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002434 return;
2435 }
Anders Carlsson77091822008-10-05 18:05:59 +00002436 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002437 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002438 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002439 return;
2440 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002441 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002442 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002443}
2444
Chandler Carruth1b03c872011-07-02 00:01:44 +00002445static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002446 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002447 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002448 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002449
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002450 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002451 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002452 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002453 return;
2454 }
Mike Stumpbf916502009-07-24 19:02:52 +00002455
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002456 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2457 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2458 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002459 return;
2460 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002461 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2462 if (MD->getResultType()->isVoidType()) {
2463 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2464 << Attr.getName() << 1;
2465 return;
2466 }
2467
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002468 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002469}
2470
Chandler Carruth1b03c872011-07-02 00:01:44 +00002471static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002472 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002473 if (Attr.hasParameterOrArguments()) {
2474 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002475 return;
2476 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002477
Chandler Carruth87c44602011-07-01 23:49:12 +00002478 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002479 if (isa<CXXRecordDecl>(D)) {
2480 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2481 return;
2482 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2484 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002485 return;
2486 }
2487
Chandler Carruth87c44602011-07-01 23:49:12 +00002488 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002489
2490 // 'weak' only applies to declarations with external linkage.
2491 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002492 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002493 return;
2494 }
Mike Stumpbf916502009-07-24 19:02:52 +00002495
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002496 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002497}
2498
Chandler Carruth1b03c872011-07-02 00:01:44 +00002499static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002500 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002501 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002502 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002503
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002504
2505 // weak_import only applies to variable & function declarations.
2506 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002507 if (!D->canBeWeakImported(isDef)) {
2508 if (isDef)
2509 S.Diag(Attr.getLoc(),
2510 diag::warn_attribute_weak_import_invalid_on_definition)
2511 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002512 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002513 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002514 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002515 // Nothing to warn about here.
2516 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002517 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002518 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002519
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002520 return;
2521 }
2522
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002523 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002524}
2525
Tanya Lattner0df579e2012-07-09 22:06:01 +00002526// Handles reqd_work_group_size and work_group_size_hint.
2527static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002528 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002529 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2530 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2531
Nate Begeman6f3d8382009-06-26 06:32:41 +00002532 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002533 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002534
2535 unsigned WGSize[3];
2536 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002537 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002538 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002539 if (E->isTypeDependent() || E->isValueDependent() ||
2540 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002541 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002542 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002543 return;
2544 }
2545 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2546 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002547
2548 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2549 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2550 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2551 if (!(A->getXDim() == WGSize[0] &&
2552 A->getYDim() == WGSize[1] &&
2553 A->getZDim() == WGSize[2])) {
2554 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2555 Attr.getName();
2556 }
2557 }
2558
2559 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2560 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2561 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2562 if (!(A->getXDim() == WGSize[0] &&
2563 A->getYDim() == WGSize[1] &&
2564 A->getZDim() == WGSize[2])) {
2565 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2566 Attr.getName();
2567 }
2568 }
2569
2570 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2571 D->addAttr(::new (S.Context)
2572 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2573 WGSize[0], WGSize[1], WGSize[2]));
2574 else
2575 D->addAttr(::new (S.Context)
2576 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2577 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002578}
2579
Rafael Espindola599f1b72012-05-13 03:25:18 +00002580SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2581 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002582 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2583 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002584 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002585 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2586 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002587 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002588 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002589 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002590}
2591
Chandler Carruth1b03c872011-07-02 00:01:44 +00002592static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002593 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002594 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002595 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002596
2597 // Make sure that there is a string literal as the sections's single
2598 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002599 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002600 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002601 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002602 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002603 return;
2604 }
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Chris Lattner797c3c42009-08-10 19:03:04 +00002606 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002607 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002608 if (!Error.empty()) {
2609 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2610 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002611 return;
2612 }
Mike Stump1eb44332009-09-09 15:08:12 +00002613
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002614 // This attribute cannot be applied to local variables.
2615 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2616 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2617 return;
2618 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002619 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2620 SE->getString());
2621 if (NewAttr)
2622 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002623}
2624
Chris Lattner6b6b5372008-06-26 18:38:35 +00002625
Chandler Carruth1b03c872011-07-02 00:01:44 +00002626static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002627 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002628 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002630 return;
2631 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002632
Chandler Carruth87c44602011-07-01 23:49:12 +00002633 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002634 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002635 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002636 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002637 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002638 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002639}
2640
Chandler Carruth1b03c872011-07-02 00:01:44 +00002641static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002642 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002643 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002645 return;
2646 }
Mike Stumpbf916502009-07-24 19:02:52 +00002647
Chandler Carruth87c44602011-07-01 23:49:12 +00002648 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002649 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002650 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002651 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002652 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002653 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002654}
2655
Chandler Carruth1b03c872011-07-02 00:01:44 +00002656static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002657 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002658 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002659 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002660
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002661 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002662}
2663
Chandler Carruth1b03c872011-07-02 00:01:44 +00002664static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002665 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2667 return;
2668 }
Mike Stumpbf916502009-07-24 19:02:52 +00002669
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002670 if (Attr.getNumArgs() != 0) {
2671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2672 return;
2673 }
Mike Stumpbf916502009-07-24 19:02:52 +00002674
Chandler Carruth87c44602011-07-01 23:49:12 +00002675 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002676
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002677 if (!VD || !VD->hasLocalStorage()) {
2678 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2679 return;
2680 }
Mike Stumpbf916502009-07-24 19:02:52 +00002681
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002682 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002683 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002684 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002685 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2686 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002687 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002688 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002689 Attr.getParameterName();
2690 return;
2691 }
Mike Stumpbf916502009-07-24 19:02:52 +00002692
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002693 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2694 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002695 S.Diag(Attr.getParameterLoc(),
2696 diag::err_attribute_cleanup_arg_not_function)
2697 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002698 return;
2699 }
2700
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002701 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002702 S.Diag(Attr.getParameterLoc(),
2703 diag::err_attribute_cleanup_func_must_take_one_arg)
2704 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002705 return;
2706 }
Mike Stumpbf916502009-07-24 19:02:52 +00002707
Anders Carlsson89941c12009-02-07 23:16:50 +00002708 // We're currently more strict than GCC about what function types we accept.
2709 // If this ever proves to be a problem it should be easy to fix.
2710 QualType Ty = S.Context.getPointerType(VD->getType());
2711 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002712 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2713 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002714 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002715 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2716 Attr.getParameterName() << ParamTy << Ty;
2717 return;
2718 }
Mike Stumpbf916502009-07-24 19:02:52 +00002719
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002720 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002721 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002722}
2723
Mike Stumpbf916502009-07-24 19:02:52 +00002724/// Handle __attribute__((format_arg((idx)))) attribute based on
2725/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002726static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002727 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002728 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002729
Chandler Carruth87c44602011-07-01 23:49:12 +00002730 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002732 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002733 return;
2734 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002735
2736 // In C++ the implicit 'this' function parameter also counts, and they are
2737 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002738 bool HasImplicitThisParam = isInstanceMethod(D);
2739 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002740 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002741
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002742 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002743 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002744 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002745 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2746 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2748 << "format" << 2 << IdxExpr->getSourceRange();
2749 return;
2750 }
Mike Stumpbf916502009-07-24 19:02:52 +00002751
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002752 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2754 << "format" << 2 << IdxExpr->getSourceRange();
2755 return;
2756 }
Mike Stumpbf916502009-07-24 19:02:52 +00002757
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002758 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002759
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002760 if (HasImplicitThisParam) {
2761 if (ArgIdx == 0) {
2762 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2763 << "format_arg" << IdxExpr->getSourceRange();
2764 return;
2765 }
2766 ArgIdx--;
2767 }
2768
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002769 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002770 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002771
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002772 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2773 if (not_nsstring_type &&
2774 !isCFStringType(Ty, S.Context) &&
2775 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002776 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002777 // FIXME: Should highlight the actual expression that has the wrong type.
2778 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002779 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002780 << IdxExpr->getSourceRange();
2781 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002782 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002783 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002784 if (!isNSStringType(Ty, S.Context) &&
2785 !isCFStringType(Ty, S.Context) &&
2786 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002787 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002788 // FIXME: Should highlight the actual expression that has the wrong type.
2789 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002790 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002791 << IdxExpr->getSourceRange();
2792 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002793 }
2794
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002795 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002796 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002797}
2798
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002799enum FormatAttrKind {
2800 CFStringFormat,
2801 NSStringFormat,
2802 StrftimeFormat,
2803 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002804 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002805 InvalidFormat
2806};
2807
2808/// getFormatAttrKind - Map from format attribute names to supported format
2809/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002810static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002811 return llvm::StringSwitch<FormatAttrKind>(Format)
2812 // Check for formats that get handled specially.
2813 .Case("NSString", NSStringFormat)
2814 .Case("CFString", CFStringFormat)
2815 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002816
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002817 // Otherwise, check for supported formats.
2818 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2819 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2820 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002821
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002822 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2823 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002824}
2825
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002826/// Handle __attribute__((init_priority(priority))) attributes based on
2827/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002828static void handleInitPriorityAttr(Sema &S, Decl *D,
2829 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002830 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002831 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2832 return;
2833 }
2834
Chandler Carruth87c44602011-07-01 23:49:12 +00002835 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002836 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2837 Attr.setInvalid();
2838 return;
2839 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002840 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002841 if (S.Context.getAsArrayType(T))
2842 T = S.Context.getBaseElementType(T);
2843 if (!T->getAs<RecordType>()) {
2844 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2845 Attr.setInvalid();
2846 return;
2847 }
2848
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002849 if (Attr.getNumArgs() != 1) {
2850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2851 Attr.setInvalid();
2852 return;
2853 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002854 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002855
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002856 llvm::APSInt priority(32);
2857 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2858 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2859 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2860 << "init_priority" << priorityExpr->getSourceRange();
2861 Attr.setInvalid();
2862 return;
2863 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002864 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002865 if (prioritynum < 101 || prioritynum > 65535) {
2866 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2867 << priorityExpr->getSourceRange();
2868 Attr.setInvalid();
2869 return;
2870 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002871 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002872 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002873}
2874
Rafael Espindola599f1b72012-05-13 03:25:18 +00002875FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2876 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002877 // Check whether we already have an equivalent format attribute.
2878 for (specific_attr_iterator<FormatAttr>
2879 i = D->specific_attr_begin<FormatAttr>(),
2880 e = D->specific_attr_end<FormatAttr>();
2881 i != e ; ++i) {
2882 FormatAttr *f = *i;
2883 if (f->getType() == Format &&
2884 f->getFormatIdx() == FormatIdx &&
2885 f->getFirstArg() == FirstArg) {
2886 // If we don't have a valid location for this attribute, adopt the
2887 // location.
2888 if (f->getLocation().isInvalid())
2889 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002890 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002891 }
2892 }
2893
Rafael Espindola599f1b72012-05-13 03:25:18 +00002894 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2895 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002896}
2897
Mike Stumpbf916502009-07-24 19:02:52 +00002898/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2899/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002900static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002901
Chris Lattner545dd342008-06-28 23:36:30 +00002902 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002903 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002904 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002905 return;
2906 }
2907
Chris Lattner545dd342008-06-28 23:36:30 +00002908 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002910 return;
2911 }
2912
Chandler Carruth87c44602011-07-01 23:49:12 +00002913 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002914 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002915 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002916 return;
2917 }
2918
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002919 // In C++ the implicit 'this' function parameter also counts, and they are
2920 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002921 bool HasImplicitThisParam = isInstanceMethod(D);
2922 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002923 unsigned FirstIdx = 1;
2924
Chris Lattner5f9e2722011-07-23 10:55:15 +00002925 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002926
2927 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002928 if (Format.startswith("__") && Format.endswith("__"))
2929 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002930
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002931 // Check for supported formats.
2932 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002933
2934 if (Kind == IgnoredFormat)
2935 return;
2936
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002937 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002938 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002939 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002940 return;
2941 }
2942
2943 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002944 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002945 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002946 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2947 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002948 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002949 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002950 return;
2951 }
2952
2953 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002954 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002955 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002956 return;
2957 }
2958
2959 // FIXME: Do we need to bounds check?
2960 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002961
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002962 if (HasImplicitThisParam) {
2963 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002964 S.Diag(Attr.getLoc(),
2965 diag::err_format_attribute_implicit_this_format_string)
2966 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002967 return;
2968 }
2969 ArgIdx--;
2970 }
Mike Stump1eb44332009-09-09 15:08:12 +00002971
Chris Lattner6b6b5372008-06-26 18:38:35 +00002972 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002973 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002974
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002975 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002976 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002977 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2978 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002979 return;
2980 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002981 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002982 // FIXME: do we need to check if the type is NSString*? What are the
2983 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002984 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002985 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002986 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2987 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002988 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002989 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002990 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002991 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002992 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002993 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2994 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002995 return;
2996 }
2997
2998 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002999 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003000 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003001 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3002 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003003 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003004 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003005 return;
3006 }
3007
3008 // check if the function is variadic if the 3rd argument non-zero
3009 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003010 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003011 ++NumArgs; // +1 for ...
3012 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003013 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003014 return;
3015 }
3016 }
3017
Chris Lattner3c73c412008-11-19 08:23:25 +00003018 // strftime requires FirstArg to be 0 because it doesn't read from any
3019 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003020 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003021 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003022 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3023 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003024 return;
3025 }
3026 // if 0 it disables parameter checking (to use with e.g. va_list)
3027 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003029 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003030 return;
3031 }
3032
Rafael Espindola599f1b72012-05-13 03:25:18 +00003033 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3034 Idx.getZExtValue(),
3035 FirstArg.getZExtValue());
3036 if (NewAttr)
3037 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003038}
3039
Chandler Carruth1b03c872011-07-02 00:01:44 +00003040static void handleTransparentUnionAttr(Sema &S, Decl *D,
3041 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003042 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003043 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003044 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003045
Chris Lattner6b6b5372008-06-26 18:38:35 +00003046
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003047 // Try to find the underlying union declaration.
3048 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003049 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003050 if (TD && TD->getUnderlyingType()->isUnionType())
3051 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3052 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003053 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003054
3055 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003056 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003057 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003058 return;
3059 }
3060
John McCall5e1cdac2011-10-07 06:10:15 +00003061 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003062 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003063 diag::warn_transparent_union_attribute_not_definition);
3064 return;
3065 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003066
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003067 RecordDecl::field_iterator Field = RD->field_begin(),
3068 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003069 if (Field == FieldEnd) {
3070 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3071 return;
3072 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003073
David Blaikie581deb32012-06-06 20:45:41 +00003074 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003075 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003076 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003077 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003078 diag::warn_transparent_union_attribute_floating)
3079 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003080 return;
3081 }
3082
3083 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3084 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3085 for (; Field != FieldEnd; ++Field) {
3086 QualType FieldType = Field->getType();
3087 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3088 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3089 // Warn if we drop the attribute.
3090 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003091 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003092 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003093 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003094 diag::warn_transparent_union_attribute_field_size_align)
3095 << isSize << Field->getDeclName() << FieldBits;
3096 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003097 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003098 diag::note_transparent_union_first_field_size_align)
3099 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003100 return;
3101 }
3102 }
3103
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003104 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003105}
3106
Chandler Carruth1b03c872011-07-02 00:01:44 +00003107static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003108 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003109 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003110 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003111
Peter Collingbourne7a730022010-11-23 20:45:58 +00003112 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003113 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003114
Chris Lattner6b6b5372008-06-26 18:38:35 +00003115 // Make sure that there is a string literal as the annotation's single
3116 // argument.
3117 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003118 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003119 return;
3120 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003121
3122 // Don't duplicate annotations that are already set.
3123 for (specific_attr_iterator<AnnotateAttr>
3124 i = D->specific_attr_begin<AnnotateAttr>(),
3125 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3126 if ((*i)->getAnnotation() == SE->getString())
3127 return;
3128 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003129 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003130 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131}
3132
Chandler Carruth1b03c872011-07-02 00:01:44 +00003133static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003135 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003136 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003137 return;
3138 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003139
Sean Huntbbd37c62009-11-21 08:43:09 +00003140 //FIXME: The C++0x version of this attribute has more limited applicabilty
3141 // than GNU's, and should error out when it is used to specify a
3142 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003143
Chris Lattner545dd342008-06-28 23:36:30 +00003144 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003145 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3146 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003147 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003148 }
Mike Stumpbf916502009-07-24 19:02:52 +00003149
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003150 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3151 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003152}
3153
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003154void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3155 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003156 // FIXME: Handle pack-expansions here.
3157 if (DiagnoseUnexpandedParameterPack(E))
3158 return;
3159
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003160 if (E->isTypeDependent() || E->isValueDependent()) {
3161 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003162 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3163 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003164 return;
3165 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003166
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003167 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003168 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003169 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003170 ExprResult ICE
3171 = VerifyIntegerConstantExpression(E, &Alignment,
3172 diag::err_aligned_attribute_argument_not_int,
3173 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003174 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003175 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003176 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003177 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3178 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003179 return;
3180 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003181 if (isDeclSpec) {
3182 // We've already verified it's a power of 2, now let's make sure it's
3183 // 8192 or less.
3184 if (Alignment.getZExtValue() > 8192) {
3185 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3186 << E->getSourceRange();
3187 return;
3188 }
3189 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003190
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003191 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3192 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003193}
3194
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003195void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3196 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003197 // FIXME: Cache the number on the Attr object if non-dependent?
3198 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003199 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3200 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003201 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003202}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003203
Chandler Carruthd309c812011-07-01 23:49:16 +00003204/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003205/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003206///
Mike Stumpbf916502009-07-24 19:02:52 +00003207/// Despite what would be logical, the mode attribute is a decl attribute, not a
3208/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3209/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003210static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003211 // This attribute isn't documented, but glibc uses it. It changes
3212 // the width of an int or unsigned int to the specified size.
3213
3214 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003215 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003216 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003217
Chris Lattnerfbf13472008-06-27 22:18:37 +00003218
3219 IdentifierInfo *Name = Attr.getParameterName();
3220 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003221 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003222 return;
3223 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003224
Chris Lattner5f9e2722011-07-23 10:55:15 +00003225 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003226
3227 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003228 if (Str.startswith("__") && Str.endswith("__"))
3229 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003230
3231 unsigned DestWidth = 0;
3232 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003233 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003234 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003235 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003236 switch (Str[0]) {
3237 case 'Q': DestWidth = 8; break;
3238 case 'H': DestWidth = 16; break;
3239 case 'S': DestWidth = 32; break;
3240 case 'D': DestWidth = 64; break;
3241 case 'X': DestWidth = 96; break;
3242 case 'T': DestWidth = 128; break;
3243 }
3244 if (Str[1] == 'F') {
3245 IntegerMode = false;
3246 } else if (Str[1] == 'C') {
3247 IntegerMode = false;
3248 ComplexMode = true;
3249 } else if (Str[1] != 'I') {
3250 DestWidth = 0;
3251 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003252 break;
3253 case 4:
3254 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3255 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003256 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003257 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003258 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003259 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003260 break;
3261 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003262 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003263 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003264 break;
3265 }
3266
3267 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003268 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003269 OldTy = TD->getUnderlyingType();
3270 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3271 OldTy = VD->getType();
3272 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003273 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003274 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003275 return;
3276 }
Eli Friedman73397492009-03-03 06:41:03 +00003277
John McCall183700f2009-09-21 23:43:11 +00003278 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003279 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3280 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003281 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003282 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3283 } else if (ComplexMode) {
3284 if (!OldTy->isComplexType())
3285 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3286 } else {
3287 if (!OldTy->isFloatingType())
3288 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3289 }
3290
Mike Stump390b4cc2009-05-16 07:39:55 +00003291 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3292 // and friends, at least with glibc.
3293 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3294 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003295 // FIXME: Make sure floating-point mappings are accurate
3296 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003297 QualType NewTy;
3298 switch (DestWidth) {
3299 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003300 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003301 return;
3302 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003303 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003304 return;
3305 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003306 if (!IntegerMode) {
3307 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3308 return;
3309 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003310 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003311 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003312 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003313 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003314 break;
3315 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003316 if (!IntegerMode) {
3317 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3318 return;
3319 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003320 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003321 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003322 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003323 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003324 break;
3325 case 32:
3326 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003327 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003328 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003329 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003330 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003331 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003332 break;
3333 case 64:
3334 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003335 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003336 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003337 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003338 NewTy = S.Context.LongTy;
3339 else
3340 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003341 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003342 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003343 NewTy = S.Context.UnsignedLongTy;
3344 else
3345 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003346 break;
Eli Friedman73397492009-03-03 06:41:03 +00003347 case 96:
3348 NewTy = S.Context.LongDoubleTy;
3349 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003350 case 128:
3351 if (!IntegerMode) {
3352 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3353 return;
3354 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003355 if (OldTy->isSignedIntegerType())
3356 NewTy = S.Context.Int128Ty;
3357 else
3358 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003359 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003360 }
3361
Eli Friedman73397492009-03-03 06:41:03 +00003362 if (ComplexMode) {
3363 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003364 }
3365
3366 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003367 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003368 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003369 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003370 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003371 cast<ValueDecl>(D)->setType(NewTy);
3372}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003373
Chandler Carruth1b03c872011-07-02 00:01:44 +00003374static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003375 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003376 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003377 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003378
Nick Lewycky78d1a102012-07-24 01:40:49 +00003379 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3380 if (!VD->hasGlobalStorage())
3381 S.Diag(Attr.getLoc(),
3382 diag::warn_attribute_requires_functions_or_static_globals)
3383 << Attr.getName();
3384 } else if (!isFunctionOrMethod(D)) {
3385 S.Diag(Attr.getLoc(),
3386 diag::warn_attribute_requires_functions_or_static_globals)
3387 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003388 return;
3389 }
Mike Stumpbf916502009-07-24 19:02:52 +00003390
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003391 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003392}
3393
Chandler Carruth1b03c872011-07-02 00:01:44 +00003394static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003395 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003396 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003397 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003398
Mike Stumpbf916502009-07-24 19:02:52 +00003399
Chandler Carruth87c44602011-07-01 23:49:12 +00003400 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003401 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003402 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003403 return;
3404 }
Mike Stumpbf916502009-07-24 19:02:52 +00003405
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003406 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003407}
3408
Chandler Carruth1b03c872011-07-02 00:01:44 +00003409static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3410 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003411 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003412 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003413 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003414
Chris Lattner7255a2d2010-06-22 00:03:40 +00003415
Chandler Carruth87c44602011-07-01 23:49:12 +00003416 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003417 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003418 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003419 return;
3420 }
3421
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003422 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003423 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003424}
3425
Chandler Carruth1b03c872011-07-02 00:01:44 +00003426static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003427 if (S.LangOpts.CUDA) {
3428 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003429 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3431 return;
3432 }
3433
Chandler Carruth87c44602011-07-01 23:49:12 +00003434 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003436 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003437 return;
3438 }
3439
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003440 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003441 } else {
3442 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3443 }
3444}
3445
Chandler Carruth1b03c872011-07-02 00:01:44 +00003446static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003447 if (S.LangOpts.CUDA) {
3448 // check the attribute arguments.
3449 if (Attr.getNumArgs() != 0) {
3450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3451 return;
3452 }
3453
Chandler Carruth87c44602011-07-01 23:49:12 +00003454 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003455 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003456 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003457 return;
3458 }
3459
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003460 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003461 } else {
3462 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3463 }
3464}
3465
Chandler Carruth1b03c872011-07-02 00:01:44 +00003466static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003467 if (S.LangOpts.CUDA) {
3468 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003469 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003470 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003471
Chandler Carruth87c44602011-07-01 23:49:12 +00003472 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003473 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003474 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003475 return;
3476 }
3477
Chandler Carruth87c44602011-07-01 23:49:12 +00003478 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003479 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003480 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003481 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3482 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3483 << FD->getType()
3484 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3485 "void");
3486 } else {
3487 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3488 << FD->getType();
3489 }
3490 return;
3491 }
3492
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003493 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003494 } else {
3495 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3496 }
3497}
3498
Chandler Carruth1b03c872011-07-02 00:01:44 +00003499static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003500 if (S.LangOpts.CUDA) {
3501 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003502 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003503 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003504
Peter Collingbourneced76712010-12-01 03:15:31 +00003505
Chandler Carruth87c44602011-07-01 23:49:12 +00003506 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003507 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003508 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003509 return;
3510 }
3511
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003512 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003513 } else {
3514 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3515 }
3516}
3517
Chandler Carruth1b03c872011-07-02 00:01:44 +00003518static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003519 if (S.LangOpts.CUDA) {
3520 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003521 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003522 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003523
Peter Collingbourneced76712010-12-01 03:15:31 +00003524
Chandler Carruth87c44602011-07-01 23:49:12 +00003525 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003526 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003527 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003528 return;
3529 }
3530
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003531 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003532 } else {
3533 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3534 }
3535}
3536
Chandler Carruth1b03c872011-07-02 00:01:44 +00003537static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003538 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003539 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003540 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003541
Chandler Carruth87c44602011-07-01 23:49:12 +00003542 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003543 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003544 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003545 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003546 return;
3547 }
Mike Stumpbf916502009-07-24 19:02:52 +00003548
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003549 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003550 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003551 return;
3552 }
Mike Stumpbf916502009-07-24 19:02:52 +00003553
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003554 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003555}
3556
Chandler Carruth1b03c872011-07-02 00:01:44 +00003557static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003558 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003559
Chandler Carruth87c44602011-07-01 23:49:12 +00003560 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003561 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3562 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003563 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003564 return;
3565
Chandler Carruth87c44602011-07-01 23:49:12 +00003566 if (!isa<ObjCMethodDecl>(D)) {
3567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3568 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003569 return;
3570 }
3571
Chandler Carruth87c44602011-07-01 23:49:12 +00003572 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003573 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003574 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003575 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003576 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003577 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003578 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003579 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003580 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003581 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003582 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003583 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003584 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003585 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003586 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003587 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003588 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003589 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003590 switch (CC) {
3591 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003592 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003593 break;
3594 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003595 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003596 break;
3597 default:
3598 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003599 }
3600
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003601 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuff263366f2012-10-16 22:30:41 +00003602 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003603 }
Derek Schuff263366f2012-10-16 22:30:41 +00003604 case AttributeList::AT_PnaclCall:
3605 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3606 return;
3607
Abramo Bagnarae215f722010-04-30 13:10:51 +00003608 default:
3609 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003610 }
3611}
3612
Chandler Carruth1b03c872011-07-02 00:01:44 +00003613static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003614 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003615 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003616}
3617
John McCall711c52b2011-01-05 12:14:39 +00003618bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3619 if (attr.isInvalid())
3620 return true;
3621
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003622 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3623 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3624 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003625 attr.setInvalid();
3626 return true;
3627 }
3628
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003629 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3630 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003631 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003632 case AttributeList::AT_CDecl: CC = CC_C; break;
3633 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3634 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3635 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3636 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3637 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003638 Expr *Arg = attr.getArg(0);
3639 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003640 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003641 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3642 << "pcs" << 1;
3643 attr.setInvalid();
3644 return true;
3645 }
3646
Chris Lattner5f9e2722011-07-23 10:55:15 +00003647 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003648 if (StrRef == "aapcs") {
3649 CC = CC_AAPCS;
3650 break;
3651 } else if (StrRef == "aapcs-vfp") {
3652 CC = CC_AAPCS_VFP;
3653 break;
3654 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003655
3656 attr.setInvalid();
3657 Diag(attr.getLoc(), diag::err_invalid_pcs);
3658 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003659 }
Derek Schuff263366f2012-10-16 22:30:41 +00003660 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
David Blaikie7530c032012-01-17 06:56:22 +00003661 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003662 }
3663
Aaron Ballman82bfa192012-10-02 14:26:08 +00003664 const TargetInfo &TI = Context.getTargetInfo();
3665 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3666 if (A == TargetInfo::CCCR_Warning) {
3667 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
3668 CC = TI.getDefaultCallingConv();
3669 }
3670
John McCall711c52b2011-01-05 12:14:39 +00003671 return false;
3672}
3673
Chandler Carruth1b03c872011-07-02 00:01:44 +00003674static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003675 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003676
3677 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003678 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003679 return;
3680
Chandler Carruth87c44602011-07-01 23:49:12 +00003681 if (!isa<ObjCMethodDecl>(D)) {
3682 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3683 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003684 return;
3685 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003686
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003687 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003688}
3689
3690/// Checks a regparm attribute, returning true if it is ill-formed and
3691/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003692bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3693 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003694 return true;
3695
Chandler Carruth87c44602011-07-01 23:49:12 +00003696 if (Attr.getNumArgs() != 1) {
3697 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3698 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003699 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003700 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003701
Chandler Carruth87c44602011-07-01 23:49:12 +00003702 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003703 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003704 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003705 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003706 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003707 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003708 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003709 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003710 }
3711
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003712 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003713 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003714 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003715 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003716 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003717 }
3718
John McCall711c52b2011-01-05 12:14:39 +00003719 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003720 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003721 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003722 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003723 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003724 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003725 }
3726
John McCall711c52b2011-01-05 12:14:39 +00003727 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003728}
3729
Chandler Carruth1b03c872011-07-02 00:01:44 +00003730static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003731 if (S.LangOpts.CUDA) {
3732 // check the attribute arguments.
3733 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003734 // FIXME: 0 is not okay.
3735 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003736 return;
3737 }
3738
Chandler Carruth87c44602011-07-01 23:49:12 +00003739 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003741 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003742 return;
3743 }
3744
3745 Expr *MaxThreadsExpr = Attr.getArg(0);
3746 llvm::APSInt MaxThreads(32);
3747 if (MaxThreadsExpr->isTypeDependent() ||
3748 MaxThreadsExpr->isValueDependent() ||
3749 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3750 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3751 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3752 return;
3753 }
3754
3755 llvm::APSInt MinBlocks(32);
3756 if (Attr.getNumArgs() > 1) {
3757 Expr *MinBlocksExpr = Attr.getArg(1);
3758 if (MinBlocksExpr->isTypeDependent() ||
3759 MinBlocksExpr->isValueDependent() ||
3760 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3761 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3762 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3763 return;
3764 }
3765 }
3766
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003767 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003768 MaxThreads.getZExtValue(),
3769 MinBlocks.getZExtValue()));
3770 } else {
3771 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3772 }
3773}
3774
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003775static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3776 const AttributeList &Attr) {
3777 StringRef AttrName = Attr.getName()->getName();
3778 if (!Attr.getParameterName()) {
3779 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3780 << Attr.getName() << /* arg num = */ 1;
3781 return;
3782 }
3783
3784 if (Attr.getNumArgs() != 2) {
3785 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3786 << /* required args = */ 3;
3787 return;
3788 }
3789
3790 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3791
3792 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3793 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3794 << Attr.getName() << ExpectedFunctionOrMethod;
3795 return;
3796 }
3797
3798 uint64_t ArgumentIdx;
3799 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3800 Attr.getLoc(), 2,
3801 Attr.getArg(0), ArgumentIdx))
3802 return;
3803
3804 uint64_t TypeTagIdx;
3805 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3806 Attr.getLoc(), 3,
3807 Attr.getArg(1), TypeTagIdx))
3808 return;
3809
3810 bool IsPointer = (AttrName == "pointer_with_type_tag");
3811 if (IsPointer) {
3812 // Ensure that buffer has a pointer type.
3813 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3814 if (!BufferTy->isPointerType()) {
3815 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3816 << AttrName;
3817 }
3818 }
3819
3820 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3821 S.Context,
3822 ArgumentKind,
3823 ArgumentIdx,
3824 TypeTagIdx,
3825 IsPointer));
3826}
3827
3828static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3829 const AttributeList &Attr) {
3830 IdentifierInfo *PointerKind = Attr.getParameterName();
3831 if (!PointerKind) {
3832 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3833 << "type_tag_for_datatype" << 1;
3834 return;
3835 }
3836
3837 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3838
3839 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3840 Attr.getRange(),
3841 S.Context,
3842 PointerKind,
3843 MatchingCType,
3844 Attr.getLayoutCompatible(),
3845 Attr.getMustBeNull()));
3846}
3847
Chris Lattner0744e5f2008-06-29 00:23:49 +00003848//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003849// Checker-specific attribute handlers.
3850//===----------------------------------------------------------------------===//
3851
John McCallc7ad3812011-01-25 03:31:58 +00003852static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003853 return type->isDependentType() ||
3854 type->isObjCObjectPointerType() ||
3855 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003856}
3857static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003858 return type->isDependentType() ||
3859 type->isPointerType() ||
3860 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003861}
3862
Chandler Carruth1b03c872011-07-02 00:01:44 +00003863static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003864 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003865 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003866 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003867 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003868 return;
3869 }
3870
3871 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003872 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003873 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3874 cf = false;
3875 } else {
3876 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3877 cf = true;
3878 }
3879
3880 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003881 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003882 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003883 return;
3884 }
3885
3886 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003887 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003888 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003889 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003890}
3891
Chandler Carruth1b03c872011-07-02 00:01:44 +00003892static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3893 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003894 if (!isa<ObjCMethodDecl>(D)) {
3895 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003896 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003897 return;
3898 }
3899
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003900 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003901}
3902
Chandler Carruth1b03c872011-07-02 00:01:44 +00003903static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3904 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003905
John McCallc7ad3812011-01-25 03:31:58 +00003906 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003907
Chandler Carruth87c44602011-07-01 23:49:12 +00003908 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003909 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003910 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003911 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003912 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003913 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3914 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003915 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003916 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003917 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003918 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003919 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003920 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003921 return;
3922 }
Mike Stumpbf916502009-07-24 19:02:52 +00003923
John McCallc7ad3812011-01-25 03:31:58 +00003924 bool typeOK;
3925 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003926 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003927 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003928 case AttributeList::AT_NSReturnsAutoreleased:
3929 case AttributeList::AT_NSReturnsRetained:
3930 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003931 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3932 cf = false;
3933 break;
3934
Sean Hunt8e083e72012-06-19 23:57:03 +00003935 case AttributeList::AT_CFReturnsRetained:
3936 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003937 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3938 cf = true;
3939 break;
3940 }
3941
3942 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003943 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003944 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003945 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003946 }
Mike Stumpbf916502009-07-24 19:02:52 +00003947
Chandler Carruth87c44602011-07-01 23:49:12 +00003948 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003949 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003950 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003951 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003952 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003953 S.Context));
3954 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003955 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003956 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003957 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003958 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003959 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003960 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003961 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003962 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003963 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003964 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003965 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003966 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003967 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003968 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003969 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003970 return;
3971 };
3972}
3973
John McCalldc7c5ad2011-07-22 08:53:00 +00003974static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3975 const AttributeList &attr) {
3976 SourceLocation loc = attr.getLoc();
3977
3978 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3979
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003980 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003981 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003982 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003983 return;
3984 }
3985
3986 // Check that the method returns a normal pointer.
3987 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003988
3989 if (!resultType->isReferenceType() &&
3990 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003991 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3992 << SourceRange(loc)
3993 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3994
3995 // Drop the attribute.
3996 return;
3997 }
3998
3999 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004000 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00004001}
4002
Fariborz Jahanian84101132012-09-07 23:46:23 +00004003static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4004 const AttributeList &attr) {
4005 SourceLocation loc = attr.getLoc();
4006 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4007
4008 if (!method) {
4009 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4010 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4011 return;
4012 }
4013 DeclContext *DC = method->getDeclContext();
4014 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4015 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4016 << attr.getName() << 0;
4017 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4018 return;
4019 }
4020 if (method->getMethodFamily() == OMF_dealloc) {
4021 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4022 << attr.getName() << 1;
4023 return;
4024 }
4025
4026 method->addAttr(
4027 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4028}
4029
John McCall8dfac0b2011-09-30 05:12:12 +00004030/// Handle cf_audited_transfer and cf_unknown_transfer.
4031static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4032 if (!isa<FunctionDecl>(D)) {
4033 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004034 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004035 return;
4036 }
4037
Sean Hunt8e083e72012-06-19 23:57:03 +00004038 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004039
4040 // Check whether there's a conflicting attribute already present.
4041 Attr *Existing;
4042 if (IsAudited) {
4043 Existing = D->getAttr<CFUnknownTransferAttr>();
4044 } else {
4045 Existing = D->getAttr<CFAuditedTransferAttr>();
4046 }
4047 if (Existing) {
4048 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4049 << A.getName()
4050 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4051 << A.getRange() << Existing->getRange();
4052 return;
4053 }
4054
4055 // All clear; add the attribute.
4056 if (IsAudited) {
4057 D->addAttr(
4058 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4059 } else {
4060 D->addAttr(
4061 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4062 }
4063}
4064
John McCallfe98da02011-09-29 07:17:38 +00004065static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4066 const AttributeList &Attr) {
4067 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4068 if (!RD || RD->isUnion()) {
4069 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004070 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004071 }
4072
4073 IdentifierInfo *ParmName = Attr.getParameterName();
4074
4075 // In Objective-C, verify that the type names an Objective-C type.
4076 // We don't want to check this outside of ObjC because people sometimes
4077 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004078 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004079 // Check for an existing type with this name.
4080 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4081 Sema::LookupOrdinaryName);
4082 if (S.LookupName(R, Sc)) {
4083 NamedDecl *Target = R.getFoundDecl();
4084 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4085 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4086 S.Diag(Target->getLocStart(), diag::note_declared_at);
4087 }
4088 }
4089 }
4090
4091 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4092 ParmName));
4093}
4094
Chandler Carruth1b03c872011-07-02 00:01:44 +00004095static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4096 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004097 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004098
Chandler Carruth87c44602011-07-01 23:49:12 +00004099 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004100 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004101}
4102
Chandler Carruth1b03c872011-07-02 00:01:44 +00004103static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4104 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004105 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004106 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004107 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004108 return;
4109 }
4110
Chandler Carruth87c44602011-07-01 23:49:12 +00004111 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004112 QualType type = vd->getType();
4113
4114 if (!type->isDependentType() &&
4115 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004116 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004117 << type;
4118 return;
4119 }
4120
4121 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4122
4123 // If we have no lifetime yet, check the lifetime we're presumably
4124 // going to infer.
4125 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4126 lifetime = type->getObjCARCImplicitLifetime();
4127
4128 switch (lifetime) {
4129 case Qualifiers::OCL_None:
4130 assert(type->isDependentType() &&
4131 "didn't infer lifetime for non-dependent type?");
4132 break;
4133
4134 case Qualifiers::OCL_Weak: // meaningful
4135 case Qualifiers::OCL_Strong: // meaningful
4136 break;
4137
4138 case Qualifiers::OCL_ExplicitNone:
4139 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004140 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004141 << (lifetime == Qualifiers::OCL_Autoreleasing);
4142 break;
4143 }
4144
Chandler Carruth87c44602011-07-01 23:49:12 +00004145 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004146 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004147}
4148
Francois Pichet11542142010-12-19 06:50:37 +00004149//===----------------------------------------------------------------------===//
4150// Microsoft specific attribute handlers.
4151//===----------------------------------------------------------------------===//
4152
Chandler Carruth1b03c872011-07-02 00:01:44 +00004153static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004154 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004155 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004156 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004157 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004158
Francois Pichet11542142010-12-19 06:50:37 +00004159 Expr *Arg = Attr.getArg(0);
4160 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004161 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4163 << "uuid" << 1;
4164 return;
4165 }
4166
Chris Lattner5f9e2722011-07-23 10:55:15 +00004167 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004168
4169 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4170 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004171
Francois Pichetd3d3be92010-12-20 01:41:49 +00004172 // Validate GUID length.
4173 if (IsCurly && StrRef.size() != 38) {
4174 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4175 return;
4176 }
4177 if (!IsCurly && StrRef.size() != 36) {
4178 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4179 return;
4180 }
4181
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004182 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004183 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004184 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004185 if (IsCurly) // Skip the optional '{'
4186 ++I;
4187
4188 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004189 if (i == 8 || i == 13 || i == 18 || i == 23) {
4190 if (*I != '-') {
4191 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4192 return;
4193 }
4194 } else if (!isxdigit(*I)) {
4195 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4196 return;
4197 }
4198 I++;
4199 }
Francois Pichet11542142010-12-19 06:50:37 +00004200
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004201 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004202 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004203 } else
Francois Pichet11542142010-12-19 06:50:37 +00004204 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004205}
4206
John McCallc052dbb2012-05-22 21:28:12 +00004207static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004208 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004209 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004210 return;
4211 }
4212
4213 AttributeList::Kind Kind = Attr.getKind();
4214 if (Kind == AttributeList::AT_SingleInheritance)
4215 D->addAttr(
4216 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4217 else if (Kind == AttributeList::AT_MultipleInheritance)
4218 D->addAttr(
4219 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4220 else if (Kind == AttributeList::AT_VirtualInheritance)
4221 D->addAttr(
4222 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
John McCallc052dbb2012-05-22 21:28:12 +00004223}
4224
4225static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4226 if (S.LangOpts.MicrosoftExt) {
4227 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004228 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004229 D->addAttr(
4230 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004231 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004232 D->addAttr(
4233 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004234 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004235 D->addAttr(
4236 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4237 } else
4238 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4239}
4240
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004241static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4242 if (S.LangOpts.MicrosoftExt)
4243 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4244 else
4245 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4246}
4247
Ted Kremenekb71368d2009-05-09 02:44:38 +00004248//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004249// Top Level Sema Entry Points
4250//===----------------------------------------------------------------------===//
4251
Chandler Carruth1b03c872011-07-02 00:01:44 +00004252static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4253 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004254 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004255 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4256 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4257 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004258 default:
4259 break;
4260 }
4261}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004262
Chandler Carruth1b03c872011-07-02 00:01:44 +00004263static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4264 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004265 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004266 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4267 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4268 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004269 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004270 case AttributeList::AT_AddressSpace:
4271 case AttributeList::AT_OpenCLImageAccess:
4272 case AttributeList::AT_ObjCGC:
4273 case AttributeList::AT_VectorSize:
4274 case AttributeList::AT_NeonVectorType:
4275 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004276 // Ignore these, these are type attributes, handled by
4277 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004278 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004279 case AttributeList::AT_CUDADevice:
4280 case AttributeList::AT_CUDAHost:
4281 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004282 // Ignore, this is a non-inheritable attribute, handled
4283 // by ProcessNonInheritableDeclAttr.
4284 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004285 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4286 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4287 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4288 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004289 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004290 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004291 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004292 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004293 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4294 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4295 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004296 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004297 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4298 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4299 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4300 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004301 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4302 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004303 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4304 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004305 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004306 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004307 case AttributeList::AT_MinSize:
4308 handleMinSizeAttr(S, D, Attr);
4309 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004310 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4311 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4312 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4313 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4314 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004315 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004316 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004317 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4318 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4319 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4320 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4321 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004322 case AttributeList::AT_ownership_returns:
4323 case AttributeList::AT_ownership_takes:
4324 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004325 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004326 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4327 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4328 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4329 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4330 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4331 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4332 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004333
Sean Hunt8e083e72012-06-19 23:57:03 +00004334 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004335 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004336 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004337 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004338
Sean Hunt8e083e72012-06-19 23:57:03 +00004339 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004340 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4341
Fariborz Jahanian84101132012-09-07 23:46:23 +00004342 case AttributeList::AT_ObjCRequiresSuper:
4343 handleObjCRequiresSuperAttr(S, D, Attr); break;
4344
Sean Hunt8e083e72012-06-19 23:57:03 +00004345 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004346 handleNSBridgedAttr(S, scope, D, Attr); break;
4347
Sean Hunt8e083e72012-06-19 23:57:03 +00004348 case AttributeList::AT_CFAuditedTransfer:
4349 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004350 handleCFTransferAttr(S, D, Attr); break;
4351
Ted Kremenekb71368d2009-05-09 02:44:38 +00004352 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004353 case AttributeList::AT_CFConsumed:
4354 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4355 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004356 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004357
Sean Hunt8e083e72012-06-19 23:57:03 +00004358 case AttributeList::AT_NSReturnsAutoreleased:
4359 case AttributeList::AT_NSReturnsNotRetained:
4360 case AttributeList::AT_CFReturnsNotRetained:
4361 case AttributeList::AT_NSReturnsRetained:
4362 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004363 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004364
Tanya Lattner0df579e2012-07-09 22:06:01 +00004365 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004366 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004367 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004368
Sean Hunt8e083e72012-06-19 23:57:03 +00004369 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004370 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004371
Sean Hunt8e083e72012-06-19 23:57:03 +00004372 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4373 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4374 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004375 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4376 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004377 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004378 handleArcWeakrefUnavailableAttr (S, D, Attr);
4379 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004380 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004381 handleObjCRootClassAttr(S, D, Attr);
4382 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004383 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004384 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004385 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004386 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4387 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004388 handleReturnsTwiceAttr(S, D, Attr);
4389 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004390 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4391 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4392 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004393 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4395 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4396 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4397 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004398 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004399 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004400 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004401 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004402 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004403 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004404 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004405 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004406 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4407 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4408 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4409 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4410 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4411 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4412 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4413 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4414 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004415 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004416 // Just ignore
4417 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004418 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004419 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004420 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004421 case AttributeList::AT_StdCall:
4422 case AttributeList::AT_CDecl:
4423 case AttributeList::AT_FastCall:
4424 case AttributeList::AT_ThisCall:
4425 case AttributeList::AT_Pascal:
4426 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004427 case AttributeList::AT_PnaclCall:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004428 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004429 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004430 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004431 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004432 break;
John McCallc052dbb2012-05-22 21:28:12 +00004433
4434 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004435 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004436 handleMsStructAttr(S, D, Attr);
4437 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004438 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004439 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004440 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004441 case AttributeList::AT_SingleInheritance:
4442 case AttributeList::AT_MultipleInheritance:
4443 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004444 handleInheritanceAttr(S, D, Attr);
4445 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004446 case AttributeList::AT_Win64:
4447 case AttributeList::AT_Ptr32:
4448 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004449 handlePortabilityAttr(S, D, Attr);
4450 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004451 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004452 handleForceInlineAttr(S, D, Attr);
4453 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004454
4455 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004456 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004457 handleGuardedVarAttr(S, D, Attr);
4458 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004459 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004460 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004461 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004462 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004463 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004464 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004465 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004466 handleNoAddressSafetyAttr(S, D, Attr);
4467 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004468 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004469 handleNoThreadSafetyAttr(S, D, Attr);
4470 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004471 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004472 handleLockableAttr(S, D, Attr);
4473 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004474 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004475 handleGuardedByAttr(S, D, Attr);
4476 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004477 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004478 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004479 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004480 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004481 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004482 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004483 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004484 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004485 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004486 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004487 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004488 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004489 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004490 handleLockReturnedAttr(S, D, Attr);
4491 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004492 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004493 handleLocksExcludedAttr(S, D, Attr);
4494 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004495 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004496 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004497 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004498 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004499 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004500 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004501 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004502 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004503 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004504 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004505 handleUnlockFunAttr(S, D, Attr);
4506 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004507 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004508 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004509 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004510 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004511 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004512 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004513
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004514 // Type safety attributes.
4515 case AttributeList::AT_ArgumentWithTypeTag:
4516 handleArgumentWithTypeTagAttr(S, D, Attr);
4517 break;
4518 case AttributeList::AT_TypeTagForDatatype:
4519 handleTypeTagForDatatypeAttr(S, D, Attr);
4520 break;
4521
Chris Lattner803d0802008-06-29 00:43:07 +00004522 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004523 // Ask target about the attribute.
4524 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4525 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004526 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4527 diag::warn_unhandled_ms_attribute_ignored :
4528 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004529 break;
4530 }
4531}
4532
Peter Collingbourne60700392011-01-21 02:08:45 +00004533/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4534/// the attribute applies to decls. If the attribute is a type attribute, just
4535/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4536/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004537static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4538 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004539 bool NonInheritable, bool Inheritable) {
4540 if (Attr.isInvalid())
4541 return;
4542
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004543 // Type attributes are still treated as declaration attributes by
4544 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4545 // want to process them, however, because we will simply warn about ignoring
4546 // them. So instead, we will bail out early.
4547 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004548 return;
4549
4550 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004551 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004552
4553 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004554 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004555}
4556
Chris Lattner803d0802008-06-29 00:43:07 +00004557/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4558/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004559void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004560 const AttributeList *AttrList,
4561 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004562 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004563 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004564 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004565
4566 // GCC accepts
4567 // static int a9 __attribute__((weakref));
4568 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004569 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004570 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004571 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004572 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004573 }
4574}
4575
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004576// Annotation attributes are the only attributes allowed after an access
4577// specifier.
4578bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4579 const AttributeList *AttrList) {
4580 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004581 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004582 handleAnnotateAttr(*this, ASDecl, *l);
4583 } else {
4584 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4585 return true;
4586 }
4587 }
4588
4589 return false;
4590}
4591
John McCalle82247a2011-10-01 05:17:03 +00004592/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4593/// contains any decl attributes that we should warn about.
4594static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4595 for ( ; A; A = A->getNext()) {
4596 // Only warn if the attribute is an unignored, non-type attribute.
4597 if (A->isUsedAsTypeAttr()) continue;
4598 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4599
4600 if (A->getKind() == AttributeList::UnknownAttribute) {
4601 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4602 << A->getName() << A->getRange();
4603 } else {
4604 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4605 << A->getName() << A->getRange();
4606 }
4607 }
4608}
4609
4610/// checkUnusedDeclAttributes - Given a declarator which is not being
4611/// used to build a declaration, complain about any decl attributes
4612/// which might be lying around on it.
4613void Sema::checkUnusedDeclAttributes(Declarator &D) {
4614 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4615 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4616 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4617 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4618}
4619
Ryan Flynne25ff832009-07-30 03:15:39 +00004620/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004621/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004622NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4623 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004624 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004625 NamedDecl *NewD = 0;
4626 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004627 FunctionDecl *NewFD;
4628 // FIXME: Missing call to CheckFunctionDeclaration().
4629 // FIXME: Mangling?
4630 // FIXME: Is the qualifier info correct?
4631 // FIXME: Is the DeclContext correct?
4632 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4633 Loc, Loc, DeclarationName(II),
4634 FD->getType(), FD->getTypeSourceInfo(),
4635 SC_None, SC_None,
4636 false/*isInlineSpecified*/,
4637 FD->hasPrototype(),
4638 false/*isConstexprSpecified*/);
4639 NewD = NewFD;
4640
4641 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004642 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004643
4644 // Fake up parameter variables; they are declared as if this were
4645 // a typedef.
4646 QualType FDTy = FD->getType();
4647 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4648 SmallVector<ParmVarDecl*, 16> Params;
4649 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4650 AE = FT->arg_type_end(); AI != AE; ++AI) {
4651 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4652 Param->setScopeInfo(0, Params.size());
4653 Params.push_back(Param);
4654 }
David Blaikie4278c652011-09-21 18:16:56 +00004655 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004656 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004657 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4658 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004659 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004660 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004661 VD->getStorageClass(),
4662 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004663 if (VD->getQualifier()) {
4664 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004665 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004666 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004667 }
4668 return NewD;
4669}
4670
James Dennett1dfbd922012-06-14 21:40:34 +00004671/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004672/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004673void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004674 if (W.getUsed()) return; // only do this once
4675 W.setUsed(true);
4676 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4677 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004678 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004679 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4680 NDId->getName()));
4681 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004682 WeakTopLevelDecl.push_back(NewD);
4683 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4684 // to insert Decl at TU scope, sorry.
4685 DeclContext *SavedContext = CurContext;
4686 CurContext = Context.getTranslationUnitDecl();
4687 PushOnScopeChains(NewD, S);
4688 CurContext = SavedContext;
4689 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004690 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004691 }
4692}
4693
Chris Lattner0744e5f2008-06-29 00:23:49 +00004694/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4695/// it, apply them to D. This is a bit tricky because PD can have attributes
4696/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004697void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4698 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004699 // It's valid to "forward-declare" #pragma weak, in which case we
4700 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004701 if (Inheritable) {
4702 LoadExternalWeakUndeclaredIdentifiers();
4703 if (!WeakUndeclaredIdentifiers.empty()) {
4704 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4705 if (IdentifierInfo *Id = ND->getIdentifier()) {
4706 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4707 = WeakUndeclaredIdentifiers.find(Id);
4708 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4709 WeakInfo W = I->second;
4710 DeclApplyPragmaWeak(S, ND, W);
4711 WeakUndeclaredIdentifiers[Id] = W;
4712 }
John McCalld4aff0e2010-10-27 00:59:00 +00004713 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004714 }
4715 }
4716 }
4717
Chris Lattner0744e5f2008-06-29 00:23:49 +00004718 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004719 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004720 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004721
Chris Lattner0744e5f2008-06-29 00:23:49 +00004722 // Walk the declarator structure, applying decl attributes that were in a type
4723 // position to the decl itself. This handles cases like:
4724 // int *__attr__(x)** D;
4725 // when X is a decl attribute.
4726 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4727 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004728 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004729
Chris Lattner0744e5f2008-06-29 00:23:49 +00004730 // Finally, apply any attributes on the decl itself.
4731 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004732 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004733}
John McCall54abf7d2009-11-04 02:18:39 +00004734
John McCallf85e1932011-06-15 23:02:42 +00004735/// Is the given declaration allowed to use a forbidden type?
4736static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4737 // Private ivars are always okay. Unfortunately, people don't
4738 // always properly make their ivars private, even in system headers.
4739 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004740 // Function declarations in sys headers will be marked unavailable.
4741 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4742 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004743 return false;
4744
4745 // Require it to be declared in a system header.
4746 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4747}
4748
4749/// Handle a delayed forbidden-type diagnostic.
4750static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4751 Decl *decl) {
4752 if (decl && isForbiddenTypeAllowed(S, decl)) {
4753 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4754 "this system declaration uses an unsupported type"));
4755 return;
4756 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004757 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004758 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004759 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004760 // kind of forbidden type messages on unavailable functions.
4761 if (FD->hasAttr<UnavailableAttr>() &&
4762 diag.getForbiddenTypeDiagnostic() ==
4763 diag::err_arc_array_param_no_ownership) {
4764 diag.Triggered = true;
4765 return;
4766 }
4767 }
John McCallf85e1932011-06-15 23:02:42 +00004768
4769 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4770 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4771 diag.Triggered = true;
4772}
4773
John McCall92576642012-05-07 06:16:41 +00004774void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4775 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004776 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004777 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004778
John McCall92576642012-05-07 06:16:41 +00004779 // When delaying diagnostics to run in the context of a parsed
4780 // declaration, we only want to actually emit anything if parsing
4781 // succeeds.
4782 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004783
John McCall92576642012-05-07 06:16:41 +00004784 // We emit all the active diagnostics in this pool or any of its
4785 // parents. In general, we'll get one pool for the decl spec
4786 // and a child pool for each declarator; in a decl group like:
4787 // deprecated_typedef foo, *bar, baz();
4788 // only the declarator pops will be passed decls. This is correct;
4789 // we really do need to consider delayed diagnostics from the decl spec
4790 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004791 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004792 do {
John McCall13489672012-05-07 06:16:58 +00004793 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004794 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4795 // This const_cast is a bit lame. Really, Triggered should be mutable.
4796 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004797 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004798 continue;
4799
John McCalleee1d542011-02-14 07:13:47 +00004800 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004801 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004802 // Don't bother giving deprecation diagnostics if the decl is invalid.
4803 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004804 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004805 break;
4806
4807 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004808 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004809 break;
John McCallf85e1932011-06-15 23:02:42 +00004810
4811 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004812 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004813 break;
John McCall2f514482010-01-27 03:50:35 +00004814 }
4815 }
John McCall92576642012-05-07 06:16:41 +00004816 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004817}
4818
John McCall13489672012-05-07 06:16:58 +00004819/// Given a set of delayed diagnostics, re-emit them as if they had
4820/// been delayed in the current context instead of in the given pool.
4821/// Essentially, this just moves them to the current pool.
4822void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4823 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4824 assert(curPool && "re-emitting in undelayed context not supported");
4825 curPool->steal(pool);
4826}
4827
John McCall54abf7d2009-11-04 02:18:39 +00004828static bool isDeclDeprecated(Decl *D) {
4829 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004830 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004831 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004832 // A category implicitly has the availability of the interface.
4833 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4834 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004835 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4836 return false;
4837}
4838
Eli Friedmanc3b23082012-08-08 21:52:41 +00004839static void
4840DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4841 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004842 const ObjCInterfaceDecl *UnknownObjCClass,
4843 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00004844 DeclarationName Name = D->getDeclName();
4845 if (!Message.empty()) {
4846 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4847 S.Diag(D->getLocation(),
4848 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4849 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004850 if (ObjCPropery)
4851 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4852 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004853 } else if (!UnknownObjCClass) {
4854 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4855 S.Diag(D->getLocation(),
4856 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4857 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004858 if (ObjCPropery)
4859 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4860 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004861 } else {
4862 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4863 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4864 }
4865}
4866
John McCall9c3087b2010-08-26 02:13:20 +00004867void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004868 Decl *Ctx) {
4869 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004870 return;
4871
John McCall2f514482010-01-27 03:50:35 +00004872 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004873 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4874 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004875 DD.getUnknownObjCClass(),
4876 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00004877}
4878
Chris Lattner5f9e2722011-07-23 10:55:15 +00004879void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004880 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004881 const ObjCInterfaceDecl *UnknownObjCClass,
4882 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00004883 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004884 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004885 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4886 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004887 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004888 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004889 return;
4890 }
4891
4892 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004893 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004894 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00004895 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00004896}