blob: 8aedaf7973e92f279b8afc444247122a34a19b97 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000027#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000028#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000029using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000030using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000031
John McCall883cc2c2011-03-02 12:29:23 +000032/// These constants match the enumerated choices of
33/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000034enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000035 ExpectedFunction,
36 ExpectedUnion,
37 ExpectedVariableOrFunction,
38 ExpectedFunctionOrMethod,
39 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000041 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedFunctionMethodOrParameter,
43 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000044 ExpectedVariable,
45 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000046 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000047 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000048 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000049 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000050 ExpectedTLSVar,
51 ExpectedVariableOrField,
52 ExpectedVariableFieldOrTag
John McCall883cc2c2011-03-02 12:29:23 +000053};
54
Chris Lattnere5c5ee12008-06-29 00:16:31 +000055//===----------------------------------------------------------------------===//
56// Helper functions
57//===----------------------------------------------------------------------===//
58
Chandler Carruth87c44602011-07-01 23:49:12 +000059static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000061 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000062 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000063 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000064 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000065 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000066 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 Ty = decl->getUnderlyingType();
68 else
69 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000070
Chris Lattner6b6b5372008-06-26 18:38:35 +000071 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000072 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000073 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000074 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000075
John McCall183700f2009-09-21 23:43:11 +000076 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000077}
78
Daniel Dunbar35682492008-09-26 04:12:28 +000079// FIXME: We should provide an abstraction around a method or function
80// to provide the following bits of information.
81
Nuno Lopesd20254f2009-12-20 23:11:08 +000082/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000083/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000084static bool isFunction(const Decl *D) {
85 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000086}
87
88/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000089/// type (function or function-typed variable) or an Objective-C
90/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000091static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000092 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000093}
94
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095/// isFunctionOrMethodOrBlock - Return true if the given decl has function
96/// type (function or function-typed variable) or an Objective-C
97/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000098static bool isFunctionOrMethodOrBlock(const Decl *D) {
99 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100 return true;
101 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000102 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000103 QualType Ty = V->getType();
104 return Ty->isBlockPointerType();
105 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000107}
108
John McCall711c52b2011-01-05 12:14:39 +0000109/// Return true if the given decl has a declarator that should have
110/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000111static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000112 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000113 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
114 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000115}
116
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117/// hasFunctionProto - Return true if the given decl has a argument
118/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000119/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000120static bool hasFunctionProto(const Decl *D) {
121 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000122 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000123 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000124 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000125 return true;
126 }
127}
128
129/// getFunctionOrMethodNumArgs - Return number of function or method
130/// arguments. It is an error to call this on a K&R function (use
131/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000132static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
133 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000134 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000135 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000136 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000137 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000138}
139
Chandler Carruth87c44602011-07-01 23:49:12 +0000140static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
141 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000142 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000143 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000144 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000145
Chandler Carruth87c44602011-07-01 23:49:12 +0000146 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000147}
148
Chandler Carruth87c44602011-07-01 23:49:12 +0000149static QualType getFunctionOrMethodResultType(const Decl *D) {
150 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000151 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000153}
154
Chandler Carruth87c44602011-07-01 23:49:12 +0000155static bool isFunctionOrMethodVariadic(const Decl *D) {
156 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000157 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000159 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000160 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000161 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000162 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000163 }
164}
165
Chandler Carruth87c44602011-07-01 23:49:12 +0000166static bool isInstanceMethod(const Decl *D) {
167 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000168 return MethodDecl->isInstance();
169 return false;
170}
171
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000173 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000174 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000176
John McCall506b57e2010-05-17 21:00:27 +0000177 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
178 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000179 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000180
John McCall506b57e2010-05-17 21:00:27 +0000181 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000182
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 // FIXME: Should we walk the chain of classes?
184 return ClsName == &Ctx.Idents.get("NSString") ||
185 ClsName == &Ctx.Idents.get("NSMutableString");
186}
187
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000188static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000189 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 if (!PT)
191 return false;
192
Ted Kremenek6217b802009-07-29 21:53:49 +0000193 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 if (!RT)
195 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000196
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000197 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000198 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000199 return false;
200
201 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
202}
203
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000204/// \brief Check if the attribute has exactly as many args as Num. May
205/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000206static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
207 unsigned int Num) {
208 if (Attr.getNumArgs() != Num) {
209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
210 return false;
211 }
212
213 return true;
214}
215
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000216
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000217/// \brief Check if the attribute has at least as many args as Num. May
218/// output an error.
219static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
220 unsigned int Num) {
221 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000222 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
223 return false;
224 }
225
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000226 return true;
227}
228
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000229/// \brief Check if IdxExpr is a valid argument index for a function or
230/// instance method D. May output an error.
231///
232/// \returns true if IdxExpr is a valid index.
233static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
234 StringRef AttrName,
235 SourceLocation AttrLoc,
236 unsigned AttrArgNum,
237 const Expr *IdxExpr,
238 uint64_t &Idx)
239{
240 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
241
242 // In C++ the implicit 'this' function parameter also counts.
243 // Parameters are counted from one.
244 const bool HasImplicitThisParam = isInstanceMethod(D);
245 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
246 const unsigned FirstIdx = 1;
247
248 llvm::APSInt IdxInt;
249 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
250 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
251 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
252 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
253 return false;
254 }
255
256 Idx = IdxInt.getLimitedValue();
257 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
258 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
259 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
260 return false;
261 }
262 Idx--; // Convert to zero-based.
263 if (HasImplicitThisParam) {
264 if (Idx == 0) {
265 S.Diag(AttrLoc,
266 diag::err_attribute_invalid_implicit_this_argument)
267 << AttrName << IdxExpr->getSourceRange();
268 return false;
269 }
270 --Idx;
271 }
272
273 return true;
274}
275
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000276///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000277/// \brief Check if passed in Decl is a field or potentially shared global var
278/// \return true if the Decl is a field or potentially shared global variable
279///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000280static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000281 if (isa<FieldDecl>(D))
282 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000283 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000284 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
285
286 return false;
287}
288
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000289/// \brief Check if the passed-in expression is of type int or bool.
290static bool isIntOrBool(Expr *Exp) {
291 QualType QT = Exp->getType();
292 return QT->isBooleanType() || QT->isIntegerType();
293}
294
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000295
296// Check to see if the type is a smart pointer of some kind. We assume
297// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000298static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
299 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
300 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000301 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000302 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000303
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000304 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
305 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000306 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000307 return false;
308
309 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000310}
311
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000312/// \brief Check if passed in Decl is a pointer type.
313/// Note that this function may produce an error message.
314/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000315static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
316 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000317 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000318 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000319 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000320 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000321
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000322 if (const RecordType *RT = QT->getAs<RecordType>()) {
323 // If it's an incomplete type, it could be a smart pointer; skip it.
324 // (We don't want to force template instantiation if we can avoid it,
325 // since that would alter the order in which templates are instantiated.)
326 if (RT->isIncompleteType())
327 return true;
328
329 if (threadSafetyCheckIsSmartPointer(S, RT))
330 return true;
331 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000332
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000333 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000334 << Attr.getName()->getName() << QT;
335 } else {
336 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
337 << Attr.getName();
338 }
339 return false;
340}
341
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000342/// \brief Checks that the passed in QualType either is of RecordType or points
343/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000344static const RecordType *getRecordType(QualType QT) {
345 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000346 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000347
348 // Now check if we point to record type.
349 if (const PointerType *PT = QT->getAs<PointerType>())
350 return PT->getPointeeType()->getAs<RecordType>();
351
352 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000353}
354
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000355
Jordy Rosefad5de92012-05-08 03:27:22 +0000356static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
357 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000358 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
359 if (RT->getDecl()->getAttr<LockableAttr>())
360 return true;
361 return false;
362}
363
364
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000365/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000366/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000367static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
368 QualType Ty) {
369 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000370
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000371 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000372 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000373 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000374 << Attr.getName() << Ty.getAsString();
375 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000376 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000377
Michael Hanf1aae3b2012-08-03 17:40:43 +0000378 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000379 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000380 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000381
382 // Allow smart pointers to be used as lockable objects.
383 // FIXME -- Check the type that the smart pointer points to.
384 if (threadSafetyCheckIsSmartPointer(S, RT))
385 return;
386
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000387 // Check if the type is lockable.
388 RecordDecl *RD = RT->getDecl();
389 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000390 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000391
392 // Else check if any base classes are lockable.
393 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
394 CXXBasePaths BPaths(false, false);
395 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
396 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000397 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000398
399 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
400 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000401}
402
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000403/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000404/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000405/// \param Sidx The attribute argument index to start checking with.
406/// \param ParamIdxOk Whether an argument can be indexing into a function
407/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000408static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000409 const AttributeList &Attr,
410 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000411 int Sidx = 0,
412 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000413 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000414 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000415
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000416 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000417 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000418 Args.push_back(ArgExp);
419 continue;
420 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000421
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000422 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000423 if (StrLit->getLength() == 0 ||
424 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000425 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000426 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000427 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000428 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000429 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000430
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000431 // We allow constant strings to be used as a placeholder for expressions
432 // that are not valid C++ syntax, but warn that they are ignored.
433 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
434 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000435 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000436 continue;
437 }
438
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000439 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000440
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000441 // A pointer to member expression of the form &MyClass::mu is treated
442 // specially -- we need to look at the type of the member.
443 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
444 if (UOp->getOpcode() == UO_AddrOf)
445 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
446 if (DRE->getDecl()->isCXXInstanceMember())
447 ArgTy = DRE->getDecl()->getType();
448
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000449 // First see if we can just cast to record type, or point to record type.
450 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000451
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000452 // Now check if we index into a record type function param.
453 if(!RT && ParamIdxOk) {
454 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000455 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
456 if(FD && IL) {
457 unsigned int NumParams = FD->getNumParams();
458 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000459 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
460 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
461 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
463 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000464 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000465 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000466 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000467 }
468 }
469
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000470 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000471
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000472 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000473 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000474}
475
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000476//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000477// Attribute Implementations
478//===----------------------------------------------------------------------===//
479
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000480// FIXME: All this manual attribute parsing code is gross. At the
481// least add some helper functions to check most argument patterns (#
482// and types of args).
483
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000484enum ThreadAttributeDeclKind {
485 ThreadExpectedFieldOrGlobalVar,
486 ThreadExpectedFunctionOrMethod,
487 ThreadExpectedClassOrStruct
488};
489
Michael Hanf1aae3b2012-08-03 17:40:43 +0000490static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000491 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000492 assert(!Attr.isInvalid());
493
494 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000495 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000496
497 // D must be either a member field or global (potentially shared) variable.
498 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000499 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
500 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000501 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000502 }
503
Michael Handc691572012-07-23 18:48:41 +0000504 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000505}
506
Michael Handc691572012-07-23 18:48:41 +0000507static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
508 if (!checkGuardedVarAttrCommon(S, D, Attr))
509 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000510
Michael Han51d8c522013-01-24 16:46:58 +0000511 D->addAttr(::new (S.Context)
512 GuardedVarAttr(Attr.getRange(), S.Context,
513 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000514}
515
Michael Hanf1aae3b2012-08-03 17:40:43 +0000516static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000517 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000518 if (!checkGuardedVarAttrCommon(S, D, Attr))
519 return;
520
521 if (!threadSafetyCheckIsPointer(S, D, Attr))
522 return;
523
Michael Han51d8c522013-01-24 16:46:58 +0000524 D->addAttr(::new (S.Context)
525 PtGuardedVarAttr(Attr.getRange(), S.Context,
526 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000527}
528
Michael Hanf1aae3b2012-08-03 17:40:43 +0000529static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
530 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000531 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000532 assert(!Attr.isInvalid());
533
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000534 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000535 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000536
537 // D must be either a member field or global (potentially shared) variable.
538 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000539 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
540 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000541 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000542 }
543
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000544 SmallVector<Expr*, 1> Args;
545 // check that all arguments are lockable objects
546 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
547 unsigned Size = Args.size();
548 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000549 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000550
Michael Handc691572012-07-23 18:48:41 +0000551 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000552
Michael Handc691572012-07-23 18:48:41 +0000553 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000554}
555
Michael Handc691572012-07-23 18:48:41 +0000556static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
557 Expr *Arg = 0;
558 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
559 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000560
Michael Handc691572012-07-23 18:48:41 +0000561 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
562}
563
Michael Hanf1aae3b2012-08-03 17:40:43 +0000564static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000565 const AttributeList &Attr) {
566 Expr *Arg = 0;
567 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
568 return;
569
570 if (!threadSafetyCheckIsPointer(S, D, Attr))
571 return;
572
573 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
574 S.Context, Arg));
575}
576
Michael Hanf1aae3b2012-08-03 17:40:43 +0000577static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000578 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000579 assert(!Attr.isInvalid());
580
581 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000582 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000583
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000584 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000585 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000586 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
587 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000588 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000589 }
590
Michael Handc691572012-07-23 18:48:41 +0000591 return true;
592}
593
594static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
595 if (!checkLockableAttrCommon(S, D, Attr))
596 return;
597
598 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
599}
600
Michael Hanf1aae3b2012-08-03 17:40:43 +0000601static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000602 const AttributeList &Attr) {
603 if (!checkLockableAttrCommon(S, D, Attr))
604 return;
605
Michael Han51d8c522013-01-24 16:46:58 +0000606 D->addAttr(::new (S.Context)
607 ScopedLockableAttr(Attr.getRange(), S.Context,
608 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000609}
610
611static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
612 const AttributeList &Attr) {
613 assert(!Attr.isInvalid());
614
615 if (!checkAttributeNumArgs(S, Attr, 0))
616 return;
617
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000618 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000619 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
620 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000621 return;
622 }
623
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000624 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000625 S.Context));
626}
627
Kostya Serebryany71efba02012-01-24 19:25:38 +0000628static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000629 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000630 assert(!Attr.isInvalid());
631
632 if (!checkAttributeNumArgs(S, Attr, 0))
633 return;
634
635 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
637 << Attr.getName() << ExpectedFunctionOrMethod;
638 return;
639 }
640
Michael Han51d8c522013-01-24 16:46:58 +0000641 D->addAttr(::new (S.Context)
642 NoAddressSafetyAnalysisAttr(Attr.getRange(), S.Context,
643 Attr.getAttributeSpellingListIndex()));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000644}
645
Michael Hanf1aae3b2012-08-03 17:40:43 +0000646static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
647 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000648 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000649 assert(!Attr.isInvalid());
650
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000651 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000652 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000653
654 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000655 ValueDecl *VD = dyn_cast<ValueDecl>(D);
656 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000657 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
658 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000659 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000660 }
661
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000662 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000663 QualType QT = VD->getType();
664 if (!QT->isDependentType()) {
665 const RecordType *RT = getRecordType(QT);
666 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000667 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000668 << Attr.getName();
669 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000670 }
671 }
672
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000673 // Check that all arguments are lockable objects.
674 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000675 if (Args.size() == 0)
676 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000677
Michael Handc691572012-07-23 18:48:41 +0000678 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000679}
680
Michael Hanf1aae3b2012-08-03 17:40:43 +0000681static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000682 const AttributeList &Attr) {
683 SmallVector<Expr*, 1> Args;
684 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
685 return;
686
687 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000688 D->addAttr(::new (S.Context)
689 AcquiredAfterAttr(Attr.getRange(), S.Context,
690 StartArg, Args.size(),
691 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000692}
693
Michael Hanf1aae3b2012-08-03 17:40:43 +0000694static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000695 const AttributeList &Attr) {
696 SmallVector<Expr*, 1> Args;
697 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
698 return;
699
700 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000701 D->addAttr(::new (S.Context)
702 AcquiredBeforeAttr(Attr.getRange(), S.Context,
703 StartArg, Args.size(),
704 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000705}
706
Michael Hanf1aae3b2012-08-03 17:40:43 +0000707static bool checkLockFunAttrCommon(Sema &S, Decl *D,
708 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000709 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000710 assert(!Attr.isInvalid());
711
712 // zero or more arguments ok
713
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000714 // check that the attribute is applied to a function
715 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000716 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
717 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000718 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000719 }
720
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000721 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000722 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000723
Michael Handc691572012-07-23 18:48:41 +0000724 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000725}
726
Michael Hanf1aae3b2012-08-03 17:40:43 +0000727static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000728 const AttributeList &Attr) {
729 SmallVector<Expr*, 1> Args;
730 if (!checkLockFunAttrCommon(S, D, Attr, Args))
731 return;
732
733 unsigned Size = Args.size();
734 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000735 D->addAttr(::new (S.Context)
736 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
737 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000738}
739
Michael Hanf1aae3b2012-08-03 17:40:43 +0000740static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000741 const AttributeList &Attr) {
742 SmallVector<Expr*, 1> Args;
743 if (!checkLockFunAttrCommon(S, D, Attr, Args))
744 return;
745
746 unsigned Size = Args.size();
747 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000748 D->addAttr(::new (S.Context)
749 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
750 StartArg, Size,
751 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000752}
753
Michael Hanf1aae3b2012-08-03 17:40:43 +0000754static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
755 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000756 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000757 assert(!Attr.isInvalid());
758
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000759 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000760 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000762 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000763 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
764 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000765 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000766 }
767
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000768 if (!isIntOrBool(Attr.getArg(0))) {
769 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000770 << Attr.getName();
771 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000772 }
773
774 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000775 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000776
Michael Handc691572012-07-23 18:48:41 +0000777 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000778}
779
Michael Hanf1aae3b2012-08-03 17:40:43 +0000780static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000781 const AttributeList &Attr) {
782 SmallVector<Expr*, 2> Args;
783 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
784 return;
785
786 unsigned Size = Args.size();
787 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000788 D->addAttr(::new (S.Context)
789 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
790 Attr.getArg(0), StartArg, Size,
791 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000792}
793
Michael Hanf1aae3b2012-08-03 17:40:43 +0000794static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000795 const AttributeList &Attr) {
796 SmallVector<Expr*, 2> Args;
797 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
798 return;
799
800 unsigned Size = Args.size();
801 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000802 D->addAttr(::new (S.Context)
803 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
804 Attr.getArg(0), StartArg, Size,
805 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000806}
807
Michael Hanf1aae3b2012-08-03 17:40:43 +0000808static bool checkLocksRequiredCommon(Sema &S, Decl *D,
809 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000810 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000811 assert(!Attr.isInvalid());
812
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000813 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000814 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000815
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000816 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000817 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
818 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000819 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000820 }
821
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000822 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000823 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000824 if (Args.size() == 0)
825 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000826
Michael Handc691572012-07-23 18:48:41 +0000827 return true;
828}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000829
Michael Hanf1aae3b2012-08-03 17:40:43 +0000830static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000831 const AttributeList &Attr) {
832 SmallVector<Expr*, 1> Args;
833 if (!checkLocksRequiredCommon(S, D, Attr, Args))
834 return;
835
836 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000837 D->addAttr(::new (S.Context)
838 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
839 StartArg, Args.size(),
840 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000841}
842
Michael Hanf1aae3b2012-08-03 17:40:43 +0000843static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000844 const AttributeList &Attr) {
845 SmallVector<Expr*, 1> Args;
846 if (!checkLocksRequiredCommon(S, D, Attr, Args))
847 return;
848
849 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000850 D->addAttr(::new (S.Context)
851 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
852 StartArg, Args.size(),
853 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000854}
855
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000856static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000857 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000858 assert(!Attr.isInvalid());
859
860 // zero or more arguments ok
861
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000862 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000863 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
864 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000865 return;
866 }
867
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000868 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000869 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000870 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000871 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000872 Expr **StartArg = Size == 0 ? 0 : &Args[0];
873
Michael Han51d8c522013-01-24 16:46:58 +0000874 D->addAttr(::new (S.Context)
875 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
876 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000877}
878
879static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000880 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000881 assert(!Attr.isInvalid());
882
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000883 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000884 return;
885
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000887 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
888 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889 return;
890 }
891
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000892 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000893 SmallVector<Expr*, 1> Args;
894 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
895 unsigned Size = Args.size();
896 if (Size == 0)
897 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000898
Michael Han51d8c522013-01-24 16:46:58 +0000899 D->addAttr(::new (S.Context)
900 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
901 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000902}
903
904static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000906 assert(!Attr.isInvalid());
907
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000908 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000909 return;
910
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000911 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000912 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
913 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000914 return;
915 }
916
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000917 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000918 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000919 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000920 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000921 if (Size == 0)
922 return;
923 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000924
Michael Han51d8c522013-01-24 16:46:58 +0000925 D->addAttr(::new (S.Context)
926 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
927 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000928}
929
930
Chandler Carruth1b03c872011-07-02 00:01:44 +0000931static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
932 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +0000933 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
934 if (TD == 0) {
935 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
936 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +0000937 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000938 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000939 }
Mike Stumpbf916502009-07-24 19:02:52 +0000940
Richard Smitha4fa9002013-01-13 02:11:23 +0000941 // Remember this typedef decl, we will need it later for diagnostics.
942 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000943}
944
Chandler Carruth1b03c872011-07-02 00:01:44 +0000945static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000946 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000947 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000948 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000949
Chandler Carruth87c44602011-07-01 23:49:12 +0000950 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000951 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000952 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000953 // If the alignment is less than or equal to 8 bits, the packed attribute
954 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000955 if (!FD->getType()->isDependentType() &&
956 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000957 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000958 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000959 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000960 else
Michael Han51d8c522013-01-24 16:46:58 +0000961 FD->addAttr(::new (S.Context)
962 PackedAttr(Attr.getRange(), S.Context,
963 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000964 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000965 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000966}
967
Chandler Carruth1b03c872011-07-02 00:01:44 +0000968static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +0000969 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +0000970 RD->addAttr(::new (S.Context)
971 MsStructAttr(Attr.getRange(), S.Context,
972 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000973 else
974 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
975}
976
Chandler Carruth1b03c872011-07-02 00:01:44 +0000977static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000978 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000979 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000980 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000981
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000982 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000983 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000984 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +0000985 D->addAttr(::new (S.Context)
986 IBActionAttr(Attr.getRange(), S.Context,
987 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000988 return;
989 }
990
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000991 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000992}
993
Ted Kremenek2f041d02011-09-29 07:02:25 +0000994static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
995 // The IBOutlet/IBOutletCollection attributes only apply to instance
996 // variables or properties of Objective-C classes. The outlet must also
997 // have an object reference type.
998 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
999 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001000 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001001 << Attr.getName() << VD->getType() << 0;
1002 return false;
1003 }
1004 }
1005 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1006 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001007 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001008 << Attr.getName() << PD->getType() << 1;
1009 return false;
1010 }
1011 }
1012 else {
1013 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1014 return false;
1015 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001016
Ted Kremenek2f041d02011-09-29 07:02:25 +00001017 return true;
1018}
1019
Chandler Carruth1b03c872011-07-02 00:01:44 +00001020static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001021 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001022 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001023 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001024
1025 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001026 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001027
Michael Han51d8c522013-01-24 16:46:58 +00001028 D->addAttr(::new (S.Context)
1029 IBOutletAttr(Attr.getRange(), S.Context,
1030 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001031}
1032
Chandler Carruth1b03c872011-07-02 00:01:44 +00001033static void handleIBOutletCollection(Sema &S, Decl *D,
1034 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001035
1036 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001037 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1039 return;
1040 }
1041
Ted Kremenek2f041d02011-09-29 07:02:25 +00001042 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001043 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001044
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001045 IdentifierInfo *II = Attr.getParameterName();
1046 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001047 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001048
John McCallb3d87482010-08-24 05:47:05 +00001049 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001050 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001051 if (!TypeRep) {
1052 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1053 return;
1054 }
John McCallb3d87482010-08-24 05:47:05 +00001055 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001056 // Diagnose use of non-object type in iboutletcollection attribute.
1057 // FIXME. Gnu attribute extension ignores use of builtin types in
1058 // attributes. So, __attribute__((iboutletcollection(char))) will be
1059 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001060 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001061 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1062 return;
1063 }
Michael Han51d8c522013-01-24 16:46:58 +00001064 D->addAttr(::new (S.Context)
1065 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1066 QT, Attr.getParameterLoc(),
1067 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001068}
1069
Chandler Carruthd309c812011-07-01 23:49:16 +00001070static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001071 if (const RecordType *UT = T->getAsUnionType())
1072 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1073 RecordDecl *UD = UT->getDecl();
1074 for (RecordDecl::field_iterator it = UD->field_begin(),
1075 itend = UD->field_end(); it != itend; ++it) {
1076 QualType QT = it->getType();
1077 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1078 T = QT;
1079 return;
1080 }
1081 }
1082 }
1083}
1084
Nuno Lopes587de5b2012-05-24 00:22:00 +00001085static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001086 if (!isFunctionOrMethod(D)) {
1087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1088 << "alloc_size" << ExpectedFunctionOrMethod;
1089 return;
1090 }
1091
Nuno Lopes587de5b2012-05-24 00:22:00 +00001092 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1093 return;
1094
1095 // In C++ the implicit 'this' function parameter also counts, and they are
1096 // counted from one.
1097 bool HasImplicitThisParam = isInstanceMethod(D);
1098 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1099
1100 SmallVector<unsigned, 8> SizeArgs;
1101
1102 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1103 E = Attr.arg_end(); I!=E; ++I) {
1104 // The argument must be an integer constant expression.
1105 Expr *Ex = *I;
1106 llvm::APSInt ArgNum;
1107 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1108 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1110 << "alloc_size" << Ex->getSourceRange();
1111 return;
1112 }
1113
1114 uint64_t x = ArgNum.getZExtValue();
1115
1116 if (x < 1 || x > NumArgs) {
1117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1118 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1119 return;
1120 }
1121
1122 --x;
1123 if (HasImplicitThisParam) {
1124 if (x == 0) {
1125 S.Diag(Attr.getLoc(),
1126 diag::err_attribute_invalid_implicit_this_argument)
1127 << "alloc_size" << Ex->getSourceRange();
1128 return;
1129 }
1130 --x;
1131 }
1132
1133 // check if the function argument is of an integer type
1134 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1135 if (!T->isIntegerType()) {
1136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1137 << "alloc_size" << Ex->getSourceRange();
1138 return;
1139 }
1140
Nuno Lopes587de5b2012-05-24 00:22:00 +00001141 SizeArgs.push_back(x);
1142 }
1143
1144 // check if the function returns a pointer
1145 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1146 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1147 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1148 }
1149
Michael Han51d8c522013-01-24 16:46:58 +00001150 D->addAttr(::new (S.Context)
1151 AllocSizeAttr(Attr.getRange(), S.Context,
1152 SizeArgs.data(), SizeArgs.size(),
1153 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001154}
1155
Chandler Carruth1b03c872011-07-02 00:01:44 +00001156static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001157 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1158 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001159 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001160 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001161 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001162 return;
1163 }
Mike Stumpbf916502009-07-24 19:02:52 +00001164
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001165 // In C++ the implicit 'this' function parameter also counts, and they are
1166 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001167 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001168 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001169
1170 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001171 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001172
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001173 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1174 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001175 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001176 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001177 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001178 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1179 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1181 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001182 return;
1183 }
Mike Stumpbf916502009-07-24 19:02:52 +00001184
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001185 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001187 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001189 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001190 return;
1191 }
Mike Stumpbf916502009-07-24 19:02:52 +00001192
Ted Kremenek465172f2008-07-21 22:09:15 +00001193 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001194 if (HasImplicitThisParam) {
1195 if (x == 0) {
1196 S.Diag(Attr.getLoc(),
1197 diag::err_attribute_invalid_implicit_this_argument)
1198 << "nonnull" << Ex->getSourceRange();
1199 return;
1200 }
1201 --x;
1202 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001203
1204 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001205 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001206 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001207
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001208 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001209 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001210 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001211 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001212 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001213 }
Mike Stumpbf916502009-07-24 19:02:52 +00001214
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001215 NonNullArgs.push_back(x);
1216 }
Mike Stumpbf916502009-07-24 19:02:52 +00001217
1218 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1219 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001220 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001221 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1222 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001223 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001224 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001225 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001226 }
Mike Stumpbf916502009-07-24 19:02:52 +00001227
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001228 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001229 if (NonNullArgs.empty()) {
1230 // Warn the trivial case only if attribute is not coming from a
1231 // macro instantiation.
1232 if (Attr.getLoc().isFileID())
1233 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001234 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001235 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001236 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001237
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001238 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001239 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001240 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001241 D->addAttr(::new (S.Context)
1242 NonNullAttr(Attr.getRange(), S.Context, start, size,
1243 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001244}
1245
Chandler Carruth1b03c872011-07-02 00:01:44 +00001246static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001247 // This attribute must be applied to a function declaration.
1248 // The first argument to the attribute must be a string,
1249 // the name of the resource, for example "malloc".
1250 // The following arguments must be argument indexes, the arguments must be
1251 // of integer type for Returns, otherwise of pointer type.
1252 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001253 // after being held. free() should be __attribute((ownership_takes)), whereas
1254 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001255
1256 if (!AL.getParameterName()) {
1257 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1258 << AL.getName()->getName() << 1;
1259 return;
1260 }
1261 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001262 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001263 switch (AL.getKind()) {
1264 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001265 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001266 if (AL.getNumArgs() < 1) {
1267 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1268 return;
1269 }
Jordy Rose2a479922010-08-12 08:54:03 +00001270 break;
1271 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001272 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001273 if (AL.getNumArgs() < 1) {
1274 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1275 return;
1276 }
Jordy Rose2a479922010-08-12 08:54:03 +00001277 break;
1278 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001279 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001280 if (AL.getNumArgs() > 1) {
1281 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1282 << AL.getNumArgs() + 1;
1283 return;
1284 }
Jordy Rose2a479922010-08-12 08:54:03 +00001285 break;
1286 default:
1287 // This should never happen given how we are called.
1288 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001289 }
1290
Chandler Carruth87c44602011-07-01 23:49:12 +00001291 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001292 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1293 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001294 return;
1295 }
1296
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001297 // In C++ the implicit 'this' function parameter also counts, and they are
1298 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001299 bool HasImplicitThisParam = isInstanceMethod(D);
1300 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001301
Chris Lattner5f9e2722011-07-23 10:55:15 +00001302 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001303
1304 // Normalize the argument, __foo__ becomes foo.
1305 if (Module.startswith("__") && Module.endswith("__"))
1306 Module = Module.substr(2, Module.size() - 4);
1307
Chris Lattner5f9e2722011-07-23 10:55:15 +00001308 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001309
Jordy Rose2a479922010-08-12 08:54:03 +00001310 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1311 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001312
Peter Collingbourne7a730022010-11-23 20:45:58 +00001313 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001314 llvm::APSInt ArgNum(32);
1315 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1316 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1317 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1318 << AL.getName()->getName() << IdxExpr->getSourceRange();
1319 continue;
1320 }
1321
1322 unsigned x = (unsigned) ArgNum.getZExtValue();
1323
1324 if (x > NumArgs || x < 1) {
1325 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1326 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1327 continue;
1328 }
1329 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001330 if (HasImplicitThisParam) {
1331 if (x == 0) {
1332 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1333 << "ownership" << IdxExpr->getSourceRange();
1334 return;
1335 }
1336 --x;
1337 }
1338
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001339 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001340 case OwnershipAttr::Takes:
1341 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001343 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001344 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1345 // FIXME: Should also highlight argument in decl.
1346 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001347 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001348 << "pointer"
1349 << IdxExpr->getSourceRange();
1350 continue;
1351 }
1352 break;
1353 }
Sean Huntcf807c42010-08-18 23:23:40 +00001354 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 if (AL.getNumArgs() > 1) {
1356 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001357 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 llvm::APSInt ArgNum(32);
1359 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1360 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1361 S.Diag(AL.getLoc(), diag::err_ownership_type)
1362 << "ownership_returns" << "integer"
1363 << IdxExpr->getSourceRange();
1364 return;
1365 }
1366 }
1367 break;
1368 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001369 } // switch
1370
1371 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001372 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001373 i = D->specific_attr_begin<OwnershipAttr>(),
1374 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001375 i != e; ++i) {
1376 if ((*i)->getOwnKind() != K) {
1377 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1378 I!=E; ++I) {
1379 if (x == *I) {
1380 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1381 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001382 }
1383 }
1384 }
1385 }
1386 OwnershipArgs.push_back(x);
1387 }
1388
1389 unsigned* start = OwnershipArgs.data();
1390 unsigned size = OwnershipArgs.size();
1391 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001392
1393 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1394 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1395 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001396 }
Sean Huntcf807c42010-08-18 23:23:40 +00001397
Michael Han51d8c522013-01-24 16:46:58 +00001398 D->addAttr(::new (S.Context)
1399 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1400 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001401}
1402
Chandler Carruth1b03c872011-07-02 00:01:44 +00001403static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001404 // Check the attribute arguments.
1405 if (Attr.getNumArgs() > 1) {
1406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1407 return;
1408 }
1409
Chandler Carruth87c44602011-07-01 23:49:12 +00001410 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001412 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001413 return;
1414 }
1415
Chandler Carruth87c44602011-07-01 23:49:12 +00001416 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001417
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001418 // gcc rejects
1419 // class c {
1420 // static int a __attribute__((weakref ("v2")));
1421 // static int b() __attribute__((weakref ("f3")));
1422 // };
1423 // and ignores the attributes of
1424 // void f(void) {
1425 // static int a __attribute__((weakref ("v2")));
1426 // }
1427 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001428 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001429 if (!Ctx->isFileContext()) {
1430 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001431 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001432 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001433 }
1434
1435 // The GCC manual says
1436 //
1437 // At present, a declaration to which `weakref' is attached can only
1438 // be `static'.
1439 //
1440 // It also says
1441 //
1442 // Without a TARGET,
1443 // given as an argument to `weakref' or to `alias', `weakref' is
1444 // equivalent to `weak'.
1445 //
1446 // gcc 4.4.1 will accept
1447 // int a7 __attribute__((weakref));
1448 // as
1449 // int a7 __attribute__((weak));
1450 // This looks like a bug in gcc. We reject that for now. We should revisit
1451 // it if this behaviour is actually used.
1452
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001453 // GCC rejects
1454 // static ((alias ("y"), weakref)).
1455 // Should we? How to check that weakref is before or after alias?
1456
1457 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001458 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001459 Arg = Arg->IgnoreParenCasts();
1460 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1461
Douglas Gregor5cee1192011-07-27 05:40:30 +00001462 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1464 << "weakref" << 1;
1465 return;
1466 }
1467 // GCC will accept anything as the argument of weakref. Should we
1468 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001469 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001470 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001471 }
1472
Michael Han51d8c522013-01-24 16:46:58 +00001473 D->addAttr(::new (S.Context)
1474 WeakRefAttr(Attr.getRange(), S.Context,
1475 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001476}
1477
Chandler Carruth1b03c872011-07-02 00:01:44 +00001478static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001479 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001480 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001482 return;
1483 }
Mike Stumpbf916502009-07-24 19:02:52 +00001484
Peter Collingbourne7a730022010-11-23 20:45:58 +00001485 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001486 Arg = Arg->IgnoreParenCasts();
1487 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001488
Douglas Gregor5cee1192011-07-27 05:40:30 +00001489 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001491 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001492 return;
1493 }
Mike Stumpbf916502009-07-24 19:02:52 +00001494
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001495 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001496 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1497 return;
1498 }
1499
Chris Lattner6b6b5372008-06-26 18:38:35 +00001500 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001501
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001502 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001503 Str->getString(),
1504 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001505}
1506
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001507static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1508 // Check the attribute arguments.
1509 if (!checkAttributeNumArgs(S, Attr, 0))
1510 return;
1511
1512 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1513 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1514 << Attr.getName() << ExpectedFunctionOrMethod;
1515 return;
1516 }
1517
Michael Han51d8c522013-01-24 16:46:58 +00001518 D->addAttr(::new (S.Context)
1519 MinSizeAttr(Attr.getRange(), S.Context,
1520 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001521}
1522
Benjamin Krameree409a92012-05-12 21:10:52 +00001523static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1524 // Check the attribute arguments.
1525 if (!checkAttributeNumArgs(S, Attr, 0))
1526 return;
1527
1528 if (!isa<FunctionDecl>(D)) {
1529 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1530 << Attr.getName() << ExpectedFunction;
1531 return;
1532 }
1533
1534 if (D->hasAttr<HotAttr>()) {
1535 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1536 << Attr.getName() << "hot";
1537 return;
1538 }
1539
Michael Han51d8c522013-01-24 16:46:58 +00001540 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1541 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001542}
1543
1544static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1545 // Check the attribute arguments.
1546 if (!checkAttributeNumArgs(S, Attr, 0))
1547 return;
1548
1549 if (!isa<FunctionDecl>(D)) {
1550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1551 << Attr.getName() << ExpectedFunction;
1552 return;
1553 }
1554
1555 if (D->hasAttr<ColdAttr>()) {
1556 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1557 << Attr.getName() << "cold";
1558 return;
1559 }
1560
Michael Han51d8c522013-01-24 16:46:58 +00001561 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1562 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001563}
1564
Chandler Carruth1b03c872011-07-02 00:01:44 +00001565static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001566 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001567 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001568 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001569
Chandler Carruth87c44602011-07-01 23:49:12 +00001570 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001571 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001572 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001573 return;
1574 }
1575
Michael Han51d8c522013-01-24 16:46:58 +00001576 D->addAttr(::new (S.Context)
1577 NakedAttr(Attr.getRange(), S.Context,
1578 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001579}
1580
Chandler Carruth1b03c872011-07-02 00:01:44 +00001581static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1582 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001583 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001584 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1586 return;
1587 }
1588
Chandler Carruth87c44602011-07-01 23:49:12 +00001589 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001591 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001592 return;
1593 }
Mike Stumpbf916502009-07-24 19:02:52 +00001594
Michael Han51d8c522013-01-24 16:46:58 +00001595 D->addAttr(::new (S.Context)
1596 AlwaysInlineAttr(Attr.getRange(), S.Context,
1597 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001598}
1599
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001600static void handleTLSModelAttr(Sema &S, Decl *D,
1601 const AttributeList &Attr) {
1602 // Check the attribute arguments.
1603 if (Attr.getNumArgs() != 1) {
1604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1605 return;
1606 }
1607
1608 Expr *Arg = Attr.getArg(0);
1609 Arg = Arg->IgnoreParenCasts();
1610 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1611
1612 // Check that it is a string.
1613 if (!Str) {
1614 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1615 return;
1616 }
1617
1618 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1620 << Attr.getName() << ExpectedTLSVar;
1621 return;
1622 }
1623
1624 // Check that the value.
1625 StringRef Model = Str->getString();
1626 if (Model != "global-dynamic" && Model != "local-dynamic"
1627 && Model != "initial-exec" && Model != "local-exec") {
1628 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1629 return;
1630 }
1631
Michael Han51d8c522013-01-24 16:46:58 +00001632 D->addAttr(::new (S.Context)
1633 TLSModelAttr(Attr.getRange(), S.Context, Model,
1634 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001635}
1636
Chandler Carruth1b03c872011-07-02 00:01:44 +00001637static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001638 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001639 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1641 return;
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Chandler Carruth87c44602011-07-01 23:49:12 +00001644 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001645 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001646 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001647 D->addAttr(::new (S.Context)
1648 MallocAttr(Attr.getRange(), S.Context,
1649 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001650 return;
1651 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001652 }
1653
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001654 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001655}
1656
Chandler Carruth1b03c872011-07-02 00:01:44 +00001657static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001658 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001659 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001660 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001661
Michael Han51d8c522013-01-24 16:46:58 +00001662 D->addAttr(::new (S.Context)
1663 MayAliasAttr(Attr.getRange(), S.Context,
1664 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001665}
1666
Chandler Carruth1b03c872011-07-02 00:01:44 +00001667static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001668 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001669 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001670 D->addAttr(::new (S.Context)
1671 NoCommonAttr(Attr.getRange(), S.Context,
1672 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001673 else
1674 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001675 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001676}
1677
Chandler Carruth1b03c872011-07-02 00:01:44 +00001678static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001679 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001680 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001681 D->addAttr(::new (S.Context)
1682 CommonAttr(Attr.getRange(), S.Context,
1683 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001684 else
1685 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001686 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001687}
1688
Chandler Carruth1b03c872011-07-02 00:01:44 +00001689static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001690 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001691
1692 if (S.CheckNoReturnAttr(attr)) return;
1693
Chandler Carruth87c44602011-07-01 23:49:12 +00001694 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001695 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001696 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001697 return;
1698 }
1699
Michael Han51d8c522013-01-24 16:46:58 +00001700 D->addAttr(::new (S.Context)
1701 NoReturnAttr(attr.getRange(), S.Context,
1702 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001703}
1704
1705bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001706 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001707 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1708 attr.setInvalid();
1709 return true;
1710 }
1711
1712 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001713}
1714
Chandler Carruth1b03c872011-07-02 00:01:44 +00001715static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1716 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001717
1718 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1719 // because 'analyzer_noreturn' does not impact the type.
1720
Chandler Carruth1731e202011-07-11 23:30:35 +00001721 if(!checkAttributeNumArgs(S, Attr, 0))
1722 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001723
Chandler Carruth87c44602011-07-01 23:49:12 +00001724 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1725 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001726 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1727 && !VD->getType()->isFunctionPointerType())) {
1728 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001729 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001730 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001731 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001732 return;
1733 }
1734 }
1735
Michael Han51d8c522013-01-24 16:46:58 +00001736 D->addAttr(::new (S.Context)
1737 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1738 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001739}
1740
Richard Smithcd8ab512013-01-17 01:30:42 +00001741static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1742 const AttributeList &Attr) {
1743 // C++11 [dcl.attr.noreturn]p1:
1744 // The attribute may be applied to the declarator-id in a function
1745 // declaration.
1746 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1747 if (!FD) {
1748 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1749 << Attr.getName() << ExpectedFunctionOrMethod;
1750 return;
1751 }
1752
Michael Han51d8c522013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context)
1754 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1755 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001756}
1757
John Thompson35cc9622010-08-09 21:53:52 +00001758// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001759static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001760/*
1761 Returning a Vector Class in Registers
1762
Eric Christopherf48f3672010-12-01 22:13:54 +00001763 According to the PPU ABI specifications, a class with a single member of
1764 vector type is returned in memory when used as the return value of a function.
1765 This results in inefficient code when implementing vector classes. To return
1766 the value in a single vector register, add the vecreturn attribute to the
1767 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001768
1769 Example:
1770
1771 struct Vector
1772 {
1773 __vector float xyzw;
1774 } __attribute__((vecreturn));
1775
1776 Vector Add(Vector lhs, Vector rhs)
1777 {
1778 Vector result;
1779 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1780 return result; // This will be returned in a register
1781 }
1782*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001783 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001784 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001785 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001786 return;
1787 }
1788
Chandler Carruth87c44602011-07-01 23:49:12 +00001789 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001790 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1791 return;
1792 }
1793
Chandler Carruth87c44602011-07-01 23:49:12 +00001794 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001795 int count = 0;
1796
1797 if (!isa<CXXRecordDecl>(record)) {
1798 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1799 return;
1800 }
1801
1802 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1803 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1804 return;
1805 }
1806
Eric Christopherf48f3672010-12-01 22:13:54 +00001807 for (RecordDecl::field_iterator iter = record->field_begin();
1808 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001809 if ((count == 1) || !iter->getType()->isVectorType()) {
1810 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1811 return;
1812 }
1813 count++;
1814 }
1815
Michael Han51d8c522013-01-24 16:46:58 +00001816 D->addAttr(::new (S.Context)
1817 VecReturnAttr(Attr.getRange(), S.Context,
1818 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001819}
1820
Richard Smith3a2b7a12013-01-28 22:42:45 +00001821static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1822 const AttributeList &Attr) {
1823 if (isa<ParmVarDecl>(D)) {
1824 // [[carries_dependency]] can only be applied to a parameter if it is a
1825 // parameter of a function declaration or lambda.
1826 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1827 S.Diag(Attr.getLoc(),
1828 diag::err_carries_dependency_param_not_function_decl);
1829 return;
1830 }
1831 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001832 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001833 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001834 return;
1835 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001836
1837 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1838 Attr.getRange(), S.Context,
1839 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001840}
1841
Chandler Carruth1b03c872011-07-02 00:01:44 +00001842static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001843 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001844 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001846 return;
1847 }
Mike Stumpbf916502009-07-24 19:02:52 +00001848
Chandler Carruth87c44602011-07-01 23:49:12 +00001849 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001850 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001851 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001852 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001853 return;
1854 }
Mike Stumpbf916502009-07-24 19:02:52 +00001855
Michael Han51d8c522013-01-24 16:46:58 +00001856 D->addAttr(::new (S.Context)
1857 UnusedAttr(Attr.getRange(), S.Context,
1858 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001859}
1860
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001861static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1862 const AttributeList &Attr) {
1863 // check the attribute arguments.
1864 if (Attr.hasParameterOrArguments()) {
1865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1866 return;
1867 }
1868
1869 if (!isa<FunctionDecl>(D)) {
1870 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1871 << Attr.getName() << ExpectedFunction;
1872 return;
1873 }
1874
Michael Han51d8c522013-01-24 16:46:58 +00001875 D->addAttr(::new (S.Context)
1876 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1877 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001878}
1879
Chandler Carruth1b03c872011-07-02 00:01:44 +00001880static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001881 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001882 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001883 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1884 return;
1885 }
Mike Stumpbf916502009-07-24 19:02:52 +00001886
Chandler Carruth87c44602011-07-01 23:49:12 +00001887 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001888 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001889 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1890 return;
1891 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001892 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001894 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001895 return;
1896 }
Mike Stumpbf916502009-07-24 19:02:52 +00001897
Michael Han51d8c522013-01-24 16:46:58 +00001898 D->addAttr(::new (S.Context)
1899 UsedAttr(Attr.getRange(), S.Context,
1900 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001901}
1902
Chandler Carruth1b03c872011-07-02 00:01:44 +00001903static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001904 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001905 if (Attr.getNumArgs() > 1) {
1906 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001907 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001908 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001909
1910 int priority = 65535; // FIXME: Do not hardcode such constants.
1911 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001912 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001913 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001914 if (E->isTypeDependent() || E->isValueDependent() ||
1915 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001916 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001917 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001918 return;
1919 }
1920 priority = Idx.getZExtValue();
1921 }
Mike Stumpbf916502009-07-24 19:02:52 +00001922
Chandler Carruth87c44602011-07-01 23:49:12 +00001923 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001925 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001926 return;
1927 }
1928
Michael Han51d8c522013-01-24 16:46:58 +00001929 D->addAttr(::new (S.Context)
1930 ConstructorAttr(Attr.getRange(), S.Context, priority,
1931 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001932}
1933
Chandler Carruth1b03c872011-07-02 00:01:44 +00001934static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001935 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001936 if (Attr.getNumArgs() > 1) {
1937 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001938 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001939 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001940
1941 int priority = 65535; // FIXME: Do not hardcode such constants.
1942 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001943 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001944 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001945 if (E->isTypeDependent() || E->isValueDependent() ||
1946 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001947 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001948 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001949 return;
1950 }
1951 priority = Idx.getZExtValue();
1952 }
Mike Stumpbf916502009-07-24 19:02:52 +00001953
Chandler Carruth87c44602011-07-01 23:49:12 +00001954 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001955 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001956 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001957 return;
1958 }
1959
Michael Han51d8c522013-01-24 16:46:58 +00001960 D->addAttr(::new (S.Context)
1961 DestructorAttr(Attr.getRange(), S.Context, priority,
1962 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001963}
1964
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001965template <typename AttrTy>
1966static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1967 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001968 unsigned NumArgs = Attr.getNumArgs();
1969 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001970 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001971 return;
1972 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001973
1974 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001975 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001976 if (NumArgs == 1) {
1977 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001978 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001979 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001980 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001981 return;
1982 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001983 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001984 }
Mike Stumpbf916502009-07-24 19:02:52 +00001985
Michael Han51d8c522013-01-24 16:46:58 +00001986 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1987 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001988}
1989
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001990static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1991 const AttributeList &Attr) {
1992 unsigned NumArgs = Attr.getNumArgs();
1993 if (NumArgs > 0) {
1994 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1995 return;
1996 }
1997
Michael Han51d8c522013-01-24 16:46:58 +00001998 D->addAttr(::new (S.Context)
1999 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2000 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002001}
2002
Patrick Beardb2f68202012-04-06 18:12:22 +00002003static void handleObjCRootClassAttr(Sema &S, Decl *D,
2004 const AttributeList &Attr) {
2005 if (!isa<ObjCInterfaceDecl>(D)) {
2006 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2007 return;
2008 }
2009
2010 unsigned NumArgs = Attr.getNumArgs();
2011 if (NumArgs > 0) {
2012 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2013 return;
2014 }
2015
Michael Han51d8c522013-01-24 16:46:58 +00002016 D->addAttr(::new (S.Context)
2017 ObjCRootClassAttr(Attr.getRange(), S.Context,
2018 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002019}
2020
Michael Han51d8c522013-01-24 16:46:58 +00002021static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2022 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002023 if (!isa<ObjCInterfaceDecl>(D)) {
2024 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2025 return;
2026 }
2027
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002028 unsigned NumArgs = Attr.getNumArgs();
2029 if (NumArgs > 0) {
2030 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2031 return;
2032 }
2033
Michael Han51d8c522013-01-24 16:46:58 +00002034 D->addAttr(::new (S.Context)
2035 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2036 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002037}
2038
Jordy Rosefad5de92012-05-08 03:27:22 +00002039static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2040 IdentifierInfo *Platform,
2041 VersionTuple Introduced,
2042 VersionTuple Deprecated,
2043 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002044 StringRef PlatformName
2045 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2046 if (PlatformName.empty())
2047 PlatformName = Platform->getName();
2048
2049 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2050 // of these steps are needed).
2051 if (!Introduced.empty() && !Deprecated.empty() &&
2052 !(Introduced <= Deprecated)) {
2053 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2054 << 1 << PlatformName << Deprecated.getAsString()
2055 << 0 << Introduced.getAsString();
2056 return true;
2057 }
2058
2059 if (!Introduced.empty() && !Obsoleted.empty() &&
2060 !(Introduced <= Obsoleted)) {
2061 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2062 << 2 << PlatformName << Obsoleted.getAsString()
2063 << 0 << Introduced.getAsString();
2064 return true;
2065 }
2066
2067 if (!Deprecated.empty() && !Obsoleted.empty() &&
2068 !(Deprecated <= Obsoleted)) {
2069 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2070 << 2 << PlatformName << Obsoleted.getAsString()
2071 << 1 << Deprecated.getAsString();
2072 return true;
2073 }
2074
2075 return false;
2076}
2077
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002078/// \brief Check whether the two versions match.
2079///
2080/// If either version tuple is empty, then they are assumed to match. If
2081/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2082static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2083 bool BeforeIsOkay) {
2084 if (X.empty() || Y.empty())
2085 return true;
2086
2087 if (X == Y)
2088 return true;
2089
2090 if (BeforeIsOkay && X < Y)
2091 return true;
2092
2093 return false;
2094}
2095
Rafael Espindola51be6e32013-01-08 22:04:34 +00002096AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002097 IdentifierInfo *Platform,
2098 VersionTuple Introduced,
2099 VersionTuple Deprecated,
2100 VersionTuple Obsoleted,
2101 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002102 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002103 bool Override,
2104 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002105 VersionTuple MergedIntroduced = Introduced;
2106 VersionTuple MergedDeprecated = Deprecated;
2107 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002108 bool FoundAny = false;
2109
Rafael Espindola98ae8342012-05-10 02:50:16 +00002110 if (D->hasAttrs()) {
2111 AttrVec &Attrs = D->getAttrs();
2112 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2113 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2114 if (!OldAA) {
2115 ++i;
2116 continue;
2117 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002118
Rafael Espindola98ae8342012-05-10 02:50:16 +00002119 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2120 if (OldPlatform != Platform) {
2121 ++i;
2122 continue;
2123 }
2124
2125 FoundAny = true;
2126 VersionTuple OldIntroduced = OldAA->getIntroduced();
2127 VersionTuple OldDeprecated = OldAA->getDeprecated();
2128 VersionTuple OldObsoleted = OldAA->getObsoleted();
2129 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002130
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002131 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2132 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2133 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2134 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002135 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002136 if (Override) {
2137 int Which = -1;
2138 VersionTuple FirstVersion;
2139 VersionTuple SecondVersion;
2140 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2141 Which = 0;
2142 FirstVersion = OldIntroduced;
2143 SecondVersion = Introduced;
2144 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2145 Which = 1;
2146 FirstVersion = Deprecated;
2147 SecondVersion = OldDeprecated;
2148 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2149 Which = 2;
2150 FirstVersion = Obsoleted;
2151 SecondVersion = OldObsoleted;
2152 }
2153
2154 if (Which == -1) {
2155 Diag(OldAA->getLocation(),
2156 diag::warn_mismatched_availability_override_unavail)
2157 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2158 } else {
2159 Diag(OldAA->getLocation(),
2160 diag::warn_mismatched_availability_override)
2161 << Which
2162 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2163 << FirstVersion.getAsString() << SecondVersion.getAsString();
2164 }
2165 Diag(Range.getBegin(), diag::note_overridden_method);
2166 } else {
2167 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2168 Diag(Range.getBegin(), diag::note_previous_attribute);
2169 }
2170
Rafael Espindola98ae8342012-05-10 02:50:16 +00002171 Attrs.erase(Attrs.begin() + i);
2172 --e;
2173 continue;
2174 }
2175
2176 VersionTuple MergedIntroduced2 = MergedIntroduced;
2177 VersionTuple MergedDeprecated2 = MergedDeprecated;
2178 VersionTuple MergedObsoleted2 = MergedObsoleted;
2179
2180 if (MergedIntroduced2.empty())
2181 MergedIntroduced2 = OldIntroduced;
2182 if (MergedDeprecated2.empty())
2183 MergedDeprecated2 = OldDeprecated;
2184 if (MergedObsoleted2.empty())
2185 MergedObsoleted2 = OldObsoleted;
2186
2187 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2188 MergedIntroduced2, MergedDeprecated2,
2189 MergedObsoleted2)) {
2190 Attrs.erase(Attrs.begin() + i);
2191 --e;
2192 continue;
2193 }
2194
2195 MergedIntroduced = MergedIntroduced2;
2196 MergedDeprecated = MergedDeprecated2;
2197 MergedObsoleted = MergedObsoleted2;
2198 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002199 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002200 }
2201
2202 if (FoundAny &&
2203 MergedIntroduced == Introduced &&
2204 MergedDeprecated == Deprecated &&
2205 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002206 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002207
Rafael Espindola98ae8342012-05-10 02:50:16 +00002208 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002209 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002210 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2211 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002212 Obsoleted, IsUnavailable, Message,
2213 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002214 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002215 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002216}
2217
Chandler Carruth1b03c872011-07-02 00:01:44 +00002218static void handleAvailabilityAttr(Sema &S, Decl *D,
2219 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002220 IdentifierInfo *Platform = Attr.getParameterName();
2221 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002222 unsigned Index = Attr.getAttributeSpellingListIndex();
2223
Rafael Espindola3b294362012-05-06 19:56:25 +00002224 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002225 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2226 << Platform;
2227
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002228 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2229 if (!ND) {
2230 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2231 return;
2232 }
2233
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002234 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2235 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2236 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002237 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002238 StringRef Str;
2239 const StringLiteral *SE =
2240 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2241 if (SE)
2242 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002243
Rafael Espindola51be6e32013-01-08 22:04:34 +00002244 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002245 Platform,
2246 Introduced.Version,
2247 Deprecated.Version,
2248 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002249 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002250 /*Override=*/false,
2251 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002252 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002253 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002254}
2255
Rafael Espindola599f1b72012-05-13 03:25:18 +00002256VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002257 VisibilityAttr::VisibilityType Vis,
2258 unsigned AttrSpellingListIndex) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002259 if (isa<TypedefNameDecl>(D)) {
2260 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002261 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002262 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002263 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2264 if (ExistingAttr) {
2265 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2266 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002267 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002268 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2269 Diag(Range.getBegin(), diag::note_previous_attribute);
2270 D->dropAttr<VisibilityAttr>();
2271 }
Michael Han51d8c522013-01-24 16:46:58 +00002272 return ::new (Context) VisibilityAttr(Range, Context, Vis,
2273 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002274}
2275
Chandler Carruth1b03c872011-07-02 00:01:44 +00002276static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002277 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002278 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002279 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002280
Peter Collingbourne7a730022010-11-23 20:45:58 +00002281 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002282 Arg = Arg->IgnoreParenCasts();
2283 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002284
Douglas Gregor5cee1192011-07-27 05:40:30 +00002285 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002287 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002288 return;
2289 }
Mike Stumpbf916502009-07-24 19:02:52 +00002290
Chris Lattner5f9e2722011-07-23 10:55:15 +00002291 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002292 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002293
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002294 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002295 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002296 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002297 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002298 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002299 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002300 else if (TypeStr == "protected") {
2301 // Complain about attempts to use protected visibility on targets
2302 // (like Darwin) that don't support it.
2303 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2304 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2305 type = VisibilityAttr::Default;
2306 } else {
2307 type = VisibilityAttr::Protected;
2308 }
2309 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002310 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002311 return;
2312 }
Mike Stumpbf916502009-07-24 19:02:52 +00002313
Michael Han51d8c522013-01-24 16:46:58 +00002314 unsigned Index = Attr.getAttributeSpellingListIndex();
2315 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type,
2316 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002317 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002318 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002319}
2320
Chandler Carruth1b03c872011-07-02 00:01:44 +00002321static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2322 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002323 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2324 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002326 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002327 return;
2328 }
2329
Chandler Carruth87c44602011-07-01 23:49:12 +00002330 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2331 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002333 << "objc_method_family" << 1;
2334 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002336 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002337 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002338 return;
2339 }
2340
Chris Lattner5f9e2722011-07-23 10:55:15 +00002341 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002342 ObjCMethodFamilyAttr::FamilyKind family;
2343 if (param == "none")
2344 family = ObjCMethodFamilyAttr::OMF_None;
2345 else if (param == "alloc")
2346 family = ObjCMethodFamilyAttr::OMF_alloc;
2347 else if (param == "copy")
2348 family = ObjCMethodFamilyAttr::OMF_copy;
2349 else if (param == "init")
2350 family = ObjCMethodFamilyAttr::OMF_init;
2351 else if (param == "mutableCopy")
2352 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2353 else if (param == "new")
2354 family = ObjCMethodFamilyAttr::OMF_new;
2355 else {
2356 // Just warn and ignore it. This is future-proof against new
2357 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002358 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002359 return;
2360 }
2361
John McCallf85e1932011-06-15 23:02:42 +00002362 if (family == ObjCMethodFamilyAttr::OMF_init &&
2363 !method->getResultType()->isObjCObjectPointerType()) {
2364 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2365 << method->getResultType();
2366 // Ignore the attribute.
2367 return;
2368 }
2369
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002370 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002371 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002372}
2373
Chandler Carruth1b03c872011-07-02 00:01:44 +00002374static void handleObjCExceptionAttr(Sema &S, Decl *D,
2375 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002376 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002377 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002378
Chris Lattner0db29ec2009-02-14 08:09:34 +00002379 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2380 if (OCI == 0) {
2381 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2382 return;
2383 }
Mike Stumpbf916502009-07-24 19:02:52 +00002384
Michael Han51d8c522013-01-24 16:46:58 +00002385 D->addAttr(::new (S.Context)
2386 ObjCExceptionAttr(Attr.getRange(), S.Context,
2387 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002388}
2389
Chandler Carruth1b03c872011-07-02 00:01:44 +00002390static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002391 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002393 return;
2394 }
Richard Smith162e1c12011-04-15 14:24:37 +00002395 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002396 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002397 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002398 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2399 return;
2400 }
2401 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002402 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2403 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002404 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002405 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2406 return;
2407 }
2408 }
2409 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002410 // It is okay to include this attribute on properties, e.g.:
2411 //
2412 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2413 //
2414 // In this case it follows tradition and suppresses an error in the above
2415 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002416 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002417 }
Michael Han51d8c522013-01-24 16:46:58 +00002418 D->addAttr(::new (S.Context)
2419 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2420 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002421}
2422
Mike Stumpbf916502009-07-24 19:02:52 +00002423static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002424handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002425 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002427 return;
2428 }
2429
2430 if (!isa<FunctionDecl>(D)) {
2431 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2432 return;
2433 }
2434
Michael Han51d8c522013-01-24 16:46:58 +00002435 D->addAttr(::new (S.Context)
2436 OverloadableAttr(Attr.getRange(), S.Context,
2437 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002438}
2439
Chandler Carruth1b03c872011-07-02 00:01:44 +00002440static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002441 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002443 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002444 return;
2445 }
Mike Stumpbf916502009-07-24 19:02:52 +00002446
Steve Naroff9eae5762008-09-18 16:44:58 +00002447 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002449 return;
2450 }
Mike Stumpbf916502009-07-24 19:02:52 +00002451
Sean Huntcf807c42010-08-18 23:23:40 +00002452 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002453 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002454 type = BlocksAttr::ByRef;
2455 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002456 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002457 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002458 return;
2459 }
Mike Stumpbf916502009-07-24 19:02:52 +00002460
Michael Han51d8c522013-01-24 16:46:58 +00002461 D->addAttr(::new (S.Context)
2462 BlocksAttr(Attr.getRange(), S.Context, type,
2463 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002464}
2465
Chandler Carruth1b03c872011-07-02 00:01:44 +00002466static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002467 // check the attribute arguments.
2468 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002469 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002470 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002471 }
2472
John McCall3323fad2011-09-09 07:56:05 +00002473 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002474 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002475 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002476 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002477 if (E->isTypeDependent() || E->isValueDependent() ||
2478 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002479 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002480 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002481 return;
2482 }
Mike Stumpbf916502009-07-24 19:02:52 +00002483
John McCall3323fad2011-09-09 07:56:05 +00002484 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002485 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2486 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002487 return;
2488 }
John McCall3323fad2011-09-09 07:56:05 +00002489
2490 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002491 }
2492
John McCall3323fad2011-09-09 07:56:05 +00002493 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002494 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002495 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002496 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002497 if (E->isTypeDependent() || E->isValueDependent() ||
2498 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002500 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002501 return;
2502 }
2503 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002504
John McCall3323fad2011-09-09 07:56:05 +00002505 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002506 // FIXME: This error message could be improved, it would be nice
2507 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002508 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2509 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002510 return;
2511 }
2512 }
2513
Chandler Carruth87c44602011-07-01 23:49:12 +00002514 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002515 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002516 if (isa<FunctionNoProtoType>(FT)) {
2517 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2518 return;
2519 }
Mike Stumpbf916502009-07-24 19:02:52 +00002520
Chris Lattner897cd902009-03-17 23:03:47 +00002521 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002522 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002523 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002524 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002525 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002526 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002527 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002528 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002529 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002530 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2531 if (!BD->isVariadic()) {
2532 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2533 return;
2534 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002535 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002536 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002537 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002538 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002539 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002540 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002541 int m = Ty->isFunctionPointerType() ? 0 : 1;
2542 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002543 return;
2544 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002545 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002547 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002548 return;
2549 }
Anders Carlsson77091822008-10-05 18:05:59 +00002550 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002552 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002553 return;
2554 }
Michael Han51d8c522013-01-24 16:46:58 +00002555 D->addAttr(::new (S.Context)
2556 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2557 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002558}
2559
Chandler Carruth1b03c872011-07-02 00:01:44 +00002560static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002561 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002562 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002563 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002564
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002565 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002566 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002567 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002568 return;
2569 }
Mike Stumpbf916502009-07-24 19:02:52 +00002570
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002571 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2572 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2573 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002574 return;
2575 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002576 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2577 if (MD->getResultType()->isVoidType()) {
2578 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2579 << Attr.getName() << 1;
2580 return;
2581 }
2582
Michael Han51d8c522013-01-24 16:46:58 +00002583 D->addAttr(::new (S.Context)
2584 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2585 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002586}
2587
Chandler Carruth1b03c872011-07-02 00:01:44 +00002588static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002589 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002590 if (Attr.hasParameterOrArguments()) {
2591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002592 return;
2593 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002594
Chandler Carruth87c44602011-07-01 23:49:12 +00002595 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002596 if (isa<CXXRecordDecl>(D)) {
2597 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2598 return;
2599 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002600 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2601 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002602 return;
2603 }
2604
Chandler Carruth87c44602011-07-01 23:49:12 +00002605 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002606
Michael Han51d8c522013-01-24 16:46:58 +00002607 nd->addAttr(::new (S.Context)
2608 WeakAttr(Attr.getRange(), S.Context,
2609 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002610}
2611
Chandler Carruth1b03c872011-07-02 00:01:44 +00002612static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002613 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002614 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002615 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002616
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002617
2618 // weak_import only applies to variable & function declarations.
2619 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002620 if (!D->canBeWeakImported(isDef)) {
2621 if (isDef)
2622 S.Diag(Attr.getLoc(),
2623 diag::warn_attribute_weak_import_invalid_on_definition)
2624 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002625 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002626 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002627 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002628 // Nothing to warn about here.
2629 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002631 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002632
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002633 return;
2634 }
2635
Michael Han51d8c522013-01-24 16:46:58 +00002636 D->addAttr(::new (S.Context)
2637 WeakImportAttr(Attr.getRange(), S.Context,
2638 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002639}
2640
Tanya Lattner0df579e2012-07-09 22:06:01 +00002641// Handles reqd_work_group_size and work_group_size_hint.
2642static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002643 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002644 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2645 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2646
Nate Begeman6f3d8382009-06-26 06:32:41 +00002647 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002648 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002649
2650 unsigned WGSize[3];
2651 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002652 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002653 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002654 if (E->isTypeDependent() || E->isValueDependent() ||
2655 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002656 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002657 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002658 return;
2659 }
2660 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2661 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002662
2663 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2664 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2665 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2666 if (!(A->getXDim() == WGSize[0] &&
2667 A->getYDim() == WGSize[1] &&
2668 A->getZDim() == WGSize[2])) {
2669 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2670 Attr.getName();
2671 }
2672 }
2673
2674 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2675 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2676 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2677 if (!(A->getXDim() == WGSize[0] &&
2678 A->getYDim() == WGSize[1] &&
2679 A->getZDim() == WGSize[2])) {
2680 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2681 Attr.getName();
2682 }
2683 }
2684
2685 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2686 D->addAttr(::new (S.Context)
2687 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002688 WGSize[0], WGSize[1], WGSize[2],
2689 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002690 else
2691 D->addAttr(::new (S.Context)
2692 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002693 WGSize[0], WGSize[1], WGSize[2],
2694 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002695}
2696
Rafael Espindola599f1b72012-05-13 03:25:18 +00002697SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002698 StringRef Name,
2699 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002700 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2701 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002702 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002703 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2704 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002705 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002706 }
Michael Han51d8c522013-01-24 16:46:58 +00002707 return ::new (Context) SectionAttr(Range, Context, Name,
2708 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002709}
2710
Chandler Carruth1b03c872011-07-02 00:01:44 +00002711static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002712 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002713 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002714 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002715
2716 // Make sure that there is a string literal as the sections's single
2717 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002718 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002719 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002720 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002721 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002722 return;
2723 }
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Chris Lattner797c3c42009-08-10 19:03:04 +00002725 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002726 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002727 if (!Error.empty()) {
2728 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2729 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002730 return;
2731 }
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002733 // This attribute cannot be applied to local variables.
2734 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2735 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2736 return;
2737 }
Michael Han51d8c522013-01-24 16:46:58 +00002738
2739 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002740 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002741 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002742 if (NewAttr)
2743 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002744}
2745
Chris Lattner6b6b5372008-06-26 18:38:35 +00002746
Chandler Carruth1b03c872011-07-02 00:01:44 +00002747static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002748 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002749 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002751 return;
2752 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002753
Chandler Carruth87c44602011-07-01 23:49:12 +00002754 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002755 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002756 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002757 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002758 D->addAttr(::new (S.Context)
2759 NoThrowAttr(Attr.getRange(), S.Context,
2760 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002761 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002762}
2763
Chandler Carruth1b03c872011-07-02 00:01:44 +00002764static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002765 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002766 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002767 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002768 return;
2769 }
Mike Stumpbf916502009-07-24 19:02:52 +00002770
Chandler Carruth87c44602011-07-01 23:49:12 +00002771 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002772 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002773 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002774 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002775 D->addAttr(::new (S.Context)
2776 ConstAttr(Attr.getRange(), S.Context,
2777 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002778 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002779}
2780
Chandler Carruth1b03c872011-07-02 00:01:44 +00002781static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002782 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002783 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002784 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002785
Michael Han51d8c522013-01-24 16:46:58 +00002786 D->addAttr(::new (S.Context)
2787 PureAttr(Attr.getRange(), S.Context,
2788 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002789}
2790
Chandler Carruth1b03c872011-07-02 00:01:44 +00002791static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002792 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002793 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2794 return;
2795 }
Mike Stumpbf916502009-07-24 19:02:52 +00002796
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002797 if (Attr.getNumArgs() != 0) {
2798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2799 return;
2800 }
Mike Stumpbf916502009-07-24 19:02:52 +00002801
Chandler Carruth87c44602011-07-01 23:49:12 +00002802 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002803
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002804 if (!VD || !VD->hasLocalStorage()) {
2805 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2806 return;
2807 }
Mike Stumpbf916502009-07-24 19:02:52 +00002808
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002809 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002810 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002811 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002812 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2813 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002814 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002815 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002816 Attr.getParameterName();
2817 return;
2818 }
Mike Stumpbf916502009-07-24 19:02:52 +00002819
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002820 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2821 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002822 S.Diag(Attr.getParameterLoc(),
2823 diag::err_attribute_cleanup_arg_not_function)
2824 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002825 return;
2826 }
2827
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002828 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002829 S.Diag(Attr.getParameterLoc(),
2830 diag::err_attribute_cleanup_func_must_take_one_arg)
2831 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002832 return;
2833 }
Mike Stumpbf916502009-07-24 19:02:52 +00002834
Anders Carlsson89941c12009-02-07 23:16:50 +00002835 // We're currently more strict than GCC about what function types we accept.
2836 // If this ever proves to be a problem it should be easy to fix.
2837 QualType Ty = S.Context.getPointerType(VD->getType());
2838 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002839 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2840 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002841 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002842 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2843 Attr.getParameterName() << ParamTy << Ty;
2844 return;
2845 }
Mike Stumpbf916502009-07-24 19:02:52 +00002846
Michael Han51d8c522013-01-24 16:46:58 +00002847 D->addAttr(::new (S.Context)
2848 CleanupAttr(Attr.getRange(), S.Context, FD,
2849 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002850 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002851}
2852
Mike Stumpbf916502009-07-24 19:02:52 +00002853/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002854/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002855static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002856 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002857 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002858
Chandler Carruth87c44602011-07-01 23:49:12 +00002859 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002860 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002861 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002862 return;
2863 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002864
2865 // In C++ the implicit 'this' function parameter also counts, and they are
2866 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002867 bool HasImplicitThisParam = isInstanceMethod(D);
2868 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002869 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002870
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002871 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002872 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002873 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002874 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2875 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002876 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2877 << "format" << 2 << IdxExpr->getSourceRange();
2878 return;
2879 }
Mike Stumpbf916502009-07-24 19:02:52 +00002880
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002881 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2882 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2883 << "format" << 2 << IdxExpr->getSourceRange();
2884 return;
2885 }
Mike Stumpbf916502009-07-24 19:02:52 +00002886
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002887 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002888
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002889 if (HasImplicitThisParam) {
2890 if (ArgIdx == 0) {
2891 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2892 << "format_arg" << IdxExpr->getSourceRange();
2893 return;
2894 }
2895 ArgIdx--;
2896 }
2897
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002898 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002899 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002900
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002901 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2902 if (not_nsstring_type &&
2903 !isCFStringType(Ty, S.Context) &&
2904 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002905 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002906 // FIXME: Should highlight the actual expression that has the wrong type.
2907 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002908 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002909 << IdxExpr->getSourceRange();
2910 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002911 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002912 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002913 if (!isNSStringType(Ty, S.Context) &&
2914 !isCFStringType(Ty, S.Context) &&
2915 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002916 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002917 // FIXME: Should highlight the actual expression that has the wrong type.
2918 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002919 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002920 << IdxExpr->getSourceRange();
2921 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002922 }
2923
Michael Han51d8c522013-01-24 16:46:58 +00002924 D->addAttr(::new (S.Context)
2925 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
2926 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002927}
2928
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002929enum FormatAttrKind {
2930 CFStringFormat,
2931 NSStringFormat,
2932 StrftimeFormat,
2933 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002934 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002935 InvalidFormat
2936};
2937
2938/// getFormatAttrKind - Map from format attribute names to supported format
2939/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002940static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002941 return llvm::StringSwitch<FormatAttrKind>(Format)
2942 // Check for formats that get handled specially.
2943 .Case("NSString", NSStringFormat)
2944 .Case("CFString", CFStringFormat)
2945 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002946
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002947 // Otherwise, check for supported formats.
2948 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2949 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2950 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002951
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002952 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2953 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002954}
2955
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002956/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002957/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002958static void handleInitPriorityAttr(Sema &S, Decl *D,
2959 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002960 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002961 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2962 return;
2963 }
2964
Chandler Carruth87c44602011-07-01 23:49:12 +00002965 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002966 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2967 Attr.setInvalid();
2968 return;
2969 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002970 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002971 if (S.Context.getAsArrayType(T))
2972 T = S.Context.getBaseElementType(T);
2973 if (!T->getAs<RecordType>()) {
2974 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2975 Attr.setInvalid();
2976 return;
2977 }
2978
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002979 if (Attr.getNumArgs() != 1) {
2980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2981 Attr.setInvalid();
2982 return;
2983 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002984 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002985
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002986 llvm::APSInt priority(32);
2987 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2988 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2989 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2990 << "init_priority" << priorityExpr->getSourceRange();
2991 Attr.setInvalid();
2992 return;
2993 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002994 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002995 if (prioritynum < 101 || prioritynum > 65535) {
2996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2997 << priorityExpr->getSourceRange();
2998 Attr.setInvalid();
2999 return;
3000 }
Michael Han51d8c522013-01-24 16:46:58 +00003001 D->addAttr(::new (S.Context)
3002 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3003 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003004}
3005
Rafael Espindola599f1b72012-05-13 03:25:18 +00003006FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003007 int FormatIdx, int FirstArg,
3008 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003009 // Check whether we already have an equivalent format attribute.
3010 for (specific_attr_iterator<FormatAttr>
3011 i = D->specific_attr_begin<FormatAttr>(),
3012 e = D->specific_attr_end<FormatAttr>();
3013 i != e ; ++i) {
3014 FormatAttr *f = *i;
3015 if (f->getType() == Format &&
3016 f->getFormatIdx() == FormatIdx &&
3017 f->getFirstArg() == FirstArg) {
3018 // If we don't have a valid location for this attribute, adopt the
3019 // location.
3020 if (f->getLocation().isInvalid())
3021 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003022 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003023 }
3024 }
3025
Michael Han51d8c522013-01-24 16:46:58 +00003026 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3027 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003028}
3029
Mike Stumpbf916502009-07-24 19:02:52 +00003030/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003031/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003032static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003033
Chris Lattner545dd342008-06-28 23:36:30 +00003034 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00003036 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003037 return;
3038 }
3039
Chris Lattner545dd342008-06-28 23:36:30 +00003040 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003041 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003042 return;
3043 }
3044
Chandler Carruth87c44602011-07-01 23:49:12 +00003045 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003047 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003048 return;
3049 }
3050
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003051 // In C++ the implicit 'this' function parameter also counts, and they are
3052 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003053 bool HasImplicitThisParam = isInstanceMethod(D);
3054 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003055 unsigned FirstIdx = 1;
3056
Chris Lattner5f9e2722011-07-23 10:55:15 +00003057 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003058
3059 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003060 if (Format.startswith("__") && Format.endswith("__"))
3061 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003062
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003063 // Check for supported formats.
3064 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003065
3066 if (Kind == IgnoredFormat)
3067 return;
3068
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003069 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003070 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003071 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003072 return;
3073 }
3074
3075 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003076 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003077 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003078 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3079 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003080 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003081 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003082 return;
3083 }
3084
3085 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003087 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003088 return;
3089 }
3090
3091 // FIXME: Do we need to bounds check?
3092 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003093
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003094 if (HasImplicitThisParam) {
3095 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003096 S.Diag(Attr.getLoc(),
3097 diag::err_format_attribute_implicit_this_format_string)
3098 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003099 return;
3100 }
3101 ArgIdx--;
3102 }
Mike Stump1eb44332009-09-09 15:08:12 +00003103
Chris Lattner6b6b5372008-06-26 18:38:35 +00003104 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003105 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003106
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003107 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003108 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003109 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3110 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003111 return;
3112 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003113 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003114 // FIXME: do we need to check if the type is NSString*? What are the
3115 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003116 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003117 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003118 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3119 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003120 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003121 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003122 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003123 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003124 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003125 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3126 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003127 return;
3128 }
3129
3130 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003131 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003132 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003133 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3134 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003136 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003137 return;
3138 }
3139
3140 // check if the function is variadic if the 3rd argument non-zero
3141 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003142 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003143 ++NumArgs; // +1 for ...
3144 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003145 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003146 return;
3147 }
3148 }
3149
Chris Lattner3c73c412008-11-19 08:23:25 +00003150 // strftime requires FirstArg to be 0 because it doesn't read from any
3151 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003152 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003153 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003154 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3155 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003156 return;
3157 }
3158 // if 0 it disables parameter checking (to use with e.g. va_list)
3159 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003161 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003162 return;
3163 }
3164
Rafael Espindola599f1b72012-05-13 03:25:18 +00003165 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3166 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003167 FirstArg.getZExtValue(),
3168 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003169 if (NewAttr)
3170 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003171}
3172
Chandler Carruth1b03c872011-07-02 00:01:44 +00003173static void handleTransparentUnionAttr(Sema &S, Decl *D,
3174 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003175 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003176 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003177 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003178
Chris Lattner6b6b5372008-06-26 18:38:35 +00003179
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003180 // Try to find the underlying union declaration.
3181 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003182 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003183 if (TD && TD->getUnderlyingType()->isUnionType())
3184 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3185 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003186 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003187
3188 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003189 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003190 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003191 return;
3192 }
3193
John McCall5e1cdac2011-10-07 06:10:15 +00003194 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003195 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003196 diag::warn_transparent_union_attribute_not_definition);
3197 return;
3198 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003199
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003200 RecordDecl::field_iterator Field = RD->field_begin(),
3201 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003202 if (Field == FieldEnd) {
3203 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3204 return;
3205 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003206
David Blaikie581deb32012-06-06 20:45:41 +00003207 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003208 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003209 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003210 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003211 diag::warn_transparent_union_attribute_floating)
3212 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003213 return;
3214 }
3215
3216 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3217 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3218 for (; Field != FieldEnd; ++Field) {
3219 QualType FieldType = Field->getType();
3220 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3221 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3222 // Warn if we drop the attribute.
3223 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003224 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003225 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003226 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003227 diag::warn_transparent_union_attribute_field_size_align)
3228 << isSize << Field->getDeclName() << FieldBits;
3229 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003230 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003231 diag::note_transparent_union_first_field_size_align)
3232 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003233 return;
3234 }
3235 }
3236
Michael Han51d8c522013-01-24 16:46:58 +00003237 RD->addAttr(::new (S.Context)
3238 TransparentUnionAttr(Attr.getRange(), S.Context,
3239 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003240}
3241
Chandler Carruth1b03c872011-07-02 00:01:44 +00003242static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003243 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003244 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003245 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003246
Peter Collingbourne7a730022010-11-23 20:45:58 +00003247 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003248 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003249
Chris Lattner6b6b5372008-06-26 18:38:35 +00003250 // Make sure that there is a string literal as the annotation's single
3251 // argument.
3252 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003253 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003254 return;
3255 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003256
3257 // Don't duplicate annotations that are already set.
3258 for (specific_attr_iterator<AnnotateAttr>
3259 i = D->specific_attr_begin<AnnotateAttr>(),
3260 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3261 if ((*i)->getAnnotation() == SE->getString())
3262 return;
3263 }
Michael Han51d8c522013-01-24 16:46:58 +00003264
3265 D->addAttr(::new (S.Context)
3266 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3267 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003268}
3269
Chandler Carruth1b03c872011-07-02 00:01:44 +00003270static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003271 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003272 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003274 return;
3275 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003276
Richard Smithbe507b62013-02-01 08:12:08 +00003277 // FIXME: The C++11 version of this attribute should error out when it is
3278 // used to specify a weaker alignment, rather than being silently
3279 // ignored. This constraint cannot be applied until we have seen
3280 // all the attributes which apply to the variable.
3281
3282 if (Attr.getNumArgs() == 0) {
3283 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3284 true, 0, Attr.getAttributeSpellingListIndex()));
3285 return;
3286 }
3287
3288 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3289 Attr.getAttributeSpellingListIndex());
3290}
3291
3292void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3293 unsigned SpellingListIndex) {
3294 // FIXME: Handle pack-expansions here.
3295 if (DiagnoseUnexpandedParameterPack(E))
3296 return;
3297
3298 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3299 SourceLocation AttrLoc = AttrRange.getBegin();
3300
Richard Smith4cd81c52013-01-29 09:02:09 +00003301 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003302 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003303 // C++11 [dcl.align]p1:
3304 // An alignment-specifier may be applied to a variable or to a class
3305 // data member, but it shall not be applied to a bit-field, a function
3306 // parameter, the formal parameter of a catch clause, or a variable
3307 // declared with the register storage class specifier. An
3308 // alignment-specifier may also be applied to the declaration of a class
3309 // or enumeration type.
3310 // C11 6.7.5/2:
3311 // An alignment attribute shall not be specified in a declaration of
3312 // a typedef, or a bit-field, or a function, or a parameter, or an
3313 // object declared with the register storage-class specifier.
3314 int DiagKind = -1;
3315 if (isa<ParmVarDecl>(D)) {
3316 DiagKind = 0;
3317 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3318 if (VD->getStorageClass() == SC_Register)
3319 DiagKind = 1;
3320 if (VD->isExceptionVariable())
3321 DiagKind = 2;
3322 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3323 if (FD->isBitField())
3324 DiagKind = 3;
3325 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003326 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3327 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003328 << (TmpAttr.isC11() ? ExpectedVariableOrField
3329 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003330 return;
3331 }
3332 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003333 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
3334 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
3335 << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003336 return;
3337 }
3338 }
3339
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003340 if (E->isTypeDependent() || E->isValueDependent()) {
3341 // Save dependent expressions in the AST to be instantiated.
Richard Smithbe507b62013-02-01 08:12:08 +00003342 D->addAttr(::new (Context) AlignedAttr(TmpAttr));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003343 return;
3344 }
Michael Hana31f65b2013-02-01 01:19:17 +00003345
Sean Huntcf807c42010-08-18 23:23:40 +00003346 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003347 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003348 ExprResult ICE
3349 = VerifyIntegerConstantExpression(E, &Alignment,
3350 diag::err_aligned_attribute_argument_not_int,
3351 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003352 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003353 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003354
3355 // C++11 [dcl.align]p2:
3356 // -- if the constant expression evaluates to zero, the alignment
3357 // specifier shall have no effect
3358 // C11 6.7.5p6:
3359 // An alignment specification of zero has no effect.
3360 if (!(TmpAttr.isAlignas() && !Alignment) &&
3361 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003362 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3363 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003364 return;
3365 }
Michael Hana31f65b2013-02-01 01:19:17 +00003366
Richard Smithbe507b62013-02-01 08:12:08 +00003367 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003368 // We've already verified it's a power of 2, now let's make sure it's
3369 // 8192 or less.
3370 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003371 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003372 << E->getSourceRange();
3373 return;
3374 }
3375 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003376
Richard Smithbe507b62013-02-01 08:12:08 +00003377 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true,
3378 ICE.take(), SpellingListIndex));
Sean Huntcf807c42010-08-18 23:23:40 +00003379}
3380
Michael Hana31f65b2013-02-01 01:19:17 +00003381void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3382 unsigned SpellingListIndex) {
Sean Huntcf807c42010-08-18 23:23:40 +00003383 // FIXME: Cache the number on the Attr object if non-dependent?
3384 // FIXME: Perform checking of type validity
Michael Hana31f65b2013-02-01 01:19:17 +00003385 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3386 SpellingListIndex));
Sean Huntcf807c42010-08-18 23:23:40 +00003387 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003388}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003389
Richard Smithbe507b62013-02-01 08:12:08 +00003390void Sema::CheckAlignasUnderalignment(Decl *D) {
3391 assert(D->hasAttrs() && "no attributes on decl");
3392
3393 QualType Ty;
3394 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3395 Ty = VD->getType();
3396 else
3397 Ty = Context.getTagDeclType(cast<TagDecl>(D));
3398 if (Ty->isDependentType())
3399 return;
3400
3401 // C++11 [dcl.align]p5, C11 6.7.5/4:
3402 // The combined effect of all alignment attributes in a declaration shall
3403 // not specify an alignment that is less strict than the alignment that
3404 // would otherwise be required for the entity being declared.
3405 AlignedAttr *AlignasAttr = 0;
3406 unsigned Align = 0;
3407 for (specific_attr_iterator<AlignedAttr>
3408 I = D->specific_attr_begin<AlignedAttr>(),
3409 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3410 if (I->isAlignmentDependent())
3411 return;
3412 if (I->isAlignas())
3413 AlignasAttr = *I;
3414 Align = std::max(Align, I->getAlignment(Context));
3415 }
3416
3417 if (AlignasAttr && Align) {
3418 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3419 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3420 if (NaturalAlign > RequestedAlign)
3421 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3422 << Ty << (unsigned)NaturalAlign.getQuantity();
3423 }
3424}
3425
Chandler Carruthd309c812011-07-01 23:49:16 +00003426/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003427/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003428///
Mike Stumpbf916502009-07-24 19:02:52 +00003429/// Despite what would be logical, the mode attribute is a decl attribute, not a
3430/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3431/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003432static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003433 // This attribute isn't documented, but glibc uses it. It changes
3434 // the width of an int or unsigned int to the specified size.
3435
3436 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003437 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003438 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003439
Chris Lattnerfbf13472008-06-27 22:18:37 +00003440
3441 IdentifierInfo *Name = Attr.getParameterName();
3442 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003443 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003444 return;
3445 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003446
Chris Lattner5f9e2722011-07-23 10:55:15 +00003447 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003448
3449 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003450 if (Str.startswith("__") && Str.endswith("__"))
3451 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003452
3453 unsigned DestWidth = 0;
3454 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003455 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003456 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003457 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003458 switch (Str[0]) {
3459 case 'Q': DestWidth = 8; break;
3460 case 'H': DestWidth = 16; break;
3461 case 'S': DestWidth = 32; break;
3462 case 'D': DestWidth = 64; break;
3463 case 'X': DestWidth = 96; break;
3464 case 'T': DestWidth = 128; break;
3465 }
3466 if (Str[1] == 'F') {
3467 IntegerMode = false;
3468 } else if (Str[1] == 'C') {
3469 IntegerMode = false;
3470 ComplexMode = true;
3471 } else if (Str[1] != 'I') {
3472 DestWidth = 0;
3473 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003474 break;
3475 case 4:
3476 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3477 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003478 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003479 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003480 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003481 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003482 break;
3483 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003484 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003485 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003486 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003487 case 11:
3488 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003489 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003490 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003491 }
3492
3493 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003494 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003495 OldTy = TD->getUnderlyingType();
3496 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3497 OldTy = VD->getType();
3498 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003499 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003500 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003501 return;
3502 }
Eli Friedman73397492009-03-03 06:41:03 +00003503
John McCall183700f2009-09-21 23:43:11 +00003504 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003505 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3506 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003507 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003508 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3509 } else if (ComplexMode) {
3510 if (!OldTy->isComplexType())
3511 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3512 } else {
3513 if (!OldTy->isFloatingType())
3514 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3515 }
3516
Mike Stump390b4cc2009-05-16 07:39:55 +00003517 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3518 // and friends, at least with glibc.
3519 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3520 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003521 // FIXME: Make sure floating-point mappings are accurate
3522 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003523 QualType NewTy;
3524 switch (DestWidth) {
3525 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003526 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003527 return;
3528 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003529 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003530 return;
3531 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003532 if (!IntegerMode) {
3533 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3534 return;
3535 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003536 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003537 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003538 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003539 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003540 break;
3541 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003542 if (!IntegerMode) {
3543 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3544 return;
3545 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003546 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003547 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003548 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003549 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003550 break;
3551 case 32:
3552 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003553 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003554 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003555 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003556 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003557 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003558 break;
3559 case 64:
3560 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003561 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003562 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003563 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003564 NewTy = S.Context.LongTy;
3565 else
3566 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003567 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003568 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003569 NewTy = S.Context.UnsignedLongTy;
3570 else
3571 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003572 break;
Eli Friedman73397492009-03-03 06:41:03 +00003573 case 96:
3574 NewTy = S.Context.LongDoubleTy;
3575 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003576 case 128:
3577 if (!IntegerMode) {
3578 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3579 return;
3580 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003581 if (OldTy->isSignedIntegerType())
3582 NewTy = S.Context.Int128Ty;
3583 else
3584 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003585 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003586 }
3587
Eli Friedman73397492009-03-03 06:41:03 +00003588 if (ComplexMode) {
3589 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003590 }
3591
3592 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003593 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003594 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003595 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003596 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003597 cast<ValueDecl>(D)->setType(NewTy);
3598}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003599
Chandler Carruth1b03c872011-07-02 00:01:44 +00003600static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003601 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003602 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003603 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003604
Nick Lewycky78d1a102012-07-24 01:40:49 +00003605 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3606 if (!VD->hasGlobalStorage())
3607 S.Diag(Attr.getLoc(),
3608 diag::warn_attribute_requires_functions_or_static_globals)
3609 << Attr.getName();
3610 } else if (!isFunctionOrMethod(D)) {
3611 S.Diag(Attr.getLoc(),
3612 diag::warn_attribute_requires_functions_or_static_globals)
3613 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003614 return;
3615 }
Mike Stumpbf916502009-07-24 19:02:52 +00003616
Michael Han51d8c522013-01-24 16:46:58 +00003617 D->addAttr(::new (S.Context)
3618 NoDebugAttr(Attr.getRange(), S.Context,
3619 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003620}
3621
Chandler Carruth1b03c872011-07-02 00:01:44 +00003622static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003623 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003624 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003625 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003626
Mike Stumpbf916502009-07-24 19:02:52 +00003627
Chandler Carruth87c44602011-07-01 23:49:12 +00003628 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003630 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003631 return;
3632 }
Mike Stumpbf916502009-07-24 19:02:52 +00003633
Michael Han51d8c522013-01-24 16:46:58 +00003634 D->addAttr(::new (S.Context)
3635 NoInlineAttr(Attr.getRange(), S.Context,
3636 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003637}
3638
Chandler Carruth1b03c872011-07-02 00:01:44 +00003639static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3640 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003641 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003642 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003643 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003644
Chris Lattner7255a2d2010-06-22 00:03:40 +00003645
Chandler Carruth87c44602011-07-01 23:49:12 +00003646 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003648 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003649 return;
3650 }
3651
Michael Han51d8c522013-01-24 16:46:58 +00003652 D->addAttr(::new (S.Context)
3653 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3654 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003655}
3656
Chandler Carruth1b03c872011-07-02 00:01:44 +00003657static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003658 if (S.LangOpts.CUDA) {
3659 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003660 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3662 return;
3663 }
3664
Chandler Carruth87c44602011-07-01 23:49:12 +00003665 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003667 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003668 return;
3669 }
3670
Michael Han51d8c522013-01-24 16:46:58 +00003671 D->addAttr(::new (S.Context)
3672 CUDAConstantAttr(Attr.getRange(), S.Context,
3673 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003674 } else {
3675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3676 }
3677}
3678
Chandler Carruth1b03c872011-07-02 00:01:44 +00003679static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003680 if (S.LangOpts.CUDA) {
3681 // check the attribute arguments.
3682 if (Attr.getNumArgs() != 0) {
3683 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3684 return;
3685 }
3686
Chandler Carruth87c44602011-07-01 23:49:12 +00003687 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003689 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003690 return;
3691 }
3692
Michael Han51d8c522013-01-24 16:46:58 +00003693 D->addAttr(::new (S.Context)
3694 CUDADeviceAttr(Attr.getRange(), S.Context,
3695 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003696 } else {
3697 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3698 }
3699}
3700
Chandler Carruth1b03c872011-07-02 00:01:44 +00003701static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003702 if (S.LangOpts.CUDA) {
3703 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003704 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003705 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003706
Chandler Carruth87c44602011-07-01 23:49:12 +00003707 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003708 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003709 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003710 return;
3711 }
3712
Chandler Carruth87c44602011-07-01 23:49:12 +00003713 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003714 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003715 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003716 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3717 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3718 << FD->getType()
3719 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3720 "void");
3721 } else {
3722 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3723 << FD->getType();
3724 }
3725 return;
3726 }
3727
Michael Han51d8c522013-01-24 16:46:58 +00003728 D->addAttr(::new (S.Context)
3729 CUDAGlobalAttr(Attr.getRange(), S.Context,
3730 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003731 } else {
3732 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3733 }
3734}
3735
Chandler Carruth1b03c872011-07-02 00:01:44 +00003736static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003737 if (S.LangOpts.CUDA) {
3738 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003739 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003740 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003741
Peter Collingbourneced76712010-12-01 03:15:31 +00003742
Chandler Carruth87c44602011-07-01 23:49:12 +00003743 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003745 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003746 return;
3747 }
3748
Michael Han51d8c522013-01-24 16:46:58 +00003749 D->addAttr(::new (S.Context)
3750 CUDAHostAttr(Attr.getRange(), S.Context,
3751 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003752 } else {
3753 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3754 }
3755}
3756
Chandler Carruth1b03c872011-07-02 00:01:44 +00003757static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003758 if (S.LangOpts.CUDA) {
3759 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003760 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003761 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003762
Chandler Carruth87c44602011-07-01 23:49:12 +00003763 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003764 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003765 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003766 return;
3767 }
3768
Michael Han51d8c522013-01-24 16:46:58 +00003769 D->addAttr(::new (S.Context)
3770 CUDASharedAttr(Attr.getRange(), S.Context,
3771 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003772 } else {
3773 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3774 }
3775}
3776
Chandler Carruth1b03c872011-07-02 00:01:44 +00003777static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003778 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003779 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003780 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003781
Chandler Carruth87c44602011-07-01 23:49:12 +00003782 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003783 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003784 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003785 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003786 return;
3787 }
Mike Stumpbf916502009-07-24 19:02:52 +00003788
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003789 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003790 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003791 return;
3792 }
Mike Stumpbf916502009-07-24 19:02:52 +00003793
Michael Han51d8c522013-01-24 16:46:58 +00003794 D->addAttr(::new (S.Context)
3795 GNUInlineAttr(Attr.getRange(), S.Context,
3796 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003797}
3798
Chandler Carruth1b03c872011-07-02 00:01:44 +00003799static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003800 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003801
Aaron Ballmanfff32482012-12-09 17:45:41 +00003802 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003803 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003804 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3805 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003806 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003807 return;
3808
Chandler Carruth87c44602011-07-01 23:49:12 +00003809 if (!isa<ObjCMethodDecl>(D)) {
3810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3811 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003812 return;
3813 }
3814
Chandler Carruth87c44602011-07-01 23:49:12 +00003815 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003816 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003817 D->addAttr(::new (S.Context)
3818 FastCallAttr(Attr.getRange(), S.Context,
3819 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003820 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003821 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003822 D->addAttr(::new (S.Context)
3823 StdCallAttr(Attr.getRange(), S.Context,
3824 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003825 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003826 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003827 D->addAttr(::new (S.Context)
3828 ThisCallAttr(Attr.getRange(), S.Context,
3829 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003830 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003831 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003832 D->addAttr(::new (S.Context)
3833 CDeclAttr(Attr.getRange(), S.Context,
3834 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003835 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003836 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003837 D->addAttr(::new (S.Context)
3838 PascalAttr(Attr.getRange(), S.Context,
3839 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003840 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003841 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003842 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003843 switch (CC) {
3844 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003845 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003846 break;
3847 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003848 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003849 break;
3850 default:
3851 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003852 }
3853
Michael Han51d8c522013-01-24 16:46:58 +00003854 D->addAttr(::new (S.Context)
3855 PcsAttr(Attr.getRange(), S.Context, PCS,
3856 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003857 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003858 }
Derek Schuff263366f2012-10-16 22:30:41 +00003859 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003860 D->addAttr(::new (S.Context)
3861 PnaclCallAttr(Attr.getRange(), S.Context,
3862 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003863 return;
Guy Benyei38980082012-12-25 08:53:55 +00003864 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003865 D->addAttr(::new (S.Context)
3866 IntelOclBiccAttr(Attr.getRange(), S.Context,
3867 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003868 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003869
Abramo Bagnarae215f722010-04-30 13:10:51 +00003870 default:
3871 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003872 }
3873}
3874
Chandler Carruth1b03c872011-07-02 00:01:44 +00003875static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003876 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003877 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003878}
3879
Aaron Ballmanfff32482012-12-09 17:45:41 +00003880bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3881 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003882 if (attr.isInvalid())
3883 return true;
3884
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003885 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3886 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3887 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003888 attr.setInvalid();
3889 return true;
3890 }
3891
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003892 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3893 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003894 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003895 case AttributeList::AT_CDecl: CC = CC_C; break;
3896 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3897 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3898 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3899 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3900 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003901 Expr *Arg = attr.getArg(0);
3902 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003903 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003904 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3905 << "pcs" << 1;
3906 attr.setInvalid();
3907 return true;
3908 }
3909
Chris Lattner5f9e2722011-07-23 10:55:15 +00003910 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003911 if (StrRef == "aapcs") {
3912 CC = CC_AAPCS;
3913 break;
3914 } else if (StrRef == "aapcs-vfp") {
3915 CC = CC_AAPCS_VFP;
3916 break;
3917 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003918
3919 attr.setInvalid();
3920 Diag(attr.getLoc(), diag::err_invalid_pcs);
3921 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003922 }
Derek Schuff263366f2012-10-16 22:30:41 +00003923 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003924 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003925 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003926 }
3927
Aaron Ballman82bfa192012-10-02 14:26:08 +00003928 const TargetInfo &TI = Context.getTargetInfo();
3929 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3930 if (A == TargetInfo::CCCR_Warning) {
3931 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003932
3933 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3934 if (FD)
3935 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3936 TargetInfo::CCMT_NonMember;
3937 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003938 }
3939
John McCall711c52b2011-01-05 12:14:39 +00003940 return false;
3941}
3942
Chandler Carruth1b03c872011-07-02 00:01:44 +00003943static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003944 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003945
3946 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003947 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003948 return;
3949
Chandler Carruth87c44602011-07-01 23:49:12 +00003950 if (!isa<ObjCMethodDecl>(D)) {
3951 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3952 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003953 return;
3954 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003955
Michael Han51d8c522013-01-24 16:46:58 +00003956 D->addAttr(::new (S.Context)
3957 RegparmAttr(Attr.getRange(), S.Context, numParams,
3958 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003959}
3960
3961/// Checks a regparm attribute, returning true if it is ill-formed and
3962/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003963bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3964 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003965 return true;
3966
Chandler Carruth87c44602011-07-01 23:49:12 +00003967 if (Attr.getNumArgs() != 1) {
3968 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3969 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003970 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003971 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003972
Chandler Carruth87c44602011-07-01 23:49:12 +00003973 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003974 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003975 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003976 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003977 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003978 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003979 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003980 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003981 }
3982
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003983 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003984 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003985 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003986 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003987 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003988 }
3989
John McCall711c52b2011-01-05 12:14:39 +00003990 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003991 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003992 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003993 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003994 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003995 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003996 }
3997
John McCall711c52b2011-01-05 12:14:39 +00003998 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003999}
4000
Chandler Carruth1b03c872011-07-02 00:01:44 +00004001static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004002 if (S.LangOpts.CUDA) {
4003 // check the attribute arguments.
4004 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004005 // FIXME: 0 is not okay.
4006 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004007 return;
4008 }
4009
Chandler Carruth87c44602011-07-01 23:49:12 +00004010 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004011 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004012 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004013 return;
4014 }
4015
4016 Expr *MaxThreadsExpr = Attr.getArg(0);
4017 llvm::APSInt MaxThreads(32);
4018 if (MaxThreadsExpr->isTypeDependent() ||
4019 MaxThreadsExpr->isValueDependent() ||
4020 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4021 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4022 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4023 return;
4024 }
4025
4026 llvm::APSInt MinBlocks(32);
4027 if (Attr.getNumArgs() > 1) {
4028 Expr *MinBlocksExpr = Attr.getArg(1);
4029 if (MinBlocksExpr->isTypeDependent() ||
4030 MinBlocksExpr->isValueDependent() ||
4031 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4032 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4033 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4034 return;
4035 }
4036 }
4037
Michael Han51d8c522013-01-24 16:46:58 +00004038 D->addAttr(::new (S.Context)
4039 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4040 MaxThreads.getZExtValue(),
4041 MinBlocks.getZExtValue(),
4042 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004043 } else {
4044 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4045 }
4046}
4047
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004048static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4049 const AttributeList &Attr) {
4050 StringRef AttrName = Attr.getName()->getName();
4051 if (!Attr.getParameterName()) {
4052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4053 << Attr.getName() << /* arg num = */ 1;
4054 return;
4055 }
4056
4057 if (Attr.getNumArgs() != 2) {
4058 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4059 << /* required args = */ 3;
4060 return;
4061 }
4062
4063 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4064
4065 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4066 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4067 << Attr.getName() << ExpectedFunctionOrMethod;
4068 return;
4069 }
4070
4071 uint64_t ArgumentIdx;
4072 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4073 Attr.getLoc(), 2,
4074 Attr.getArg(0), ArgumentIdx))
4075 return;
4076
4077 uint64_t TypeTagIdx;
4078 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4079 Attr.getLoc(), 3,
4080 Attr.getArg(1), TypeTagIdx))
4081 return;
4082
4083 bool IsPointer = (AttrName == "pointer_with_type_tag");
4084 if (IsPointer) {
4085 // Ensure that buffer has a pointer type.
4086 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4087 if (!BufferTy->isPointerType()) {
4088 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
4089 << AttrName;
4090 }
4091 }
4092
Michael Han51d8c522013-01-24 16:46:58 +00004093 D->addAttr(::new (S.Context)
4094 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4095 ArgumentIdx, TypeTagIdx, IsPointer,
4096 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004097}
4098
4099static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4100 const AttributeList &Attr) {
4101 IdentifierInfo *PointerKind = Attr.getParameterName();
4102 if (!PointerKind) {
4103 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4104 << "type_tag_for_datatype" << 1;
4105 return;
4106 }
4107
4108 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4109
Michael Han51d8c522013-01-24 16:46:58 +00004110 D->addAttr(::new (S.Context)
4111 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4112 MatchingCType,
4113 Attr.getLayoutCompatible(),
4114 Attr.getMustBeNull(),
4115 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004116}
4117
Chris Lattner0744e5f2008-06-29 00:23:49 +00004118//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004119// Checker-specific attribute handlers.
4120//===----------------------------------------------------------------------===//
4121
John McCallc7ad3812011-01-25 03:31:58 +00004122static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004123 return type->isDependentType() ||
4124 type->isObjCObjectPointerType() ||
4125 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004126}
4127static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004128 return type->isDependentType() ||
4129 type->isPointerType() ||
4130 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004131}
4132
Chandler Carruth1b03c872011-07-02 00:01:44 +00004133static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004134 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004135 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004136 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004137 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004138 return;
4139 }
4140
4141 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004142 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004143 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4144 cf = false;
4145 } else {
4146 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4147 cf = true;
4148 }
4149
4150 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004151 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004152 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004153 return;
4154 }
4155
4156 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004157 param->addAttr(::new (S.Context)
4158 CFConsumedAttr(Attr.getRange(), S.Context,
4159 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004160 else
Michael Han51d8c522013-01-24 16:46:58 +00004161 param->addAttr(::new (S.Context)
4162 NSConsumedAttr(Attr.getRange(), S.Context,
4163 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004164}
4165
Chandler Carruth1b03c872011-07-02 00:01:44 +00004166static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4167 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004168 if (!isa<ObjCMethodDecl>(D)) {
4169 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004170 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004171 return;
4172 }
4173
Michael Han51d8c522013-01-24 16:46:58 +00004174 D->addAttr(::new (S.Context)
4175 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4176 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004177}
4178
Chandler Carruth1b03c872011-07-02 00:01:44 +00004179static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4180 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004181
John McCallc7ad3812011-01-25 03:31:58 +00004182 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004183
Chandler Carruth87c44602011-07-01 23:49:12 +00004184 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004185 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004186 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004187 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004188 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004189 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4190 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004191 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004192 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004193 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004194 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004195 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004196 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004197 return;
4198 }
Mike Stumpbf916502009-07-24 19:02:52 +00004199
John McCallc7ad3812011-01-25 03:31:58 +00004200 bool typeOK;
4201 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004202 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004203 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004204 case AttributeList::AT_NSReturnsAutoreleased:
4205 case AttributeList::AT_NSReturnsRetained:
4206 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004207 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4208 cf = false;
4209 break;
4210
Sean Hunt8e083e72012-06-19 23:57:03 +00004211 case AttributeList::AT_CFReturnsRetained:
4212 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004213 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4214 cf = true;
4215 break;
4216 }
4217
4218 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004219 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004220 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004221 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004222 }
Mike Stumpbf916502009-07-24 19:02:52 +00004223
Chandler Carruth87c44602011-07-01 23:49:12 +00004224 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004225 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004226 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004227 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004228 D->addAttr(::new (S.Context)
4229 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4230 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004231 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004232 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004233 D->addAttr(::new (S.Context)
4234 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4235 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004236 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004237 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004238 D->addAttr(::new (S.Context)
4239 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4240 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004241 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004242 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004243 D->addAttr(::new (S.Context)
4244 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4245 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004246 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004247 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004248 D->addAttr(::new (S.Context)
4249 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4250 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004251 return;
4252 };
4253}
4254
John McCalldc7c5ad2011-07-22 08:53:00 +00004255static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4256 const AttributeList &attr) {
4257 SourceLocation loc = attr.getLoc();
4258
4259 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4260
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004261 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004262 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004263 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004264 return;
4265 }
4266
4267 // Check that the method returns a normal pointer.
4268 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004269
4270 if (!resultType->isReferenceType() &&
4271 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004272 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4273 << SourceRange(loc)
4274 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4275
4276 // Drop the attribute.
4277 return;
4278 }
4279
Michael Han51d8c522013-01-24 16:46:58 +00004280 method->addAttr(::new (S.Context)
4281 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4282 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004283}
4284
Fariborz Jahanian84101132012-09-07 23:46:23 +00004285static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4286 const AttributeList &attr) {
4287 SourceLocation loc = attr.getLoc();
4288 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4289
4290 if (!method) {
4291 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4292 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4293 return;
4294 }
4295 DeclContext *DC = method->getDeclContext();
4296 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4297 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4298 << attr.getName() << 0;
4299 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4300 return;
4301 }
4302 if (method->getMethodFamily() == OMF_dealloc) {
4303 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4304 << attr.getName() << 1;
4305 return;
4306 }
4307
Michael Han51d8c522013-01-24 16:46:58 +00004308 method->addAttr(::new (S.Context)
4309 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4310 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004311}
4312
John McCall8dfac0b2011-09-30 05:12:12 +00004313/// Handle cf_audited_transfer and cf_unknown_transfer.
4314static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4315 if (!isa<FunctionDecl>(D)) {
4316 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004317 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004318 return;
4319 }
4320
Sean Hunt8e083e72012-06-19 23:57:03 +00004321 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004322
4323 // Check whether there's a conflicting attribute already present.
4324 Attr *Existing;
4325 if (IsAudited) {
4326 Existing = D->getAttr<CFUnknownTransferAttr>();
4327 } else {
4328 Existing = D->getAttr<CFAuditedTransferAttr>();
4329 }
4330 if (Existing) {
4331 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4332 << A.getName()
4333 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4334 << A.getRange() << Existing->getRange();
4335 return;
4336 }
4337
4338 // All clear; add the attribute.
4339 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004340 D->addAttr(::new (S.Context)
4341 CFAuditedTransferAttr(A.getRange(), S.Context,
4342 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004343 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004344 D->addAttr(::new (S.Context)
4345 CFUnknownTransferAttr(A.getRange(), S.Context,
4346 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004347 }
4348}
4349
John McCallfe98da02011-09-29 07:17:38 +00004350static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4351 const AttributeList &Attr) {
4352 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4353 if (!RD || RD->isUnion()) {
4354 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004355 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004356 }
4357
4358 IdentifierInfo *ParmName = Attr.getParameterName();
4359
4360 // In Objective-C, verify that the type names an Objective-C type.
4361 // We don't want to check this outside of ObjC because people sometimes
4362 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004363 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004364 // Check for an existing type with this name.
4365 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4366 Sema::LookupOrdinaryName);
4367 if (S.LookupName(R, Sc)) {
4368 NamedDecl *Target = R.getFoundDecl();
4369 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4370 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4371 S.Diag(Target->getLocStart(), diag::note_declared_at);
4372 }
4373 }
4374 }
4375
Michael Han51d8c522013-01-24 16:46:58 +00004376 D->addAttr(::new (S.Context)
4377 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4378 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004379}
4380
Chandler Carruth1b03c872011-07-02 00:01:44 +00004381static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4382 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004383 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004384
Chandler Carruth87c44602011-07-01 23:49:12 +00004385 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004386 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004387}
4388
Chandler Carruth1b03c872011-07-02 00:01:44 +00004389static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4390 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004391 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004392 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004393 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004394 return;
4395 }
4396
Chandler Carruth87c44602011-07-01 23:49:12 +00004397 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004398 QualType type = vd->getType();
4399
4400 if (!type->isDependentType() &&
4401 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004402 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004403 << type;
4404 return;
4405 }
4406
4407 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4408
4409 // If we have no lifetime yet, check the lifetime we're presumably
4410 // going to infer.
4411 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4412 lifetime = type->getObjCARCImplicitLifetime();
4413
4414 switch (lifetime) {
4415 case Qualifiers::OCL_None:
4416 assert(type->isDependentType() &&
4417 "didn't infer lifetime for non-dependent type?");
4418 break;
4419
4420 case Qualifiers::OCL_Weak: // meaningful
4421 case Qualifiers::OCL_Strong: // meaningful
4422 break;
4423
4424 case Qualifiers::OCL_ExplicitNone:
4425 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004426 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004427 << (lifetime == Qualifiers::OCL_Autoreleasing);
4428 break;
4429 }
4430
Chandler Carruth87c44602011-07-01 23:49:12 +00004431 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004432 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4433 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004434}
4435
Francois Pichet11542142010-12-19 06:50:37 +00004436//===----------------------------------------------------------------------===//
4437// Microsoft specific attribute handlers.
4438//===----------------------------------------------------------------------===//
4439
Chandler Carruth1b03c872011-07-02 00:01:44 +00004440static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004441 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004442 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004443 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004444 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004445
Francois Pichet11542142010-12-19 06:50:37 +00004446 Expr *Arg = Attr.getArg(0);
4447 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004448 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004449 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4450 << "uuid" << 1;
4451 return;
4452 }
4453
Chris Lattner5f9e2722011-07-23 10:55:15 +00004454 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004455
4456 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4457 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004458
Francois Pichetd3d3be92010-12-20 01:41:49 +00004459 // Validate GUID length.
4460 if (IsCurly && StrRef.size() != 38) {
4461 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4462 return;
4463 }
4464 if (!IsCurly && StrRef.size() != 36) {
4465 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4466 return;
4467 }
4468
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004469 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004470 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004471 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004472 if (IsCurly) // Skip the optional '{'
4473 ++I;
4474
4475 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004476 if (i == 8 || i == 13 || i == 18 || i == 23) {
4477 if (*I != '-') {
4478 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4479 return;
4480 }
4481 } else if (!isxdigit(*I)) {
4482 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4483 return;
4484 }
4485 I++;
4486 }
Francois Pichet11542142010-12-19 06:50:37 +00004487
Michael Han51d8c522013-01-24 16:46:58 +00004488 D->addAttr(::new (S.Context)
4489 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4490 Attr.getAttributeSpellingListIndex()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004491 } else
Francois Pichet11542142010-12-19 06:50:37 +00004492 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004493}
4494
John McCallc052dbb2012-05-22 21:28:12 +00004495static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004496 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004497 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004498 return;
4499 }
4500
4501 AttributeList::Kind Kind = Attr.getKind();
4502 if (Kind == AttributeList::AT_SingleInheritance)
4503 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004504 ::new (S.Context)
4505 SingleInheritanceAttr(Attr.getRange(), S.Context,
4506 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004507 else if (Kind == AttributeList::AT_MultipleInheritance)
4508 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004509 ::new (S.Context)
4510 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4511 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004512 else if (Kind == AttributeList::AT_VirtualInheritance)
4513 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004514 ::new (S.Context)
4515 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4516 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004517}
4518
4519static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4520 if (S.LangOpts.MicrosoftExt) {
4521 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004522 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004523 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004524 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4525 Attr.getAttributeSpellingListIndex()));
Sean Hunt8e083e72012-06-19 23:57:03 +00004526 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004527 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004528 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4529 Attr.getAttributeSpellingListIndex()));
Sean Hunt8e083e72012-06-19 23:57:03 +00004530 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004531 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004532 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4533 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004534 } else
4535 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4536}
4537
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004538static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4539 if (S.LangOpts.MicrosoftExt)
Michael Han51d8c522013-01-24 16:46:58 +00004540 D->addAttr(::new (S.Context)
4541 ForceInlineAttr(Attr.getRange(), S.Context,
4542 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004543 else
4544 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4545}
4546
Ted Kremenekb71368d2009-05-09 02:44:38 +00004547//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004548// Top Level Sema Entry Points
4549//===----------------------------------------------------------------------===//
4550
Chandler Carruth1b03c872011-07-02 00:01:44 +00004551static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4552 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004553 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004554 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4555 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4556 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004557 default:
4558 break;
4559 }
4560}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004561
Chandler Carruth1b03c872011-07-02 00:01:44 +00004562static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4563 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004564 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004565 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4566 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4567 case AttributeList::AT_IBOutletCollection:
4568 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004569 case AttributeList::AT_AddressSpace:
4570 case AttributeList::AT_OpenCLImageAccess:
4571 case AttributeList::AT_ObjCGC:
4572 case AttributeList::AT_VectorSize:
4573 case AttributeList::AT_NeonVectorType:
4574 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004575 // Ignore these, these are type attributes, handled by
4576 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004577 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004578 case AttributeList::AT_CUDADevice:
4579 case AttributeList::AT_CUDAHost:
4580 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004581 // Ignore, this is a non-inheritable attribute, handled
4582 // by ProcessNonInheritableDeclAttr.
4583 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004584 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4585 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4586 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4587 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004588 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004589 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004590 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004591 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004592 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4593 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4594 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004595 handleDependencyAttr(S, scope, D, Attr);
4596 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004597 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4598 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4599 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004600 case AttributeList::AT_CXX11NoReturn:
4601 handleCXX11NoReturnAttr(S, D, Attr);
4602 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004603 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004604 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4605 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004606 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4607 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004608 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004609 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004610 case AttributeList::AT_MinSize:
4611 handleMinSizeAttr(S, D, Attr);
4612 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004613 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4614 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4615 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4616 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4617 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004618 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004619 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004620 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4621 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4622 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4623 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4624 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004625 case AttributeList::AT_ownership_returns:
4626 case AttributeList::AT_ownership_takes:
4627 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004628 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004629 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4630 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4631 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4632 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4633 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4634 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4635 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004636
Sean Hunt8e083e72012-06-19 23:57:03 +00004637 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004638 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004639 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004640 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004641
Sean Hunt8e083e72012-06-19 23:57:03 +00004642 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004643 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4644
Fariborz Jahanian84101132012-09-07 23:46:23 +00004645 case AttributeList::AT_ObjCRequiresSuper:
4646 handleObjCRequiresSuperAttr(S, D, Attr); break;
4647
Sean Hunt8e083e72012-06-19 23:57:03 +00004648 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004649 handleNSBridgedAttr(S, scope, D, Attr); break;
4650
Sean Hunt8e083e72012-06-19 23:57:03 +00004651 case AttributeList::AT_CFAuditedTransfer:
4652 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004653 handleCFTransferAttr(S, D, Attr); break;
4654
Ted Kremenekb71368d2009-05-09 02:44:38 +00004655 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004656 case AttributeList::AT_CFConsumed:
4657 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4658 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004659 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004660
Sean Hunt8e083e72012-06-19 23:57:03 +00004661 case AttributeList::AT_NSReturnsAutoreleased:
4662 case AttributeList::AT_NSReturnsNotRetained:
4663 case AttributeList::AT_CFReturnsNotRetained:
4664 case AttributeList::AT_NSReturnsRetained:
4665 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004666 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004667
Tanya Lattner0df579e2012-07-09 22:06:01 +00004668 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004669 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004670 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004671
Sean Hunt8e083e72012-06-19 23:57:03 +00004672 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004673 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004674
Sean Hunt8e083e72012-06-19 23:57:03 +00004675 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4676 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4677 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004678 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4679 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004680 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004681 handleArcWeakrefUnavailableAttr (S, D, Attr);
4682 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004683 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004684 handleObjCRootClassAttr(S, D, Attr);
4685 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004686 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004687 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004688 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004689 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4690 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004691 handleReturnsTwiceAttr(S, D, Attr);
4692 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004693 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4694 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4695 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004696 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004697 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4698 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4699 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4700 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004701 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004702 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004703 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004704 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004705 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004706 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004707 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004708 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004709 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4710 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4711 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4712 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4713 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4714 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4715 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4716 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4717 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004718 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004719 // Just ignore
4720 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004721 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004722 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004723 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004724 case AttributeList::AT_StdCall:
4725 case AttributeList::AT_CDecl:
4726 case AttributeList::AT_FastCall:
4727 case AttributeList::AT_ThisCall:
4728 case AttributeList::AT_Pascal:
4729 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004730 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004731 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004732 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004733 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004734 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004735 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004736 break;
John McCallc052dbb2012-05-22 21:28:12 +00004737
4738 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004739 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004740 handleMsStructAttr(S, D, Attr);
4741 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004742 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004743 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004744 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_SingleInheritance:
4746 case AttributeList::AT_MultipleInheritance:
4747 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004748 handleInheritanceAttr(S, D, Attr);
4749 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004750 case AttributeList::AT_Win64:
4751 case AttributeList::AT_Ptr32:
4752 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004753 handlePortabilityAttr(S, D, Attr);
4754 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004755 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004756 handleForceInlineAttr(S, D, Attr);
4757 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004758
4759 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004761 handleGuardedVarAttr(S, D, Attr);
4762 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004763 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004764 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004765 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004766 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004767 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004768 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004769 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004770 handleNoAddressSafetyAttr(S, D, Attr);
4771 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004772 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004773 handleNoThreadSafetyAttr(S, D, Attr);
4774 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004775 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004776 handleLockableAttr(S, D, Attr);
4777 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004778 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004779 handleGuardedByAttr(S, D, Attr);
4780 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004781 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004782 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004783 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004784 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004785 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004786 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004787 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004788 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004789 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004790 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004791 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004792 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004793 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004794 handleLockReturnedAttr(S, D, Attr);
4795 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004796 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004797 handleLocksExcludedAttr(S, D, Attr);
4798 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004799 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004800 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004801 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004803 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004804 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004805 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004806 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004807 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004809 handleUnlockFunAttr(S, D, Attr);
4810 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004811 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004812 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004813 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004815 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004816 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004817
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004818 // Type safety attributes.
4819 case AttributeList::AT_ArgumentWithTypeTag:
4820 handleArgumentWithTypeTagAttr(S, D, Attr);
4821 break;
4822 case AttributeList::AT_TypeTagForDatatype:
4823 handleTypeTagForDatatypeAttr(S, D, Attr);
4824 break;
4825
Chris Lattner803d0802008-06-29 00:43:07 +00004826 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004827 // Ask target about the attribute.
4828 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4829 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004830 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4831 diag::warn_unhandled_ms_attribute_ignored :
4832 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004833 break;
4834 }
4835}
4836
Peter Collingbourne60700392011-01-21 02:08:45 +00004837/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4838/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00004839/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004840static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4841 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00004842 bool NonInheritable, bool Inheritable,
4843 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004844 if (Attr.isInvalid())
4845 return;
4846
Richard Smithcd8ab512013-01-17 01:30:42 +00004847 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4848 // instead.
4849 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4850 return;
4851
Peter Collingbourne60700392011-01-21 02:08:45 +00004852 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004853 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004854
4855 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004856 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004857}
4858
Chris Lattner803d0802008-06-29 00:43:07 +00004859/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4860/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004861void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004862 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004863 bool NonInheritable, bool Inheritable,
4864 bool IncludeCXX11Attributes) {
4865 for (const AttributeList* l = AttrList; l; l = l->getNext())
4866 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
4867 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004868
4869 // GCC accepts
4870 // static int a9 __attribute__((weakref));
4871 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004872 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004873 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004874 cast<NamedDecl>(D)->getNameAsString();
4875 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004876 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004877 }
4878}
4879
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004880// Annotation attributes are the only attributes allowed after an access
4881// specifier.
4882bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4883 const AttributeList *AttrList) {
4884 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004885 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004886 handleAnnotateAttr(*this, ASDecl, *l);
4887 } else {
4888 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4889 return true;
4890 }
4891 }
4892
4893 return false;
4894}
4895
John McCalle82247a2011-10-01 05:17:03 +00004896/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4897/// contains any decl attributes that we should warn about.
4898static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4899 for ( ; A; A = A->getNext()) {
4900 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004901 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004902 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4903
4904 if (A->getKind() == AttributeList::UnknownAttribute) {
4905 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4906 << A->getName() << A->getRange();
4907 } else {
4908 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4909 << A->getName() << A->getRange();
4910 }
4911 }
4912}
4913
4914/// checkUnusedDeclAttributes - Given a declarator which is not being
4915/// used to build a declaration, complain about any decl attributes
4916/// which might be lying around on it.
4917void Sema::checkUnusedDeclAttributes(Declarator &D) {
4918 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4919 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4920 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4921 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4922}
4923
Ryan Flynne25ff832009-07-30 03:15:39 +00004924/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004925/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004926NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4927 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004928 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004929 NamedDecl *NewD = 0;
4930 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004931 FunctionDecl *NewFD;
4932 // FIXME: Missing call to CheckFunctionDeclaration().
4933 // FIXME: Mangling?
4934 // FIXME: Is the qualifier info correct?
4935 // FIXME: Is the DeclContext correct?
4936 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4937 Loc, Loc, DeclarationName(II),
4938 FD->getType(), FD->getTypeSourceInfo(),
4939 SC_None, SC_None,
4940 false/*isInlineSpecified*/,
4941 FD->hasPrototype(),
4942 false/*isConstexprSpecified*/);
4943 NewD = NewFD;
4944
4945 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004946 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004947
4948 // Fake up parameter variables; they are declared as if this were
4949 // a typedef.
4950 QualType FDTy = FD->getType();
4951 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4952 SmallVector<ParmVarDecl*, 16> Params;
4953 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4954 AE = FT->arg_type_end(); AI != AE; ++AI) {
4955 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4956 Param->setScopeInfo(0, Params.size());
4957 Params.push_back(Param);
4958 }
David Blaikie4278c652011-09-21 18:16:56 +00004959 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004960 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004961 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4962 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004963 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004964 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004965 VD->getStorageClass(),
4966 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004967 if (VD->getQualifier()) {
4968 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004969 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004970 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004971 }
4972 return NewD;
4973}
4974
James Dennett1dfbd922012-06-14 21:40:34 +00004975/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004976/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004977void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004978 if (W.getUsed()) return; // only do this once
4979 W.setUsed(true);
4980 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4981 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004982 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004983 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4984 NDId->getName()));
4985 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004986 WeakTopLevelDecl.push_back(NewD);
4987 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4988 // to insert Decl at TU scope, sorry.
4989 DeclContext *SavedContext = CurContext;
4990 CurContext = Context.getTranslationUnitDecl();
4991 PushOnScopeChains(NewD, S);
4992 CurContext = SavedContext;
4993 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004994 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004995 }
4996}
4997
Chris Lattner0744e5f2008-06-29 00:23:49 +00004998/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4999/// it, apply them to D. This is a bit tricky because PD can have attributes
5000/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005001void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5002 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00005003 // It's valid to "forward-declare" #pragma weak, in which case we
5004 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00005005 if (Inheritable) {
5006 LoadExternalWeakUndeclaredIdentifiers();
5007 if (!WeakUndeclaredIdentifiers.empty()) {
5008 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
5009 if (IdentifierInfo *Id = ND->getIdentifier()) {
5010 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5011 = WeakUndeclaredIdentifiers.find(Id);
5012 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
5013 WeakInfo W = I->second;
5014 DeclApplyPragmaWeak(S, ND, W);
5015 WeakUndeclaredIdentifiers[Id] = W;
5016 }
John McCalld4aff0e2010-10-27 00:59:00 +00005017 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005018 }
5019 }
5020 }
5021
Chris Lattner0744e5f2008-06-29 00:23:49 +00005022 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005023 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005024 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005025
Chris Lattner0744e5f2008-06-29 00:23:49 +00005026 // Walk the declarator structure, applying decl attributes that were in a type
5027 // position to the decl itself. This handles cases like:
5028 // int *__attr__(x)** D;
5029 // when X is a decl attribute.
5030 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5031 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005032 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5033 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005034
Chris Lattner0744e5f2008-06-29 00:23:49 +00005035 // Finally, apply any attributes on the decl itself.
5036 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005037 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005038}
John McCall54abf7d2009-11-04 02:18:39 +00005039
John McCallf85e1932011-06-15 23:02:42 +00005040/// Is the given declaration allowed to use a forbidden type?
5041static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5042 // Private ivars are always okay. Unfortunately, people don't
5043 // always properly make their ivars private, even in system headers.
5044 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005045 // Function declarations in sys headers will be marked unavailable.
5046 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5047 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005048 return false;
5049
5050 // Require it to be declared in a system header.
5051 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5052}
5053
5054/// Handle a delayed forbidden-type diagnostic.
5055static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5056 Decl *decl) {
5057 if (decl && isForbiddenTypeAllowed(S, decl)) {
5058 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5059 "this system declaration uses an unsupported type"));
5060 return;
5061 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005062 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005063 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005064 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005065 // kind of forbidden type messages on unavailable functions.
5066 if (FD->hasAttr<UnavailableAttr>() &&
5067 diag.getForbiddenTypeDiagnostic() ==
5068 diag::err_arc_array_param_no_ownership) {
5069 diag.Triggered = true;
5070 return;
5071 }
5072 }
John McCallf85e1932011-06-15 23:02:42 +00005073
5074 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5075 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5076 diag.Triggered = true;
5077}
5078
John McCall92576642012-05-07 06:16:41 +00005079void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5080 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005081 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005082 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005083
John McCall92576642012-05-07 06:16:41 +00005084 // When delaying diagnostics to run in the context of a parsed
5085 // declaration, we only want to actually emit anything if parsing
5086 // succeeds.
5087 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005088
John McCall92576642012-05-07 06:16:41 +00005089 // We emit all the active diagnostics in this pool or any of its
5090 // parents. In general, we'll get one pool for the decl spec
5091 // and a child pool for each declarator; in a decl group like:
5092 // deprecated_typedef foo, *bar, baz();
5093 // only the declarator pops will be passed decls. This is correct;
5094 // we really do need to consider delayed diagnostics from the decl spec
5095 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005096 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005097 do {
John McCall13489672012-05-07 06:16:58 +00005098 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005099 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5100 // This const_cast is a bit lame. Really, Triggered should be mutable.
5101 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005102 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005103 continue;
5104
John McCalleee1d542011-02-14 07:13:47 +00005105 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005106 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005107 // Don't bother giving deprecation diagnostics if the decl is invalid.
5108 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005109 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005110 break;
5111
5112 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005113 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005114 break;
John McCallf85e1932011-06-15 23:02:42 +00005115
5116 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005117 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005118 break;
John McCall2f514482010-01-27 03:50:35 +00005119 }
5120 }
John McCall92576642012-05-07 06:16:41 +00005121 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005122}
5123
John McCall13489672012-05-07 06:16:58 +00005124/// Given a set of delayed diagnostics, re-emit them as if they had
5125/// been delayed in the current context instead of in the given pool.
5126/// Essentially, this just moves them to the current pool.
5127void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5128 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5129 assert(curPool && "re-emitting in undelayed context not supported");
5130 curPool->steal(pool);
5131}
5132
John McCall54abf7d2009-11-04 02:18:39 +00005133static bool isDeclDeprecated(Decl *D) {
5134 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005135 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005136 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005137 // A category implicitly has the availability of the interface.
5138 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5139 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005140 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5141 return false;
5142}
5143
Eli Friedmanc3b23082012-08-08 21:52:41 +00005144static void
5145DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5146 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005147 const ObjCInterfaceDecl *UnknownObjCClass,
5148 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005149 DeclarationName Name = D->getDeclName();
5150 if (!Message.empty()) {
5151 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5152 S.Diag(D->getLocation(),
5153 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5154 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005155 if (ObjCPropery)
5156 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5157 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005158 } else if (!UnknownObjCClass) {
5159 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5160 S.Diag(D->getLocation(),
5161 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5162 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005163 if (ObjCPropery)
5164 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5165 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005166 } else {
5167 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5168 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5169 }
5170}
5171
John McCall9c3087b2010-08-26 02:13:20 +00005172void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005173 Decl *Ctx) {
5174 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005175 return;
5176
John McCall2f514482010-01-27 03:50:35 +00005177 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005178 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5179 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005180 DD.getUnknownObjCClass(),
5181 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005182}
5183
Chris Lattner5f9e2722011-07-23 10:55:15 +00005184void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005185 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005186 const ObjCInterfaceDecl *UnknownObjCClass,
5187 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005188 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005189 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005190 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5191 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005192 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005193 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005194 return;
5195 }
5196
5197 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005198 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005199 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005200 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005201}