blob: 77c7dc59c8a6b728519319b725994c7d95cce24a [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"
Rafael Espindolad7a60ad2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000031using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000033
John McCall883cc2c2011-03-02 12:29:23 +000034/// These constants match the enumerated choices of
35/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000036enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000037 ExpectedFunction,
38 ExpectedUnion,
39 ExpectedVariableOrFunction,
40 ExpectedFunctionOrMethod,
41 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000043 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000044 ExpectedFunctionMethodOrParameter,
45 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000046 ExpectedVariable,
47 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000048 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000049 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000050 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000051 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000052 ExpectedTLSVar,
53 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000054 ExpectedVariableFieldOrTag,
55 ExpectedTypeOrNamespace
John McCall883cc2c2011-03-02 12:29:23 +000056};
57
Chris Lattnere5c5ee12008-06-29 00:16:31 +000058//===----------------------------------------------------------------------===//
59// Helper functions
60//===----------------------------------------------------------------------===//
61
Chandler Carruth87c44602011-07-01 23:49:12 +000062static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000063 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000064 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000065 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000067 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000068 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000069 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000070 Ty = decl->getUnderlyingType();
71 else
72 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000073
Chris Lattner6b6b5372008-06-26 18:38:35 +000074 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000075 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000076 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000077 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078
John McCall183700f2009-09-21 23:43:11 +000079 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000080}
81
Daniel Dunbar35682492008-09-26 04:12:28 +000082// FIXME: We should provide an abstraction around a method or function
83// to provide the following bits of information.
84
Nuno Lopesd20254f2009-12-20 23:11:08 +000085/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000086/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000087static bool isFunction(const Decl *D) {
88 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000089}
90
91/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000092/// type (function or function-typed variable) or an Objective-C
93/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000094static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000095 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000096}
97
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098/// isFunctionOrMethodOrBlock - Return true if the given decl has function
99/// type (function or function-typed variable) or an Objective-C
100/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000101static bool isFunctionOrMethodOrBlock(const Decl *D) {
102 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000103 return true;
104 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000105 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000106 QualType Ty = V->getType();
107 return Ty->isBlockPointerType();
108 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000109 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000110}
111
John McCall711c52b2011-01-05 12:14:39 +0000112/// Return true if the given decl has a declarator that should have
113/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000114static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000115 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000116 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
117 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000118}
119
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120/// hasFunctionProto - Return true if the given decl has a argument
121/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000122/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000123static bool hasFunctionProto(const Decl *D) {
124 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000125 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000126 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000127 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000128 return true;
129 }
130}
131
132/// getFunctionOrMethodNumArgs - Return number of function or method
133/// arguments. It is an error to call this on a K&R function (use
134/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000140 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000141}
142
Chandler Carruth87c44602011-07-01 23:49:12 +0000143static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
144 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000145 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000146 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000147 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000148
Chandler Carruth87c44602011-07-01 23:49:12 +0000149 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000150}
151
Chandler Carruth87c44602011-07-01 23:49:12 +0000152static QualType getFunctionOrMethodResultType(const Decl *D) {
153 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000154 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000156}
157
Chandler Carruth87c44602011-07-01 23:49:12 +0000158static bool isFunctionOrMethodVariadic(const Decl *D) {
159 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000160 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000161 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000162 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000163 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000164 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000165 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000166 }
167}
168
Chandler Carruth87c44602011-07-01 23:49:12 +0000169static bool isInstanceMethod(const Decl *D) {
170 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000171 return MethodDecl->isInstance();
172 return false;
173}
174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000176 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000177 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000179
John McCall506b57e2010-05-17 21:00:27 +0000180 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
181 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000182 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000183
John McCall506b57e2010-05-17 21:00:27 +0000184 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000185
Chris Lattner6b6b5372008-06-26 18:38:35 +0000186 // FIXME: Should we walk the chain of classes?
187 return ClsName == &Ctx.Idents.get("NSString") ||
188 ClsName == &Ctx.Idents.get("NSMutableString");
189}
190
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000191static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000192 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000193 if (!PT)
194 return false;
195
Ted Kremenek6217b802009-07-29 21:53:49 +0000196 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000197 if (!RT)
198 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000199
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000200 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000201 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000202 return false;
203
204 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
205}
206
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000207/// \brief Check if the attribute has exactly as many args as Num. May
208/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000209static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
210 unsigned int Num) {
211 if (Attr.getNumArgs() != Num) {
212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
213 return false;
214 }
215
216 return true;
217}
218
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000219
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000220/// \brief Check if the attribute has at least as many args as Num. May
221/// output an error.
222static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
223 unsigned int Num) {
224 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
226 return false;
227 }
228
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000229 return true;
230}
231
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000232/// \brief Check if IdxExpr is a valid argument index for a function or
233/// instance method D. May output an error.
234///
235/// \returns true if IdxExpr is a valid index.
236static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
237 StringRef AttrName,
238 SourceLocation AttrLoc,
239 unsigned AttrArgNum,
240 const Expr *IdxExpr,
241 uint64_t &Idx)
242{
243 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
244
245 // In C++ the implicit 'this' function parameter also counts.
246 // Parameters are counted from one.
247 const bool HasImplicitThisParam = isInstanceMethod(D);
248 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
249 const unsigned FirstIdx = 1;
250
251 llvm::APSInt IdxInt;
252 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
253 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
254 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
255 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
256 return false;
257 }
258
259 Idx = IdxInt.getLimitedValue();
260 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
261 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
262 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
263 return false;
264 }
265 Idx--; // Convert to zero-based.
266 if (HasImplicitThisParam) {
267 if (Idx == 0) {
268 S.Diag(AttrLoc,
269 diag::err_attribute_invalid_implicit_this_argument)
270 << AttrName << IdxExpr->getSourceRange();
271 return false;
272 }
273 --Idx;
274 }
275
276 return true;
277}
278
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000279///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000280/// \brief Check if passed in Decl is a field or potentially shared global var
281/// \return true if the Decl is a field or potentially shared global variable
282///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000283static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000284 if (isa<FieldDecl>(D))
285 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000286 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000287 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
288
289 return false;
290}
291
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000292/// \brief Check if the passed-in expression is of type int or bool.
293static bool isIntOrBool(Expr *Exp) {
294 QualType QT = Exp->getType();
295 return QT->isBooleanType() || QT->isIntegerType();
296}
297
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000298
299// Check to see if the type is a smart pointer of some kind. We assume
300// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000301static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
302 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
303 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000304 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000305 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000306
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000307 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
308 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000309 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000310 return false;
311
312 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000313}
314
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000315/// \brief Check if passed in Decl is a pointer type.
316/// Note that this function may produce an error message.
317/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000318static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
319 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000320 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000321 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000322 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000323 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000324
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000325 if (const RecordType *RT = QT->getAs<RecordType>()) {
326 // If it's an incomplete type, it could be a smart pointer; skip it.
327 // (We don't want to force template instantiation if we can avoid it,
328 // since that would alter the order in which templates are instantiated.)
329 if (RT->isIncompleteType())
330 return true;
331
332 if (threadSafetyCheckIsSmartPointer(S, RT))
333 return true;
334 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000335
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000336 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000337 << Attr.getName()->getName() << QT;
338 } else {
339 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
340 << Attr.getName();
341 }
342 return false;
343}
344
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000345/// \brief Checks that the passed in QualType either is of RecordType or points
346/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000347static const RecordType *getRecordType(QualType QT) {
348 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000349 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000350
351 // Now check if we point to record type.
352 if (const PointerType *PT = QT->getAs<PointerType>())
353 return PT->getPointeeType()->getAs<RecordType>();
354
355 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000356}
357
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000358
Jordy Rosefad5de92012-05-08 03:27:22 +0000359static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
360 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000361 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
362 if (RT->getDecl()->getAttr<LockableAttr>())
363 return true;
364 return false;
365}
366
367
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000368/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000369/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000370static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
371 QualType Ty) {
372 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000373
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000374 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000375 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000376 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000377 << Attr.getName() << Ty.getAsString();
378 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000379 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000380
Michael Hanf1aae3b2012-08-03 17:40:43 +0000381 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000382 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000383 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000384
385 // Allow smart pointers to be used as lockable objects.
386 // FIXME -- Check the type that the smart pointer points to.
387 if (threadSafetyCheckIsSmartPointer(S, RT))
388 return;
389
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000390 // Check if the type is lockable.
391 RecordDecl *RD = RT->getDecl();
392 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000393 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000394
395 // Else check if any base classes are lockable.
396 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
397 CXXBasePaths BPaths(false, false);
398 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
399 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000400 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000401
402 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
403 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000404}
405
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000406/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000407/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000408/// \param Sidx The attribute argument index to start checking with.
409/// \param ParamIdxOk Whether an argument can be indexing into a function
410/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000411static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000412 const AttributeList &Attr,
413 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000414 int Sidx = 0,
415 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000416 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000417 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000418
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000419 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000420 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000421 Args.push_back(ArgExp);
422 continue;
423 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000424
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000425 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000426 if (StrLit->getLength() == 0 ||
427 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000428 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000429 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000430 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000431 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000432 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000433
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000434 // We allow constant strings to be used as a placeholder for expressions
435 // that are not valid C++ syntax, but warn that they are ignored.
436 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
437 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000438 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000439 continue;
440 }
441
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000442 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000443
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000444 // A pointer to member expression of the form &MyClass::mu is treated
445 // specially -- we need to look at the type of the member.
446 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
447 if (UOp->getOpcode() == UO_AddrOf)
448 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
449 if (DRE->getDecl()->isCXXInstanceMember())
450 ArgTy = DRE->getDecl()->getType();
451
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000452 // First see if we can just cast to record type, or point to record type.
453 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000454
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000455 // Now check if we index into a record type function param.
456 if(!RT && ParamIdxOk) {
457 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000458 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
459 if(FD && IL) {
460 unsigned int NumParams = FD->getNumParams();
461 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000462 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
463 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
464 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
466 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000467 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000469 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 }
471 }
472
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000473 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000474
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000475 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000476 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000477}
478
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000479//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000480// Attribute Implementations
481//===----------------------------------------------------------------------===//
482
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000483// FIXME: All this manual attribute parsing code is gross. At the
484// least add some helper functions to check most argument patterns (#
485// and types of args).
486
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000487enum ThreadAttributeDeclKind {
488 ThreadExpectedFieldOrGlobalVar,
489 ThreadExpectedFunctionOrMethod,
490 ThreadExpectedClassOrStruct
491};
492
Michael Hanf1aae3b2012-08-03 17:40:43 +0000493static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000494 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000495 assert(!Attr.isInvalid());
496
497 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000498 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000499
500 // D must be either a member field or global (potentially shared) variable.
501 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000502 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
503 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000504 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000505 }
506
Michael Handc691572012-07-23 18:48:41 +0000507 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000508}
509
Michael Handc691572012-07-23 18:48:41 +0000510static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000513
Michael Han51d8c522013-01-24 16:46:58 +0000514 D->addAttr(::new (S.Context)
515 GuardedVarAttr(Attr.getRange(), S.Context,
516 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000517}
518
Michael Hanf1aae3b2012-08-03 17:40:43 +0000519static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000520 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000521 if (!checkGuardedVarAttrCommon(S, D, Attr))
522 return;
523
524 if (!threadSafetyCheckIsPointer(S, D, Attr))
525 return;
526
Michael Han51d8c522013-01-24 16:46:58 +0000527 D->addAttr(::new (S.Context)
528 PtGuardedVarAttr(Attr.getRange(), S.Context,
529 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000530}
531
Michael Hanf1aae3b2012-08-03 17:40:43 +0000532static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
533 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000534 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000535 assert(!Attr.isInvalid());
536
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000537 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000538 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000539
540 // D must be either a member field or global (potentially shared) variable.
541 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000542 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
543 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000544 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000545 }
546
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000547 SmallVector<Expr*, 1> Args;
548 // check that all arguments are lockable objects
549 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
550 unsigned Size = Args.size();
551 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000552 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000553
Michael Handc691572012-07-23 18:48:41 +0000554 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000555
Michael Handc691572012-07-23 18:48:41 +0000556 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000557}
558
Michael Handc691572012-07-23 18:48:41 +0000559static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
560 Expr *Arg = 0;
561 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
562 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000563
Michael Handc691572012-07-23 18:48:41 +0000564 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
565}
566
Michael Hanf1aae3b2012-08-03 17:40:43 +0000567static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000568 const AttributeList &Attr) {
569 Expr *Arg = 0;
570 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
571 return;
572
573 if (!threadSafetyCheckIsPointer(S, D, Attr))
574 return;
575
576 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
577 S.Context, Arg));
578}
579
Michael Hanf1aae3b2012-08-03 17:40:43 +0000580static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000581 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000582 assert(!Attr.isInvalid());
583
584 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000585 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000586
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000587 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000588 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000589 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
590 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000591 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000592 }
593
Michael Handc691572012-07-23 18:48:41 +0000594 return true;
595}
596
597static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
598 if (!checkLockableAttrCommon(S, D, Attr))
599 return;
600
601 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
602}
603
Michael Hanf1aae3b2012-08-03 17:40:43 +0000604static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000605 const AttributeList &Attr) {
606 if (!checkLockableAttrCommon(S, D, Attr))
607 return;
608
Michael Han51d8c522013-01-24 16:46:58 +0000609 D->addAttr(::new (S.Context)
610 ScopedLockableAttr(Attr.getRange(), S.Context,
611 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000612}
613
Kostya Serebryany85aee962013-02-26 06:58:27 +0000614static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
615 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000616 assert(!Attr.isInvalid());
617
618 if (!checkAttributeNumArgs(S, Attr, 0))
619 return;
620
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000621 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000622 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
623 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000624 return;
625 }
626
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000627 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000628 S.Context));
629}
630
Kostya Serebryany85aee962013-02-26 06:58:27 +0000631static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000632 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000633 assert(!Attr.isInvalid());
634
635 if (!checkAttributeNumArgs(S, Attr, 0))
636 return;
637
638 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000640 << Attr.getName() << ExpectedFunctionOrMethod;
641 return;
642 }
643
Michael Han51d8c522013-01-24 16:46:58 +0000644 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000645 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
646 Attr.getAttributeSpellingListIndex()));
647}
648
649static void handleNoSanitizeMemory(Sema &S, Decl *D,
650 const AttributeList &Attr) {
651 assert(!Attr.isInvalid());
652
653 if (!checkAttributeNumArgs(S, Attr, 0))
654 return;
655
656 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
658 << Attr.getName() << ExpectedFunctionOrMethod;
659 return;
660 }
661
662 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
663 S.Context));
664}
665
666static void handleNoSanitizeThread(Sema &S, Decl *D,
667 const AttributeList &Attr) {
668 assert(!Attr.isInvalid());
669
670 if (!checkAttributeNumArgs(S, Attr, 0))
671 return;
672
673 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
675 << Attr.getName() << ExpectedFunctionOrMethod;
676 return;
677 }
678
679 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
680 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000681}
682
Michael Hanf1aae3b2012-08-03 17:40:43 +0000683static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
684 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000685 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000686 assert(!Attr.isInvalid());
687
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000688 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000689 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000690
691 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000692 ValueDecl *VD = dyn_cast<ValueDecl>(D);
693 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000694 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
695 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000696 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000697 }
698
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000699 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000700 QualType QT = VD->getType();
701 if (!QT->isDependentType()) {
702 const RecordType *RT = getRecordType(QT);
703 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000704 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000705 << Attr.getName();
706 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000707 }
708 }
709
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000710 // Check that all arguments are lockable objects.
711 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000712 if (Args.size() == 0)
713 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000714
Michael Handc691572012-07-23 18:48:41 +0000715 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000716}
717
Michael Hanf1aae3b2012-08-03 17:40:43 +0000718static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000719 const AttributeList &Attr) {
720 SmallVector<Expr*, 1> Args;
721 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
722 return;
723
724 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000725 D->addAttr(::new (S.Context)
726 AcquiredAfterAttr(Attr.getRange(), S.Context,
727 StartArg, Args.size(),
728 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000729}
730
Michael Hanf1aae3b2012-08-03 17:40:43 +0000731static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 1> Args;
734 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
735 return;
736
737 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000738 D->addAttr(::new (S.Context)
739 AcquiredBeforeAttr(Attr.getRange(), S.Context,
740 StartArg, Args.size(),
741 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000742}
743
Michael Hanf1aae3b2012-08-03 17:40:43 +0000744static bool checkLockFunAttrCommon(Sema &S, Decl *D,
745 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000746 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000747 assert(!Attr.isInvalid());
748
749 // zero or more arguments ok
750
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000751 // check that the attribute is applied to a function
752 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000753 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
754 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000755 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000756 }
757
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000758 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000759 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000760
Michael Handc691572012-07-23 18:48:41 +0000761 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000762}
763
Michael Hanf1aae3b2012-08-03 17:40:43 +0000764static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000765 const AttributeList &Attr) {
766 SmallVector<Expr*, 1> Args;
767 if (!checkLockFunAttrCommon(S, D, Attr, Args))
768 return;
769
770 unsigned Size = Args.size();
771 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000772 D->addAttr(::new (S.Context)
773 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
774 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000775}
776
Michael Hanf1aae3b2012-08-03 17:40:43 +0000777static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 1> Args;
780 if (!checkLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000785 D->addAttr(::new (S.Context)
786 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
787 StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
792 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000793 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000794 assert(!Attr.isInvalid());
795
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000796 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000797 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000798
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000799 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000800 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
801 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000802 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000803 }
804
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000805 if (!isIntOrBool(Attr.getArg(0))) {
806 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000807 << Attr.getName();
808 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000809 }
810
811 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000812 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000813
Michael Handc691572012-07-23 18:48:41 +0000814 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000815}
816
Michael Hanf1aae3b2012-08-03 17:40:43 +0000817static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000818 const AttributeList &Attr) {
819 SmallVector<Expr*, 2> Args;
820 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
821 return;
822
823 unsigned Size = Args.size();
824 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000825 D->addAttr(::new (S.Context)
826 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
827 Attr.getArg(0), StartArg, Size,
828 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000829}
830
Michael Hanf1aae3b2012-08-03 17:40:43 +0000831static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000832 const AttributeList &Attr) {
833 SmallVector<Expr*, 2> Args;
834 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
835 return;
836
837 unsigned Size = Args.size();
838 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000839 D->addAttr(::new (S.Context)
840 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
841 Attr.getArg(0), StartArg, Size,
842 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000843}
844
Michael Hanf1aae3b2012-08-03 17:40:43 +0000845static bool checkLocksRequiredCommon(Sema &S, Decl *D,
846 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000847 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000848 assert(!Attr.isInvalid());
849
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000850 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000851 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000852
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000853 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000854 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
855 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000856 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000857 }
858
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000859 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000860 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000861 if (Args.size() == 0)
862 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000863
Michael Handc691572012-07-23 18:48:41 +0000864 return true;
865}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000866
Michael Hanf1aae3b2012-08-03 17:40:43 +0000867static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000868 const AttributeList &Attr) {
869 SmallVector<Expr*, 1> Args;
870 if (!checkLocksRequiredCommon(S, D, Attr, Args))
871 return;
872
873 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000874 D->addAttr(::new (S.Context)
875 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
876 StartArg, Args.size(),
877 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000878}
879
Michael Hanf1aae3b2012-08-03 17:40:43 +0000880static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000881 const AttributeList &Attr) {
882 SmallVector<Expr*, 1> Args;
883 if (!checkLocksRequiredCommon(S, D, Attr, Args))
884 return;
885
886 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000887 D->addAttr(::new (S.Context)
888 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
889 StartArg, Args.size(),
890 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000891}
892
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000893static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000894 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000895 assert(!Attr.isInvalid());
896
897 // zero or more arguments ok
898
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000899 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000900 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
901 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000902 return;
903 }
904
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000906 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000907 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000908 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000909 Expr **StartArg = Size == 0 ? 0 : &Args[0];
910
Michael Han51d8c522013-01-24 16:46:58 +0000911 D->addAttr(::new (S.Context)
912 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
913 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000914}
915
916static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000917 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000918 assert(!Attr.isInvalid());
919
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000920 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000921 return;
922
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000923 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000924 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
925 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926 return;
927 }
928
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000929 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000930 SmallVector<Expr*, 1> Args;
931 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
932 unsigned Size = Args.size();
933 if (Size == 0)
934 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000935
Michael Han51d8c522013-01-24 16:46:58 +0000936 D->addAttr(::new (S.Context)
937 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
938 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000939}
940
941static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000942 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000943 assert(!Attr.isInvalid());
944
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000945 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000946 return;
947
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000948 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000949 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
950 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000951 return;
952 }
953
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000954 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000955 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000956 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000957 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000958 if (Size == 0)
959 return;
960 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000961
Michael Han51d8c522013-01-24 16:46:58 +0000962 D->addAttr(::new (S.Context)
963 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
964 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000965}
966
967
Chandler Carruth1b03c872011-07-02 00:01:44 +0000968static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
969 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +0000970 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
971 if (TD == 0) {
972 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
973 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +0000974 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000975 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000976 }
Mike Stumpbf916502009-07-24 19:02:52 +0000977
Richard Smitha4fa9002013-01-13 02:11:23 +0000978 // Remember this typedef decl, we will need it later for diagnostics.
979 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000980}
981
Chandler Carruth1b03c872011-07-02 00:01:44 +0000982static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000983 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000984 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000985 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000986
Chandler Carruth87c44602011-07-01 23:49:12 +0000987 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000988 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000989 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000990 // If the alignment is less than or equal to 8 bits, the packed attribute
991 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +0000992 if (!FD->getType()->isDependentType() &&
993 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000994 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000995 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000996 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000997 else
Michael Han51d8c522013-01-24 16:46:58 +0000998 FD->addAttr(::new (S.Context)
999 PackedAttr(Attr.getRange(), S.Context,
1000 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001001 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001002 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001003}
1004
Chandler Carruth1b03c872011-07-02 00:01:44 +00001005static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001006 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001007 RD->addAttr(::new (S.Context)
1008 MsStructAttr(Attr.getRange(), S.Context,
1009 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001010 else
1011 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1012}
1013
Chandler Carruth1b03c872011-07-02 00:01:44 +00001014static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001015 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001016 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001017 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001018
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001019 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001020 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001021 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001022 D->addAttr(::new (S.Context)
1023 IBActionAttr(Attr.getRange(), S.Context,
1024 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001025 return;
1026 }
1027
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001028 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001029}
1030
Ted Kremenek2f041d02011-09-29 07:02:25 +00001031static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1032 // The IBOutlet/IBOutletCollection attributes only apply to instance
1033 // variables or properties of Objective-C classes. The outlet must also
1034 // have an object reference type.
1035 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1036 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001037 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001038 << Attr.getName() << VD->getType() << 0;
1039 return false;
1040 }
1041 }
1042 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1043 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001044 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001045 << Attr.getName() << PD->getType() << 1;
1046 return false;
1047 }
1048 }
1049 else {
1050 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1051 return false;
1052 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001053
Ted Kremenek2f041d02011-09-29 07:02:25 +00001054 return true;
1055}
1056
Chandler Carruth1b03c872011-07-02 00:01:44 +00001057static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001058 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001059 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001060 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001061
1062 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001063 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001064
Michael Han51d8c522013-01-24 16:46:58 +00001065 D->addAttr(::new (S.Context)
1066 IBOutletAttr(Attr.getRange(), S.Context,
1067 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001068}
1069
Chandler Carruth1b03c872011-07-02 00:01:44 +00001070static void handleIBOutletCollection(Sema &S, Decl *D,
1071 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001072
1073 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001074 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001075 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1076 return;
1077 }
1078
Ted Kremenek2f041d02011-09-29 07:02:25 +00001079 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001080 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001081
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001082 IdentifierInfo *II = Attr.getParameterName();
1083 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001084 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001085
John McCallb3d87482010-08-24 05:47:05 +00001086 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001087 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001088 if (!TypeRep) {
1089 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1090 return;
1091 }
John McCallb3d87482010-08-24 05:47:05 +00001092 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001093 // Diagnose use of non-object type in iboutletcollection attribute.
1094 // FIXME. Gnu attribute extension ignores use of builtin types in
1095 // attributes. So, __attribute__((iboutletcollection(char))) will be
1096 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001097 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001098 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1099 return;
1100 }
Michael Han51d8c522013-01-24 16:46:58 +00001101 D->addAttr(::new (S.Context)
1102 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1103 QT, Attr.getParameterLoc(),
1104 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001105}
1106
Chandler Carruthd309c812011-07-01 23:49:16 +00001107static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001108 if (const RecordType *UT = T->getAsUnionType())
1109 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1110 RecordDecl *UD = UT->getDecl();
1111 for (RecordDecl::field_iterator it = UD->field_begin(),
1112 itend = UD->field_end(); it != itend; ++it) {
1113 QualType QT = it->getType();
1114 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1115 T = QT;
1116 return;
1117 }
1118 }
1119 }
1120}
1121
Nuno Lopes587de5b2012-05-24 00:22:00 +00001122static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001123 if (!isFunctionOrMethod(D)) {
1124 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1125 << "alloc_size" << ExpectedFunctionOrMethod;
1126 return;
1127 }
1128
Nuno Lopes587de5b2012-05-24 00:22:00 +00001129 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1130 return;
1131
1132 // In C++ the implicit 'this' function parameter also counts, and they are
1133 // counted from one.
1134 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidis28965bf2013-02-22 06:58:28 +00001135 unsigned NumArgs;
1136 if (hasFunctionProto(D))
1137 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1138 else
1139 NumArgs = 0;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001140
1141 SmallVector<unsigned, 8> SizeArgs;
1142
1143 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1144 E = Attr.arg_end(); I!=E; ++I) {
1145 // The argument must be an integer constant expression.
1146 Expr *Ex = *I;
1147 llvm::APSInt ArgNum;
1148 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1149 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1151 << "alloc_size" << Ex->getSourceRange();
1152 return;
1153 }
1154
1155 uint64_t x = ArgNum.getZExtValue();
1156
1157 if (x < 1 || x > NumArgs) {
1158 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1159 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1160 return;
1161 }
1162
1163 --x;
1164 if (HasImplicitThisParam) {
1165 if (x == 0) {
1166 S.Diag(Attr.getLoc(),
1167 diag::err_attribute_invalid_implicit_this_argument)
1168 << "alloc_size" << Ex->getSourceRange();
1169 return;
1170 }
1171 --x;
1172 }
1173
1174 // check if the function argument is of an integer type
1175 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1176 if (!T->isIntegerType()) {
1177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1178 << "alloc_size" << Ex->getSourceRange();
1179 return;
1180 }
1181
Nuno Lopes587de5b2012-05-24 00:22:00 +00001182 SizeArgs.push_back(x);
1183 }
1184
1185 // check if the function returns a pointer
1186 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1187 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1188 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1189 }
1190
Michael Han51d8c522013-01-24 16:46:58 +00001191 D->addAttr(::new (S.Context)
1192 AllocSizeAttr(Attr.getRange(), S.Context,
1193 SizeArgs.data(), SizeArgs.size(),
1194 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001195}
1196
Chandler Carruth1b03c872011-07-02 00:01:44 +00001197static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001198 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1199 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001200 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001202 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001203 return;
1204 }
Mike Stumpbf916502009-07-24 19:02:52 +00001205
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001206 // In C++ the implicit 'this' function parameter also counts, and they are
1207 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001208 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001209 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001210
1211 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001212 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001213
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001214 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1215 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001216 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001217 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001218 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001219 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1220 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001221 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1222 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001223 return;
1224 }
Mike Stumpbf916502009-07-24 19:02:52 +00001225
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001226 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001227
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001228 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001230 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001231 return;
1232 }
Mike Stumpbf916502009-07-24 19:02:52 +00001233
Ted Kremenek465172f2008-07-21 22:09:15 +00001234 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001235 if (HasImplicitThisParam) {
1236 if (x == 0) {
1237 S.Diag(Attr.getLoc(),
1238 diag::err_attribute_invalid_implicit_this_argument)
1239 << "nonnull" << Ex->getSourceRange();
1240 return;
1241 }
1242 --x;
1243 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001244
1245 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001246 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001247 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001248
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001249 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001250 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001251 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001252 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001253 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001254 }
Mike Stumpbf916502009-07-24 19:02:52 +00001255
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001256 NonNullArgs.push_back(x);
1257 }
Mike Stumpbf916502009-07-24 19:02:52 +00001258
1259 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1260 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001261 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001262 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1263 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001264 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001265 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001266 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001267 }
Mike Stumpbf916502009-07-24 19:02:52 +00001268
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001269 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001270 if (NonNullArgs.empty()) {
1271 // Warn the trivial case only if attribute is not coming from a
1272 // macro instantiation.
1273 if (Attr.getLoc().isFileID())
1274 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001275 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001276 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001277 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001278
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001279 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001280 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001281 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001282 D->addAttr(::new (S.Context)
1283 NonNullAttr(Attr.getRange(), S.Context, start, size,
1284 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001285}
1286
Chandler Carruth1b03c872011-07-02 00:01:44 +00001287static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001288 // This attribute must be applied to a function declaration.
1289 // The first argument to the attribute must be a string,
1290 // the name of the resource, for example "malloc".
1291 // The following arguments must be argument indexes, the arguments must be
1292 // of integer type for Returns, otherwise of pointer type.
1293 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001294 // after being held. free() should be __attribute((ownership_takes)), whereas
1295 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001296
1297 if (!AL.getParameterName()) {
1298 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1299 << AL.getName()->getName() << 1;
1300 return;
1301 }
1302 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001303 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001304 switch (AL.getKind()) {
1305 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001306 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001307 if (AL.getNumArgs() < 1) {
1308 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1309 return;
1310 }
Jordy Rose2a479922010-08-12 08:54:03 +00001311 break;
1312 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001313 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001314 if (AL.getNumArgs() < 1) {
1315 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1316 return;
1317 }
Jordy Rose2a479922010-08-12 08:54:03 +00001318 break;
1319 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001320 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001321 if (AL.getNumArgs() > 1) {
1322 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1323 << AL.getNumArgs() + 1;
1324 return;
1325 }
Jordy Rose2a479922010-08-12 08:54:03 +00001326 break;
1327 default:
1328 // This should never happen given how we are called.
1329 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001330 }
1331
Chandler Carruth87c44602011-07-01 23:49:12 +00001332 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001333 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1334 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001335 return;
1336 }
1337
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001338 // In C++ the implicit 'this' function parameter also counts, and they are
1339 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001340 bool HasImplicitThisParam = isInstanceMethod(D);
1341 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342
Chris Lattner5f9e2722011-07-23 10:55:15 +00001343 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001344
1345 // Normalize the argument, __foo__ becomes foo.
1346 if (Module.startswith("__") && Module.endswith("__"))
1347 Module = Module.substr(2, Module.size() - 4);
1348
Chris Lattner5f9e2722011-07-23 10:55:15 +00001349 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001350
Jordy Rose2a479922010-08-12 08:54:03 +00001351 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1352 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001353
Peter Collingbourne7a730022010-11-23 20:45:58 +00001354 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 llvm::APSInt ArgNum(32);
1356 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1357 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1358 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1359 << AL.getName()->getName() << IdxExpr->getSourceRange();
1360 continue;
1361 }
1362
1363 unsigned x = (unsigned) ArgNum.getZExtValue();
1364
1365 if (x > NumArgs || x < 1) {
1366 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1367 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1368 continue;
1369 }
1370 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001371 if (HasImplicitThisParam) {
1372 if (x == 0) {
1373 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1374 << "ownership" << IdxExpr->getSourceRange();
1375 return;
1376 }
1377 --x;
1378 }
1379
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001380 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001381 case OwnershipAttr::Takes:
1382 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001383 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001384 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001385 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1386 // FIXME: Should also highlight argument in decl.
1387 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001388 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001389 << "pointer"
1390 << IdxExpr->getSourceRange();
1391 continue;
1392 }
1393 break;
1394 }
Sean Huntcf807c42010-08-18 23:23:40 +00001395 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001396 if (AL.getNumArgs() > 1) {
1397 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001398 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001399 llvm::APSInt ArgNum(32);
1400 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1401 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1402 S.Diag(AL.getLoc(), diag::err_ownership_type)
1403 << "ownership_returns" << "integer"
1404 << IdxExpr->getSourceRange();
1405 return;
1406 }
1407 }
1408 break;
1409 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001410 } // switch
1411
1412 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001413 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001414 i = D->specific_attr_begin<OwnershipAttr>(),
1415 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001416 i != e; ++i) {
1417 if ((*i)->getOwnKind() != K) {
1418 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1419 I!=E; ++I) {
1420 if (x == *I) {
1421 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1422 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001423 }
1424 }
1425 }
1426 }
1427 OwnershipArgs.push_back(x);
1428 }
1429
1430 unsigned* start = OwnershipArgs.data();
1431 unsigned size = OwnershipArgs.size();
1432 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001433
1434 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1435 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1436 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001437 }
Sean Huntcf807c42010-08-18 23:23:40 +00001438
Michael Han51d8c522013-01-24 16:46:58 +00001439 D->addAttr(::new (S.Context)
1440 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1441 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001442}
1443
Chandler Carruth1b03c872011-07-02 00:01:44 +00001444static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001445 // Check the attribute arguments.
1446 if (Attr.getNumArgs() > 1) {
1447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1448 return;
1449 }
1450
Chandler Carruth87c44602011-07-01 23:49:12 +00001451 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001452 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001453 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001454 return;
1455 }
1456
Chandler Carruth87c44602011-07-01 23:49:12 +00001457 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001458
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001459 // gcc rejects
1460 // class c {
1461 // static int a __attribute__((weakref ("v2")));
1462 // static int b() __attribute__((weakref ("f3")));
1463 // };
1464 // and ignores the attributes of
1465 // void f(void) {
1466 // static int a __attribute__((weakref ("v2")));
1467 // }
1468 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001469 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001470 if (!Ctx->isFileContext()) {
1471 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001472 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001473 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001474 }
1475
1476 // The GCC manual says
1477 //
1478 // At present, a declaration to which `weakref' is attached can only
1479 // be `static'.
1480 //
1481 // It also says
1482 //
1483 // Without a TARGET,
1484 // given as an argument to `weakref' or to `alias', `weakref' is
1485 // equivalent to `weak'.
1486 //
1487 // gcc 4.4.1 will accept
1488 // int a7 __attribute__((weakref));
1489 // as
1490 // int a7 __attribute__((weak));
1491 // This looks like a bug in gcc. We reject that for now. We should revisit
1492 // it if this behaviour is actually used.
1493
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001494 // GCC rejects
1495 // static ((alias ("y"), weakref)).
1496 // Should we? How to check that weakref is before or after alias?
1497
1498 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001499 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001500 Arg = Arg->IgnoreParenCasts();
1501 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1502
Douglas Gregor5cee1192011-07-27 05:40:30 +00001503 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001504 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1505 << "weakref" << 1;
1506 return;
1507 }
1508 // GCC will accept anything as the argument of weakref. Should we
1509 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001510 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001511 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001512 }
1513
Michael Han51d8c522013-01-24 16:46:58 +00001514 D->addAttr(::new (S.Context)
1515 WeakRefAttr(Attr.getRange(), S.Context,
1516 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001517}
1518
Chandler Carruth1b03c872011-07-02 00:01:44 +00001519static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001520 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001521 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001522 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523 return;
1524 }
Mike Stumpbf916502009-07-24 19:02:52 +00001525
Peter Collingbourne7a730022010-11-23 20:45:58 +00001526 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001527 Arg = Arg->IgnoreParenCasts();
1528 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001529
Douglas Gregor5cee1192011-07-27 05:40:30 +00001530 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001531 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001532 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001533 return;
1534 }
Mike Stumpbf916502009-07-24 19:02:52 +00001535
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001536 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001537 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1538 return;
1539 }
1540
Chris Lattner6b6b5372008-06-26 18:38:35 +00001541 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001542
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001543 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001544 Str->getString(),
1545 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001546}
1547
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001548static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1549 // Check the attribute arguments.
1550 if (!checkAttributeNumArgs(S, Attr, 0))
1551 return;
1552
1553 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1555 << Attr.getName() << ExpectedFunctionOrMethod;
1556 return;
1557 }
1558
Michael Han51d8c522013-01-24 16:46:58 +00001559 D->addAttr(::new (S.Context)
1560 MinSizeAttr(Attr.getRange(), S.Context,
1561 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001562}
1563
Benjamin Krameree409a92012-05-12 21:10:52 +00001564static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1565 // Check the attribute arguments.
1566 if (!checkAttributeNumArgs(S, Attr, 0))
1567 return;
1568
1569 if (!isa<FunctionDecl>(D)) {
1570 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1571 << Attr.getName() << ExpectedFunction;
1572 return;
1573 }
1574
1575 if (D->hasAttr<HotAttr>()) {
1576 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1577 << Attr.getName() << "hot";
1578 return;
1579 }
1580
Michael Han51d8c522013-01-24 16:46:58 +00001581 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1582 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001583}
1584
1585static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1586 // Check the attribute arguments.
1587 if (!checkAttributeNumArgs(S, Attr, 0))
1588 return;
1589
1590 if (!isa<FunctionDecl>(D)) {
1591 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1592 << Attr.getName() << ExpectedFunction;
1593 return;
1594 }
1595
1596 if (D->hasAttr<ColdAttr>()) {
1597 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1598 << Attr.getName() << "cold";
1599 return;
1600 }
1601
Michael Han51d8c522013-01-24 16:46:58 +00001602 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1603 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001604}
1605
Chandler Carruth1b03c872011-07-02 00:01:44 +00001606static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001607 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001608 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001609 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001610
Chandler Carruth87c44602011-07-01 23:49:12 +00001611 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001613 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001614 return;
1615 }
1616
Michael Han51d8c522013-01-24 16:46:58 +00001617 D->addAttr(::new (S.Context)
1618 NakedAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001620}
1621
Chandler Carruth1b03c872011-07-02 00:01:44 +00001622static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1623 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001624 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001625 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001626 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1627 return;
1628 }
1629
Chandler Carruth87c44602011-07-01 23:49:12 +00001630 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001632 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001633 return;
1634 }
Mike Stumpbf916502009-07-24 19:02:52 +00001635
Michael Han51d8c522013-01-24 16:46:58 +00001636 D->addAttr(::new (S.Context)
1637 AlwaysInlineAttr(Attr.getRange(), S.Context,
1638 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001639}
1640
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001641static void handleTLSModelAttr(Sema &S, Decl *D,
1642 const AttributeList &Attr) {
1643 // Check the attribute arguments.
1644 if (Attr.getNumArgs() != 1) {
1645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1646 return;
1647 }
1648
1649 Expr *Arg = Attr.getArg(0);
1650 Arg = Arg->IgnoreParenCasts();
1651 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1652
1653 // Check that it is a string.
1654 if (!Str) {
1655 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1656 return;
1657 }
1658
1659 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1660 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1661 << Attr.getName() << ExpectedTLSVar;
1662 return;
1663 }
1664
1665 // Check that the value.
1666 StringRef Model = Str->getString();
1667 if (Model != "global-dynamic" && Model != "local-dynamic"
1668 && Model != "initial-exec" && Model != "local-exec") {
1669 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1670 return;
1671 }
1672
Michael Han51d8c522013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 TLSModelAttr(Attr.getRange(), S.Context, Model,
1675 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001676}
1677
Chandler Carruth1b03c872011-07-02 00:01:44 +00001678static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001679 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001680 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1682 return;
1683 }
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Chandler Carruth87c44602011-07-01 23:49:12 +00001685 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001686 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001687 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001688 D->addAttr(::new (S.Context)
1689 MallocAttr(Attr.getRange(), S.Context,
1690 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001691 return;
1692 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001693 }
1694
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001695 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001696}
1697
Chandler Carruth1b03c872011-07-02 00:01:44 +00001698static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001699 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001700 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001701 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001702
Michael Han51d8c522013-01-24 16:46:58 +00001703 D->addAttr(::new (S.Context)
1704 MayAliasAttr(Attr.getRange(), S.Context,
1705 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001706}
1707
Chandler Carruth1b03c872011-07-02 00:01:44 +00001708static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001709 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001710 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001711 D->addAttr(::new (S.Context)
1712 NoCommonAttr(Attr.getRange(), S.Context,
1713 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001714 else
1715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001716 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001717}
1718
Chandler Carruth1b03c872011-07-02 00:01:44 +00001719static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001720 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001721 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001722 D->addAttr(::new (S.Context)
1723 CommonAttr(Attr.getRange(), S.Context,
1724 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001725 else
1726 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001727 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001728}
1729
Chandler Carruth1b03c872011-07-02 00:01:44 +00001730static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001731 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001732
1733 if (S.CheckNoReturnAttr(attr)) return;
1734
Chandler Carruth87c44602011-07-01 23:49:12 +00001735 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001736 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001737 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001738 return;
1739 }
1740
Michael Han51d8c522013-01-24 16:46:58 +00001741 D->addAttr(::new (S.Context)
1742 NoReturnAttr(attr.getRange(), S.Context,
1743 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001744}
1745
1746bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001747 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001748 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1749 attr.setInvalid();
1750 return true;
1751 }
1752
1753 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001754}
1755
Chandler Carruth1b03c872011-07-02 00:01:44 +00001756static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1757 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001758
1759 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1760 // because 'analyzer_noreturn' does not impact the type.
1761
Chandler Carruth1731e202011-07-11 23:30:35 +00001762 if(!checkAttributeNumArgs(S, Attr, 0))
1763 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001764
Chandler Carruth87c44602011-07-01 23:49:12 +00001765 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1766 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001767 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1768 && !VD->getType()->isFunctionPointerType())) {
1769 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001770 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001771 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001772 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001773 return;
1774 }
1775 }
1776
Michael Han51d8c522013-01-24 16:46:58 +00001777 D->addAttr(::new (S.Context)
1778 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1779 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001780}
1781
Richard Smithcd8ab512013-01-17 01:30:42 +00001782static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1783 const AttributeList &Attr) {
1784 // C++11 [dcl.attr.noreturn]p1:
1785 // The attribute may be applied to the declarator-id in a function
1786 // declaration.
1787 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1788 if (!FD) {
1789 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1790 << Attr.getName() << ExpectedFunctionOrMethod;
1791 return;
1792 }
1793
Michael Han51d8c522013-01-24 16:46:58 +00001794 D->addAttr(::new (S.Context)
1795 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1796 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001797}
1798
John Thompson35cc9622010-08-09 21:53:52 +00001799// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001800static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001801/*
1802 Returning a Vector Class in Registers
1803
Eric Christopherf48f3672010-12-01 22:13:54 +00001804 According to the PPU ABI specifications, a class with a single member of
1805 vector type is returned in memory when used as the return value of a function.
1806 This results in inefficient code when implementing vector classes. To return
1807 the value in a single vector register, add the vecreturn attribute to the
1808 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001809
1810 Example:
1811
1812 struct Vector
1813 {
1814 __vector float xyzw;
1815 } __attribute__((vecreturn));
1816
1817 Vector Add(Vector lhs, Vector rhs)
1818 {
1819 Vector result;
1820 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1821 return result; // This will be returned in a register
1822 }
1823*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001824 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001825 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001826 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001827 return;
1828 }
1829
Chandler Carruth87c44602011-07-01 23:49:12 +00001830 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001831 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1832 return;
1833 }
1834
Chandler Carruth87c44602011-07-01 23:49:12 +00001835 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001836 int count = 0;
1837
1838 if (!isa<CXXRecordDecl>(record)) {
1839 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1840 return;
1841 }
1842
1843 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1844 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1845 return;
1846 }
1847
Eric Christopherf48f3672010-12-01 22:13:54 +00001848 for (RecordDecl::field_iterator iter = record->field_begin();
1849 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001850 if ((count == 1) || !iter->getType()->isVectorType()) {
1851 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1852 return;
1853 }
1854 count++;
1855 }
1856
Michael Han51d8c522013-01-24 16:46:58 +00001857 D->addAttr(::new (S.Context)
1858 VecReturnAttr(Attr.getRange(), S.Context,
1859 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001860}
1861
Richard Smith3a2b7a12013-01-28 22:42:45 +00001862static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1863 const AttributeList &Attr) {
1864 if (isa<ParmVarDecl>(D)) {
1865 // [[carries_dependency]] can only be applied to a parameter if it is a
1866 // parameter of a function declaration or lambda.
1867 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1868 S.Diag(Attr.getLoc(),
1869 diag::err_carries_dependency_param_not_function_decl);
1870 return;
1871 }
1872 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001874 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001875 return;
1876 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001877
1878 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1879 Attr.getRange(), S.Context,
1880 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001881}
1882
Chandler Carruth1b03c872011-07-02 00:01:44 +00001883static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001884 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001885 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001887 return;
1888 }
Mike Stumpbf916502009-07-24 19:02:52 +00001889
Chandler Carruth87c44602011-07-01 23:49:12 +00001890 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001891 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001892 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001893 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001894 return;
1895 }
Mike Stumpbf916502009-07-24 19:02:52 +00001896
Michael Han51d8c522013-01-24 16:46:58 +00001897 D->addAttr(::new (S.Context)
1898 UnusedAttr(Attr.getRange(), S.Context,
1899 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001900}
1901
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001902static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1903 const AttributeList &Attr) {
1904 // check the attribute arguments.
1905 if (Attr.hasParameterOrArguments()) {
1906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1907 return;
1908 }
1909
1910 if (!isa<FunctionDecl>(D)) {
1911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1912 << Attr.getName() << ExpectedFunction;
1913 return;
1914 }
1915
Michael Han51d8c522013-01-24 16:46:58 +00001916 D->addAttr(::new (S.Context)
1917 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1918 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001919}
1920
Chandler Carruth1b03c872011-07-02 00:01:44 +00001921static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001922 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001923 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001924 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1925 return;
1926 }
Mike Stumpbf916502009-07-24 19:02:52 +00001927
Chandler Carruth87c44602011-07-01 23:49:12 +00001928 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001929 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001930 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1931 return;
1932 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001933 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001935 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001936 return;
1937 }
Mike Stumpbf916502009-07-24 19:02:52 +00001938
Michael Han51d8c522013-01-24 16:46:58 +00001939 D->addAttr(::new (S.Context)
1940 UsedAttr(Attr.getRange(), S.Context,
1941 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001942}
1943
Chandler Carruth1b03c872011-07-02 00:01:44 +00001944static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001945 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001946 if (Attr.getNumArgs() > 1) {
1947 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001948 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001949 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001950
1951 int priority = 65535; // FIXME: Do not hardcode such constants.
1952 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001953 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001954 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001955 if (E->isTypeDependent() || E->isValueDependent() ||
1956 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001957 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001958 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001959 return;
1960 }
1961 priority = Idx.getZExtValue();
1962 }
Mike Stumpbf916502009-07-24 19:02:52 +00001963
Chandler Carruth87c44602011-07-01 23:49:12 +00001964 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001966 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001967 return;
1968 }
1969
Michael Han51d8c522013-01-24 16:46:58 +00001970 D->addAttr(::new (S.Context)
1971 ConstructorAttr(Attr.getRange(), S.Context, priority,
1972 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001973}
1974
Chandler Carruth1b03c872011-07-02 00:01:44 +00001975static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001976 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001977 if (Attr.getNumArgs() > 1) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001979 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001980 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001981
1982 int priority = 65535; // FIXME: Do not hardcode such constants.
1983 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001984 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001985 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001986 if (E->isTypeDependent() || E->isValueDependent() ||
1987 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001989 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001990 return;
1991 }
1992 priority = Idx.getZExtValue();
1993 }
Mike Stumpbf916502009-07-24 19:02:52 +00001994
Chandler Carruth87c44602011-07-01 23:49:12 +00001995 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001997 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001998 return;
1999 }
2000
Michael Han51d8c522013-01-24 16:46:58 +00002001 D->addAttr(::new (S.Context)
2002 DestructorAttr(Attr.getRange(), S.Context, priority,
2003 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002004}
2005
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002006template <typename AttrTy>
2007static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
2008 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002009 unsigned NumArgs = Attr.getNumArgs();
2010 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002011 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002012 return;
2013 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002014
2015 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002016 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002017 if (NumArgs == 1) {
2018 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002019 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002020 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002021 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002022 return;
2023 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002024 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002025 }
Mike Stumpbf916502009-07-24 19:02:52 +00002026
Michael Han51d8c522013-01-24 16:46:58 +00002027 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2028 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002029}
2030
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002031static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2032 const AttributeList &Attr) {
2033 unsigned NumArgs = Attr.getNumArgs();
2034 if (NumArgs > 0) {
2035 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2036 return;
2037 }
2038
Michael Han51d8c522013-01-24 16:46:58 +00002039 D->addAttr(::new (S.Context)
2040 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2041 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002042}
2043
Patrick Beardb2f68202012-04-06 18:12:22 +00002044static void handleObjCRootClassAttr(Sema &S, Decl *D,
2045 const AttributeList &Attr) {
2046 if (!isa<ObjCInterfaceDecl>(D)) {
2047 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2048 return;
2049 }
2050
2051 unsigned NumArgs = Attr.getNumArgs();
2052 if (NumArgs > 0) {
2053 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2054 return;
2055 }
2056
Michael Han51d8c522013-01-24 16:46:58 +00002057 D->addAttr(::new (S.Context)
2058 ObjCRootClassAttr(Attr.getRange(), S.Context,
2059 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002060}
2061
Michael Han51d8c522013-01-24 16:46:58 +00002062static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2063 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002064 if (!isa<ObjCInterfaceDecl>(D)) {
2065 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2066 return;
2067 }
2068
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002069 unsigned NumArgs = Attr.getNumArgs();
2070 if (NumArgs > 0) {
2071 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2072 return;
2073 }
2074
Michael Han51d8c522013-01-24 16:46:58 +00002075 D->addAttr(::new (S.Context)
2076 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2077 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002078}
2079
Jordy Rosefad5de92012-05-08 03:27:22 +00002080static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2081 IdentifierInfo *Platform,
2082 VersionTuple Introduced,
2083 VersionTuple Deprecated,
2084 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002085 StringRef PlatformName
2086 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2087 if (PlatformName.empty())
2088 PlatformName = Platform->getName();
2089
2090 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2091 // of these steps are needed).
2092 if (!Introduced.empty() && !Deprecated.empty() &&
2093 !(Introduced <= Deprecated)) {
2094 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2095 << 1 << PlatformName << Deprecated.getAsString()
2096 << 0 << Introduced.getAsString();
2097 return true;
2098 }
2099
2100 if (!Introduced.empty() && !Obsoleted.empty() &&
2101 !(Introduced <= Obsoleted)) {
2102 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2103 << 2 << PlatformName << Obsoleted.getAsString()
2104 << 0 << Introduced.getAsString();
2105 return true;
2106 }
2107
2108 if (!Deprecated.empty() && !Obsoleted.empty() &&
2109 !(Deprecated <= Obsoleted)) {
2110 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2111 << 2 << PlatformName << Obsoleted.getAsString()
2112 << 1 << Deprecated.getAsString();
2113 return true;
2114 }
2115
2116 return false;
2117}
2118
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002119/// \brief Check whether the two versions match.
2120///
2121/// If either version tuple is empty, then they are assumed to match. If
2122/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2123static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2124 bool BeforeIsOkay) {
2125 if (X.empty() || Y.empty())
2126 return true;
2127
2128 if (X == Y)
2129 return true;
2130
2131 if (BeforeIsOkay && X < Y)
2132 return true;
2133
2134 return false;
2135}
2136
Rafael Espindola51be6e32013-01-08 22:04:34 +00002137AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002138 IdentifierInfo *Platform,
2139 VersionTuple Introduced,
2140 VersionTuple Deprecated,
2141 VersionTuple Obsoleted,
2142 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002143 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002144 bool Override,
2145 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002146 VersionTuple MergedIntroduced = Introduced;
2147 VersionTuple MergedDeprecated = Deprecated;
2148 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002149 bool FoundAny = false;
2150
Rafael Espindola98ae8342012-05-10 02:50:16 +00002151 if (D->hasAttrs()) {
2152 AttrVec &Attrs = D->getAttrs();
2153 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2154 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2155 if (!OldAA) {
2156 ++i;
2157 continue;
2158 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002159
Rafael Espindola98ae8342012-05-10 02:50:16 +00002160 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2161 if (OldPlatform != Platform) {
2162 ++i;
2163 continue;
2164 }
2165
2166 FoundAny = true;
2167 VersionTuple OldIntroduced = OldAA->getIntroduced();
2168 VersionTuple OldDeprecated = OldAA->getDeprecated();
2169 VersionTuple OldObsoleted = OldAA->getObsoleted();
2170 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002171
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002172 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2173 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2174 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2175 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002176 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002177 if (Override) {
2178 int Which = -1;
2179 VersionTuple FirstVersion;
2180 VersionTuple SecondVersion;
2181 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2182 Which = 0;
2183 FirstVersion = OldIntroduced;
2184 SecondVersion = Introduced;
2185 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2186 Which = 1;
2187 FirstVersion = Deprecated;
2188 SecondVersion = OldDeprecated;
2189 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2190 Which = 2;
2191 FirstVersion = Obsoleted;
2192 SecondVersion = OldObsoleted;
2193 }
2194
2195 if (Which == -1) {
2196 Diag(OldAA->getLocation(),
2197 diag::warn_mismatched_availability_override_unavail)
2198 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2199 } else {
2200 Diag(OldAA->getLocation(),
2201 diag::warn_mismatched_availability_override)
2202 << Which
2203 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2204 << FirstVersion.getAsString() << SecondVersion.getAsString();
2205 }
2206 Diag(Range.getBegin(), diag::note_overridden_method);
2207 } else {
2208 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2209 Diag(Range.getBegin(), diag::note_previous_attribute);
2210 }
2211
Rafael Espindola98ae8342012-05-10 02:50:16 +00002212 Attrs.erase(Attrs.begin() + i);
2213 --e;
2214 continue;
2215 }
2216
2217 VersionTuple MergedIntroduced2 = MergedIntroduced;
2218 VersionTuple MergedDeprecated2 = MergedDeprecated;
2219 VersionTuple MergedObsoleted2 = MergedObsoleted;
2220
2221 if (MergedIntroduced2.empty())
2222 MergedIntroduced2 = OldIntroduced;
2223 if (MergedDeprecated2.empty())
2224 MergedDeprecated2 = OldDeprecated;
2225 if (MergedObsoleted2.empty())
2226 MergedObsoleted2 = OldObsoleted;
2227
2228 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2229 MergedIntroduced2, MergedDeprecated2,
2230 MergedObsoleted2)) {
2231 Attrs.erase(Attrs.begin() + i);
2232 --e;
2233 continue;
2234 }
2235
2236 MergedIntroduced = MergedIntroduced2;
2237 MergedDeprecated = MergedDeprecated2;
2238 MergedObsoleted = MergedObsoleted2;
2239 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002240 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002241 }
2242
2243 if (FoundAny &&
2244 MergedIntroduced == Introduced &&
2245 MergedDeprecated == Deprecated &&
2246 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002247 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002248
Rafael Espindola98ae8342012-05-10 02:50:16 +00002249 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002250 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002251 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2252 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002253 Obsoleted, IsUnavailable, Message,
2254 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002255 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002256 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002257}
2258
Chandler Carruth1b03c872011-07-02 00:01:44 +00002259static void handleAvailabilityAttr(Sema &S, Decl *D,
2260 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002261 IdentifierInfo *Platform = Attr.getParameterName();
2262 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002263 unsigned Index = Attr.getAttributeSpellingListIndex();
2264
Rafael Espindola3b294362012-05-06 19:56:25 +00002265 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002266 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2267 << Platform;
2268
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002269 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2270 if (!ND) {
2271 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2272 return;
2273 }
2274
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002275 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2276 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2277 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002278 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002279 StringRef Str;
2280 const StringLiteral *SE =
2281 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2282 if (SE)
2283 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002284
Rafael Espindola51be6e32013-01-08 22:04:34 +00002285 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002286 Platform,
2287 Introduced.Version,
2288 Deprecated.Version,
2289 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002290 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002291 /*Override=*/false,
2292 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002293 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002294 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002295}
2296
John McCalld4c3d662013-02-20 01:54:26 +00002297template <class T>
2298static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2299 typename T::VisibilityType value,
2300 unsigned attrSpellingListIndex) {
2301 T *existingAttr = D->getAttr<T>();
2302 if (existingAttr) {
2303 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2304 if (existingValue == value)
2305 return NULL;
2306 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2307 S.Diag(range.getBegin(), diag::note_previous_attribute);
2308 D->dropAttr<T>();
2309 }
2310 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2311}
2312
Rafael Espindola599f1b72012-05-13 03:25:18 +00002313VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002314 VisibilityAttr::VisibilityType Vis,
2315 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002316 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2317 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002318}
2319
John McCalld4c3d662013-02-20 01:54:26 +00002320TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2321 TypeVisibilityAttr::VisibilityType Vis,
2322 unsigned AttrSpellingListIndex) {
2323 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2324 AttrSpellingListIndex);
2325}
2326
2327static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2328 bool isTypeVisibility) {
2329 // Visibility attributes don't mean anything on a typedef.
2330 if (isa<TypedefNameDecl>(D)) {
2331 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2332 << Attr.getName();
2333 return;
2334 }
2335
2336 // 'type_visibility' can only go on a type or namespace.
2337 if (isTypeVisibility &&
2338 !(isa<TagDecl>(D) ||
2339 isa<ObjCInterfaceDecl>(D) ||
2340 isa<NamespaceDecl>(D))) {
2341 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2342 << Attr.getName() << ExpectedTypeOrNamespace;
2343 return;
2344 }
2345
Chris Lattner6b6b5372008-06-26 18:38:35 +00002346 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002347 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002348 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002349
Peter Collingbourne7a730022010-11-23 20:45:58 +00002350 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002351 Arg = Arg->IgnoreParenCasts();
2352 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002353
Douglas Gregor5cee1192011-07-27 05:40:30 +00002354 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002355 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld4c3d662013-02-20 01:54:26 +00002356 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002357 return;
2358 }
Mike Stumpbf916502009-07-24 19:02:52 +00002359
Chris Lattner5f9e2722011-07-23 10:55:15 +00002360 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002361 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002362
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002363 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002364 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002365 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002366 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002367 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002368 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002369 else if (TypeStr == "protected") {
2370 // Complain about attempts to use protected visibility on targets
2371 // (like Darwin) that don't support it.
2372 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2373 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2374 type = VisibilityAttr::Default;
2375 } else {
2376 type = VisibilityAttr::Protected;
2377 }
2378 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002379 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002380 return;
2381 }
Mike Stumpbf916502009-07-24 19:02:52 +00002382
Michael Han51d8c522013-01-24 16:46:58 +00002383 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002384 clang::Attr *newAttr;
2385 if (isTypeVisibility) {
2386 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2387 (TypeVisibilityAttr::VisibilityType) type,
2388 Index);
2389 } else {
2390 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2391 }
2392 if (newAttr)
2393 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002394}
2395
Chandler Carruth1b03c872011-07-02 00:01:44 +00002396static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2397 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002398 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2399 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002401 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002402 return;
2403 }
2404
Chandler Carruth87c44602011-07-01 23:49:12 +00002405 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2406 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2407 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002408 << "objc_method_family" << 1;
2409 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002411 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002412 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002413 return;
2414 }
2415
Chris Lattner5f9e2722011-07-23 10:55:15 +00002416 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002417 ObjCMethodFamilyAttr::FamilyKind family;
2418 if (param == "none")
2419 family = ObjCMethodFamilyAttr::OMF_None;
2420 else if (param == "alloc")
2421 family = ObjCMethodFamilyAttr::OMF_alloc;
2422 else if (param == "copy")
2423 family = ObjCMethodFamilyAttr::OMF_copy;
2424 else if (param == "init")
2425 family = ObjCMethodFamilyAttr::OMF_init;
2426 else if (param == "mutableCopy")
2427 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2428 else if (param == "new")
2429 family = ObjCMethodFamilyAttr::OMF_new;
2430 else {
2431 // Just warn and ignore it. This is future-proof against new
2432 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002433 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002434 return;
2435 }
2436
John McCallf85e1932011-06-15 23:02:42 +00002437 if (family == ObjCMethodFamilyAttr::OMF_init &&
2438 !method->getResultType()->isObjCObjectPointerType()) {
2439 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2440 << method->getResultType();
2441 // Ignore the attribute.
2442 return;
2443 }
2444
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002445 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002446 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002447}
2448
Chandler Carruth1b03c872011-07-02 00:01:44 +00002449static void handleObjCExceptionAttr(Sema &S, Decl *D,
2450 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002451 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002452 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002453
Chris Lattner0db29ec2009-02-14 08:09:34 +00002454 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2455 if (OCI == 0) {
2456 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2457 return;
2458 }
Mike Stumpbf916502009-07-24 19:02:52 +00002459
Michael Han51d8c522013-01-24 16:46:58 +00002460 D->addAttr(::new (S.Context)
2461 ObjCExceptionAttr(Attr.getRange(), S.Context,
2462 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002463}
2464
Chandler Carruth1b03c872011-07-02 00:01:44 +00002465static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002466 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002468 return;
2469 }
Richard Smith162e1c12011-04-15 14:24:37 +00002470 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002471 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002472 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002473 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2474 return;
2475 }
2476 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002477 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2478 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002479 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002480 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2481 return;
2482 }
2483 }
2484 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002485 // It is okay to include this attribute on properties, e.g.:
2486 //
2487 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2488 //
2489 // In this case it follows tradition and suppresses an error in the above
2490 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002491 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002492 }
Michael Han51d8c522013-01-24 16:46:58 +00002493 D->addAttr(::new (S.Context)
2494 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2495 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002496}
2497
Mike Stumpbf916502009-07-24 19:02:52 +00002498static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002499handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002500 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002501 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002502 return;
2503 }
2504
2505 if (!isa<FunctionDecl>(D)) {
2506 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2507 return;
2508 }
2509
Michael Han51d8c522013-01-24 16:46:58 +00002510 D->addAttr(::new (S.Context)
2511 OverloadableAttr(Attr.getRange(), S.Context,
2512 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002513}
2514
Chandler Carruth1b03c872011-07-02 00:01:44 +00002515static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002516 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002517 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002518 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002519 return;
2520 }
Mike Stumpbf916502009-07-24 19:02:52 +00002521
Steve Naroff9eae5762008-09-18 16:44:58 +00002522 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002524 return;
2525 }
Mike Stumpbf916502009-07-24 19:02:52 +00002526
Sean Huntcf807c42010-08-18 23:23:40 +00002527 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002528 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002529 type = BlocksAttr::ByRef;
2530 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002531 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002532 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002533 return;
2534 }
Mike Stumpbf916502009-07-24 19:02:52 +00002535
Michael Han51d8c522013-01-24 16:46:58 +00002536 D->addAttr(::new (S.Context)
2537 BlocksAttr(Attr.getRange(), S.Context, type,
2538 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002539}
2540
Chandler Carruth1b03c872011-07-02 00:01:44 +00002541static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002542 // check the attribute arguments.
2543 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002544 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002545 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002546 }
2547
John McCall3323fad2011-09-09 07:56:05 +00002548 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002549 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002550 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002551 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002552 if (E->isTypeDependent() || E->isValueDependent() ||
2553 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002555 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002556 return;
2557 }
Mike Stumpbf916502009-07-24 19:02:52 +00002558
John McCall3323fad2011-09-09 07:56:05 +00002559 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002560 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2561 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002562 return;
2563 }
John McCall3323fad2011-09-09 07:56:05 +00002564
2565 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002566 }
2567
John McCall3323fad2011-09-09 07:56:05 +00002568 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002569 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002570 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002571 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002572 if (E->isTypeDependent() || E->isValueDependent() ||
2573 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002574 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002575 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002576 return;
2577 }
2578 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002579
John McCall3323fad2011-09-09 07:56:05 +00002580 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002581 // FIXME: This error message could be improved, it would be nice
2582 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002583 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2584 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002585 return;
2586 }
2587 }
2588
Chandler Carruth87c44602011-07-01 23:49:12 +00002589 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002590 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002591 if (isa<FunctionNoProtoType>(FT)) {
2592 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2593 return;
2594 }
Mike Stumpbf916502009-07-24 19:02:52 +00002595
Chris Lattner897cd902009-03-17 23:03:47 +00002596 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002597 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002598 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002599 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002600 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002601 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002602 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002603 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002604 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002605 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2606 if (!BD->isVariadic()) {
2607 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2608 return;
2609 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002610 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002611 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002612 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002613 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002614 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002615 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002616 int m = Ty->isFunctionPointerType() ? 0 : 1;
2617 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002618 return;
2619 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002620 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002621 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002622 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002623 return;
2624 }
Anders Carlsson77091822008-10-05 18:05:59 +00002625 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002626 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002627 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002628 return;
2629 }
Michael Han51d8c522013-01-24 16:46:58 +00002630 D->addAttr(::new (S.Context)
2631 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2632 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002633}
2634
Chandler Carruth1b03c872011-07-02 00:01:44 +00002635static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002636 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002637 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002638 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002639
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002640 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002642 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002643 return;
2644 }
Mike Stumpbf916502009-07-24 19:02:52 +00002645
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002646 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2647 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2648 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002649 return;
2650 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002651 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2652 if (MD->getResultType()->isVoidType()) {
2653 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2654 << Attr.getName() << 1;
2655 return;
2656 }
2657
Michael Han51d8c522013-01-24 16:46:58 +00002658 D->addAttr(::new (S.Context)
2659 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2660 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002661}
2662
Chandler Carruth1b03c872011-07-02 00:01:44 +00002663static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002664 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002665 if (Attr.hasParameterOrArguments()) {
2666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002667 return;
2668 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002669
Chandler Carruth87c44602011-07-01 23:49:12 +00002670 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002671 if (isa<CXXRecordDecl>(D)) {
2672 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2673 return;
2674 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2676 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002677 return;
2678 }
2679
Chandler Carruth87c44602011-07-01 23:49:12 +00002680 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002681
Michael Han51d8c522013-01-24 16:46:58 +00002682 nd->addAttr(::new (S.Context)
2683 WeakAttr(Attr.getRange(), S.Context,
2684 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002685}
2686
Chandler Carruth1b03c872011-07-02 00:01:44 +00002687static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002688 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002689 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002690 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002691
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002692
2693 // weak_import only applies to variable & function declarations.
2694 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002695 if (!D->canBeWeakImported(isDef)) {
2696 if (isDef)
2697 S.Diag(Attr.getLoc(),
2698 diag::warn_attribute_weak_import_invalid_on_definition)
2699 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002700 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002701 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002702 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002703 // Nothing to warn about here.
2704 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002705 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002706 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002707
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002708 return;
2709 }
2710
Michael Han51d8c522013-01-24 16:46:58 +00002711 D->addAttr(::new (S.Context)
2712 WeakImportAttr(Attr.getRange(), S.Context,
2713 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002714}
2715
Tanya Lattner0df579e2012-07-09 22:06:01 +00002716// Handles reqd_work_group_size and work_group_size_hint.
2717static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002718 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002719 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2720 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2721
Nate Begeman6f3d8382009-06-26 06:32:41 +00002722 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002723 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002724
2725 unsigned WGSize[3];
2726 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002727 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002728 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002729 if (E->isTypeDependent() || E->isValueDependent() ||
2730 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002731 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002732 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002733 return;
2734 }
2735 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2736 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002737
2738 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2739 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2740 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2741 if (!(A->getXDim() == WGSize[0] &&
2742 A->getYDim() == WGSize[1] &&
2743 A->getZDim() == WGSize[2])) {
2744 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2745 Attr.getName();
2746 }
2747 }
2748
2749 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2750 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2751 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2752 if (!(A->getXDim() == WGSize[0] &&
2753 A->getYDim() == WGSize[1] &&
2754 A->getZDim() == WGSize[2])) {
2755 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2756 Attr.getName();
2757 }
2758 }
2759
2760 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2761 D->addAttr(::new (S.Context)
2762 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002763 WGSize[0], WGSize[1], WGSize[2],
2764 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002765 else
2766 D->addAttr(::new (S.Context)
2767 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002768 WGSize[0], WGSize[1], WGSize[2],
2769 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002770}
2771
Joey Gouly37453b92013-03-08 09:42:32 +00002772static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2773 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2774
2775 // Attribute has 1 argument.
2776 if (!checkAttributeNumArgs(S, Attr, 1))
2777 return;
2778
2779 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2780
2781 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2782 (ParmType->isBooleanType() ||
2783 !ParmType->isIntegralType(S.getASTContext()))) {
2784 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2785 << ParmType;
2786 return;
2787 }
2788
2789 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2790 D->hasAttr<VecTypeHintAttr>()) {
2791 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2792 if (A->getTypeHint() != ParmType) {
2793 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2794 return;
2795 }
2796 }
2797
2798 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2799 ParmType, Attr.getLoc()));
2800}
2801
Joey Gouly96cead52013-03-14 09:54:43 +00002802static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2803 if (!dyn_cast<VarDecl>(D))
2804 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2805 << 9;
2806 StringRef EndianType = Attr.getParameterName()->getName();
2807 if (EndianType != "host" && EndianType != "device")
2808 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2809}
2810
Rafael Espindola599f1b72012-05-13 03:25:18 +00002811SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002812 StringRef Name,
2813 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002814 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2815 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002816 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002817 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2818 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002819 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002820 }
Michael Han51d8c522013-01-24 16:46:58 +00002821 return ::new (Context) SectionAttr(Range, Context, Name,
2822 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002823}
2824
Chandler Carruth1b03c872011-07-02 00:01:44 +00002825static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002826 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002827 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002828 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002829
2830 // Make sure that there is a string literal as the sections's single
2831 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002832 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002833 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002834 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002835 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002836 return;
2837 }
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Chris Lattner797c3c42009-08-10 19:03:04 +00002839 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002840 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002841 if (!Error.empty()) {
2842 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2843 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002844 return;
2845 }
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002847 // This attribute cannot be applied to local variables.
2848 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2849 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2850 return;
2851 }
Michael Han51d8c522013-01-24 16:46:58 +00002852
2853 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002854 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002855 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002856 if (NewAttr)
2857 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002858}
2859
Chris Lattner6b6b5372008-06-26 18:38:35 +00002860
Chandler Carruth1b03c872011-07-02 00:01:44 +00002861static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002862 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002863 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002865 return;
2866 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002867
Chandler Carruth87c44602011-07-01 23:49:12 +00002868 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002869 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002870 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002871 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002872 D->addAttr(::new (S.Context)
2873 NoThrowAttr(Attr.getRange(), S.Context,
2874 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002875 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002876}
2877
Chandler Carruth1b03c872011-07-02 00:01:44 +00002878static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002879 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002880 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002881 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002882 return;
2883 }
Mike Stumpbf916502009-07-24 19:02:52 +00002884
Chandler Carruth87c44602011-07-01 23:49:12 +00002885 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002886 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002887 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002888 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002889 D->addAttr(::new (S.Context)
2890 ConstAttr(Attr.getRange(), S.Context,
2891 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002892 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002893}
2894
Chandler Carruth1b03c872011-07-02 00:01:44 +00002895static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002896 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002897 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002898 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002899
Michael Han51d8c522013-01-24 16:46:58 +00002900 D->addAttr(::new (S.Context)
2901 PureAttr(Attr.getRange(), S.Context,
2902 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002903}
2904
Chandler Carruth1b03c872011-07-02 00:01:44 +00002905static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002906 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2908 return;
2909 }
Mike Stumpbf916502009-07-24 19:02:52 +00002910
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002911 if (Attr.getNumArgs() != 0) {
2912 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2913 return;
2914 }
Mike Stumpbf916502009-07-24 19:02:52 +00002915
Chandler Carruth87c44602011-07-01 23:49:12 +00002916 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002917
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002918 if (!VD || !VD->hasLocalStorage()) {
2919 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2920 return;
2921 }
Mike Stumpbf916502009-07-24 19:02:52 +00002922
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002923 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002924 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002925 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002926 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2927 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002928 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002929 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002930 Attr.getParameterName();
2931 return;
2932 }
Mike Stumpbf916502009-07-24 19:02:52 +00002933
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002934 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2935 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002936 S.Diag(Attr.getParameterLoc(),
2937 diag::err_attribute_cleanup_arg_not_function)
2938 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002939 return;
2940 }
2941
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002942 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002943 S.Diag(Attr.getParameterLoc(),
2944 diag::err_attribute_cleanup_func_must_take_one_arg)
2945 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002946 return;
2947 }
Mike Stumpbf916502009-07-24 19:02:52 +00002948
Anders Carlsson89941c12009-02-07 23:16:50 +00002949 // We're currently more strict than GCC about what function types we accept.
2950 // If this ever proves to be a problem it should be easy to fix.
2951 QualType Ty = S.Context.getPointerType(VD->getType());
2952 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002953 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2954 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002955 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002956 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2957 Attr.getParameterName() << ParamTy << Ty;
2958 return;
2959 }
Mike Stumpbf916502009-07-24 19:02:52 +00002960
Michael Han51d8c522013-01-24 16:46:58 +00002961 D->addAttr(::new (S.Context)
2962 CleanupAttr(Attr.getRange(), S.Context, FD,
2963 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002964 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00002965 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002966}
2967
Mike Stumpbf916502009-07-24 19:02:52 +00002968/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002969/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002970static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002971 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002972 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002973
Chandler Carruth87c44602011-07-01 23:49:12 +00002974 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002976 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002977 return;
2978 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002979
2980 // In C++ the implicit 'this' function parameter also counts, and they are
2981 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002982 bool HasImplicitThisParam = isInstanceMethod(D);
2983 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002984 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002985
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002986 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002987 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002988 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002989 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2990 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002991 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2992 << "format" << 2 << IdxExpr->getSourceRange();
2993 return;
2994 }
Mike Stumpbf916502009-07-24 19:02:52 +00002995
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002996 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2997 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2998 << "format" << 2 << IdxExpr->getSourceRange();
2999 return;
3000 }
Mike Stumpbf916502009-07-24 19:02:52 +00003001
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003002 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003003
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003004 if (HasImplicitThisParam) {
3005 if (ArgIdx == 0) {
3006 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3007 << "format_arg" << IdxExpr->getSourceRange();
3008 return;
3009 }
3010 ArgIdx--;
3011 }
3012
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003013 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003014 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003015
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003016 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3017 if (not_nsstring_type &&
3018 !isCFStringType(Ty, S.Context) &&
3019 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003020 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003021 // FIXME: Should highlight the actual expression that has the wrong type.
3022 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003023 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003024 << IdxExpr->getSourceRange();
3025 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003026 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003027 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003028 if (!isNSStringType(Ty, S.Context) &&
3029 !isCFStringType(Ty, S.Context) &&
3030 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003031 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003032 // FIXME: Should highlight the actual expression that has the wrong type.
3033 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003034 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003035 << IdxExpr->getSourceRange();
3036 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003037 }
3038
Michael Han51d8c522013-01-24 16:46:58 +00003039 D->addAttr(::new (S.Context)
3040 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3041 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003042}
3043
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003044enum FormatAttrKind {
3045 CFStringFormat,
3046 NSStringFormat,
3047 StrftimeFormat,
3048 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003049 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003050 InvalidFormat
3051};
3052
3053/// getFormatAttrKind - Map from format attribute names to supported format
3054/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003055static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003056 return llvm::StringSwitch<FormatAttrKind>(Format)
3057 // Check for formats that get handled specially.
3058 .Case("NSString", NSStringFormat)
3059 .Case("CFString", CFStringFormat)
3060 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003061
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003062 // Otherwise, check for supported formats.
3063 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3064 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3065 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003066
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003067 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3068 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003069}
3070
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003071/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003072/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003073static void handleInitPriorityAttr(Sema &S, Decl *D,
3074 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003075 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003076 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3077 return;
3078 }
3079
Chandler Carruth87c44602011-07-01 23:49:12 +00003080 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003081 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3082 Attr.setInvalid();
3083 return;
3084 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003085 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003086 if (S.Context.getAsArrayType(T))
3087 T = S.Context.getBaseElementType(T);
3088 if (!T->getAs<RecordType>()) {
3089 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3090 Attr.setInvalid();
3091 return;
3092 }
3093
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003094 if (Attr.getNumArgs() != 1) {
3095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3096 Attr.setInvalid();
3097 return;
3098 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003099 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003100
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003101 llvm::APSInt priority(32);
3102 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3103 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3104 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3105 << "init_priority" << priorityExpr->getSourceRange();
3106 Attr.setInvalid();
3107 return;
3108 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003109 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003110 if (prioritynum < 101 || prioritynum > 65535) {
3111 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3112 << priorityExpr->getSourceRange();
3113 Attr.setInvalid();
3114 return;
3115 }
Michael Han51d8c522013-01-24 16:46:58 +00003116 D->addAttr(::new (S.Context)
3117 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3118 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003119}
3120
Rafael Espindola599f1b72012-05-13 03:25:18 +00003121FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003122 int FormatIdx, int FirstArg,
3123 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003124 // Check whether we already have an equivalent format attribute.
3125 for (specific_attr_iterator<FormatAttr>
3126 i = D->specific_attr_begin<FormatAttr>(),
3127 e = D->specific_attr_end<FormatAttr>();
3128 i != e ; ++i) {
3129 FormatAttr *f = *i;
3130 if (f->getType() == Format &&
3131 f->getFormatIdx() == FormatIdx &&
3132 f->getFirstArg() == FirstArg) {
3133 // If we don't have a valid location for this attribute, adopt the
3134 // location.
3135 if (f->getLocation().isInvalid())
3136 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003137 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003138 }
3139 }
3140
Michael Han51d8c522013-01-24 16:46:58 +00003141 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3142 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003143}
3144
Mike Stumpbf916502009-07-24 19:02:52 +00003145/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003146/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003147static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003148
Chris Lattner545dd342008-06-28 23:36:30 +00003149 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00003151 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003152 return;
3153 }
3154
Chris Lattner545dd342008-06-28 23:36:30 +00003155 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003157 return;
3158 }
3159
Chandler Carruth87c44602011-07-01 23:49:12 +00003160 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003162 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003163 return;
3164 }
3165
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003166 // In C++ the implicit 'this' function parameter also counts, and they are
3167 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003168 bool HasImplicitThisParam = isInstanceMethod(D);
3169 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003170 unsigned FirstIdx = 1;
3171
Chris Lattner5f9e2722011-07-23 10:55:15 +00003172 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003173
3174 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003175 if (Format.startswith("__") && Format.endswith("__"))
3176 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003177
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003178 // Check for supported formats.
3179 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003180
3181 if (Kind == IgnoredFormat)
3182 return;
3183
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003184 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003185 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003186 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003187 return;
3188 }
3189
3190 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003191 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003192 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003193 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3194 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003196 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197 return;
3198 }
3199
3200 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003202 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003203 return;
3204 }
3205
3206 // FIXME: Do we need to bounds check?
3207 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003208
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003209 if (HasImplicitThisParam) {
3210 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003211 S.Diag(Attr.getLoc(),
3212 diag::err_format_attribute_implicit_this_format_string)
3213 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003214 return;
3215 }
3216 ArgIdx--;
3217 }
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Chris Lattner6b6b5372008-06-26 18:38:35 +00003219 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003220 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003221
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003222 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003223 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003224 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3225 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003226 return;
3227 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003228 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003229 // FIXME: do we need to check if the type is NSString*? What are the
3230 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003231 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003232 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003233 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3234 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003235 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003236 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003237 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003238 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003239 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003240 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3241 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242 return;
3243 }
3244
3245 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003246 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003247 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003248 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3249 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003251 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003252 return;
3253 }
3254
3255 // check if the function is variadic if the 3rd argument non-zero
3256 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003257 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003258 ++NumArgs; // +1 for ...
3259 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003260 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003261 return;
3262 }
3263 }
3264
Chris Lattner3c73c412008-11-19 08:23:25 +00003265 // strftime requires FirstArg to be 0 because it doesn't read from any
3266 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003267 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003268 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003269 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3270 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003271 return;
3272 }
3273 // if 0 it disables parameter checking (to use with e.g. va_list)
3274 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003276 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003277 return;
3278 }
3279
Rafael Espindola599f1b72012-05-13 03:25:18 +00003280 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3281 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003282 FirstArg.getZExtValue(),
3283 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003284 if (NewAttr)
3285 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003286}
3287
Chandler Carruth1b03c872011-07-02 00:01:44 +00003288static void handleTransparentUnionAttr(Sema &S, Decl *D,
3289 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003290 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003291 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003292 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003293
Chris Lattner6b6b5372008-06-26 18:38:35 +00003294
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003295 // Try to find the underlying union declaration.
3296 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003297 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003298 if (TD && TD->getUnderlyingType()->isUnionType())
3299 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3300 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003301 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003302
3303 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003304 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003305 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003306 return;
3307 }
3308
John McCall5e1cdac2011-10-07 06:10:15 +00003309 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003310 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003311 diag::warn_transparent_union_attribute_not_definition);
3312 return;
3313 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003314
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003315 RecordDecl::field_iterator Field = RD->field_begin(),
3316 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003317 if (Field == FieldEnd) {
3318 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3319 return;
3320 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003321
David Blaikie581deb32012-06-06 20:45:41 +00003322 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003323 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003324 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003325 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003326 diag::warn_transparent_union_attribute_floating)
3327 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003328 return;
3329 }
3330
3331 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3332 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3333 for (; Field != FieldEnd; ++Field) {
3334 QualType FieldType = Field->getType();
3335 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3336 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3337 // Warn if we drop the attribute.
3338 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003339 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003340 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003341 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003342 diag::warn_transparent_union_attribute_field_size_align)
3343 << isSize << Field->getDeclName() << FieldBits;
3344 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003345 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003346 diag::note_transparent_union_first_field_size_align)
3347 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003348 return;
3349 }
3350 }
3351
Michael Han51d8c522013-01-24 16:46:58 +00003352 RD->addAttr(::new (S.Context)
3353 TransparentUnionAttr(Attr.getRange(), S.Context,
3354 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003355}
3356
Chandler Carruth1b03c872011-07-02 00:01:44 +00003357static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003358 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003359 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003360 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003361
Peter Collingbourne7a730022010-11-23 20:45:58 +00003362 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003363 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003364
Chris Lattner6b6b5372008-06-26 18:38:35 +00003365 // Make sure that there is a string literal as the annotation's single
3366 // argument.
3367 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003368 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003369 return;
3370 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003371
3372 // Don't duplicate annotations that are already set.
3373 for (specific_attr_iterator<AnnotateAttr>
3374 i = D->specific_attr_begin<AnnotateAttr>(),
3375 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3376 if ((*i)->getAnnotation() == SE->getString())
3377 return;
3378 }
Michael Han51d8c522013-01-24 16:46:58 +00003379
3380 D->addAttr(::new (S.Context)
3381 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3382 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003383}
3384
Chandler Carruth1b03c872011-07-02 00:01:44 +00003385static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003386 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003387 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003389 return;
3390 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003391
Richard Smithbe507b62013-02-01 08:12:08 +00003392 if (Attr.getNumArgs() == 0) {
3393 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3394 true, 0, Attr.getAttributeSpellingListIndex()));
3395 return;
3396 }
3397
Richard Smithf6565a92013-02-22 08:32:16 +00003398 Expr *E = Attr.getArg(0);
3399 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3400 S.Diag(Attr.getEllipsisLoc(),
3401 diag::err_pack_expansion_without_parameter_packs);
3402 return;
3403 }
3404
3405 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3406 return;
3407
3408 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3409 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003410}
3411
3412void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003413 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003414 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3415 SourceLocation AttrLoc = AttrRange.getBegin();
3416
Richard Smith4cd81c52013-01-29 09:02:09 +00003417 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003418 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003419 // C++11 [dcl.align]p1:
3420 // An alignment-specifier may be applied to a variable or to a class
3421 // data member, but it shall not be applied to a bit-field, a function
3422 // parameter, the formal parameter of a catch clause, or a variable
3423 // declared with the register storage class specifier. An
3424 // alignment-specifier may also be applied to the declaration of a class
3425 // or enumeration type.
3426 // C11 6.7.5/2:
3427 // An alignment attribute shall not be specified in a declaration of
3428 // a typedef, or a bit-field, or a function, or a parameter, or an
3429 // object declared with the register storage-class specifier.
3430 int DiagKind = -1;
3431 if (isa<ParmVarDecl>(D)) {
3432 DiagKind = 0;
3433 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3434 if (VD->getStorageClass() == SC_Register)
3435 DiagKind = 1;
3436 if (VD->isExceptionVariable())
3437 DiagKind = 2;
3438 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3439 if (FD->isBitField())
3440 DiagKind = 3;
3441 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003442 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3443 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003444 << (TmpAttr.isC11() ? ExpectedVariableOrField
3445 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003446 return;
3447 }
3448 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003449 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003450 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003451 return;
3452 }
3453 }
3454
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003455 if (E->isTypeDependent() || E->isValueDependent()) {
3456 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003457 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3458 AA->setPackExpansion(IsPackExpansion);
3459 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003460 return;
3461 }
Michael Hana31f65b2013-02-01 01:19:17 +00003462
Sean Huntcf807c42010-08-18 23:23:40 +00003463 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003464 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003465 ExprResult ICE
3466 = VerifyIntegerConstantExpression(E, &Alignment,
3467 diag::err_aligned_attribute_argument_not_int,
3468 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003469 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003470 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003471
3472 // C++11 [dcl.align]p2:
3473 // -- if the constant expression evaluates to zero, the alignment
3474 // specifier shall have no effect
3475 // C11 6.7.5p6:
3476 // An alignment specification of zero has no effect.
3477 if (!(TmpAttr.isAlignas() && !Alignment) &&
3478 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003479 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3480 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003481 return;
3482 }
Michael Hana31f65b2013-02-01 01:19:17 +00003483
Richard Smithbe507b62013-02-01 08:12:08 +00003484 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003485 // We've already verified it's a power of 2, now let's make sure it's
3486 // 8192 or less.
3487 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003488 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003489 << E->getSourceRange();
3490 return;
3491 }
3492 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003493
Richard Smithf6565a92013-02-22 08:32:16 +00003494 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3495 ICE.take(), SpellingListIndex);
3496 AA->setPackExpansion(IsPackExpansion);
3497 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003498}
3499
Michael Hana31f65b2013-02-01 01:19:17 +00003500void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003501 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003502 // FIXME: Cache the number on the Attr object if non-dependent?
3503 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003504 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3505 SpellingListIndex);
3506 AA->setPackExpansion(IsPackExpansion);
3507 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003508}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003509
Richard Smithbe507b62013-02-01 08:12:08 +00003510void Sema::CheckAlignasUnderalignment(Decl *D) {
3511 assert(D->hasAttrs() && "no attributes on decl");
3512
3513 QualType Ty;
3514 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3515 Ty = VD->getType();
3516 else
3517 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003518 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003519 return;
3520
3521 // C++11 [dcl.align]p5, C11 6.7.5/4:
3522 // The combined effect of all alignment attributes in a declaration shall
3523 // not specify an alignment that is less strict than the alignment that
3524 // would otherwise be required for the entity being declared.
3525 AlignedAttr *AlignasAttr = 0;
3526 unsigned Align = 0;
3527 for (specific_attr_iterator<AlignedAttr>
3528 I = D->specific_attr_begin<AlignedAttr>(),
3529 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3530 if (I->isAlignmentDependent())
3531 return;
3532 if (I->isAlignas())
3533 AlignasAttr = *I;
3534 Align = std::max(Align, I->getAlignment(Context));
3535 }
3536
3537 if (AlignasAttr && Align) {
3538 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3539 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3540 if (NaturalAlign > RequestedAlign)
3541 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3542 << Ty << (unsigned)NaturalAlign.getQuantity();
3543 }
3544}
3545
Chandler Carruthd309c812011-07-01 23:49:16 +00003546/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003547/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003548///
Mike Stumpbf916502009-07-24 19:02:52 +00003549/// Despite what would be logical, the mode attribute is a decl attribute, not a
3550/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3551/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003552static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003553 // This attribute isn't documented, but glibc uses it. It changes
3554 // the width of an int or unsigned int to the specified size.
3555
3556 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003557 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003558 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003559
Chris Lattnerfbf13472008-06-27 22:18:37 +00003560
3561 IdentifierInfo *Name = Attr.getParameterName();
3562 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003563 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003564 return;
3565 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003566
Chris Lattner5f9e2722011-07-23 10:55:15 +00003567 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003568
3569 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003570 if (Str.startswith("__") && Str.endswith("__"))
3571 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003572
3573 unsigned DestWidth = 0;
3574 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003575 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003576 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003577 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003578 switch (Str[0]) {
3579 case 'Q': DestWidth = 8; break;
3580 case 'H': DestWidth = 16; break;
3581 case 'S': DestWidth = 32; break;
3582 case 'D': DestWidth = 64; break;
3583 case 'X': DestWidth = 96; break;
3584 case 'T': DestWidth = 128; break;
3585 }
3586 if (Str[1] == 'F') {
3587 IntegerMode = false;
3588 } else if (Str[1] == 'C') {
3589 IntegerMode = false;
3590 ComplexMode = true;
3591 } else if (Str[1] != 'I') {
3592 DestWidth = 0;
3593 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003594 break;
3595 case 4:
3596 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3597 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003598 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003599 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003600 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003601 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003602 break;
3603 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003604 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003605 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003606 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003607 case 11:
3608 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003609 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003610 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003611 }
3612
3613 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003614 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003615 OldTy = TD->getUnderlyingType();
3616 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3617 OldTy = VD->getType();
3618 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003619 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003620 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003621 return;
3622 }
Eli Friedman73397492009-03-03 06:41:03 +00003623
John McCall183700f2009-09-21 23:43:11 +00003624 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003625 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3626 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003627 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003628 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3629 } else if (ComplexMode) {
3630 if (!OldTy->isComplexType())
3631 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3632 } else {
3633 if (!OldTy->isFloatingType())
3634 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3635 }
3636
Mike Stump390b4cc2009-05-16 07:39:55 +00003637 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3638 // and friends, at least with glibc.
3639 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3640 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003641 // FIXME: Make sure floating-point mappings are accurate
3642 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003643 QualType NewTy;
3644 switch (DestWidth) {
3645 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003646 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003647 return;
3648 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003649 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003650 return;
3651 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003652 if (!IntegerMode) {
3653 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3654 return;
3655 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003656 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003657 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003658 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003659 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003660 break;
3661 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003662 if (!IntegerMode) {
3663 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3664 return;
3665 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003666 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003667 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003668 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003669 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003670 break;
3671 case 32:
3672 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003673 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003674 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003675 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003676 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003677 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003678 break;
3679 case 64:
3680 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003681 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003682 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003683 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003684 NewTy = S.Context.LongTy;
3685 else
3686 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003687 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003688 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003689 NewTy = S.Context.UnsignedLongTy;
3690 else
3691 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003692 break;
Eli Friedman73397492009-03-03 06:41:03 +00003693 case 96:
3694 NewTy = S.Context.LongDoubleTy;
3695 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003696 case 128:
3697 if (!IntegerMode) {
3698 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3699 return;
3700 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003701 if (OldTy->isSignedIntegerType())
3702 NewTy = S.Context.Int128Ty;
3703 else
3704 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003705 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003706 }
3707
Eli Friedman73397492009-03-03 06:41:03 +00003708 if (ComplexMode) {
3709 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003710 }
3711
3712 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003713 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003714 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003715 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003716 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003717 cast<ValueDecl>(D)->setType(NewTy);
3718}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003719
Chandler Carruth1b03c872011-07-02 00:01:44 +00003720static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003721 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003722 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003723 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003724
Nick Lewycky78d1a102012-07-24 01:40:49 +00003725 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3726 if (!VD->hasGlobalStorage())
3727 S.Diag(Attr.getLoc(),
3728 diag::warn_attribute_requires_functions_or_static_globals)
3729 << Attr.getName();
3730 } else if (!isFunctionOrMethod(D)) {
3731 S.Diag(Attr.getLoc(),
3732 diag::warn_attribute_requires_functions_or_static_globals)
3733 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003734 return;
3735 }
Mike Stumpbf916502009-07-24 19:02:52 +00003736
Michael Han51d8c522013-01-24 16:46:58 +00003737 D->addAttr(::new (S.Context)
3738 NoDebugAttr(Attr.getRange(), S.Context,
3739 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003740}
3741
Chandler Carruth1b03c872011-07-02 00:01:44 +00003742static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003743 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003744 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003745 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003746
Mike Stumpbf916502009-07-24 19:02:52 +00003747
Chandler Carruth87c44602011-07-01 23:49:12 +00003748 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003750 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003751 return;
3752 }
Mike Stumpbf916502009-07-24 19:02:52 +00003753
Michael Han51d8c522013-01-24 16:46:58 +00003754 D->addAttr(::new (S.Context)
3755 NoInlineAttr(Attr.getRange(), S.Context,
3756 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003757}
3758
Chandler Carruth1b03c872011-07-02 00:01:44 +00003759static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3760 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003761 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003762 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003763 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003764
Chris Lattner7255a2d2010-06-22 00:03:40 +00003765
Chandler Carruth87c44602011-07-01 23:49:12 +00003766 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003768 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003769 return;
3770 }
3771
Michael Han51d8c522013-01-24 16:46:58 +00003772 D->addAttr(::new (S.Context)
3773 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3774 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003775}
3776
Chandler Carruth1b03c872011-07-02 00:01:44 +00003777static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003778 if (S.LangOpts.CUDA) {
3779 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003780 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3782 return;
3783 }
3784
Chandler Carruth87c44602011-07-01 23:49:12 +00003785 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003786 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003787 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003788 return;
3789 }
3790
Michael Han51d8c522013-01-24 16:46:58 +00003791 D->addAttr(::new (S.Context)
3792 CUDAConstantAttr(Attr.getRange(), S.Context,
3793 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003794 } else {
3795 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3796 }
3797}
3798
Chandler Carruth1b03c872011-07-02 00:01:44 +00003799static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003800 if (S.LangOpts.CUDA) {
3801 // check the attribute arguments.
3802 if (Attr.getNumArgs() != 0) {
3803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3804 return;
3805 }
3806
Chandler Carruth87c44602011-07-01 23:49:12 +00003807 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003808 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003809 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003810 return;
3811 }
3812
Michael Han51d8c522013-01-24 16:46:58 +00003813 D->addAttr(::new (S.Context)
3814 CUDADeviceAttr(Attr.getRange(), S.Context,
3815 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003816 } else {
3817 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3818 }
3819}
3820
Chandler Carruth1b03c872011-07-02 00:01:44 +00003821static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003822 if (S.LangOpts.CUDA) {
3823 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003824 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003825 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003826
Chandler Carruth87c44602011-07-01 23:49:12 +00003827 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003829 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003830 return;
3831 }
3832
Chandler Carruth87c44602011-07-01 23:49:12 +00003833 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003834 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003835 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003836 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003837 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3838 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003839 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003840 "void");
3841 } else {
3842 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3843 << FD->getType();
3844 }
3845 return;
3846 }
3847
Michael Han51d8c522013-01-24 16:46:58 +00003848 D->addAttr(::new (S.Context)
3849 CUDAGlobalAttr(Attr.getRange(), S.Context,
3850 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003851 } else {
3852 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3853 }
3854}
3855
Chandler Carruth1b03c872011-07-02 00:01:44 +00003856static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003857 if (S.LangOpts.CUDA) {
3858 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003859 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003860 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003861
Peter Collingbourneced76712010-12-01 03:15:31 +00003862
Chandler Carruth87c44602011-07-01 23:49:12 +00003863 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003864 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003865 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003866 return;
3867 }
3868
Michael Han51d8c522013-01-24 16:46:58 +00003869 D->addAttr(::new (S.Context)
3870 CUDAHostAttr(Attr.getRange(), S.Context,
3871 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003872 } else {
3873 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3874 }
3875}
3876
Chandler Carruth1b03c872011-07-02 00:01:44 +00003877static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 if (S.LangOpts.CUDA) {
3879 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003880 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003881 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003882
Chandler Carruth87c44602011-07-01 23:49:12 +00003883 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003885 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003886 return;
3887 }
3888
Michael Han51d8c522013-01-24 16:46:58 +00003889 D->addAttr(::new (S.Context)
3890 CUDASharedAttr(Attr.getRange(), S.Context,
3891 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003892 } else {
3893 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3894 }
3895}
3896
Chandler Carruth1b03c872011-07-02 00:01:44 +00003897static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003898 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003899 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003900 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003901
Chandler Carruth87c44602011-07-01 23:49:12 +00003902 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003903 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003905 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003906 return;
3907 }
Mike Stumpbf916502009-07-24 19:02:52 +00003908
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003909 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003910 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003911 return;
3912 }
Mike Stumpbf916502009-07-24 19:02:52 +00003913
Michael Han51d8c522013-01-24 16:46:58 +00003914 D->addAttr(::new (S.Context)
3915 GNUInlineAttr(Attr.getRange(), S.Context,
3916 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003917}
3918
Chandler Carruth1b03c872011-07-02 00:01:44 +00003919static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003920 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003921
Aaron Ballmanfff32482012-12-09 17:45:41 +00003922 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003923 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003924 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3925 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003926 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003927 return;
3928
Chandler Carruth87c44602011-07-01 23:49:12 +00003929 if (!isa<ObjCMethodDecl>(D)) {
3930 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3931 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003932 return;
3933 }
3934
Chandler Carruth87c44602011-07-01 23:49:12 +00003935 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003936 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003937 D->addAttr(::new (S.Context)
3938 FastCallAttr(Attr.getRange(), S.Context,
3939 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003940 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003941 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003942 D->addAttr(::new (S.Context)
3943 StdCallAttr(Attr.getRange(), S.Context,
3944 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003945 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003946 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003947 D->addAttr(::new (S.Context)
3948 ThisCallAttr(Attr.getRange(), S.Context,
3949 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003950 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003951 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003952 D->addAttr(::new (S.Context)
3953 CDeclAttr(Attr.getRange(), S.Context,
3954 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003955 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003956 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003957 D->addAttr(::new (S.Context)
3958 PascalAttr(Attr.getRange(), S.Context,
3959 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003960 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003961 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003962 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003963 switch (CC) {
3964 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003965 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003966 break;
3967 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003968 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003969 break;
3970 default:
3971 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003972 }
3973
Michael Han51d8c522013-01-24 16:46:58 +00003974 D->addAttr(::new (S.Context)
3975 PcsAttr(Attr.getRange(), S.Context, PCS,
3976 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003977 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003978 }
Derek Schuff263366f2012-10-16 22:30:41 +00003979 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003980 D->addAttr(::new (S.Context)
3981 PnaclCallAttr(Attr.getRange(), S.Context,
3982 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003983 return;
Guy Benyei38980082012-12-25 08:53:55 +00003984 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003985 D->addAttr(::new (S.Context)
3986 IntelOclBiccAttr(Attr.getRange(), S.Context,
3987 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003988 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003989
Abramo Bagnarae215f722010-04-30 13:10:51 +00003990 default:
3991 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003992 }
3993}
3994
Chandler Carruth1b03c872011-07-02 00:01:44 +00003995static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003996 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003997 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003998}
3999
Guy Benyei1db70402013-03-24 13:58:12 +00004000static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4001 assert(!Attr.isInvalid());
4002
4003 Expr *E = Attr.getArg(0);
4004 llvm::APSInt ArgNum(32);
4005 if (E->isTypeDependent() || E->isValueDependent() ||
4006 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4007 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4008 << Attr.getName()->getName() << E->getSourceRange();
4009 return;
4010 }
4011
4012 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4013 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4014}
4015
Aaron Ballmanfff32482012-12-09 17:45:41 +00004016bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4017 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004018 if (attr.isInvalid())
4019 return true;
4020
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004021 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4022 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4023 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004024 attr.setInvalid();
4025 return true;
4026 }
4027
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004028 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4029 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004030 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004031 case AttributeList::AT_CDecl: CC = CC_C; break;
4032 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4033 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4034 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4035 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4036 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004037 Expr *Arg = attr.getArg(0);
4038 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004039 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004040 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4041 << "pcs" << 1;
4042 attr.setInvalid();
4043 return true;
4044 }
4045
Chris Lattner5f9e2722011-07-23 10:55:15 +00004046 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004047 if (StrRef == "aapcs") {
4048 CC = CC_AAPCS;
4049 break;
4050 } else if (StrRef == "aapcs-vfp") {
4051 CC = CC_AAPCS_VFP;
4052 break;
4053 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004054
4055 attr.setInvalid();
4056 Diag(attr.getLoc(), diag::err_invalid_pcs);
4057 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004058 }
Derek Schuff263366f2012-10-16 22:30:41 +00004059 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004060 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004061 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004062 }
4063
Aaron Ballman82bfa192012-10-02 14:26:08 +00004064 const TargetInfo &TI = Context.getTargetInfo();
4065 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4066 if (A == TargetInfo::CCCR_Warning) {
4067 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004068
4069 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4070 if (FD)
4071 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4072 TargetInfo::CCMT_NonMember;
4073 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004074 }
4075
John McCall711c52b2011-01-05 12:14:39 +00004076 return false;
4077}
4078
Chandler Carruth1b03c872011-07-02 00:01:44 +00004079static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004080 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004081
4082 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004083 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004084 return;
4085
Chandler Carruth87c44602011-07-01 23:49:12 +00004086 if (!isa<ObjCMethodDecl>(D)) {
4087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4088 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004089 return;
4090 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004091
Michael Han51d8c522013-01-24 16:46:58 +00004092 D->addAttr(::new (S.Context)
4093 RegparmAttr(Attr.getRange(), S.Context, numParams,
4094 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004095}
4096
4097/// Checks a regparm attribute, returning true if it is ill-formed and
4098/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004099bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4100 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004101 return true;
4102
Chandler Carruth87c44602011-07-01 23:49:12 +00004103 if (Attr.getNumArgs() != 1) {
4104 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4105 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004106 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004107 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004108
Chandler Carruth87c44602011-07-01 23:49:12 +00004109 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004110 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004111 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004112 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004113 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004114 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004115 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004116 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004117 }
4118
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004119 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004120 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004121 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004122 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004123 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004124 }
4125
John McCall711c52b2011-01-05 12:14:39 +00004126 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004127 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004128 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004129 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004130 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004131 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004132 }
4133
John McCall711c52b2011-01-05 12:14:39 +00004134 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004135}
4136
Chandler Carruth1b03c872011-07-02 00:01:44 +00004137static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004138 if (S.LangOpts.CUDA) {
4139 // check the attribute arguments.
4140 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004141 // FIXME: 0 is not okay.
4142 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004143 return;
4144 }
4145
Chandler Carruth87c44602011-07-01 23:49:12 +00004146 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004147 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004148 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004149 return;
4150 }
4151
4152 Expr *MaxThreadsExpr = Attr.getArg(0);
4153 llvm::APSInt MaxThreads(32);
4154 if (MaxThreadsExpr->isTypeDependent() ||
4155 MaxThreadsExpr->isValueDependent() ||
4156 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4157 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4158 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4159 return;
4160 }
4161
4162 llvm::APSInt MinBlocks(32);
4163 if (Attr.getNumArgs() > 1) {
4164 Expr *MinBlocksExpr = Attr.getArg(1);
4165 if (MinBlocksExpr->isTypeDependent() ||
4166 MinBlocksExpr->isValueDependent() ||
4167 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4169 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4170 return;
4171 }
4172 }
4173
Michael Han51d8c522013-01-24 16:46:58 +00004174 D->addAttr(::new (S.Context)
4175 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4176 MaxThreads.getZExtValue(),
4177 MinBlocks.getZExtValue(),
4178 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004179 } else {
4180 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4181 }
4182}
4183
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004184static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4185 const AttributeList &Attr) {
4186 StringRef AttrName = Attr.getName()->getName();
4187 if (!Attr.getParameterName()) {
4188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4189 << Attr.getName() << /* arg num = */ 1;
4190 return;
4191 }
4192
4193 if (Attr.getNumArgs() != 2) {
4194 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4195 << /* required args = */ 3;
4196 return;
4197 }
4198
4199 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4200
4201 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4203 << Attr.getName() << ExpectedFunctionOrMethod;
4204 return;
4205 }
4206
4207 uint64_t ArgumentIdx;
4208 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4209 Attr.getLoc(), 2,
4210 Attr.getArg(0), ArgumentIdx))
4211 return;
4212
4213 uint64_t TypeTagIdx;
4214 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4215 Attr.getLoc(), 3,
4216 Attr.getArg(1), TypeTagIdx))
4217 return;
4218
4219 bool IsPointer = (AttrName == "pointer_with_type_tag");
4220 if (IsPointer) {
4221 // Ensure that buffer has a pointer type.
4222 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4223 if (!BufferTy->isPointerType()) {
4224 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
4225 << AttrName;
4226 }
4227 }
4228
Michael Han51d8c522013-01-24 16:46:58 +00004229 D->addAttr(::new (S.Context)
4230 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4231 ArgumentIdx, TypeTagIdx, IsPointer,
4232 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004233}
4234
4235static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4236 const AttributeList &Attr) {
4237 IdentifierInfo *PointerKind = Attr.getParameterName();
4238 if (!PointerKind) {
4239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4240 << "type_tag_for_datatype" << 1;
4241 return;
4242 }
4243
4244 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4245
Michael Han51d8c522013-01-24 16:46:58 +00004246 D->addAttr(::new (S.Context)
4247 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4248 MatchingCType,
4249 Attr.getLayoutCompatible(),
4250 Attr.getMustBeNull(),
4251 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004252}
4253
Chris Lattner0744e5f2008-06-29 00:23:49 +00004254//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004255// Checker-specific attribute handlers.
4256//===----------------------------------------------------------------------===//
4257
John McCallc7ad3812011-01-25 03:31:58 +00004258static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004259 return type->isDependentType() ||
4260 type->isObjCObjectPointerType() ||
4261 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004262}
4263static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004264 return type->isDependentType() ||
4265 type->isPointerType() ||
4266 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004267}
4268
Chandler Carruth1b03c872011-07-02 00:01:44 +00004269static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004270 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004271 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004272 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004273 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004274 return;
4275 }
4276
4277 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004278 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004279 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4280 cf = false;
4281 } else {
4282 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4283 cf = true;
4284 }
4285
4286 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004287 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004288 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004289 return;
4290 }
4291
4292 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004293 param->addAttr(::new (S.Context)
4294 CFConsumedAttr(Attr.getRange(), S.Context,
4295 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004296 else
Michael Han51d8c522013-01-24 16:46:58 +00004297 param->addAttr(::new (S.Context)
4298 NSConsumedAttr(Attr.getRange(), S.Context,
4299 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004300}
4301
Chandler Carruth1b03c872011-07-02 00:01:44 +00004302static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4303 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004304 if (!isa<ObjCMethodDecl>(D)) {
4305 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004306 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004307 return;
4308 }
4309
Michael Han51d8c522013-01-24 16:46:58 +00004310 D->addAttr(::new (S.Context)
4311 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4312 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004313}
4314
Chandler Carruth1b03c872011-07-02 00:01:44 +00004315static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4316 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004317
John McCallc7ad3812011-01-25 03:31:58 +00004318 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004319
Chandler Carruth87c44602011-07-01 23:49:12 +00004320 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004321 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004322 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004323 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004324 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004325 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4326 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004327 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004328 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004329 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004330 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004331 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004332 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004333 return;
4334 }
Mike Stumpbf916502009-07-24 19:02:52 +00004335
John McCallc7ad3812011-01-25 03:31:58 +00004336 bool typeOK;
4337 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004338 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004339 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004340 case AttributeList::AT_NSReturnsAutoreleased:
4341 case AttributeList::AT_NSReturnsRetained:
4342 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004343 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4344 cf = false;
4345 break;
4346
Sean Hunt8e083e72012-06-19 23:57:03 +00004347 case AttributeList::AT_CFReturnsRetained:
4348 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004349 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4350 cf = true;
4351 break;
4352 }
4353
4354 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004355 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004356 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004357 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004358 }
Mike Stumpbf916502009-07-24 19:02:52 +00004359
Chandler Carruth87c44602011-07-01 23:49:12 +00004360 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004361 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004362 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004363 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004364 D->addAttr(::new (S.Context)
4365 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4366 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004367 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004368 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004369 D->addAttr(::new (S.Context)
4370 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4371 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004372 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004374 D->addAttr(::new (S.Context)
4375 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4376 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004377 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004378 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004379 D->addAttr(::new (S.Context)
4380 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4381 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004382 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004383 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004384 D->addAttr(::new (S.Context)
4385 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4386 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004387 return;
4388 };
4389}
4390
John McCalldc7c5ad2011-07-22 08:53:00 +00004391static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4392 const AttributeList &attr) {
4393 SourceLocation loc = attr.getLoc();
4394
4395 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4396
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004397 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004398 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004399 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004400 return;
4401 }
4402
4403 // Check that the method returns a normal pointer.
4404 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004405
4406 if (!resultType->isReferenceType() &&
4407 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004408 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4409 << SourceRange(loc)
4410 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4411
4412 // Drop the attribute.
4413 return;
4414 }
4415
Michael Han51d8c522013-01-24 16:46:58 +00004416 method->addAttr(::new (S.Context)
4417 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4418 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004419}
4420
Fariborz Jahanian84101132012-09-07 23:46:23 +00004421static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4422 const AttributeList &attr) {
4423 SourceLocation loc = attr.getLoc();
4424 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4425
4426 if (!method) {
4427 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4428 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4429 return;
4430 }
4431 DeclContext *DC = method->getDeclContext();
4432 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4433 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4434 << attr.getName() << 0;
4435 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4436 return;
4437 }
4438 if (method->getMethodFamily() == OMF_dealloc) {
4439 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4440 << attr.getName() << 1;
4441 return;
4442 }
4443
Michael Han51d8c522013-01-24 16:46:58 +00004444 method->addAttr(::new (S.Context)
4445 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4446 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004447}
4448
John McCall8dfac0b2011-09-30 05:12:12 +00004449/// Handle cf_audited_transfer and cf_unknown_transfer.
4450static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4451 if (!isa<FunctionDecl>(D)) {
4452 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004453 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004454 return;
4455 }
4456
Sean Hunt8e083e72012-06-19 23:57:03 +00004457 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004458
4459 // Check whether there's a conflicting attribute already present.
4460 Attr *Existing;
4461 if (IsAudited) {
4462 Existing = D->getAttr<CFUnknownTransferAttr>();
4463 } else {
4464 Existing = D->getAttr<CFAuditedTransferAttr>();
4465 }
4466 if (Existing) {
4467 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4468 << A.getName()
4469 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4470 << A.getRange() << Existing->getRange();
4471 return;
4472 }
4473
4474 // All clear; add the attribute.
4475 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004476 D->addAttr(::new (S.Context)
4477 CFAuditedTransferAttr(A.getRange(), S.Context,
4478 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004479 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004480 D->addAttr(::new (S.Context)
4481 CFUnknownTransferAttr(A.getRange(), S.Context,
4482 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004483 }
4484}
4485
John McCallfe98da02011-09-29 07:17:38 +00004486static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4487 const AttributeList &Attr) {
4488 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4489 if (!RD || RD->isUnion()) {
4490 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004491 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004492 }
4493
4494 IdentifierInfo *ParmName = Attr.getParameterName();
4495
4496 // In Objective-C, verify that the type names an Objective-C type.
4497 // We don't want to check this outside of ObjC because people sometimes
4498 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004499 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004500 // Check for an existing type with this name.
4501 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4502 Sema::LookupOrdinaryName);
4503 if (S.LookupName(R, Sc)) {
4504 NamedDecl *Target = R.getFoundDecl();
4505 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4506 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4507 S.Diag(Target->getLocStart(), diag::note_declared_at);
4508 }
4509 }
4510 }
4511
Michael Han51d8c522013-01-24 16:46:58 +00004512 D->addAttr(::new (S.Context)
4513 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4514 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004515}
4516
Chandler Carruth1b03c872011-07-02 00:01:44 +00004517static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4518 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004519 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004520
Chandler Carruth87c44602011-07-01 23:49:12 +00004521 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004522 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004523}
4524
Chandler Carruth1b03c872011-07-02 00:01:44 +00004525static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4526 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004527 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004528 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004529 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004530 return;
4531 }
4532
Chandler Carruth87c44602011-07-01 23:49:12 +00004533 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004534 QualType type = vd->getType();
4535
4536 if (!type->isDependentType() &&
4537 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004538 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004539 << type;
4540 return;
4541 }
4542
4543 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4544
4545 // If we have no lifetime yet, check the lifetime we're presumably
4546 // going to infer.
4547 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4548 lifetime = type->getObjCARCImplicitLifetime();
4549
4550 switch (lifetime) {
4551 case Qualifiers::OCL_None:
4552 assert(type->isDependentType() &&
4553 "didn't infer lifetime for non-dependent type?");
4554 break;
4555
4556 case Qualifiers::OCL_Weak: // meaningful
4557 case Qualifiers::OCL_Strong: // meaningful
4558 break;
4559
4560 case Qualifiers::OCL_ExplicitNone:
4561 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004562 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004563 << (lifetime == Qualifiers::OCL_Autoreleasing);
4564 break;
4565 }
4566
Chandler Carruth87c44602011-07-01 23:49:12 +00004567 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004568 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4569 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004570}
4571
Francois Pichet11542142010-12-19 06:50:37 +00004572//===----------------------------------------------------------------------===//
4573// Microsoft specific attribute handlers.
4574//===----------------------------------------------------------------------===//
4575
Chandler Carruth1b03c872011-07-02 00:01:44 +00004576static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004577 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004578 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004579 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004580 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004581
Francois Pichet11542142010-12-19 06:50:37 +00004582 Expr *Arg = Attr.getArg(0);
4583 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004584 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4586 << "uuid" << 1;
4587 return;
4588 }
4589
Chris Lattner5f9e2722011-07-23 10:55:15 +00004590 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004591
4592 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4593 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004594
Francois Pichetd3d3be92010-12-20 01:41:49 +00004595 // Validate GUID length.
4596 if (IsCurly && StrRef.size() != 38) {
4597 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4598 return;
4599 }
4600 if (!IsCurly && StrRef.size() != 36) {
4601 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4602 return;
4603 }
4604
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004605 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004606 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004607 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004608 if (IsCurly) // Skip the optional '{'
4609 ++I;
4610
4611 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004612 if (i == 8 || i == 13 || i == 18 || i == 23) {
4613 if (*I != '-') {
4614 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4615 return;
4616 }
Jordan Rose3f6f51e2013-02-08 22:30:41 +00004617 } else if (!isHexDigit(*I)) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004618 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4619 return;
4620 }
4621 I++;
4622 }
Francois Pichet11542142010-12-19 06:50:37 +00004623
Michael Han51d8c522013-01-24 16:46:58 +00004624 D->addAttr(::new (S.Context)
4625 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4626 Attr.getAttributeSpellingListIndex()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004627 } else
Francois Pichet11542142010-12-19 06:50:37 +00004628 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004629}
4630
John McCallc052dbb2012-05-22 21:28:12 +00004631static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weber7b89ab72012-11-07 21:31:36 +00004632 if (!S.LangOpts.MicrosoftExt) {
John McCallc052dbb2012-05-22 21:28:12 +00004633 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weber7b89ab72012-11-07 21:31:36 +00004634 return;
4635 }
4636
4637 AttributeList::Kind Kind = Attr.getKind();
4638 if (Kind == AttributeList::AT_SingleInheritance)
4639 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004640 ::new (S.Context)
4641 SingleInheritanceAttr(Attr.getRange(), S.Context,
4642 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004643 else if (Kind == AttributeList::AT_MultipleInheritance)
4644 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004645 ::new (S.Context)
4646 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4647 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004648 else if (Kind == AttributeList::AT_VirtualInheritance)
4649 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004650 ::new (S.Context)
4651 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4652 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004653}
4654
4655static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4656 if (S.LangOpts.MicrosoftExt) {
4657 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004658 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004659 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004660 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4661 Attr.getAttributeSpellingListIndex()));
Sean Hunt8e083e72012-06-19 23:57:03 +00004662 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004663 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004664 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4665 Attr.getAttributeSpellingListIndex()));
Sean Hunt8e083e72012-06-19 23:57:03 +00004666 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004667 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004668 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4669 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004670 } else
4671 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4672}
4673
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004674static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4675 if (S.LangOpts.MicrosoftExt)
Michael Han51d8c522013-01-24 16:46:58 +00004676 D->addAttr(::new (S.Context)
4677 ForceInlineAttr(Attr.getRange(), S.Context,
4678 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004679 else
4680 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4681}
4682
Ted Kremenekb71368d2009-05-09 02:44:38 +00004683//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004684// Top Level Sema Entry Points
4685//===----------------------------------------------------------------------===//
4686
Chandler Carruth1b03c872011-07-02 00:01:44 +00004687static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4688 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004689 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004690 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4691 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4692 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004693 default:
4694 break;
4695 }
4696}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004697
Chandler Carruth1b03c872011-07-02 00:01:44 +00004698static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4699 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004700 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004701 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4702 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4703 case AttributeList::AT_IBOutletCollection:
4704 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004705 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004706 case AttributeList::AT_ObjCGC:
4707 case AttributeList::AT_VectorSize:
4708 case AttributeList::AT_NeonVectorType:
4709 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004710 // Ignore these, these are type attributes, handled by
4711 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004712 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004713 case AttributeList::AT_CUDADevice:
4714 case AttributeList::AT_CUDAHost:
4715 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004716 // Ignore, this is a non-inheritable attribute, handled
4717 // by ProcessNonInheritableDeclAttr.
4718 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4720 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4721 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4722 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004723 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004724 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004725 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004726 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004727 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4728 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4729 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004730 handleDependencyAttr(S, scope, D, Attr);
4731 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004732 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4733 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4734 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004735 case AttributeList::AT_CXX11NoReturn:
4736 handleCXX11NoReturnAttr(S, D, Attr);
4737 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004738 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004739 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4740 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004741 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4742 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004743 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004744 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004745 case AttributeList::AT_MinSize:
4746 handleMinSizeAttr(S, D, Attr);
4747 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004748 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4749 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4750 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4751 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4752 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004753 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004754 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004755 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4756 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4757 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4758 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4759 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004760 case AttributeList::AT_ownership_returns:
4761 case AttributeList::AT_ownership_takes:
4762 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004763 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004764 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4765 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4766 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4767 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4768 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4769 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4770 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004771
Sean Hunt8e083e72012-06-19 23:57:03 +00004772 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004773 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004775 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004776
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004778 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4779
Fariborz Jahanian84101132012-09-07 23:46:23 +00004780 case AttributeList::AT_ObjCRequiresSuper:
4781 handleObjCRequiresSuperAttr(S, D, Attr); break;
4782
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004784 handleNSBridgedAttr(S, scope, D, Attr); break;
4785
Sean Hunt8e083e72012-06-19 23:57:03 +00004786 case AttributeList::AT_CFAuditedTransfer:
4787 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004788 handleCFTransferAttr(S, D, Attr); break;
4789
Ted Kremenekb71368d2009-05-09 02:44:38 +00004790 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_CFConsumed:
4792 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4793 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004794 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004795
Sean Hunt8e083e72012-06-19 23:57:03 +00004796 case AttributeList::AT_NSReturnsAutoreleased:
4797 case AttributeList::AT_NSReturnsNotRetained:
4798 case AttributeList::AT_CFReturnsNotRetained:
4799 case AttributeList::AT_NSReturnsRetained:
4800 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004801 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004802
Tanya Lattner0df579e2012-07-09 22:06:01 +00004803 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004804 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004805 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004806
Joey Gouly37453b92013-03-08 09:42:32 +00004807 case AttributeList::AT_VecTypeHint:
4808 handleVecTypeHint(S, D, Attr); break;
4809
Joey Gouly96cead52013-03-14 09:54:43 +00004810 case AttributeList::AT_Endian:
4811 handleEndianAttr(S, D, Attr);
4812 break;
4813
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004815 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004816
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4818 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4819 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004820 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4821 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004822 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004823 handleArcWeakrefUnavailableAttr (S, D, Attr);
4824 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004825 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004826 handleObjCRootClassAttr(S, D, Attr);
4827 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004828 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004829 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004830 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004831 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4832 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004833 handleReturnsTwiceAttr(S, D, Attr);
4834 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004835 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004836 case AttributeList::AT_Visibility:
4837 handleVisibilityAttr(S, D, Attr, false);
4838 break;
4839 case AttributeList::AT_TypeVisibility:
4840 handleVisibilityAttr(S, D, Attr, true);
4841 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004843 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4845 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4846 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4847 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004848 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004849 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004851 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004852 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004853 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004854 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004855 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4857 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4858 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4859 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4860 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4861 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4862 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4863 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4864 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004865 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004866 // Just ignore
4867 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004869 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004870 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004871 case AttributeList::AT_StdCall:
4872 case AttributeList::AT_CDecl:
4873 case AttributeList::AT_FastCall:
4874 case AttributeList::AT_ThisCall:
4875 case AttributeList::AT_Pascal:
4876 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004877 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004878 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004879 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004880 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004881 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004882 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004883 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004884 case AttributeList::AT_OpenCLImageAccess:
4885 handleOpenCLImageAccessAttr(S, D, Attr);
4886 break;
John McCallc052dbb2012-05-22 21:28:12 +00004887
4888 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004889 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004890 handleMsStructAttr(S, D, Attr);
4891 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004892 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004893 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004894 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004895 case AttributeList::AT_SingleInheritance:
4896 case AttributeList::AT_MultipleInheritance:
4897 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004898 handleInheritanceAttr(S, D, Attr);
4899 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004900 case AttributeList::AT_Win64:
4901 case AttributeList::AT_Ptr32:
4902 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004903 handlePortabilityAttr(S, D, Attr);
4904 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004905 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004906 handleForceInlineAttr(S, D, Attr);
4907 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004908
4909 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004910 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004911 handleGuardedVarAttr(S, D, Attr);
4912 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004913 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004914 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004915 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004916 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004917 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004918 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004919 case AttributeList::AT_NoSanitizeAddress:
4920 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004921 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004922 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004923 handleNoThreadSafetyAnalysis(S, D, Attr);
4924 break;
4925 case AttributeList::AT_NoSanitizeThread:
4926 handleNoSanitizeThread(S, D, Attr);
4927 break;
4928 case AttributeList::AT_NoSanitizeMemory:
4929 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004930 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004931 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004932 handleLockableAttr(S, D, Attr);
4933 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004934 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004935 handleGuardedByAttr(S, D, Attr);
4936 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004937 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004938 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004939 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004940 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004941 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004942 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004943 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004944 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004945 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004946 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004947 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004948 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004949 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004950 handleLockReturnedAttr(S, D, Attr);
4951 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004952 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004953 handleLocksExcludedAttr(S, D, Attr);
4954 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004955 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004956 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004957 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004958 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004959 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004960 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004961 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004962 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004963 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004964 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004965 handleUnlockFunAttr(S, D, Attr);
4966 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004967 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004968 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004969 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004970 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004971 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004972 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004973
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004974 // Type safety attributes.
4975 case AttributeList::AT_ArgumentWithTypeTag:
4976 handleArgumentWithTypeTagAttr(S, D, Attr);
4977 break;
4978 case AttributeList::AT_TypeTagForDatatype:
4979 handleTypeTagForDatatypeAttr(S, D, Attr);
4980 break;
4981
Chris Lattner803d0802008-06-29 00:43:07 +00004982 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004983 // Ask target about the attribute.
4984 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4985 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004986 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4987 diag::warn_unhandled_ms_attribute_ignored :
4988 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004989 break;
4990 }
4991}
4992
Peter Collingbourne60700392011-01-21 02:08:45 +00004993/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4994/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00004995/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004996static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4997 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00004998 bool NonInheritable, bool Inheritable,
4999 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005000 if (Attr.isInvalid())
5001 return;
5002
Richard Smithcd8ab512013-01-17 01:30:42 +00005003 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5004 // instead.
5005 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5006 return;
5007
Peter Collingbourne60700392011-01-21 02:08:45 +00005008 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005009 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005010
5011 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005012 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005013}
5014
Chris Lattner803d0802008-06-29 00:43:07 +00005015/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5016/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005017void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005018 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005019 bool NonInheritable, bool Inheritable,
5020 bool IncludeCXX11Attributes) {
5021 for (const AttributeList* l = AttrList; l; l = l->getNext())
5022 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5023 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005024
5025 // GCC accepts
5026 // static int a9 __attribute__((weakref));
5027 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005028 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005029 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005030 cast<NamedDecl>(D)->getNameAsString();
5031 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005032 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005033 }
5034}
5035
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005036// Annotation attributes are the only attributes allowed after an access
5037// specifier.
5038bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5039 const AttributeList *AttrList) {
5040 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005041 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005042 handleAnnotateAttr(*this, ASDecl, *l);
5043 } else {
5044 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5045 return true;
5046 }
5047 }
5048
5049 return false;
5050}
5051
John McCalle82247a2011-10-01 05:17:03 +00005052/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5053/// contains any decl attributes that we should warn about.
5054static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5055 for ( ; A; A = A->getNext()) {
5056 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005057 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005058 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5059
5060 if (A->getKind() == AttributeList::UnknownAttribute) {
5061 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5062 << A->getName() << A->getRange();
5063 } else {
5064 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5065 << A->getName() << A->getRange();
5066 }
5067 }
5068}
5069
5070/// checkUnusedDeclAttributes - Given a declarator which is not being
5071/// used to build a declaration, complain about any decl attributes
5072/// which might be lying around on it.
5073void Sema::checkUnusedDeclAttributes(Declarator &D) {
5074 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5075 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5076 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5077 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5078}
5079
Ryan Flynne25ff832009-07-30 03:15:39 +00005080/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005081/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005082NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5083 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005084 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005085 NamedDecl *NewD = 0;
5086 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005087 FunctionDecl *NewFD;
5088 // FIXME: Missing call to CheckFunctionDeclaration().
5089 // FIXME: Mangling?
5090 // FIXME: Is the qualifier info correct?
5091 // FIXME: Is the DeclContext correct?
5092 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5093 Loc, Loc, DeclarationName(II),
5094 FD->getType(), FD->getTypeSourceInfo(),
5095 SC_None, SC_None,
5096 false/*isInlineSpecified*/,
5097 FD->hasPrototype(),
5098 false/*isConstexprSpecified*/);
5099 NewD = NewFD;
5100
5101 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005102 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005103
5104 // Fake up parameter variables; they are declared as if this were
5105 // a typedef.
5106 QualType FDTy = FD->getType();
5107 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5108 SmallVector<ParmVarDecl*, 16> Params;
5109 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5110 AE = FT->arg_type_end(); AI != AE; ++AI) {
5111 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5112 Param->setScopeInfo(0, Params.size());
5113 Params.push_back(Param);
5114 }
David Blaikie4278c652011-09-21 18:16:56 +00005115 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005116 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005117 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5118 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005119 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005120 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00005121 VD->getStorageClass(),
5122 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00005123 if (VD->getQualifier()) {
5124 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005125 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005126 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005127 }
5128 return NewD;
5129}
5130
James Dennett1dfbd922012-06-14 21:40:34 +00005131/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005132/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005133void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005134 if (W.getUsed()) return; // only do this once
5135 W.setUsed(true);
5136 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5137 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005138 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005139 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5140 NDId->getName()));
5141 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005142 WeakTopLevelDecl.push_back(NewD);
5143 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5144 // to insert Decl at TU scope, sorry.
5145 DeclContext *SavedContext = CurContext;
5146 CurContext = Context.getTranslationUnitDecl();
5147 PushOnScopeChains(NewD, S);
5148 CurContext = SavedContext;
5149 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005150 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005151 }
5152}
5153
Rafael Espindola65611bf2013-03-02 21:41:48 +00005154void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5155 // It's valid to "forward-declare" #pragma weak, in which case we
5156 // have to do this.
5157 LoadExternalWeakUndeclaredIdentifiers();
5158 if (!WeakUndeclaredIdentifiers.empty()) {
5159 NamedDecl *ND = NULL;
5160 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5161 if (VD->isExternC())
5162 ND = VD;
5163 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5164 if (FD->isExternC())
5165 ND = FD;
5166 if (ND) {
5167 if (IdentifierInfo *Id = ND->getIdentifier()) {
5168 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5169 = WeakUndeclaredIdentifiers.find(Id);
5170 if (I != WeakUndeclaredIdentifiers.end()) {
5171 WeakInfo W = I->second;
5172 DeclApplyPragmaWeak(S, ND, W);
5173 WeakUndeclaredIdentifiers[Id] = W;
5174 }
5175 }
5176 }
5177 }
5178}
5179
Chris Lattner0744e5f2008-06-29 00:23:49 +00005180/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5181/// it, apply them to D. This is a bit tricky because PD can have attributes
5182/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005183void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5184 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005185 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005186 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005187 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005188
Chris Lattner0744e5f2008-06-29 00:23:49 +00005189 // Walk the declarator structure, applying decl attributes that were in a type
5190 // position to the decl itself. This handles cases like:
5191 // int *__attr__(x)** D;
5192 // when X is a decl attribute.
5193 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5194 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005195 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5196 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005197
Chris Lattner0744e5f2008-06-29 00:23:49 +00005198 // Finally, apply any attributes on the decl itself.
5199 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005200 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005201}
John McCall54abf7d2009-11-04 02:18:39 +00005202
John McCallf85e1932011-06-15 23:02:42 +00005203/// Is the given declaration allowed to use a forbidden type?
5204static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5205 // Private ivars are always okay. Unfortunately, people don't
5206 // always properly make their ivars private, even in system headers.
5207 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005208 // Function declarations in sys headers will be marked unavailable.
5209 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5210 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005211 return false;
5212
5213 // Require it to be declared in a system header.
5214 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5215}
5216
5217/// Handle a delayed forbidden-type diagnostic.
5218static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5219 Decl *decl) {
5220 if (decl && isForbiddenTypeAllowed(S, decl)) {
5221 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5222 "this system declaration uses an unsupported type"));
5223 return;
5224 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005225 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005226 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005227 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005228 // kind of forbidden type messages on unavailable functions.
5229 if (FD->hasAttr<UnavailableAttr>() &&
5230 diag.getForbiddenTypeDiagnostic() ==
5231 diag::err_arc_array_param_no_ownership) {
5232 diag.Triggered = true;
5233 return;
5234 }
5235 }
John McCallf85e1932011-06-15 23:02:42 +00005236
5237 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5238 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5239 diag.Triggered = true;
5240}
5241
John McCall92576642012-05-07 06:16:41 +00005242void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5243 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005244 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005245 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005246
John McCall92576642012-05-07 06:16:41 +00005247 // When delaying diagnostics to run in the context of a parsed
5248 // declaration, we only want to actually emit anything if parsing
5249 // succeeds.
5250 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005251
John McCall92576642012-05-07 06:16:41 +00005252 // We emit all the active diagnostics in this pool or any of its
5253 // parents. In general, we'll get one pool for the decl spec
5254 // and a child pool for each declarator; in a decl group like:
5255 // deprecated_typedef foo, *bar, baz();
5256 // only the declarator pops will be passed decls. This is correct;
5257 // we really do need to consider delayed diagnostics from the decl spec
5258 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005259 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005260 do {
John McCall13489672012-05-07 06:16:58 +00005261 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005262 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5263 // This const_cast is a bit lame. Really, Triggered should be mutable.
5264 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005265 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005266 continue;
5267
John McCalleee1d542011-02-14 07:13:47 +00005268 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005269 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005270 // Don't bother giving deprecation diagnostics if the decl is invalid.
5271 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005272 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005273 break;
5274
5275 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005276 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005277 break;
John McCallf85e1932011-06-15 23:02:42 +00005278
5279 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005280 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005281 break;
John McCall2f514482010-01-27 03:50:35 +00005282 }
5283 }
John McCall92576642012-05-07 06:16:41 +00005284 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005285}
5286
John McCall13489672012-05-07 06:16:58 +00005287/// Given a set of delayed diagnostics, re-emit them as if they had
5288/// been delayed in the current context instead of in the given pool.
5289/// Essentially, this just moves them to the current pool.
5290void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5291 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5292 assert(curPool && "re-emitting in undelayed context not supported");
5293 curPool->steal(pool);
5294}
5295
John McCall54abf7d2009-11-04 02:18:39 +00005296static bool isDeclDeprecated(Decl *D) {
5297 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005298 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005299 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005300 // A category implicitly has the availability of the interface.
5301 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5302 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005303 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5304 return false;
5305}
5306
Eli Friedmanc3b23082012-08-08 21:52:41 +00005307static void
5308DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5309 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005310 const ObjCInterfaceDecl *UnknownObjCClass,
5311 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005312 DeclarationName Name = D->getDeclName();
5313 if (!Message.empty()) {
5314 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5315 S.Diag(D->getLocation(),
5316 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5317 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005318 if (ObjCPropery)
5319 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5320 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005321 } else if (!UnknownObjCClass) {
5322 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5323 S.Diag(D->getLocation(),
5324 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5325 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005326 if (ObjCPropery)
5327 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5328 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005329 } else {
5330 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5331 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5332 }
5333}
5334
John McCall9c3087b2010-08-26 02:13:20 +00005335void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005336 Decl *Ctx) {
5337 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005338 return;
5339
John McCall2f514482010-01-27 03:50:35 +00005340 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005341 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5342 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005343 DD.getUnknownObjCClass(),
5344 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005345}
5346
Chris Lattner5f9e2722011-07-23 10:55:15 +00005347void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005348 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005349 const ObjCInterfaceDecl *UnknownObjCClass,
5350 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005351 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005352 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005353 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5354 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005355 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005356 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005357 return;
5358 }
5359
5360 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005361 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005362 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005363 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005364}