blob: 684148879b262508dd83f9f131578f318c9aa111 [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))
Richard Smith38afbc72013-04-13 02:43:54 +0000287 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000288
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);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000712 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000713 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
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000791static void handleAssertSharedLockAttr(Sema &S, Decl *D,
792 const AttributeList &Attr) {
793 SmallVector<Expr*, 1> Args;
794 if (!checkLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
799 D->addAttr(::new (S.Context)
800 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
801 Attr.getAttributeSpellingListIndex()));
802}
803
804static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
805 const AttributeList &Attr) {
806 SmallVector<Expr*, 1> Args;
807 if (!checkLockFunAttrCommon(S, D, Attr, Args))
808 return;
809
810 unsigned Size = Args.size();
811 Expr **StartArg = Size == 0 ? 0 : &Args[0];
812 D->addAttr(::new (S.Context)
813 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
814 StartArg, Size,
815 Attr.getAttributeSpellingListIndex()));
816}
817
818
Michael Hanf1aae3b2012-08-03 17:40:43 +0000819static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
820 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000821 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000822 assert(!Attr.isInvalid());
823
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000824 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000825 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000826
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000827 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000828 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
829 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000830 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000831 }
832
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000833 if (!isIntOrBool(Attr.getArg(0))) {
834 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000835 << Attr.getName();
836 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000837 }
838
839 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000840 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000841
Michael Handc691572012-07-23 18:48:41 +0000842 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000843}
844
Michael Hanf1aae3b2012-08-03 17:40:43 +0000845static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000846 const AttributeList &Attr) {
847 SmallVector<Expr*, 2> Args;
848 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
849 return;
850
851 unsigned Size = Args.size();
852 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000853 D->addAttr(::new (S.Context)
854 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
855 Attr.getArg(0), StartArg, Size,
856 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000857}
858
Michael Hanf1aae3b2012-08-03 17:40:43 +0000859static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000860 const AttributeList &Attr) {
861 SmallVector<Expr*, 2> Args;
862 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
863 return;
864
865 unsigned Size = Args.size();
866 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000867 D->addAttr(::new (S.Context)
868 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
869 Attr.getArg(0), StartArg, Size,
870 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000871}
872
Michael Hanf1aae3b2012-08-03 17:40:43 +0000873static bool checkLocksRequiredCommon(Sema &S, Decl *D,
874 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000875 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000876 assert(!Attr.isInvalid());
877
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000878 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000879 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000880
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000881 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000882 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
883 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000884 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000885 }
886
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000887 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000888 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000889 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000890 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000891
Michael Handc691572012-07-23 18:48:41 +0000892 return true;
893}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000894
Michael Hanf1aae3b2012-08-03 17:40:43 +0000895static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000896 const AttributeList &Attr) {
897 SmallVector<Expr*, 1> Args;
898 if (!checkLocksRequiredCommon(S, D, Attr, Args))
899 return;
900
901 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000902 D->addAttr(::new (S.Context)
903 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
904 StartArg, Args.size(),
905 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000906}
907
Michael Hanf1aae3b2012-08-03 17:40:43 +0000908static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000909 const AttributeList &Attr) {
910 SmallVector<Expr*, 1> Args;
911 if (!checkLocksRequiredCommon(S, D, Attr, Args))
912 return;
913
914 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000915 D->addAttr(::new (S.Context)
916 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
917 StartArg, Args.size(),
918 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000919}
920
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000921static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000922 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000923 assert(!Attr.isInvalid());
924
925 // zero or more arguments ok
926
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000927 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000928 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
929 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000930 return;
931 }
932
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000933 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000934 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000935 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000936 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000937 Expr **StartArg = Size == 0 ? 0 : &Args[0];
938
Michael Han51d8c522013-01-24 16:46:58 +0000939 D->addAttr(::new (S.Context)
940 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
941 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000942}
943
944static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000945 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000946 assert(!Attr.isInvalid());
947
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000948 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000949 return;
950
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000951 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000952 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
953 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000954 return;
955 }
956
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000957 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000958 SmallVector<Expr*, 1> Args;
959 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
960 unsigned Size = Args.size();
961 if (Size == 0)
962 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000963
Michael Han51d8c522013-01-24 16:46:58 +0000964 D->addAttr(::new (S.Context)
965 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
966 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000967}
968
969static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000970 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000971 assert(!Attr.isInvalid());
972
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000973 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000974 return;
975
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000976 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000977 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
978 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000979 return;
980 }
981
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000982 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000983 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000984 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000985 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000986 if (Size == 0)
987 return;
988 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000989
Michael Han51d8c522013-01-24 16:46:58 +0000990 D->addAttr(::new (S.Context)
991 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
992 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000993}
994
995
Chandler Carruth1b03c872011-07-02 00:01:44 +0000996static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
997 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +0000998 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
999 if (TD == 0) {
1000 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1001 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001002 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001003 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001004 }
Mike Stumpbf916502009-07-24 19:02:52 +00001005
Richard Smitha4fa9002013-01-13 02:11:23 +00001006 // Remember this typedef decl, we will need it later for diagnostics.
1007 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001008}
1009
Chandler Carruth1b03c872011-07-02 00:01:44 +00001010static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001011 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001012 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001013 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001014
Chandler Carruth87c44602011-07-01 23:49:12 +00001015 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001016 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001017 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001018 // If the alignment is less than or equal to 8 bits, the packed attribute
1019 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001020 if (!FD->getType()->isDependentType() &&
1021 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001022 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001023 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001024 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001025 else
Michael Han51d8c522013-01-24 16:46:58 +00001026 FD->addAttr(::new (S.Context)
1027 PackedAttr(Attr.getRange(), S.Context,
1028 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001029 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001030 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001031}
1032
Chandler Carruth1b03c872011-07-02 00:01:44 +00001033static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001034 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001035 RD->addAttr(::new (S.Context)
1036 MsStructAttr(Attr.getRange(), S.Context,
1037 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001038 else
1039 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1040}
1041
Chandler Carruth1b03c872011-07-02 00:01:44 +00001042static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001043 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001044 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001045 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001046
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001047 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001048 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001049 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001050 D->addAttr(::new (S.Context)
1051 IBActionAttr(Attr.getRange(), S.Context,
1052 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001053 return;
1054 }
1055
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001056 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001057}
1058
Ted Kremenek2f041d02011-09-29 07:02:25 +00001059static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1060 // The IBOutlet/IBOutletCollection attributes only apply to instance
1061 // variables or properties of Objective-C classes. The outlet must also
1062 // have an object reference type.
1063 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1064 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001065 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001066 << Attr.getName() << VD->getType() << 0;
1067 return false;
1068 }
1069 }
1070 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1071 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001072 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001073 << Attr.getName() << PD->getType() << 1;
1074 return false;
1075 }
1076 }
1077 else {
1078 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1079 return false;
1080 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001081
Ted Kremenek2f041d02011-09-29 07:02:25 +00001082 return true;
1083}
1084
Chandler Carruth1b03c872011-07-02 00:01:44 +00001085static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001086 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001087 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001088 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001089
1090 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001091 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001092
Michael Han51d8c522013-01-24 16:46:58 +00001093 D->addAttr(::new (S.Context)
1094 IBOutletAttr(Attr.getRange(), S.Context,
1095 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001096}
1097
Chandler Carruth1b03c872011-07-02 00:01:44 +00001098static void handleIBOutletCollection(Sema &S, Decl *D,
1099 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001100
1101 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001102 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001103 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1104 return;
1105 }
1106
Ted Kremenek2f041d02011-09-29 07:02:25 +00001107 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001108 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001109
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001110 IdentifierInfo *II = Attr.getParameterName();
1111 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001112 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001113
John McCallb3d87482010-08-24 05:47:05 +00001114 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001115 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001116 if (!TypeRep) {
1117 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1118 return;
1119 }
John McCallb3d87482010-08-24 05:47:05 +00001120 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001121 // Diagnose use of non-object type in iboutletcollection attribute.
1122 // FIXME. Gnu attribute extension ignores use of builtin types in
1123 // attributes. So, __attribute__((iboutletcollection(char))) will be
1124 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001125 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001126 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1127 return;
1128 }
Michael Han51d8c522013-01-24 16:46:58 +00001129 D->addAttr(::new (S.Context)
1130 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1131 QT, Attr.getParameterLoc(),
1132 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001133}
1134
Chandler Carruthd309c812011-07-01 23:49:16 +00001135static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001136 if (const RecordType *UT = T->getAsUnionType())
1137 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1138 RecordDecl *UD = UT->getDecl();
1139 for (RecordDecl::field_iterator it = UD->field_begin(),
1140 itend = UD->field_end(); it != itend; ++it) {
1141 QualType QT = it->getType();
1142 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1143 T = QT;
1144 return;
1145 }
1146 }
1147 }
1148}
1149
Nuno Lopes587de5b2012-05-24 00:22:00 +00001150static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001151 if (!isFunctionOrMethod(D)) {
1152 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1153 << "alloc_size" << ExpectedFunctionOrMethod;
1154 return;
1155 }
1156
Nuno Lopes587de5b2012-05-24 00:22:00 +00001157 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1158 return;
1159
1160 // In C++ the implicit 'this' function parameter also counts, and they are
1161 // counted from one.
1162 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidis28965bf2013-02-22 06:58:28 +00001163 unsigned NumArgs;
1164 if (hasFunctionProto(D))
1165 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1166 else
1167 NumArgs = 0;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001168
1169 SmallVector<unsigned, 8> SizeArgs;
1170
1171 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1172 E = Attr.arg_end(); I!=E; ++I) {
1173 // The argument must be an integer constant expression.
1174 Expr *Ex = *I;
1175 llvm::APSInt ArgNum;
1176 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1177 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1178 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1179 << "alloc_size" << Ex->getSourceRange();
1180 return;
1181 }
1182
1183 uint64_t x = ArgNum.getZExtValue();
1184
1185 if (x < 1 || x > NumArgs) {
1186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1187 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1188 return;
1189 }
1190
1191 --x;
1192 if (HasImplicitThisParam) {
1193 if (x == 0) {
1194 S.Diag(Attr.getLoc(),
1195 diag::err_attribute_invalid_implicit_this_argument)
1196 << "alloc_size" << Ex->getSourceRange();
1197 return;
1198 }
1199 --x;
1200 }
1201
1202 // check if the function argument is of an integer type
1203 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1204 if (!T->isIntegerType()) {
1205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1206 << "alloc_size" << Ex->getSourceRange();
1207 return;
1208 }
1209
Nuno Lopes587de5b2012-05-24 00:22:00 +00001210 SizeArgs.push_back(x);
1211 }
1212
1213 // check if the function returns a pointer
1214 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1215 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1216 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1217 }
1218
Michael Han51d8c522013-01-24 16:46:58 +00001219 D->addAttr(::new (S.Context)
1220 AllocSizeAttr(Attr.getRange(), S.Context,
1221 SizeArgs.data(), SizeArgs.size(),
1222 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001223}
1224
Chandler Carruth1b03c872011-07-02 00:01:44 +00001225static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001226 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1227 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001228 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001229 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001230 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001231 return;
1232 }
Mike Stumpbf916502009-07-24 19:02:52 +00001233
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001234 // In C++ the implicit 'this' function parameter also counts, and they are
1235 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001236 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001237 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001238
1239 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001240 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001241
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001242 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1243 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001244 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001245 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001246 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001247 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1248 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1250 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001251 return;
1252 }
Mike Stumpbf916502009-07-24 19:02:52 +00001253
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001254 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001255
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001256 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001257 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001258 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001259 return;
1260 }
Mike Stumpbf916502009-07-24 19:02:52 +00001261
Ted Kremenek465172f2008-07-21 22:09:15 +00001262 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001263 if (HasImplicitThisParam) {
1264 if (x == 0) {
1265 S.Diag(Attr.getLoc(),
1266 diag::err_attribute_invalid_implicit_this_argument)
1267 << "nonnull" << Ex->getSourceRange();
1268 return;
1269 }
1270 --x;
1271 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001272
1273 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001274 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001275 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001276
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001277 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001278 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001279 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001280 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001281 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001282 }
Mike Stumpbf916502009-07-24 19:02:52 +00001283
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001284 NonNullArgs.push_back(x);
1285 }
Mike Stumpbf916502009-07-24 19:02:52 +00001286
1287 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1288 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001289 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001290 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1291 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001292 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001293 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001294 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001295 }
Mike Stumpbf916502009-07-24 19:02:52 +00001296
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001297 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001298 if (NonNullArgs.empty()) {
1299 // Warn the trivial case only if attribute is not coming from a
1300 // macro instantiation.
1301 if (Attr.getLoc().isFileID())
1302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001303 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001304 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001305 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001306
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001307 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001308 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001309 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001310 D->addAttr(::new (S.Context)
1311 NonNullAttr(Attr.getRange(), S.Context, start, size,
1312 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001313}
1314
Chandler Carruth1b03c872011-07-02 00:01:44 +00001315static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001316 // This attribute must be applied to a function declaration.
1317 // The first argument to the attribute must be a string,
1318 // the name of the resource, for example "malloc".
1319 // The following arguments must be argument indexes, the arguments must be
1320 // of integer type for Returns, otherwise of pointer type.
1321 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001322 // after being held. free() should be __attribute((ownership_takes)), whereas
1323 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001324
1325 if (!AL.getParameterName()) {
1326 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1327 << AL.getName()->getName() << 1;
1328 return;
1329 }
1330 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001331 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001332 switch (AL.getKind()) {
1333 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001334 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001335 if (AL.getNumArgs() < 1) {
1336 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1337 return;
1338 }
Jordy Rose2a479922010-08-12 08:54:03 +00001339 break;
1340 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001341 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 if (AL.getNumArgs() < 1) {
1343 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1344 return;
1345 }
Jordy Rose2a479922010-08-12 08:54:03 +00001346 break;
1347 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001348 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001349 if (AL.getNumArgs() > 1) {
1350 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1351 << AL.getNumArgs() + 1;
1352 return;
1353 }
Jordy Rose2a479922010-08-12 08:54:03 +00001354 break;
1355 default:
1356 // This should never happen given how we are called.
1357 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 }
1359
Chandler Carruth87c44602011-07-01 23:49:12 +00001360 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001361 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1362 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001363 return;
1364 }
1365
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001366 // In C++ the implicit 'this' function parameter also counts, and they are
1367 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001368 bool HasImplicitThisParam = isInstanceMethod(D);
1369 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001370
Chris Lattner5f9e2722011-07-23 10:55:15 +00001371 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001372
1373 // Normalize the argument, __foo__ becomes foo.
1374 if (Module.startswith("__") && Module.endswith("__"))
1375 Module = Module.substr(2, Module.size() - 4);
1376
Chris Lattner5f9e2722011-07-23 10:55:15 +00001377 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001378
Jordy Rose2a479922010-08-12 08:54:03 +00001379 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1380 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001381
Peter Collingbourne7a730022010-11-23 20:45:58 +00001382 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001383 llvm::APSInt ArgNum(32);
1384 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1385 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1386 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1387 << AL.getName()->getName() << IdxExpr->getSourceRange();
1388 continue;
1389 }
1390
1391 unsigned x = (unsigned) ArgNum.getZExtValue();
1392
1393 if (x > NumArgs || x < 1) {
1394 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1395 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1396 continue;
1397 }
1398 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001399 if (HasImplicitThisParam) {
1400 if (x == 0) {
1401 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1402 << "ownership" << IdxExpr->getSourceRange();
1403 return;
1404 }
1405 --x;
1406 }
1407
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001408 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001409 case OwnershipAttr::Takes:
1410 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001411 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001412 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001413 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1414 // FIXME: Should also highlight argument in decl.
1415 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001416 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001417 << "pointer"
1418 << IdxExpr->getSourceRange();
1419 continue;
1420 }
1421 break;
1422 }
Sean Huntcf807c42010-08-18 23:23:40 +00001423 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001424 if (AL.getNumArgs() > 1) {
1425 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001426 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001427 llvm::APSInt ArgNum(32);
1428 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1429 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1430 S.Diag(AL.getLoc(), diag::err_ownership_type)
1431 << "ownership_returns" << "integer"
1432 << IdxExpr->getSourceRange();
1433 return;
1434 }
1435 }
1436 break;
1437 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001438 } // switch
1439
1440 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001441 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001442 i = D->specific_attr_begin<OwnershipAttr>(),
1443 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001444 i != e; ++i) {
1445 if ((*i)->getOwnKind() != K) {
1446 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1447 I!=E; ++I) {
1448 if (x == *I) {
1449 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1450 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001451 }
1452 }
1453 }
1454 }
1455 OwnershipArgs.push_back(x);
1456 }
1457
1458 unsigned* start = OwnershipArgs.data();
1459 unsigned size = OwnershipArgs.size();
1460 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001461
1462 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1463 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1464 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001465 }
Sean Huntcf807c42010-08-18 23:23:40 +00001466
Michael Han51d8c522013-01-24 16:46:58 +00001467 D->addAttr(::new (S.Context)
1468 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1469 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001470}
1471
Chandler Carruth1b03c872011-07-02 00:01:44 +00001472static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001473 // Check the attribute arguments.
1474 if (Attr.getNumArgs() > 1) {
1475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1476 return;
1477 }
1478
Chandler Carruth87c44602011-07-01 23:49:12 +00001479 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001481 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001482 return;
1483 }
1484
Chandler Carruth87c44602011-07-01 23:49:12 +00001485 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001486
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001487 // gcc rejects
1488 // class c {
1489 // static int a __attribute__((weakref ("v2")));
1490 // static int b() __attribute__((weakref ("f3")));
1491 // };
1492 // and ignores the attributes of
1493 // void f(void) {
1494 // static int a __attribute__((weakref ("v2")));
1495 // }
1496 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001497 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001498 if (!Ctx->isFileContext()) {
1499 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001500 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001501 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001502 }
1503
1504 // The GCC manual says
1505 //
1506 // At present, a declaration to which `weakref' is attached can only
1507 // be `static'.
1508 //
1509 // It also says
1510 //
1511 // Without a TARGET,
1512 // given as an argument to `weakref' or to `alias', `weakref' is
1513 // equivalent to `weak'.
1514 //
1515 // gcc 4.4.1 will accept
1516 // int a7 __attribute__((weakref));
1517 // as
1518 // int a7 __attribute__((weak));
1519 // This looks like a bug in gcc. We reject that for now. We should revisit
1520 // it if this behaviour is actually used.
1521
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001522 // GCC rejects
1523 // static ((alias ("y"), weakref)).
1524 // Should we? How to check that weakref is before or after alias?
1525
1526 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001527 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001528 Arg = Arg->IgnoreParenCasts();
1529 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1530
Douglas Gregor5cee1192011-07-27 05:40:30 +00001531 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1533 << "weakref" << 1;
1534 return;
1535 }
1536 // GCC will accept anything as the argument of weakref. Should we
1537 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001538 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001539 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001540 }
1541
Michael Han51d8c522013-01-24 16:46:58 +00001542 D->addAttr(::new (S.Context)
1543 WeakRefAttr(Attr.getRange(), S.Context,
1544 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001545}
1546
Chandler Carruth1b03c872011-07-02 00:01:44 +00001547static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001548 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001549 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001551 return;
1552 }
Mike Stumpbf916502009-07-24 19:02:52 +00001553
Peter Collingbourne7a730022010-11-23 20:45:58 +00001554 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001555 Arg = Arg->IgnoreParenCasts();
1556 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001557
Douglas Gregor5cee1192011-07-27 05:40:30 +00001558 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001559 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001560 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001561 return;
1562 }
Mike Stumpbf916502009-07-24 19:02:52 +00001563
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001564 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001565 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1566 return;
1567 }
1568
Chris Lattner6b6b5372008-06-26 18:38:35 +00001569 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001570
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001571 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001572 Str->getString(),
1573 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001574}
1575
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001576static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1577 // Check the attribute arguments.
1578 if (!checkAttributeNumArgs(S, Attr, 0))
1579 return;
1580
1581 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1583 << Attr.getName() << ExpectedFunctionOrMethod;
1584 return;
1585 }
1586
Michael Han51d8c522013-01-24 16:46:58 +00001587 D->addAttr(::new (S.Context)
1588 MinSizeAttr(Attr.getRange(), S.Context,
1589 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001590}
1591
Benjamin Krameree409a92012-05-12 21:10:52 +00001592static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1593 // Check the attribute arguments.
1594 if (!checkAttributeNumArgs(S, Attr, 0))
1595 return;
1596
1597 if (!isa<FunctionDecl>(D)) {
1598 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1599 << Attr.getName() << ExpectedFunction;
1600 return;
1601 }
1602
1603 if (D->hasAttr<HotAttr>()) {
1604 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1605 << Attr.getName() << "hot";
1606 return;
1607 }
1608
Michael Han51d8c522013-01-24 16:46:58 +00001609 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1610 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001611}
1612
1613static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1614 // Check the attribute arguments.
1615 if (!checkAttributeNumArgs(S, Attr, 0))
1616 return;
1617
1618 if (!isa<FunctionDecl>(D)) {
1619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1620 << Attr.getName() << ExpectedFunction;
1621 return;
1622 }
1623
1624 if (D->hasAttr<ColdAttr>()) {
1625 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1626 << Attr.getName() << "cold";
1627 return;
1628 }
1629
Michael Han51d8c522013-01-24 16:46:58 +00001630 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1631 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001632}
1633
Chandler Carruth1b03c872011-07-02 00:01:44 +00001634static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001635 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001636 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001637 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001638
Chandler Carruth87c44602011-07-01 23:49:12 +00001639 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001641 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001642 return;
1643 }
1644
Michael Han51d8c522013-01-24 16:46:58 +00001645 D->addAttr(::new (S.Context)
1646 NakedAttr(Attr.getRange(), S.Context,
1647 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001648}
1649
Chandler Carruth1b03c872011-07-02 00:01:44 +00001650static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1651 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001652 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001653 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1655 return;
1656 }
1657
Chandler Carruth87c44602011-07-01 23:49:12 +00001658 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001660 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001661 return;
1662 }
Mike Stumpbf916502009-07-24 19:02:52 +00001663
Michael Han51d8c522013-01-24 16:46:58 +00001664 D->addAttr(::new (S.Context)
1665 AlwaysInlineAttr(Attr.getRange(), S.Context,
1666 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001667}
1668
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001669static void handleTLSModelAttr(Sema &S, Decl *D,
1670 const AttributeList &Attr) {
1671 // Check the attribute arguments.
1672 if (Attr.getNumArgs() != 1) {
1673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1674 return;
1675 }
1676
1677 Expr *Arg = Attr.getArg(0);
1678 Arg = Arg->IgnoreParenCasts();
1679 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1680
1681 // Check that it is a string.
1682 if (!Str) {
1683 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1684 return;
1685 }
1686
Richard Smith38afbc72013-04-13 02:43:54 +00001687 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1689 << Attr.getName() << ExpectedTLSVar;
1690 return;
1691 }
1692
1693 // Check that the value.
1694 StringRef Model = Str->getString();
1695 if (Model != "global-dynamic" && Model != "local-dynamic"
1696 && Model != "initial-exec" && Model != "local-exec") {
1697 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1698 return;
1699 }
1700
Michael Han51d8c522013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 TLSModelAttr(Attr.getRange(), S.Context, Model,
1703 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001704}
1705
Chandler Carruth1b03c872011-07-02 00:01:44 +00001706static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001707 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001708 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001709 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1710 return;
1711 }
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Chandler Carruth87c44602011-07-01 23:49:12 +00001713 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001714 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001715 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001716 D->addAttr(::new (S.Context)
1717 MallocAttr(Attr.getRange(), S.Context,
1718 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001719 return;
1720 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001721 }
1722
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001723 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001724}
1725
Chandler Carruth1b03c872011-07-02 00:01:44 +00001726static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001727 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001728 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001729 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001730
Michael Han51d8c522013-01-24 16:46:58 +00001731 D->addAttr(::new (S.Context)
1732 MayAliasAttr(Attr.getRange(), S.Context,
1733 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001737 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001738 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001739 D->addAttr(::new (S.Context)
1740 NoCommonAttr(Attr.getRange(), S.Context,
1741 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001742 else
1743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001744 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001745}
1746
Chandler Carruth1b03c872011-07-02 00:01:44 +00001747static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001748 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001749 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001750 D->addAttr(::new (S.Context)
1751 CommonAttr(Attr.getRange(), S.Context,
1752 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001753 else
1754 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001755 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001756}
1757
Chandler Carruth1b03c872011-07-02 00:01:44 +00001758static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001759 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001760
1761 if (S.CheckNoReturnAttr(attr)) return;
1762
Chandler Carruth87c44602011-07-01 23:49:12 +00001763 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001764 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001765 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001766 return;
1767 }
1768
Michael Han51d8c522013-01-24 16:46:58 +00001769 D->addAttr(::new (S.Context)
1770 NoReturnAttr(attr.getRange(), S.Context,
1771 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001772}
1773
1774bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001775 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001776 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1777 attr.setInvalid();
1778 return true;
1779 }
1780
1781 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001782}
1783
Chandler Carruth1b03c872011-07-02 00:01:44 +00001784static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1785 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001786
1787 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1788 // because 'analyzer_noreturn' does not impact the type.
1789
Chandler Carruth1731e202011-07-11 23:30:35 +00001790 if(!checkAttributeNumArgs(S, Attr, 0))
1791 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001792
Chandler Carruth87c44602011-07-01 23:49:12 +00001793 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1794 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001795 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1796 && !VD->getType()->isFunctionPointerType())) {
1797 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001798 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001799 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001800 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001801 return;
1802 }
1803 }
1804
Michael Han51d8c522013-01-24 16:46:58 +00001805 D->addAttr(::new (S.Context)
1806 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1807 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001808}
1809
Richard Smithcd8ab512013-01-17 01:30:42 +00001810static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1811 const AttributeList &Attr) {
1812 // C++11 [dcl.attr.noreturn]p1:
1813 // The attribute may be applied to the declarator-id in a function
1814 // declaration.
1815 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1816 if (!FD) {
1817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1818 << Attr.getName() << ExpectedFunctionOrMethod;
1819 return;
1820 }
1821
Michael Han51d8c522013-01-24 16:46:58 +00001822 D->addAttr(::new (S.Context)
1823 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1824 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001825}
1826
John Thompson35cc9622010-08-09 21:53:52 +00001827// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001828static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001829/*
1830 Returning a Vector Class in Registers
1831
Eric Christopherf48f3672010-12-01 22:13:54 +00001832 According to the PPU ABI specifications, a class with a single member of
1833 vector type is returned in memory when used as the return value of a function.
1834 This results in inefficient code when implementing vector classes. To return
1835 the value in a single vector register, add the vecreturn attribute to the
1836 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001837
1838 Example:
1839
1840 struct Vector
1841 {
1842 __vector float xyzw;
1843 } __attribute__((vecreturn));
1844
1845 Vector Add(Vector lhs, Vector rhs)
1846 {
1847 Vector result;
1848 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1849 return result; // This will be returned in a register
1850 }
1851*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001854 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001855 return;
1856 }
1857
Chandler Carruth87c44602011-07-01 23:49:12 +00001858 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001859 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1860 return;
1861 }
1862
Chandler Carruth87c44602011-07-01 23:49:12 +00001863 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001864 int count = 0;
1865
1866 if (!isa<CXXRecordDecl>(record)) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1868 return;
1869 }
1870
1871 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1872 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1873 return;
1874 }
1875
Eric Christopherf48f3672010-12-01 22:13:54 +00001876 for (RecordDecl::field_iterator iter = record->field_begin();
1877 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001878 if ((count == 1) || !iter->getType()->isVectorType()) {
1879 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1880 return;
1881 }
1882 count++;
1883 }
1884
Michael Han51d8c522013-01-24 16:46:58 +00001885 D->addAttr(::new (S.Context)
1886 VecReturnAttr(Attr.getRange(), S.Context,
1887 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001888}
1889
Richard Smith3a2b7a12013-01-28 22:42:45 +00001890static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1891 const AttributeList &Attr) {
1892 if (isa<ParmVarDecl>(D)) {
1893 // [[carries_dependency]] can only be applied to a parameter if it is a
1894 // parameter of a function declaration or lambda.
1895 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1896 S.Diag(Attr.getLoc(),
1897 diag::err_carries_dependency_param_not_function_decl);
1898 return;
1899 }
1900 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001902 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001903 return;
1904 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001905
1906 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1907 Attr.getRange(), S.Context,
1908 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001909}
1910
Chandler Carruth1b03c872011-07-02 00:01:44 +00001911static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001912 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001913 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001915 return;
1916 }
Mike Stumpbf916502009-07-24 19:02:52 +00001917
Chandler Carruth87c44602011-07-01 23:49:12 +00001918 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001919 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001921 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001922 return;
1923 }
Mike Stumpbf916502009-07-24 19:02:52 +00001924
Michael Han51d8c522013-01-24 16:46:58 +00001925 D->addAttr(::new (S.Context)
1926 UnusedAttr(Attr.getRange(), S.Context,
1927 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001928}
1929
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001930static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1931 const AttributeList &Attr) {
1932 // check the attribute arguments.
1933 if (Attr.hasParameterOrArguments()) {
1934 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1935 return;
1936 }
1937
1938 if (!isa<FunctionDecl>(D)) {
1939 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1940 << Attr.getName() << ExpectedFunction;
1941 return;
1942 }
1943
Michael Han51d8c522013-01-24 16:46:58 +00001944 D->addAttr(::new (S.Context)
1945 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1946 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001947}
1948
Chandler Carruth1b03c872011-07-02 00:01:44 +00001949static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001950 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001951 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001952 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1953 return;
1954 }
Mike Stumpbf916502009-07-24 19:02:52 +00001955
Chandler Carruth87c44602011-07-01 23:49:12 +00001956 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001957 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001958 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1959 return;
1960 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001961 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001962 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001963 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001964 return;
1965 }
Mike Stumpbf916502009-07-24 19:02:52 +00001966
Michael Han51d8c522013-01-24 16:46:58 +00001967 D->addAttr(::new (S.Context)
1968 UsedAttr(Attr.getRange(), S.Context,
1969 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001970}
1971
Chandler Carruth1b03c872011-07-02 00:01:44 +00001972static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001973 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001974 if (Attr.getNumArgs() > 1) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001976 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001977 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001978
1979 int priority = 65535; // FIXME: Do not hardcode such constants.
1980 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001981 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001982 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001983 if (E->isTypeDependent() || E->isValueDependent() ||
1984 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001985 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001986 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001987 return;
1988 }
1989 priority = Idx.getZExtValue();
1990 }
Mike Stumpbf916502009-07-24 19:02:52 +00001991
Chandler Carruth87c44602011-07-01 23:49:12 +00001992 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001993 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001994 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001995 return;
1996 }
1997
Michael Han51d8c522013-01-24 16:46:58 +00001998 D->addAttr(::new (S.Context)
1999 ConstructorAttr(Attr.getRange(), S.Context, priority,
2000 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002001}
2002
Chandler Carruth1b03c872011-07-02 00:01:44 +00002003static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002004 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002005 if (Attr.getNumArgs() > 1) {
2006 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002007 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002008 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002009
2010 int priority = 65535; // FIXME: Do not hardcode such constants.
2011 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002012 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002013 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002014 if (E->isTypeDependent() || E->isValueDependent() ||
2015 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002016 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002017 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002018 return;
2019 }
2020 priority = Idx.getZExtValue();
2021 }
Mike Stumpbf916502009-07-24 19:02:52 +00002022
Chandler Carruth87c44602011-07-01 23:49:12 +00002023 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002024 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002025 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002026 return;
2027 }
2028
Michael Han51d8c522013-01-24 16:46:58 +00002029 D->addAttr(::new (S.Context)
2030 DestructorAttr(Attr.getRange(), S.Context, priority,
2031 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002032}
2033
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002034template <typename AttrTy>
2035static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
2036 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002037 unsigned NumArgs = Attr.getNumArgs();
2038 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002039 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002040 return;
2041 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002042
2043 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002044 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002045 if (NumArgs == 1) {
2046 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002047 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002048 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002049 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002050 return;
2051 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002052 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002053 }
Mike Stumpbf916502009-07-24 19:02:52 +00002054
Michael Han51d8c522013-01-24 16:46:58 +00002055 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2056 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002057}
2058
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002059static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2060 const AttributeList &Attr) {
2061 unsigned NumArgs = Attr.getNumArgs();
2062 if (NumArgs > 0) {
2063 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2064 return;
2065 }
2066
Michael Han51d8c522013-01-24 16:46:58 +00002067 D->addAttr(::new (S.Context)
2068 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2069 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002070}
2071
Patrick Beardb2f68202012-04-06 18:12:22 +00002072static void handleObjCRootClassAttr(Sema &S, Decl *D,
2073 const AttributeList &Attr) {
2074 if (!isa<ObjCInterfaceDecl>(D)) {
2075 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2076 return;
2077 }
2078
2079 unsigned NumArgs = Attr.getNumArgs();
2080 if (NumArgs > 0) {
2081 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2082 return;
2083 }
2084
Michael Han51d8c522013-01-24 16:46:58 +00002085 D->addAttr(::new (S.Context)
2086 ObjCRootClassAttr(Attr.getRange(), S.Context,
2087 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002088}
2089
Michael Han51d8c522013-01-24 16:46:58 +00002090static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2091 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002092 if (!isa<ObjCInterfaceDecl>(D)) {
2093 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2094 return;
2095 }
2096
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002097 unsigned NumArgs = Attr.getNumArgs();
2098 if (NumArgs > 0) {
2099 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2100 return;
2101 }
2102
Michael Han51d8c522013-01-24 16:46:58 +00002103 D->addAttr(::new (S.Context)
2104 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2105 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002106}
2107
Jordy Rosefad5de92012-05-08 03:27:22 +00002108static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2109 IdentifierInfo *Platform,
2110 VersionTuple Introduced,
2111 VersionTuple Deprecated,
2112 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002113 StringRef PlatformName
2114 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2115 if (PlatformName.empty())
2116 PlatformName = Platform->getName();
2117
2118 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2119 // of these steps are needed).
2120 if (!Introduced.empty() && !Deprecated.empty() &&
2121 !(Introduced <= Deprecated)) {
2122 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2123 << 1 << PlatformName << Deprecated.getAsString()
2124 << 0 << Introduced.getAsString();
2125 return true;
2126 }
2127
2128 if (!Introduced.empty() && !Obsoleted.empty() &&
2129 !(Introduced <= Obsoleted)) {
2130 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2131 << 2 << PlatformName << Obsoleted.getAsString()
2132 << 0 << Introduced.getAsString();
2133 return true;
2134 }
2135
2136 if (!Deprecated.empty() && !Obsoleted.empty() &&
2137 !(Deprecated <= Obsoleted)) {
2138 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2139 << 2 << PlatformName << Obsoleted.getAsString()
2140 << 1 << Deprecated.getAsString();
2141 return true;
2142 }
2143
2144 return false;
2145}
2146
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002147/// \brief Check whether the two versions match.
2148///
2149/// If either version tuple is empty, then they are assumed to match. If
2150/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2151static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2152 bool BeforeIsOkay) {
2153 if (X.empty() || Y.empty())
2154 return true;
2155
2156 if (X == Y)
2157 return true;
2158
2159 if (BeforeIsOkay && X < Y)
2160 return true;
2161
2162 return false;
2163}
2164
Rafael Espindola51be6e32013-01-08 22:04:34 +00002165AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002166 IdentifierInfo *Platform,
2167 VersionTuple Introduced,
2168 VersionTuple Deprecated,
2169 VersionTuple Obsoleted,
2170 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002171 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002172 bool Override,
2173 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002174 VersionTuple MergedIntroduced = Introduced;
2175 VersionTuple MergedDeprecated = Deprecated;
2176 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002177 bool FoundAny = false;
2178
Rafael Espindola98ae8342012-05-10 02:50:16 +00002179 if (D->hasAttrs()) {
2180 AttrVec &Attrs = D->getAttrs();
2181 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2182 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2183 if (!OldAA) {
2184 ++i;
2185 continue;
2186 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002187
Rafael Espindola98ae8342012-05-10 02:50:16 +00002188 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2189 if (OldPlatform != Platform) {
2190 ++i;
2191 continue;
2192 }
2193
2194 FoundAny = true;
2195 VersionTuple OldIntroduced = OldAA->getIntroduced();
2196 VersionTuple OldDeprecated = OldAA->getDeprecated();
2197 VersionTuple OldObsoleted = OldAA->getObsoleted();
2198 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002199
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002200 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2201 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2202 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2203 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002204 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002205 if (Override) {
2206 int Which = -1;
2207 VersionTuple FirstVersion;
2208 VersionTuple SecondVersion;
2209 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2210 Which = 0;
2211 FirstVersion = OldIntroduced;
2212 SecondVersion = Introduced;
2213 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2214 Which = 1;
2215 FirstVersion = Deprecated;
2216 SecondVersion = OldDeprecated;
2217 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2218 Which = 2;
2219 FirstVersion = Obsoleted;
2220 SecondVersion = OldObsoleted;
2221 }
2222
2223 if (Which == -1) {
2224 Diag(OldAA->getLocation(),
2225 diag::warn_mismatched_availability_override_unavail)
2226 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2227 } else {
2228 Diag(OldAA->getLocation(),
2229 diag::warn_mismatched_availability_override)
2230 << Which
2231 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2232 << FirstVersion.getAsString() << SecondVersion.getAsString();
2233 }
2234 Diag(Range.getBegin(), diag::note_overridden_method);
2235 } else {
2236 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2237 Diag(Range.getBegin(), diag::note_previous_attribute);
2238 }
2239
Rafael Espindola98ae8342012-05-10 02:50:16 +00002240 Attrs.erase(Attrs.begin() + i);
2241 --e;
2242 continue;
2243 }
2244
2245 VersionTuple MergedIntroduced2 = MergedIntroduced;
2246 VersionTuple MergedDeprecated2 = MergedDeprecated;
2247 VersionTuple MergedObsoleted2 = MergedObsoleted;
2248
2249 if (MergedIntroduced2.empty())
2250 MergedIntroduced2 = OldIntroduced;
2251 if (MergedDeprecated2.empty())
2252 MergedDeprecated2 = OldDeprecated;
2253 if (MergedObsoleted2.empty())
2254 MergedObsoleted2 = OldObsoleted;
2255
2256 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2257 MergedIntroduced2, MergedDeprecated2,
2258 MergedObsoleted2)) {
2259 Attrs.erase(Attrs.begin() + i);
2260 --e;
2261 continue;
2262 }
2263
2264 MergedIntroduced = MergedIntroduced2;
2265 MergedDeprecated = MergedDeprecated2;
2266 MergedObsoleted = MergedObsoleted2;
2267 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002268 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002269 }
2270
2271 if (FoundAny &&
2272 MergedIntroduced == Introduced &&
2273 MergedDeprecated == Deprecated &&
2274 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002275 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002276
Ted Kremenekcb344392013-04-06 00:34:27 +00002277 // Only create a new attribute if !Override, but we want to do
2278 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002279 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002280 MergedDeprecated, MergedObsoleted) &&
2281 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002282 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2283 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002284 Obsoleted, IsUnavailable, Message,
2285 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002286 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002287 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002288}
2289
Chandler Carruth1b03c872011-07-02 00:01:44 +00002290static void handleAvailabilityAttr(Sema &S, Decl *D,
2291 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002292 IdentifierInfo *Platform = Attr.getParameterName();
2293 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002294 unsigned Index = Attr.getAttributeSpellingListIndex();
2295
Rafael Espindola3b294362012-05-06 19:56:25 +00002296 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002297 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2298 << Platform;
2299
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002300 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2301 if (!ND) {
2302 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2303 return;
2304 }
2305
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002306 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2307 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2308 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002309 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002310 StringRef Str;
2311 const StringLiteral *SE =
2312 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2313 if (SE)
2314 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002315
Rafael Espindola51be6e32013-01-08 22:04:34 +00002316 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002317 Platform,
2318 Introduced.Version,
2319 Deprecated.Version,
2320 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002321 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002322 /*Override=*/false,
2323 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002324 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002325 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002326}
2327
John McCalld4c3d662013-02-20 01:54:26 +00002328template <class T>
2329static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2330 typename T::VisibilityType value,
2331 unsigned attrSpellingListIndex) {
2332 T *existingAttr = D->getAttr<T>();
2333 if (existingAttr) {
2334 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2335 if (existingValue == value)
2336 return NULL;
2337 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2338 S.Diag(range.getBegin(), diag::note_previous_attribute);
2339 D->dropAttr<T>();
2340 }
2341 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2342}
2343
Rafael Espindola599f1b72012-05-13 03:25:18 +00002344VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002345 VisibilityAttr::VisibilityType Vis,
2346 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002347 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2348 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002349}
2350
John McCalld4c3d662013-02-20 01:54:26 +00002351TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2352 TypeVisibilityAttr::VisibilityType Vis,
2353 unsigned AttrSpellingListIndex) {
2354 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2355 AttrSpellingListIndex);
2356}
2357
2358static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2359 bool isTypeVisibility) {
2360 // Visibility attributes don't mean anything on a typedef.
2361 if (isa<TypedefNameDecl>(D)) {
2362 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2363 << Attr.getName();
2364 return;
2365 }
2366
2367 // 'type_visibility' can only go on a type or namespace.
2368 if (isTypeVisibility &&
2369 !(isa<TagDecl>(D) ||
2370 isa<ObjCInterfaceDecl>(D) ||
2371 isa<NamespaceDecl>(D))) {
2372 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2373 << Attr.getName() << ExpectedTypeOrNamespace;
2374 return;
2375 }
2376
Chris Lattner6b6b5372008-06-26 18:38:35 +00002377 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002378 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002379 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002380
Peter Collingbourne7a730022010-11-23 20:45:58 +00002381 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002382 Arg = Arg->IgnoreParenCasts();
2383 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002384
Douglas Gregor5cee1192011-07-27 05:40:30 +00002385 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld4c3d662013-02-20 01:54:26 +00002387 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002388 return;
2389 }
Mike Stumpbf916502009-07-24 19:02:52 +00002390
Chris Lattner5f9e2722011-07-23 10:55:15 +00002391 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002392 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002393
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002394 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002395 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002396 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002397 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002398 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002399 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002400 else if (TypeStr == "protected") {
2401 // Complain about attempts to use protected visibility on targets
2402 // (like Darwin) that don't support it.
2403 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2404 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2405 type = VisibilityAttr::Default;
2406 } else {
2407 type = VisibilityAttr::Protected;
2408 }
2409 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002410 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002411 return;
2412 }
Mike Stumpbf916502009-07-24 19:02:52 +00002413
Michael Han51d8c522013-01-24 16:46:58 +00002414 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002415 clang::Attr *newAttr;
2416 if (isTypeVisibility) {
2417 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2418 (TypeVisibilityAttr::VisibilityType) type,
2419 Index);
2420 } else {
2421 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2422 }
2423 if (newAttr)
2424 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002425}
2426
Chandler Carruth1b03c872011-07-02 00:01:44 +00002427static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2428 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002429 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2430 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002432 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002433 return;
2434 }
2435
Chandler Carruth87c44602011-07-01 23:49:12 +00002436 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2437 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2438 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002439 << "objc_method_family" << 1;
2440 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002442 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002443 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002444 return;
2445 }
2446
Chris Lattner5f9e2722011-07-23 10:55:15 +00002447 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002448 ObjCMethodFamilyAttr::FamilyKind family;
2449 if (param == "none")
2450 family = ObjCMethodFamilyAttr::OMF_None;
2451 else if (param == "alloc")
2452 family = ObjCMethodFamilyAttr::OMF_alloc;
2453 else if (param == "copy")
2454 family = ObjCMethodFamilyAttr::OMF_copy;
2455 else if (param == "init")
2456 family = ObjCMethodFamilyAttr::OMF_init;
2457 else if (param == "mutableCopy")
2458 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2459 else if (param == "new")
2460 family = ObjCMethodFamilyAttr::OMF_new;
2461 else {
2462 // Just warn and ignore it. This is future-proof against new
2463 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002464 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002465 return;
2466 }
2467
John McCallf85e1932011-06-15 23:02:42 +00002468 if (family == ObjCMethodFamilyAttr::OMF_init &&
2469 !method->getResultType()->isObjCObjectPointerType()) {
2470 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2471 << method->getResultType();
2472 // Ignore the attribute.
2473 return;
2474 }
2475
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002476 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002477 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002478}
2479
Chandler Carruth1b03c872011-07-02 00:01:44 +00002480static void handleObjCExceptionAttr(Sema &S, Decl *D,
2481 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002482 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002483 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002484
Chris Lattner0db29ec2009-02-14 08:09:34 +00002485 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2486 if (OCI == 0) {
2487 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2488 return;
2489 }
Mike Stumpbf916502009-07-24 19:02:52 +00002490
Michael Han51d8c522013-01-24 16:46:58 +00002491 D->addAttr(::new (S.Context)
2492 ObjCExceptionAttr(Attr.getRange(), S.Context,
2493 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002494}
2495
Chandler Carruth1b03c872011-07-02 00:01:44 +00002496static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002497 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002499 return;
2500 }
Richard Smith162e1c12011-04-15 14:24:37 +00002501 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002502 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002503 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002504 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2505 return;
2506 }
2507 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002508 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2509 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002510 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002511 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2512 return;
2513 }
2514 }
2515 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002516 // It is okay to include this attribute on properties, e.g.:
2517 //
2518 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2519 //
2520 // In this case it follows tradition and suppresses an error in the above
2521 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002522 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002523 }
Michael Han51d8c522013-01-24 16:46:58 +00002524 D->addAttr(::new (S.Context)
2525 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2526 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002527}
2528
Mike Stumpbf916502009-07-24 19:02:52 +00002529static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002530handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002531 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002533 return;
2534 }
2535
2536 if (!isa<FunctionDecl>(D)) {
2537 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2538 return;
2539 }
2540
Michael Han51d8c522013-01-24 16:46:58 +00002541 D->addAttr(::new (S.Context)
2542 OverloadableAttr(Attr.getRange(), S.Context,
2543 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002544}
2545
Chandler Carruth1b03c872011-07-02 00:01:44 +00002546static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002547 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002549 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002550 return;
2551 }
Mike Stumpbf916502009-07-24 19:02:52 +00002552
Steve Naroff9eae5762008-09-18 16:44:58 +00002553 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002555 return;
2556 }
Mike Stumpbf916502009-07-24 19:02:52 +00002557
Sean Huntcf807c42010-08-18 23:23:40 +00002558 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002559 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002560 type = BlocksAttr::ByRef;
2561 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002562 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002563 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002564 return;
2565 }
Mike Stumpbf916502009-07-24 19:02:52 +00002566
Michael Han51d8c522013-01-24 16:46:58 +00002567 D->addAttr(::new (S.Context)
2568 BlocksAttr(Attr.getRange(), S.Context, type,
2569 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002570}
2571
Chandler Carruth1b03c872011-07-02 00:01:44 +00002572static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002573 // check the attribute arguments.
2574 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002575 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002576 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002577 }
2578
John McCall3323fad2011-09-09 07:56:05 +00002579 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002580 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002581 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002582 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002583 if (E->isTypeDependent() || E->isValueDependent() ||
2584 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002586 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002587 return;
2588 }
Mike Stumpbf916502009-07-24 19:02:52 +00002589
John McCall3323fad2011-09-09 07:56:05 +00002590 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002591 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2592 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002593 return;
2594 }
John McCall3323fad2011-09-09 07:56:05 +00002595
2596 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002597 }
2598
John McCall3323fad2011-09-09 07:56:05 +00002599 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002600 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002601 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002602 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002603 if (E->isTypeDependent() || E->isValueDependent() ||
2604 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002606 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002607 return;
2608 }
2609 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002610
John McCall3323fad2011-09-09 07:56:05 +00002611 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002612 // FIXME: This error message could be improved, it would be nice
2613 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002614 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2615 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002616 return;
2617 }
2618 }
2619
Chandler Carruth87c44602011-07-01 23:49:12 +00002620 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002621 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002622 if (isa<FunctionNoProtoType>(FT)) {
2623 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2624 return;
2625 }
Mike Stumpbf916502009-07-24 19:02:52 +00002626
Chris Lattner897cd902009-03-17 23:03:47 +00002627 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002628 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002629 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002630 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002631 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002632 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002633 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002634 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002635 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002636 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2637 if (!BD->isVariadic()) {
2638 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2639 return;
2640 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002641 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002642 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002643 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002644 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002645 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002646 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002647 int m = Ty->isFunctionPointerType() ? 0 : 1;
2648 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002649 return;
2650 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002651 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002653 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002654 return;
2655 }
Anders Carlsson77091822008-10-05 18:05:59 +00002656 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002658 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002659 return;
2660 }
Michael Han51d8c522013-01-24 16:46:58 +00002661 D->addAttr(::new (S.Context)
2662 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2663 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002664}
2665
Chandler Carruth1b03c872011-07-02 00:01:44 +00002666static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002667 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002668 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002669 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002670
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002671 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002673 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002674 return;
2675 }
Mike Stumpbf916502009-07-24 19:02:52 +00002676
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002677 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2678 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2679 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002680 return;
2681 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002682 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2683 if (MD->getResultType()->isVoidType()) {
2684 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2685 << Attr.getName() << 1;
2686 return;
2687 }
2688
Michael Han51d8c522013-01-24 16:46:58 +00002689 D->addAttr(::new (S.Context)
2690 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2691 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002692}
2693
Chandler Carruth1b03c872011-07-02 00:01:44 +00002694static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002695 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002696 if (Attr.hasParameterOrArguments()) {
2697 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002698 return;
2699 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002700
Chandler Carruth87c44602011-07-01 23:49:12 +00002701 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002702 if (isa<CXXRecordDecl>(D)) {
2703 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2704 return;
2705 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2707 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002708 return;
2709 }
2710
Chandler Carruth87c44602011-07-01 23:49:12 +00002711 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002712
Michael Han51d8c522013-01-24 16:46:58 +00002713 nd->addAttr(::new (S.Context)
2714 WeakAttr(Attr.getRange(), S.Context,
2715 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002716}
2717
Chandler Carruth1b03c872011-07-02 00:01:44 +00002718static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002719 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002720 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002721 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002722
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002723
2724 // weak_import only applies to variable & function declarations.
2725 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002726 if (!D->canBeWeakImported(isDef)) {
2727 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002728 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2729 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002730 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002731 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002732 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002733 // Nothing to warn about here.
2734 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002735 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002736 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002737
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002738 return;
2739 }
2740
Michael Han51d8c522013-01-24 16:46:58 +00002741 D->addAttr(::new (S.Context)
2742 WeakImportAttr(Attr.getRange(), S.Context,
2743 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002744}
2745
Tanya Lattner0df579e2012-07-09 22:06:01 +00002746// Handles reqd_work_group_size and work_group_size_hint.
2747static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002748 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002749 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2750 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2751
Nate Begeman6f3d8382009-06-26 06:32:41 +00002752 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002753 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002754
2755 unsigned WGSize[3];
2756 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002757 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002758 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002759 if (E->isTypeDependent() || E->isValueDependent() ||
2760 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002761 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002762 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002763 return;
2764 }
2765 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2766 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002767
2768 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2769 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2770 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2771 if (!(A->getXDim() == WGSize[0] &&
2772 A->getYDim() == WGSize[1] &&
2773 A->getZDim() == WGSize[2])) {
2774 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2775 Attr.getName();
2776 }
2777 }
2778
2779 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2780 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2781 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2782 if (!(A->getXDim() == WGSize[0] &&
2783 A->getYDim() == WGSize[1] &&
2784 A->getZDim() == WGSize[2])) {
2785 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2786 Attr.getName();
2787 }
2788 }
2789
2790 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2791 D->addAttr(::new (S.Context)
2792 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002793 WGSize[0], WGSize[1], WGSize[2],
2794 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002795 else
2796 D->addAttr(::new (S.Context)
2797 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002798 WGSize[0], WGSize[1], WGSize[2],
2799 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002800}
2801
Joey Gouly37453b92013-03-08 09:42:32 +00002802static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2803 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2804
2805 // Attribute has 1 argument.
2806 if (!checkAttributeNumArgs(S, Attr, 1))
2807 return;
2808
2809 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2810
2811 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2812 (ParmType->isBooleanType() ||
2813 !ParmType->isIntegralType(S.getASTContext()))) {
2814 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2815 << ParmType;
2816 return;
2817 }
2818
2819 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2820 D->hasAttr<VecTypeHintAttr>()) {
2821 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2822 if (A->getTypeHint() != ParmType) {
2823 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2824 return;
2825 }
2826 }
2827
2828 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2829 ParmType, Attr.getLoc()));
2830}
2831
Joey Gouly96cead52013-03-14 09:54:43 +00002832static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2833 if (!dyn_cast<VarDecl>(D))
2834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2835 << 9;
2836 StringRef EndianType = Attr.getParameterName()->getName();
2837 if (EndianType != "host" && EndianType != "device")
2838 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2839}
2840
Rafael Espindola599f1b72012-05-13 03:25:18 +00002841SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002842 StringRef Name,
2843 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002844 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2845 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002846 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002847 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2848 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002849 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002850 }
Michael Han51d8c522013-01-24 16:46:58 +00002851 return ::new (Context) SectionAttr(Range, Context, Name,
2852 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002853}
2854
Chandler Carruth1b03c872011-07-02 00:01:44 +00002855static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002856 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002857 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002858 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002859
2860 // Make sure that there is a string literal as the sections's single
2861 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002862 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002863 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002864 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002865 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002866 return;
2867 }
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Chris Lattner797c3c42009-08-10 19:03:04 +00002869 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002870 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002871 if (!Error.empty()) {
2872 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2873 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002874 return;
2875 }
Mike Stump1eb44332009-09-09 15:08:12 +00002876
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002877 // This attribute cannot be applied to local variables.
2878 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2879 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2880 return;
2881 }
Michael Han51d8c522013-01-24 16:46:58 +00002882
2883 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002884 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002885 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002886 if (NewAttr)
2887 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002888}
2889
Chris Lattner6b6b5372008-06-26 18:38:35 +00002890
Chandler Carruth1b03c872011-07-02 00:01:44 +00002891static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002892 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002893 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002895 return;
2896 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002897
Chandler Carruth87c44602011-07-01 23:49:12 +00002898 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002899 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002900 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002901 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002902 D->addAttr(::new (S.Context)
2903 NoThrowAttr(Attr.getRange(), S.Context,
2904 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002905 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002906}
2907
Chandler Carruth1b03c872011-07-02 00:01:44 +00002908static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002909 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002910 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002911 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002912 return;
2913 }
Mike Stumpbf916502009-07-24 19:02:52 +00002914
Chandler Carruth87c44602011-07-01 23:49:12 +00002915 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002916 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002917 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002918 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002919 D->addAttr(::new (S.Context)
2920 ConstAttr(Attr.getRange(), S.Context,
2921 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002922 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002923}
2924
Chandler Carruth1b03c872011-07-02 00:01:44 +00002925static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002926 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002927 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002928 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002929
Michael Han51d8c522013-01-24 16:46:58 +00002930 D->addAttr(::new (S.Context)
2931 PureAttr(Attr.getRange(), S.Context,
2932 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002933}
2934
Chandler Carruth1b03c872011-07-02 00:01:44 +00002935static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002936 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2938 return;
2939 }
Mike Stumpbf916502009-07-24 19:02:52 +00002940
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002941 if (Attr.getNumArgs() != 0) {
2942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2943 return;
2944 }
Mike Stumpbf916502009-07-24 19:02:52 +00002945
Chandler Carruth87c44602011-07-01 23:49:12 +00002946 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002947
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002948 if (!VD || !VD->hasLocalStorage()) {
2949 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2950 return;
2951 }
Mike Stumpbf916502009-07-24 19:02:52 +00002952
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002953 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002954 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002955 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002956 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2957 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002958 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002959 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002960 Attr.getParameterName();
2961 return;
2962 }
Mike Stumpbf916502009-07-24 19:02:52 +00002963
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002964 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2965 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002966 S.Diag(Attr.getParameterLoc(),
2967 diag::err_attribute_cleanup_arg_not_function)
2968 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002969 return;
2970 }
2971
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002972 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002973 S.Diag(Attr.getParameterLoc(),
2974 diag::err_attribute_cleanup_func_must_take_one_arg)
2975 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002976 return;
2977 }
Mike Stumpbf916502009-07-24 19:02:52 +00002978
Anders Carlsson89941c12009-02-07 23:16:50 +00002979 // We're currently more strict than GCC about what function types we accept.
2980 // If this ever proves to be a problem it should be easy to fix.
2981 QualType Ty = S.Context.getPointerType(VD->getType());
2982 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002983 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2984 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002985 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002986 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2987 Attr.getParameterName() << ParamTy << Ty;
2988 return;
2989 }
Mike Stumpbf916502009-07-24 19:02:52 +00002990
Michael Han51d8c522013-01-24 16:46:58 +00002991 D->addAttr(::new (S.Context)
2992 CleanupAttr(Attr.getRange(), S.Context, FD,
2993 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002994 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00002995 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002996}
2997
Mike Stumpbf916502009-07-24 19:02:52 +00002998/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002999/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003000static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003001 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003002 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003003
Chandler Carruth87c44602011-07-01 23:49:12 +00003004 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003006 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003007 return;
3008 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003009
3010 // In C++ the implicit 'this' function parameter also counts, and they are
3011 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003012 bool HasImplicitThisParam = isInstanceMethod(D);
3013 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003014 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003015
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003016 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003017 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003018 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003019 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3020 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003021 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3022 << "format" << 2 << IdxExpr->getSourceRange();
3023 return;
3024 }
Mike Stumpbf916502009-07-24 19:02:52 +00003025
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003026 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3027 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3028 << "format" << 2 << IdxExpr->getSourceRange();
3029 return;
3030 }
Mike Stumpbf916502009-07-24 19:02:52 +00003031
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003032 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003033
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003034 if (HasImplicitThisParam) {
3035 if (ArgIdx == 0) {
3036 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3037 << "format_arg" << IdxExpr->getSourceRange();
3038 return;
3039 }
3040 ArgIdx--;
3041 }
3042
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003043 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003044 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003045
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003046 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3047 if (not_nsstring_type &&
3048 !isCFStringType(Ty, S.Context) &&
3049 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003050 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003051 // FIXME: Should highlight the actual expression that has the wrong type.
3052 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003053 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003054 << IdxExpr->getSourceRange();
3055 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003056 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003057 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003058 if (!isNSStringType(Ty, S.Context) &&
3059 !isCFStringType(Ty, S.Context) &&
3060 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003061 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003062 // FIXME: Should highlight the actual expression that has the wrong type.
3063 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003064 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003065 << IdxExpr->getSourceRange();
3066 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003067 }
3068
Michael Han51d8c522013-01-24 16:46:58 +00003069 D->addAttr(::new (S.Context)
3070 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3071 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003072}
3073
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003074enum FormatAttrKind {
3075 CFStringFormat,
3076 NSStringFormat,
3077 StrftimeFormat,
3078 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003079 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003080 InvalidFormat
3081};
3082
3083/// getFormatAttrKind - Map from format attribute names to supported format
3084/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003085static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003086 return llvm::StringSwitch<FormatAttrKind>(Format)
3087 // Check for formats that get handled specially.
3088 .Case("NSString", NSStringFormat)
3089 .Case("CFString", CFStringFormat)
3090 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003091
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003092 // Otherwise, check for supported formats.
3093 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3094 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3095 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003096
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003097 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3098 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003099}
3100
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003101/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003102/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003103static void handleInitPriorityAttr(Sema &S, Decl *D,
3104 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003105 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003106 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3107 return;
3108 }
3109
Chandler Carruth87c44602011-07-01 23:49:12 +00003110 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003111 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3112 Attr.setInvalid();
3113 return;
3114 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003115 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003116 if (S.Context.getAsArrayType(T))
3117 T = S.Context.getBaseElementType(T);
3118 if (!T->getAs<RecordType>()) {
3119 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3120 Attr.setInvalid();
3121 return;
3122 }
3123
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003124 if (Attr.getNumArgs() != 1) {
3125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3126 Attr.setInvalid();
3127 return;
3128 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003129 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003130
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003131 llvm::APSInt priority(32);
3132 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3133 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3134 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3135 << "init_priority" << priorityExpr->getSourceRange();
3136 Attr.setInvalid();
3137 return;
3138 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003139 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003140 if (prioritynum < 101 || prioritynum > 65535) {
3141 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3142 << priorityExpr->getSourceRange();
3143 Attr.setInvalid();
3144 return;
3145 }
Michael Han51d8c522013-01-24 16:46:58 +00003146 D->addAttr(::new (S.Context)
3147 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3148 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003149}
3150
Rafael Espindola599f1b72012-05-13 03:25:18 +00003151FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003152 int FormatIdx, int FirstArg,
3153 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003154 // Check whether we already have an equivalent format attribute.
3155 for (specific_attr_iterator<FormatAttr>
3156 i = D->specific_attr_begin<FormatAttr>(),
3157 e = D->specific_attr_end<FormatAttr>();
3158 i != e ; ++i) {
3159 FormatAttr *f = *i;
3160 if (f->getType() == Format &&
3161 f->getFormatIdx() == FormatIdx &&
3162 f->getFirstArg() == FirstArg) {
3163 // If we don't have a valid location for this attribute, adopt the
3164 // location.
3165 if (f->getLocation().isInvalid())
3166 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003167 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003168 }
3169 }
3170
Michael Han51d8c522013-01-24 16:46:58 +00003171 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3172 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003173}
3174
Mike Stumpbf916502009-07-24 19:02:52 +00003175/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003176/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003177static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003178
Chris Lattner545dd342008-06-28 23:36:30 +00003179 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00003181 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003182 return;
3183 }
3184
Chris Lattner545dd342008-06-28 23:36:30 +00003185 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003187 return;
3188 }
3189
Chandler Carruth87c44602011-07-01 23:49:12 +00003190 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003192 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003193 return;
3194 }
3195
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003196 // In C++ the implicit 'this' function parameter also counts, and they are
3197 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003198 bool HasImplicitThisParam = isInstanceMethod(D);
3199 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003200 unsigned FirstIdx = 1;
3201
Chris Lattner5f9e2722011-07-23 10:55:15 +00003202 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003203
3204 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003205 if (Format.startswith("__") && Format.endswith("__"))
3206 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003207
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003208 // Check for supported formats.
3209 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003210
3211 if (Kind == IgnoredFormat)
3212 return;
3213
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003214 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003215 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003216 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003217 return;
3218 }
3219
3220 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003221 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003222 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003223 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3224 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003226 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003227 return;
3228 }
3229
3230 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003232 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003233 return;
3234 }
3235
3236 // FIXME: Do we need to bounds check?
3237 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003238
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003239 if (HasImplicitThisParam) {
3240 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003241 S.Diag(Attr.getLoc(),
3242 diag::err_format_attribute_implicit_this_format_string)
3243 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003244 return;
3245 }
3246 ArgIdx--;
3247 }
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Chris Lattner6b6b5372008-06-26 18:38:35 +00003249 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003250 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003251
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003252 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003253 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003254 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3255 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003256 return;
3257 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003258 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003259 // FIXME: do we need to check if the type is NSString*? What are the
3260 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003261 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003262 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003263 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3264 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003265 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003266 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003267 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003268 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003269 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003270 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3271 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003272 return;
3273 }
3274
3275 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003276 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003277 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003278 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3279 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003280 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003281 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003282 return;
3283 }
3284
3285 // check if the function is variadic if the 3rd argument non-zero
3286 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003287 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003288 ++NumArgs; // +1 for ...
3289 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003290 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003291 return;
3292 }
3293 }
3294
Chris Lattner3c73c412008-11-19 08:23:25 +00003295 // strftime requires FirstArg to be 0 because it doesn't read from any
3296 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003297 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003298 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003299 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3300 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003301 return;
3302 }
3303 // if 0 it disables parameter checking (to use with e.g. va_list)
3304 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003305 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003306 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307 return;
3308 }
3309
Rafael Espindola599f1b72012-05-13 03:25:18 +00003310 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3311 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003312 FirstArg.getZExtValue(),
3313 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003314 if (NewAttr)
3315 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003316}
3317
Chandler Carruth1b03c872011-07-02 00:01:44 +00003318static void handleTransparentUnionAttr(Sema &S, Decl *D,
3319 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003320 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003321 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003322 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003323
Chris Lattner6b6b5372008-06-26 18:38:35 +00003324
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003325 // Try to find the underlying union declaration.
3326 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003327 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003328 if (TD && TD->getUnderlyingType()->isUnionType())
3329 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3330 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003331 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003332
3333 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003334 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003335 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003336 return;
3337 }
3338
John McCall5e1cdac2011-10-07 06:10:15 +00003339 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003340 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003341 diag::warn_transparent_union_attribute_not_definition);
3342 return;
3343 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003344
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003345 RecordDecl::field_iterator Field = RD->field_begin(),
3346 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003347 if (Field == FieldEnd) {
3348 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3349 return;
3350 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003351
David Blaikie581deb32012-06-06 20:45:41 +00003352 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003353 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003354 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003355 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003356 diag::warn_transparent_union_attribute_floating)
3357 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003358 return;
3359 }
3360
3361 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3362 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3363 for (; Field != FieldEnd; ++Field) {
3364 QualType FieldType = Field->getType();
3365 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3366 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3367 // Warn if we drop the attribute.
3368 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003369 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003370 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003371 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003372 diag::warn_transparent_union_attribute_field_size_align)
3373 << isSize << Field->getDeclName() << FieldBits;
3374 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003375 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003376 diag::note_transparent_union_first_field_size_align)
3377 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003378 return;
3379 }
3380 }
3381
Michael Han51d8c522013-01-24 16:46:58 +00003382 RD->addAttr(::new (S.Context)
3383 TransparentUnionAttr(Attr.getRange(), S.Context,
3384 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003385}
3386
Chandler Carruth1b03c872011-07-02 00:01:44 +00003387static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003388 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003389 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003390 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003391
Peter Collingbourne7a730022010-11-23 20:45:58 +00003392 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003393 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003394
Chris Lattner6b6b5372008-06-26 18:38:35 +00003395 // Make sure that there is a string literal as the annotation's single
3396 // argument.
3397 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003398 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003399 return;
3400 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003401
3402 // Don't duplicate annotations that are already set.
3403 for (specific_attr_iterator<AnnotateAttr>
3404 i = D->specific_attr_begin<AnnotateAttr>(),
3405 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3406 if ((*i)->getAnnotation() == SE->getString())
3407 return;
3408 }
Michael Han51d8c522013-01-24 16:46:58 +00003409
3410 D->addAttr(::new (S.Context)
3411 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3412 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003413}
3414
Chandler Carruth1b03c872011-07-02 00:01:44 +00003415static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003416 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003417 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003418 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003419 return;
3420 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003421
Richard Smithbe507b62013-02-01 08:12:08 +00003422 if (Attr.getNumArgs() == 0) {
3423 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3424 true, 0, Attr.getAttributeSpellingListIndex()));
3425 return;
3426 }
3427
Richard Smithf6565a92013-02-22 08:32:16 +00003428 Expr *E = Attr.getArg(0);
3429 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3430 S.Diag(Attr.getEllipsisLoc(),
3431 diag::err_pack_expansion_without_parameter_packs);
3432 return;
3433 }
3434
3435 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3436 return;
3437
3438 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3439 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003440}
3441
3442void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003443 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003444 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3445 SourceLocation AttrLoc = AttrRange.getBegin();
3446
Richard Smith4cd81c52013-01-29 09:02:09 +00003447 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003448 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003449 // C++11 [dcl.align]p1:
3450 // An alignment-specifier may be applied to a variable or to a class
3451 // data member, but it shall not be applied to a bit-field, a function
3452 // parameter, the formal parameter of a catch clause, or a variable
3453 // declared with the register storage class specifier. An
3454 // alignment-specifier may also be applied to the declaration of a class
3455 // or enumeration type.
3456 // C11 6.7.5/2:
3457 // An alignment attribute shall not be specified in a declaration of
3458 // a typedef, or a bit-field, or a function, or a parameter, or an
3459 // object declared with the register storage-class specifier.
3460 int DiagKind = -1;
3461 if (isa<ParmVarDecl>(D)) {
3462 DiagKind = 0;
3463 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3464 if (VD->getStorageClass() == SC_Register)
3465 DiagKind = 1;
3466 if (VD->isExceptionVariable())
3467 DiagKind = 2;
3468 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3469 if (FD->isBitField())
3470 DiagKind = 3;
3471 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003472 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3473 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003474 << (TmpAttr.isC11() ? ExpectedVariableOrField
3475 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003476 return;
3477 }
3478 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003479 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003480 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003481 return;
3482 }
3483 }
3484
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003485 if (E->isTypeDependent() || E->isValueDependent()) {
3486 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003487 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3488 AA->setPackExpansion(IsPackExpansion);
3489 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003490 return;
3491 }
Michael Hana31f65b2013-02-01 01:19:17 +00003492
Sean Huntcf807c42010-08-18 23:23:40 +00003493 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003494 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003495 ExprResult ICE
3496 = VerifyIntegerConstantExpression(E, &Alignment,
3497 diag::err_aligned_attribute_argument_not_int,
3498 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003499 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003500 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003501
3502 // C++11 [dcl.align]p2:
3503 // -- if the constant expression evaluates to zero, the alignment
3504 // specifier shall have no effect
3505 // C11 6.7.5p6:
3506 // An alignment specification of zero has no effect.
3507 if (!(TmpAttr.isAlignas() && !Alignment) &&
3508 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003509 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3510 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003511 return;
3512 }
Michael Hana31f65b2013-02-01 01:19:17 +00003513
Richard Smithbe507b62013-02-01 08:12:08 +00003514 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003515 // We've already verified it's a power of 2, now let's make sure it's
3516 // 8192 or less.
3517 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003518 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003519 << E->getSourceRange();
3520 return;
3521 }
3522 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003523
Richard Smithf6565a92013-02-22 08:32:16 +00003524 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3525 ICE.take(), SpellingListIndex);
3526 AA->setPackExpansion(IsPackExpansion);
3527 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003528}
3529
Michael Hana31f65b2013-02-01 01:19:17 +00003530void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003531 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003532 // FIXME: Cache the number on the Attr object if non-dependent?
3533 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003534 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3535 SpellingListIndex);
3536 AA->setPackExpansion(IsPackExpansion);
3537 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003538}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003539
Richard Smithbe507b62013-02-01 08:12:08 +00003540void Sema::CheckAlignasUnderalignment(Decl *D) {
3541 assert(D->hasAttrs() && "no attributes on decl");
3542
3543 QualType Ty;
3544 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3545 Ty = VD->getType();
3546 else
3547 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003548 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003549 return;
3550
3551 // C++11 [dcl.align]p5, C11 6.7.5/4:
3552 // The combined effect of all alignment attributes in a declaration shall
3553 // not specify an alignment that is less strict than the alignment that
3554 // would otherwise be required for the entity being declared.
3555 AlignedAttr *AlignasAttr = 0;
3556 unsigned Align = 0;
3557 for (specific_attr_iterator<AlignedAttr>
3558 I = D->specific_attr_begin<AlignedAttr>(),
3559 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3560 if (I->isAlignmentDependent())
3561 return;
3562 if (I->isAlignas())
3563 AlignasAttr = *I;
3564 Align = std::max(Align, I->getAlignment(Context));
3565 }
3566
3567 if (AlignasAttr && Align) {
3568 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3569 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3570 if (NaturalAlign > RequestedAlign)
3571 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3572 << Ty << (unsigned)NaturalAlign.getQuantity();
3573 }
3574}
3575
Chandler Carruthd309c812011-07-01 23:49:16 +00003576/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003577/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003578///
Mike Stumpbf916502009-07-24 19:02:52 +00003579/// Despite what would be logical, the mode attribute is a decl attribute, not a
3580/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3581/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003582static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003583 // This attribute isn't documented, but glibc uses it. It changes
3584 // the width of an int or unsigned int to the specified size.
3585
3586 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003587 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003588 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003589
Chris Lattnerfbf13472008-06-27 22:18:37 +00003590
3591 IdentifierInfo *Name = Attr.getParameterName();
3592 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003593 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003594 return;
3595 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003596
Chris Lattner5f9e2722011-07-23 10:55:15 +00003597 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003598
3599 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003600 if (Str.startswith("__") && Str.endswith("__"))
3601 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003602
3603 unsigned DestWidth = 0;
3604 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003605 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003606 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003607 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003608 switch (Str[0]) {
3609 case 'Q': DestWidth = 8; break;
3610 case 'H': DestWidth = 16; break;
3611 case 'S': DestWidth = 32; break;
3612 case 'D': DestWidth = 64; break;
3613 case 'X': DestWidth = 96; break;
3614 case 'T': DestWidth = 128; break;
3615 }
3616 if (Str[1] == 'F') {
3617 IntegerMode = false;
3618 } else if (Str[1] == 'C') {
3619 IntegerMode = false;
3620 ComplexMode = true;
3621 } else if (Str[1] != 'I') {
3622 DestWidth = 0;
3623 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003624 break;
3625 case 4:
3626 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3627 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003628 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003629 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003630 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003631 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003632 break;
3633 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003634 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003635 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003636 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003637 case 11:
3638 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003639 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003640 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003641 }
3642
3643 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003644 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003645 OldTy = TD->getUnderlyingType();
3646 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3647 OldTy = VD->getType();
3648 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003649 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003650 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003651 return;
3652 }
Eli Friedman73397492009-03-03 06:41:03 +00003653
John McCall183700f2009-09-21 23:43:11 +00003654 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003655 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3656 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003657 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003658 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3659 } else if (ComplexMode) {
3660 if (!OldTy->isComplexType())
3661 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3662 } else {
3663 if (!OldTy->isFloatingType())
3664 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3665 }
3666
Mike Stump390b4cc2009-05-16 07:39:55 +00003667 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3668 // and friends, at least with glibc.
3669 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3670 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003671 // FIXME: Make sure floating-point mappings are accurate
3672 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003673 QualType NewTy;
3674 switch (DestWidth) {
3675 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003676 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003677 return;
3678 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003679 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003680 return;
3681 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003682 if (!IntegerMode) {
3683 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3684 return;
3685 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003686 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003687 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003688 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003689 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003690 break;
3691 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003692 if (!IntegerMode) {
3693 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3694 return;
3695 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003696 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003697 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003698 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003699 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003700 break;
3701 case 32:
3702 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003703 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003704 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003705 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003706 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003707 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003708 break;
3709 case 64:
3710 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003711 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003712 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003713 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003714 NewTy = S.Context.LongTy;
3715 else
3716 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003717 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003718 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003719 NewTy = S.Context.UnsignedLongTy;
3720 else
3721 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003722 break;
Eli Friedman73397492009-03-03 06:41:03 +00003723 case 96:
3724 NewTy = S.Context.LongDoubleTy;
3725 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003726 case 128:
3727 if (!IntegerMode) {
3728 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3729 return;
3730 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003731 if (OldTy->isSignedIntegerType())
3732 NewTy = S.Context.Int128Ty;
3733 else
3734 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003735 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003736 }
3737
Eli Friedman73397492009-03-03 06:41:03 +00003738 if (ComplexMode) {
3739 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003740 }
3741
3742 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003743 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003744 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003745 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003746 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003747 cast<ValueDecl>(D)->setType(NewTy);
3748}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003749
Chandler Carruth1b03c872011-07-02 00:01:44 +00003750static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003751 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003752 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003753 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003754
Nick Lewycky78d1a102012-07-24 01:40:49 +00003755 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3756 if (!VD->hasGlobalStorage())
3757 S.Diag(Attr.getLoc(),
3758 diag::warn_attribute_requires_functions_or_static_globals)
3759 << Attr.getName();
3760 } else if (!isFunctionOrMethod(D)) {
3761 S.Diag(Attr.getLoc(),
3762 diag::warn_attribute_requires_functions_or_static_globals)
3763 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003764 return;
3765 }
Mike Stumpbf916502009-07-24 19:02:52 +00003766
Michael Han51d8c522013-01-24 16:46:58 +00003767 D->addAttr(::new (S.Context)
3768 NoDebugAttr(Attr.getRange(), S.Context,
3769 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003770}
3771
Chandler Carruth1b03c872011-07-02 00:01:44 +00003772static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003773 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003774 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003775 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003776
Mike Stumpbf916502009-07-24 19:02:52 +00003777
Chandler Carruth87c44602011-07-01 23:49:12 +00003778 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003780 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003781 return;
3782 }
Mike Stumpbf916502009-07-24 19:02:52 +00003783
Michael Han51d8c522013-01-24 16:46:58 +00003784 D->addAttr(::new (S.Context)
3785 NoInlineAttr(Attr.getRange(), S.Context,
3786 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003787}
3788
Chandler Carruth1b03c872011-07-02 00:01:44 +00003789static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3790 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003791 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003792 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003793 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003794
Chris Lattner7255a2d2010-06-22 00:03:40 +00003795
Chandler Carruth87c44602011-07-01 23:49:12 +00003796 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003797 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003798 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003799 return;
3800 }
3801
Michael Han51d8c522013-01-24 16:46:58 +00003802 D->addAttr(::new (S.Context)
3803 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3804 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003805}
3806
Chandler Carruth1b03c872011-07-02 00:01:44 +00003807static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003808 if (S.LangOpts.CUDA) {
3809 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003810 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3812 return;
3813 }
3814
Chandler Carruth87c44602011-07-01 23:49:12 +00003815 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003816 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003817 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003818 return;
3819 }
3820
Michael Han51d8c522013-01-24 16:46:58 +00003821 D->addAttr(::new (S.Context)
3822 CUDAConstantAttr(Attr.getRange(), S.Context,
3823 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003824 } else {
3825 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3826 }
3827}
3828
Chandler Carruth1b03c872011-07-02 00:01:44 +00003829static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003830 if (S.LangOpts.CUDA) {
3831 // check the attribute arguments.
3832 if (Attr.getNumArgs() != 0) {
3833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3834 return;
3835 }
3836
Chandler Carruth87c44602011-07-01 23:49:12 +00003837 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003839 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003840 return;
3841 }
3842
Michael Han51d8c522013-01-24 16:46:58 +00003843 D->addAttr(::new (S.Context)
3844 CUDADeviceAttr(Attr.getRange(), S.Context,
3845 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003846 } else {
3847 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3848 }
3849}
3850
Chandler Carruth1b03c872011-07-02 00:01:44 +00003851static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003852 if (S.LangOpts.CUDA) {
3853 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003854 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003855 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003856
Chandler Carruth87c44602011-07-01 23:49:12 +00003857 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003859 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003860 return;
3861 }
3862
Chandler Carruth87c44602011-07-01 23:49:12 +00003863 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003864 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003865 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003866 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003867 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3868 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003869 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003870 "void");
3871 } else {
3872 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3873 << FD->getType();
3874 }
3875 return;
3876 }
3877
Michael Han51d8c522013-01-24 16:46:58 +00003878 D->addAttr(::new (S.Context)
3879 CUDAGlobalAttr(Attr.getRange(), S.Context,
3880 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003881 } else {
3882 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3883 }
3884}
3885
Chandler Carruth1b03c872011-07-02 00:01:44 +00003886static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003887 if (S.LangOpts.CUDA) {
3888 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003889 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003890 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003891
Peter Collingbourneced76712010-12-01 03:15:31 +00003892
Chandler Carruth87c44602011-07-01 23:49:12 +00003893 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003894 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003895 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003896 return;
3897 }
3898
Michael Han51d8c522013-01-24 16:46:58 +00003899 D->addAttr(::new (S.Context)
3900 CUDAHostAttr(Attr.getRange(), S.Context,
3901 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003902 } else {
3903 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3904 }
3905}
3906
Chandler Carruth1b03c872011-07-02 00:01:44 +00003907static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003908 if (S.LangOpts.CUDA) {
3909 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003910 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003911 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003912
Chandler Carruth87c44602011-07-01 23:49:12 +00003913 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003914 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003915 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003916 return;
3917 }
3918
Michael Han51d8c522013-01-24 16:46:58 +00003919 D->addAttr(::new (S.Context)
3920 CUDASharedAttr(Attr.getRange(), S.Context,
3921 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003922 } else {
3923 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3924 }
3925}
3926
Chandler Carruth1b03c872011-07-02 00:01:44 +00003927static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003928 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003929 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003930 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003931
Chandler Carruth87c44602011-07-01 23:49:12 +00003932 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003933 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003934 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003935 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003936 return;
3937 }
Mike Stumpbf916502009-07-24 19:02:52 +00003938
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003939 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003940 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003941 return;
3942 }
Mike Stumpbf916502009-07-24 19:02:52 +00003943
Michael Han51d8c522013-01-24 16:46:58 +00003944 D->addAttr(::new (S.Context)
3945 GNUInlineAttr(Attr.getRange(), S.Context,
3946 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003947}
3948
Chandler Carruth1b03c872011-07-02 00:01:44 +00003949static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003950 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003951
Aaron Ballmanfff32482012-12-09 17:45:41 +00003952 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003953 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003954 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3955 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003956 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003957 return;
3958
Chandler Carruth87c44602011-07-01 23:49:12 +00003959 if (!isa<ObjCMethodDecl>(D)) {
3960 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3961 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003962 return;
3963 }
3964
Chandler Carruth87c44602011-07-01 23:49:12 +00003965 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003966 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003967 D->addAttr(::new (S.Context)
3968 FastCallAttr(Attr.getRange(), S.Context,
3969 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003970 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003971 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003972 D->addAttr(::new (S.Context)
3973 StdCallAttr(Attr.getRange(), S.Context,
3974 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003975 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003976 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003977 D->addAttr(::new (S.Context)
3978 ThisCallAttr(Attr.getRange(), S.Context,
3979 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003980 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003981 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003982 D->addAttr(::new (S.Context)
3983 CDeclAttr(Attr.getRange(), S.Context,
3984 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003985 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003986 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003987 D->addAttr(::new (S.Context)
3988 PascalAttr(Attr.getRange(), S.Context,
3989 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003990 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003991 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003992 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003993 switch (CC) {
3994 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003995 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003996 break;
3997 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003998 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003999 break;
4000 default:
4001 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004002 }
4003
Michael Han51d8c522013-01-24 16:46:58 +00004004 D->addAttr(::new (S.Context)
4005 PcsAttr(Attr.getRange(), S.Context, PCS,
4006 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004007 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004008 }
Derek Schuff263366f2012-10-16 22:30:41 +00004009 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004010 D->addAttr(::new (S.Context)
4011 PnaclCallAttr(Attr.getRange(), S.Context,
4012 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004013 return;
Guy Benyei38980082012-12-25 08:53:55 +00004014 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004015 D->addAttr(::new (S.Context)
4016 IntelOclBiccAttr(Attr.getRange(), S.Context,
4017 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004018 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004019
Abramo Bagnarae215f722010-04-30 13:10:51 +00004020 default:
4021 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004022 }
4023}
4024
Chandler Carruth1b03c872011-07-02 00:01:44 +00004025static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00004026 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004027 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004028}
4029
Guy Benyei1db70402013-03-24 13:58:12 +00004030static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4031 assert(!Attr.isInvalid());
4032
4033 Expr *E = Attr.getArg(0);
4034 llvm::APSInt ArgNum(32);
4035 if (E->isTypeDependent() || E->isValueDependent() ||
4036 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4037 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4038 << Attr.getName()->getName() << E->getSourceRange();
4039 return;
4040 }
4041
4042 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4043 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4044}
4045
Aaron Ballmanfff32482012-12-09 17:45:41 +00004046bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4047 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004048 if (attr.isInvalid())
4049 return true;
4050
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004051 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4052 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4053 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004054 attr.setInvalid();
4055 return true;
4056 }
4057
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004058 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4059 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004060 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004061 case AttributeList::AT_CDecl: CC = CC_C; break;
4062 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4063 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4064 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4065 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4066 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004067 Expr *Arg = attr.getArg(0);
4068 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004069 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004070 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4071 << "pcs" << 1;
4072 attr.setInvalid();
4073 return true;
4074 }
4075
Chris Lattner5f9e2722011-07-23 10:55:15 +00004076 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004077 if (StrRef == "aapcs") {
4078 CC = CC_AAPCS;
4079 break;
4080 } else if (StrRef == "aapcs-vfp") {
4081 CC = CC_AAPCS_VFP;
4082 break;
4083 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004084
4085 attr.setInvalid();
4086 Diag(attr.getLoc(), diag::err_invalid_pcs);
4087 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004088 }
Derek Schuff263366f2012-10-16 22:30:41 +00004089 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004090 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004091 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004092 }
4093
Aaron Ballman82bfa192012-10-02 14:26:08 +00004094 const TargetInfo &TI = Context.getTargetInfo();
4095 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4096 if (A == TargetInfo::CCCR_Warning) {
4097 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004098
4099 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4100 if (FD)
4101 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4102 TargetInfo::CCMT_NonMember;
4103 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004104 }
4105
John McCall711c52b2011-01-05 12:14:39 +00004106 return false;
4107}
4108
Chandler Carruth1b03c872011-07-02 00:01:44 +00004109static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004110 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004111
4112 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004113 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004114 return;
4115
Chandler Carruth87c44602011-07-01 23:49:12 +00004116 if (!isa<ObjCMethodDecl>(D)) {
4117 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4118 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004119 return;
4120 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004121
Michael Han51d8c522013-01-24 16:46:58 +00004122 D->addAttr(::new (S.Context)
4123 RegparmAttr(Attr.getRange(), S.Context, numParams,
4124 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004125}
4126
4127/// Checks a regparm attribute, returning true if it is ill-formed and
4128/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004129bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4130 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004131 return true;
4132
Chandler Carruth87c44602011-07-01 23:49:12 +00004133 if (Attr.getNumArgs() != 1) {
4134 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4135 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004136 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004137 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004138
Chandler Carruth87c44602011-07-01 23:49:12 +00004139 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004140 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004141 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004142 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004143 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004144 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004145 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004146 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004147 }
4148
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004149 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004150 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004151 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004152 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004153 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004154 }
4155
John McCall711c52b2011-01-05 12:14:39 +00004156 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004157 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004158 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004159 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004160 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004161 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004162 }
4163
John McCall711c52b2011-01-05 12:14:39 +00004164 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004165}
4166
Chandler Carruth1b03c872011-07-02 00:01:44 +00004167static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004168 if (S.LangOpts.CUDA) {
4169 // check the attribute arguments.
4170 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004171 // FIXME: 0 is not okay.
4172 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004173 return;
4174 }
4175
Chandler Carruth87c44602011-07-01 23:49:12 +00004176 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004178 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004179 return;
4180 }
4181
4182 Expr *MaxThreadsExpr = Attr.getArg(0);
4183 llvm::APSInt MaxThreads(32);
4184 if (MaxThreadsExpr->isTypeDependent() ||
4185 MaxThreadsExpr->isValueDependent() ||
4186 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4187 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4188 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4189 return;
4190 }
4191
4192 llvm::APSInt MinBlocks(32);
4193 if (Attr.getNumArgs() > 1) {
4194 Expr *MinBlocksExpr = Attr.getArg(1);
4195 if (MinBlocksExpr->isTypeDependent() ||
4196 MinBlocksExpr->isValueDependent() ||
4197 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4198 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4199 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4200 return;
4201 }
4202 }
4203
Michael Han51d8c522013-01-24 16:46:58 +00004204 D->addAttr(::new (S.Context)
4205 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4206 MaxThreads.getZExtValue(),
4207 MinBlocks.getZExtValue(),
4208 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004209 } else {
4210 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4211 }
4212}
4213
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004214static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4215 const AttributeList &Attr) {
4216 StringRef AttrName = Attr.getName()->getName();
4217 if (!Attr.getParameterName()) {
4218 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4219 << Attr.getName() << /* arg num = */ 1;
4220 return;
4221 }
4222
4223 if (Attr.getNumArgs() != 2) {
4224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4225 << /* required args = */ 3;
4226 return;
4227 }
4228
4229 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4230
4231 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4233 << Attr.getName() << ExpectedFunctionOrMethod;
4234 return;
4235 }
4236
4237 uint64_t ArgumentIdx;
4238 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4239 Attr.getLoc(), 2,
4240 Attr.getArg(0), ArgumentIdx))
4241 return;
4242
4243 uint64_t TypeTagIdx;
4244 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4245 Attr.getLoc(), 3,
4246 Attr.getArg(1), TypeTagIdx))
4247 return;
4248
4249 bool IsPointer = (AttrName == "pointer_with_type_tag");
4250 if (IsPointer) {
4251 // Ensure that buffer has a pointer type.
4252 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4253 if (!BufferTy->isPointerType()) {
4254 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004255 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004256 }
4257 }
4258
Michael Han51d8c522013-01-24 16:46:58 +00004259 D->addAttr(::new (S.Context)
4260 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4261 ArgumentIdx, TypeTagIdx, IsPointer,
4262 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004263}
4264
4265static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4266 const AttributeList &Attr) {
4267 IdentifierInfo *PointerKind = Attr.getParameterName();
4268 if (!PointerKind) {
4269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4270 << "type_tag_for_datatype" << 1;
4271 return;
4272 }
4273
4274 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4275
Michael Han51d8c522013-01-24 16:46:58 +00004276 D->addAttr(::new (S.Context)
4277 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4278 MatchingCType,
4279 Attr.getLayoutCompatible(),
4280 Attr.getMustBeNull(),
4281 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004282}
4283
Chris Lattner0744e5f2008-06-29 00:23:49 +00004284//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004285// Checker-specific attribute handlers.
4286//===----------------------------------------------------------------------===//
4287
John McCallc7ad3812011-01-25 03:31:58 +00004288static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004289 return type->isDependentType() ||
4290 type->isObjCObjectPointerType() ||
4291 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004292}
4293static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004294 return type->isDependentType() ||
4295 type->isPointerType() ||
4296 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004297}
4298
Chandler Carruth1b03c872011-07-02 00:01:44 +00004299static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004300 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004301 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004302 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004303 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004304 return;
4305 }
4306
4307 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004308 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004309 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4310 cf = false;
4311 } else {
4312 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4313 cf = true;
4314 }
4315
4316 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004317 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004318 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004319 return;
4320 }
4321
4322 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004323 param->addAttr(::new (S.Context)
4324 CFConsumedAttr(Attr.getRange(), S.Context,
4325 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004326 else
Michael Han51d8c522013-01-24 16:46:58 +00004327 param->addAttr(::new (S.Context)
4328 NSConsumedAttr(Attr.getRange(), S.Context,
4329 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004330}
4331
Chandler Carruth1b03c872011-07-02 00:01:44 +00004332static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4333 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004334 if (!isa<ObjCMethodDecl>(D)) {
4335 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004336 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004337 return;
4338 }
4339
Michael Han51d8c522013-01-24 16:46:58 +00004340 D->addAttr(::new (S.Context)
4341 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4342 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004343}
4344
Chandler Carruth1b03c872011-07-02 00:01:44 +00004345static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4346 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004347
John McCallc7ad3812011-01-25 03:31:58 +00004348 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004349
Chandler Carruth87c44602011-07-01 23:49:12 +00004350 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004351 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004352 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004353 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004354 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004355 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4356 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004357 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004358 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004359 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004360 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004361 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004362 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004363 return;
4364 }
Mike Stumpbf916502009-07-24 19:02:52 +00004365
John McCallc7ad3812011-01-25 03:31:58 +00004366 bool typeOK;
4367 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004368 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004369 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004370 case AttributeList::AT_NSReturnsAutoreleased:
4371 case AttributeList::AT_NSReturnsRetained:
4372 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004373 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4374 cf = false;
4375 break;
4376
Sean Hunt8e083e72012-06-19 23:57:03 +00004377 case AttributeList::AT_CFReturnsRetained:
4378 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004379 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4380 cf = true;
4381 break;
4382 }
4383
4384 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004385 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004386 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004387 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004388 }
Mike Stumpbf916502009-07-24 19:02:52 +00004389
Chandler Carruth87c44602011-07-01 23:49:12 +00004390 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004391 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004392 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004393 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004394 D->addAttr(::new (S.Context)
4395 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4396 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004397 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004398 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004399 D->addAttr(::new (S.Context)
4400 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4401 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004402 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004403 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004404 D->addAttr(::new (S.Context)
4405 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4406 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004407 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004408 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004409 D->addAttr(::new (S.Context)
4410 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4411 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004412 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004413 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004414 D->addAttr(::new (S.Context)
4415 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4416 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004417 return;
4418 };
4419}
4420
John McCalldc7c5ad2011-07-22 08:53:00 +00004421static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4422 const AttributeList &attr) {
4423 SourceLocation loc = attr.getLoc();
4424
4425 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4426
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004427 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004428 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004429 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004430 return;
4431 }
4432
4433 // Check that the method returns a normal pointer.
4434 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004435
4436 if (!resultType->isReferenceType() &&
4437 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004438 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4439 << SourceRange(loc)
4440 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4441
4442 // Drop the attribute.
4443 return;
4444 }
4445
Michael Han51d8c522013-01-24 16:46:58 +00004446 method->addAttr(::new (S.Context)
4447 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4448 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004449}
4450
Fariborz Jahanian84101132012-09-07 23:46:23 +00004451static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4452 const AttributeList &attr) {
4453 SourceLocation loc = attr.getLoc();
4454 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4455
4456 if (!method) {
4457 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4458 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4459 return;
4460 }
4461 DeclContext *DC = method->getDeclContext();
4462 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4463 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4464 << attr.getName() << 0;
4465 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4466 return;
4467 }
4468 if (method->getMethodFamily() == OMF_dealloc) {
4469 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4470 << attr.getName() << 1;
4471 return;
4472 }
4473
Michael Han51d8c522013-01-24 16:46:58 +00004474 method->addAttr(::new (S.Context)
4475 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4476 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004477}
4478
John McCall8dfac0b2011-09-30 05:12:12 +00004479/// Handle cf_audited_transfer and cf_unknown_transfer.
4480static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4481 if (!isa<FunctionDecl>(D)) {
4482 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004483 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004484 return;
4485 }
4486
Sean Hunt8e083e72012-06-19 23:57:03 +00004487 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004488
4489 // Check whether there's a conflicting attribute already present.
4490 Attr *Existing;
4491 if (IsAudited) {
4492 Existing = D->getAttr<CFUnknownTransferAttr>();
4493 } else {
4494 Existing = D->getAttr<CFAuditedTransferAttr>();
4495 }
4496 if (Existing) {
4497 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4498 << A.getName()
4499 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4500 << A.getRange() << Existing->getRange();
4501 return;
4502 }
4503
4504 // All clear; add the attribute.
4505 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004506 D->addAttr(::new (S.Context)
4507 CFAuditedTransferAttr(A.getRange(), S.Context,
4508 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004509 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004510 D->addAttr(::new (S.Context)
4511 CFUnknownTransferAttr(A.getRange(), S.Context,
4512 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004513 }
4514}
4515
John McCallfe98da02011-09-29 07:17:38 +00004516static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4517 const AttributeList &Attr) {
4518 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4519 if (!RD || RD->isUnion()) {
4520 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004521 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004522 }
4523
4524 IdentifierInfo *ParmName = Attr.getParameterName();
4525
4526 // In Objective-C, verify that the type names an Objective-C type.
4527 // We don't want to check this outside of ObjC because people sometimes
4528 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004529 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004530 // Check for an existing type with this name.
4531 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4532 Sema::LookupOrdinaryName);
4533 if (S.LookupName(R, Sc)) {
4534 NamedDecl *Target = R.getFoundDecl();
4535 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4536 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4537 S.Diag(Target->getLocStart(), diag::note_declared_at);
4538 }
4539 }
4540 }
4541
Michael Han51d8c522013-01-24 16:46:58 +00004542 D->addAttr(::new (S.Context)
4543 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4544 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004545}
4546
Chandler Carruth1b03c872011-07-02 00:01:44 +00004547static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4548 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004549 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004550
Chandler Carruth87c44602011-07-01 23:49:12 +00004551 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004552 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004553}
4554
Chandler Carruth1b03c872011-07-02 00:01:44 +00004555static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4556 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004557 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004558 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004559 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004560 return;
4561 }
4562
Chandler Carruth87c44602011-07-01 23:49:12 +00004563 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004564 QualType type = vd->getType();
4565
4566 if (!type->isDependentType() &&
4567 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004568 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004569 << type;
4570 return;
4571 }
4572
4573 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4574
4575 // If we have no lifetime yet, check the lifetime we're presumably
4576 // going to infer.
4577 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4578 lifetime = type->getObjCARCImplicitLifetime();
4579
4580 switch (lifetime) {
4581 case Qualifiers::OCL_None:
4582 assert(type->isDependentType() &&
4583 "didn't infer lifetime for non-dependent type?");
4584 break;
4585
4586 case Qualifiers::OCL_Weak: // meaningful
4587 case Qualifiers::OCL_Strong: // meaningful
4588 break;
4589
4590 case Qualifiers::OCL_ExplicitNone:
4591 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004592 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004593 << (lifetime == Qualifiers::OCL_Autoreleasing);
4594 break;
4595 }
4596
Chandler Carruth87c44602011-07-01 23:49:12 +00004597 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004598 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4599 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004600}
4601
Francois Pichet11542142010-12-19 06:50:37 +00004602//===----------------------------------------------------------------------===//
4603// Microsoft specific attribute handlers.
4604//===----------------------------------------------------------------------===//
4605
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004606// Check if MS extensions or some other language extensions are enabled. If
4607// not, issue a diagnostic that the given attribute is unused.
4608static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4609 bool OtherExtension = false) {
4610 if (S.LangOpts.MicrosoftExt || OtherExtension)
4611 return true;
4612 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4613 return false;
4614}
4615
Chandler Carruth1b03c872011-07-02 00:01:44 +00004616static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004617 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4618 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004619
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004620 // check the attribute arguments.
4621 if (!checkAttributeNumArgs(S, Attr, 1))
4622 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004623
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004624 Expr *Arg = Attr.getArg(0);
4625 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4626 if (!Str || !Str->isAscii()) {
4627 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4628 << "uuid" << 1;
4629 return;
4630 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004631
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004632 StringRef StrRef = Str->getString();
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004633
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004634 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4635 StrRef.back() == '}';
Francois Pichetd3d3be92010-12-20 01:41:49 +00004636
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004637 // Validate GUID length.
4638 if (IsCurly && StrRef.size() != 38) {
4639 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4640 return;
4641 }
4642 if (!IsCurly && StrRef.size() != 36) {
4643 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4644 return;
4645 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004646
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004647 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4648 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4649 StringRef::iterator I = StrRef.begin();
4650 if (IsCurly) // Skip the optional '{'
4651 ++I;
4652
4653 for (int i = 0; i < 36; ++i) {
4654 if (i == 8 || i == 13 || i == 18 || i == 23) {
4655 if (*I != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004656 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4657 return;
4658 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004659 } else if (!isHexDigit(*I)) {
4660 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4661 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004662 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004663 I++;
4664 }
Francois Pichet11542142010-12-19 06:50:37 +00004665
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004666 D->addAttr(::new (S.Context)
4667 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4668 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004669}
4670
John McCallc052dbb2012-05-22 21:28:12 +00004671static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004672 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004673 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004674
4675 AttributeList::Kind Kind = Attr.getKind();
4676 if (Kind == AttributeList::AT_SingleInheritance)
4677 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004678 ::new (S.Context)
4679 SingleInheritanceAttr(Attr.getRange(), S.Context,
4680 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004681 else if (Kind == AttributeList::AT_MultipleInheritance)
4682 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004683 ::new (S.Context)
4684 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4685 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004686 else if (Kind == AttributeList::AT_VirtualInheritance)
4687 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004688 ::new (S.Context)
4689 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4690 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004691}
4692
4693static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004694 if (!checkMicrosoftExt(S, Attr))
4695 return;
4696
4697 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004698 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004699 D->addAttr(
4700 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4701 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004702}
4703
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004704static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004705 if (!checkMicrosoftExt(S, Attr))
4706 return;
4707 D->addAttr(::new (S.Context)
4708 ForceInlineAttr(Attr.getRange(), S.Context,
4709 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004710}
4711
Reid Klecknera7225342013-05-20 14:02:37 +00004712static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4713 if (!checkMicrosoftExt(S, Attr))
4714 return;
4715 // Check linkage after possibly merging declaratinos. See
4716 // checkAttributesAfterMerging().
4717 D->addAttr(::new (S.Context)
4718 SelectAnyAttr(Attr.getRange(), S.Context,
4719 Attr.getAttributeSpellingListIndex()));
4720}
4721
Ted Kremenekb71368d2009-05-09 02:44:38 +00004722//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004723// Top Level Sema Entry Points
4724//===----------------------------------------------------------------------===//
4725
Chandler Carruth1b03c872011-07-02 00:01:44 +00004726static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4727 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004728 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004729 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4730 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4731 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004732 default:
4733 break;
4734 }
4735}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004736
Chandler Carruth1b03c872011-07-02 00:01:44 +00004737static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4738 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004739 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004740 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4741 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4742 case AttributeList::AT_IBOutletCollection:
4743 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004744 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_ObjCGC:
4746 case AttributeList::AT_VectorSize:
4747 case AttributeList::AT_NeonVectorType:
4748 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004749 case AttributeList::AT_Ptr32:
4750 case AttributeList::AT_Ptr64:
4751 case AttributeList::AT_SPtr:
4752 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004753 // Ignore these, these are type attributes, handled by
4754 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004755 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_CUDADevice:
4757 case AttributeList::AT_CUDAHost:
4758 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004759 // Ignore, this is a non-inheritable attribute, handled
4760 // by ProcessNonInheritableDeclAttr.
4761 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004762 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4763 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4764 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4765 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004766 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004767 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004768 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004769 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004770 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4771 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4772 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004773 handleDependencyAttr(S, scope, D, Attr);
4774 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004775 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4776 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4777 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004778 case AttributeList::AT_CXX11NoReturn:
4779 handleCXX11NoReturnAttr(S, D, Attr);
4780 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004781 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004782 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4783 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004784 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4785 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004786 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004787 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004788 case AttributeList::AT_MinSize:
4789 handleMinSizeAttr(S, D, Attr);
4790 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4792 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4793 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4794 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4795 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004796 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004797 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4799 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4800 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4801 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4802 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004803 case AttributeList::AT_ownership_returns:
4804 case AttributeList::AT_ownership_takes:
4805 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004806 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4808 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4809 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4810 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4811 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4812 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4813 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004814
Sean Hunt8e083e72012-06-19 23:57:03 +00004815 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004816 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004818 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004819
Sean Hunt8e083e72012-06-19 23:57:03 +00004820 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004821 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4822
Fariborz Jahanian84101132012-09-07 23:46:23 +00004823 case AttributeList::AT_ObjCRequiresSuper:
4824 handleObjCRequiresSuperAttr(S, D, Attr); break;
4825
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004827 handleNSBridgedAttr(S, scope, D, Attr); break;
4828
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 case AttributeList::AT_CFAuditedTransfer:
4830 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004831 handleCFTransferAttr(S, D, Attr); break;
4832
Ted Kremenekb71368d2009-05-09 02:44:38 +00004833 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004834 case AttributeList::AT_CFConsumed:
4835 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4836 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004837 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004838
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_NSReturnsAutoreleased:
4840 case AttributeList::AT_NSReturnsNotRetained:
4841 case AttributeList::AT_CFReturnsNotRetained:
4842 case AttributeList::AT_NSReturnsRetained:
4843 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004844 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004845
Tanya Lattner0df579e2012-07-09 22:06:01 +00004846 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004848 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004849
Joey Gouly37453b92013-03-08 09:42:32 +00004850 case AttributeList::AT_VecTypeHint:
4851 handleVecTypeHint(S, D, Attr); break;
4852
Joey Gouly96cead52013-03-14 09:54:43 +00004853 case AttributeList::AT_Endian:
4854 handleEndianAttr(S, D, Attr);
4855 break;
4856
Sean Hunt8e083e72012-06-19 23:57:03 +00004857 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004858 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004859
Sean Hunt8e083e72012-06-19 23:57:03 +00004860 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4861 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4862 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004863 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4864 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004865 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004866 handleArcWeakrefUnavailableAttr (S, D, Attr);
4867 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004869 handleObjCRootClassAttr(S, D, Attr);
4870 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004871 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004872 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004873 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4875 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004876 handleReturnsTwiceAttr(S, D, Attr);
4877 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004879 case AttributeList::AT_Visibility:
4880 handleVisibilityAttr(S, D, Attr, false);
4881 break;
4882 case AttributeList::AT_TypeVisibility:
4883 handleVisibilityAttr(S, D, Attr, true);
4884 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004885 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004886 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004887 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4888 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4889 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4890 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004891 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004892 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004893 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004894 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004895 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004896 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004897 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004898 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004899 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4900 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4901 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4902 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4903 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4904 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4905 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4906 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4907 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004908 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004909 // Just ignore
4910 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004911 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004912 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004913 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004914 case AttributeList::AT_StdCall:
4915 case AttributeList::AT_CDecl:
4916 case AttributeList::AT_FastCall:
4917 case AttributeList::AT_ThisCall:
4918 case AttributeList::AT_Pascal:
4919 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004920 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004921 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004922 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004923 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004924 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004925 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004926 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004927 case AttributeList::AT_OpenCLImageAccess:
4928 handleOpenCLImageAccessAttr(S, D, Attr);
4929 break;
John McCallc052dbb2012-05-22 21:28:12 +00004930
4931 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004932 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004933 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004934 handleMsStructAttr(S, D, Attr);
4935 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004936 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004937 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004938 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004939 case AttributeList::AT_SingleInheritance:
4940 case AttributeList::AT_MultipleInheritance:
4941 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004942 handleInheritanceAttr(S, D, Attr);
4943 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004944 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004945 handlePortabilityAttr(S, D, Attr);
4946 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004947 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004948 handleForceInlineAttr(S, D, Attr);
4949 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004950 case AttributeList::AT_SelectAny:
4951 handleSelectAnyAttr(S, D, Attr);
4952 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004953
4954 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004955 case AttributeList::AT_AssertExclusiveLock:
4956 handleAssertExclusiveLockAttr(S, D, Attr);
4957 break;
4958 case AttributeList::AT_AssertSharedLock:
4959 handleAssertSharedLockAttr(S, D, Attr);
4960 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004961 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004962 handleGuardedVarAttr(S, D, Attr);
4963 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004964 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004965 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004966 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004967 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004968 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004969 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004970 case AttributeList::AT_NoSanitizeAddress:
4971 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004972 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004973 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004974 handleNoThreadSafetyAnalysis(S, D, Attr);
4975 break;
4976 case AttributeList::AT_NoSanitizeThread:
4977 handleNoSanitizeThread(S, D, Attr);
4978 break;
4979 case AttributeList::AT_NoSanitizeMemory:
4980 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004981 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004982 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004983 handleLockableAttr(S, D, Attr);
4984 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004985 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004986 handleGuardedByAttr(S, D, Attr);
4987 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004988 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004989 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004990 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004991 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004992 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004993 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004994 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004995 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004996 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004997 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004998 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004999 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005000 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005001 handleLockReturnedAttr(S, D, Attr);
5002 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005003 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005004 handleLocksExcludedAttr(S, D, Attr);
5005 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005006 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005007 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005008 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005009 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005010 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005011 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005012 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005013 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005014 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005015 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005016 handleUnlockFunAttr(S, D, Attr);
5017 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005018 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005019 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005020 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005021 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005022 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005023 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005024
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005025 // Type safety attributes.
5026 case AttributeList::AT_ArgumentWithTypeTag:
5027 handleArgumentWithTypeTagAttr(S, D, Attr);
5028 break;
5029 case AttributeList::AT_TypeTagForDatatype:
5030 handleTypeTagForDatatypeAttr(S, D, Attr);
5031 break;
5032
Chris Lattner803d0802008-06-29 00:43:07 +00005033 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005034 // Ask target about the attribute.
5035 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5036 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005037 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5038 diag::warn_unhandled_ms_attribute_ignored :
5039 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005040 break;
5041 }
5042}
5043
Peter Collingbourne60700392011-01-21 02:08:45 +00005044/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5045/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00005046/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00005047static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5048 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00005049 bool NonInheritable, bool Inheritable,
5050 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005051 if (Attr.isInvalid())
5052 return;
5053
Richard Smithcd8ab512013-01-17 01:30:42 +00005054 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5055 // instead.
5056 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5057 return;
5058
Peter Collingbourne60700392011-01-21 02:08:45 +00005059 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005060 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005061
5062 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005063 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005064}
5065
Chris Lattner803d0802008-06-29 00:43:07 +00005066/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5067/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005068void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005069 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005070 bool NonInheritable, bool Inheritable,
5071 bool IncludeCXX11Attributes) {
5072 for (const AttributeList* l = AttrList; l; l = l->getNext())
5073 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5074 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005075
5076 // GCC accepts
5077 // static int a9 __attribute__((weakref));
5078 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005079 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005080 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005081 cast<NamedDecl>(D)->getNameAsString();
5082 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005083 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005084 }
5085}
5086
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005087// Annotation attributes are the only attributes allowed after an access
5088// specifier.
5089bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5090 const AttributeList *AttrList) {
5091 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005092 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005093 handleAnnotateAttr(*this, ASDecl, *l);
5094 } else {
5095 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5096 return true;
5097 }
5098 }
5099
5100 return false;
5101}
5102
John McCalle82247a2011-10-01 05:17:03 +00005103/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5104/// contains any decl attributes that we should warn about.
5105static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5106 for ( ; A; A = A->getNext()) {
5107 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005108 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005109 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5110
5111 if (A->getKind() == AttributeList::UnknownAttribute) {
5112 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5113 << A->getName() << A->getRange();
5114 } else {
5115 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5116 << A->getName() << A->getRange();
5117 }
5118 }
5119}
5120
5121/// checkUnusedDeclAttributes - Given a declarator which is not being
5122/// used to build a declaration, complain about any decl attributes
5123/// which might be lying around on it.
5124void Sema::checkUnusedDeclAttributes(Declarator &D) {
5125 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5126 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5127 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5128 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5129}
5130
Ryan Flynne25ff832009-07-30 03:15:39 +00005131/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005132/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005133NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5134 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005135 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005136 NamedDecl *NewD = 0;
5137 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005138 FunctionDecl *NewFD;
5139 // FIXME: Missing call to CheckFunctionDeclaration().
5140 // FIXME: Mangling?
5141 // FIXME: Is the qualifier info correct?
5142 // FIXME: Is the DeclContext correct?
5143 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5144 Loc, Loc, DeclarationName(II),
5145 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005146 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005147 FD->hasPrototype(),
5148 false/*isConstexprSpecified*/);
5149 NewD = NewFD;
5150
5151 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005152 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005153
5154 // Fake up parameter variables; they are declared as if this were
5155 // a typedef.
5156 QualType FDTy = FD->getType();
5157 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5158 SmallVector<ParmVarDecl*, 16> Params;
5159 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5160 AE = FT->arg_type_end(); AI != AE; ++AI) {
5161 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5162 Param->setScopeInfo(0, Params.size());
5163 Params.push_back(Param);
5164 }
David Blaikie4278c652011-09-21 18:16:56 +00005165 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005166 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005167 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5168 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005169 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005170 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005171 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005172 if (VD->getQualifier()) {
5173 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005174 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005175 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005176 }
5177 return NewD;
5178}
5179
James Dennett1dfbd922012-06-14 21:40:34 +00005180/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005181/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005182void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005183 if (W.getUsed()) return; // only do this once
5184 W.setUsed(true);
5185 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5186 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005187 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005188 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5189 NDId->getName()));
5190 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005191 WeakTopLevelDecl.push_back(NewD);
5192 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5193 // to insert Decl at TU scope, sorry.
5194 DeclContext *SavedContext = CurContext;
5195 CurContext = Context.getTranslationUnitDecl();
5196 PushOnScopeChains(NewD, S);
5197 CurContext = SavedContext;
5198 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005199 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005200 }
5201}
5202
Rafael Espindola65611bf2013-03-02 21:41:48 +00005203void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5204 // It's valid to "forward-declare" #pragma weak, in which case we
5205 // have to do this.
5206 LoadExternalWeakUndeclaredIdentifiers();
5207 if (!WeakUndeclaredIdentifiers.empty()) {
5208 NamedDecl *ND = NULL;
5209 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5210 if (VD->isExternC())
5211 ND = VD;
5212 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5213 if (FD->isExternC())
5214 ND = FD;
5215 if (ND) {
5216 if (IdentifierInfo *Id = ND->getIdentifier()) {
5217 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5218 = WeakUndeclaredIdentifiers.find(Id);
5219 if (I != WeakUndeclaredIdentifiers.end()) {
5220 WeakInfo W = I->second;
5221 DeclApplyPragmaWeak(S, ND, W);
5222 WeakUndeclaredIdentifiers[Id] = W;
5223 }
5224 }
5225 }
5226 }
5227}
5228
Chris Lattner0744e5f2008-06-29 00:23:49 +00005229/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5230/// it, apply them to D. This is a bit tricky because PD can have attributes
5231/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005232void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5233 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005234 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005235 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005236 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005237
Chris Lattner0744e5f2008-06-29 00:23:49 +00005238 // Walk the declarator structure, applying decl attributes that were in a type
5239 // position to the decl itself. This handles cases like:
5240 // int *__attr__(x)** D;
5241 // when X is a decl attribute.
5242 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5243 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005244 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5245 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005246
Chris Lattner0744e5f2008-06-29 00:23:49 +00005247 // Finally, apply any attributes on the decl itself.
5248 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005249 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005250}
John McCall54abf7d2009-11-04 02:18:39 +00005251
John McCallf85e1932011-06-15 23:02:42 +00005252/// Is the given declaration allowed to use a forbidden type?
5253static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5254 // Private ivars are always okay. Unfortunately, people don't
5255 // always properly make their ivars private, even in system headers.
5256 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005257 // Function declarations in sys headers will be marked unavailable.
5258 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5259 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005260 return false;
5261
5262 // Require it to be declared in a system header.
5263 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5264}
5265
5266/// Handle a delayed forbidden-type diagnostic.
5267static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5268 Decl *decl) {
5269 if (decl && isForbiddenTypeAllowed(S, decl)) {
5270 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5271 "this system declaration uses an unsupported type"));
5272 return;
5273 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005274 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005275 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005276 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005277 // kind of forbidden type messages on unavailable functions.
5278 if (FD->hasAttr<UnavailableAttr>() &&
5279 diag.getForbiddenTypeDiagnostic() ==
5280 diag::err_arc_array_param_no_ownership) {
5281 diag.Triggered = true;
5282 return;
5283 }
5284 }
John McCallf85e1932011-06-15 23:02:42 +00005285
5286 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5287 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5288 diag.Triggered = true;
5289}
5290
John McCall92576642012-05-07 06:16:41 +00005291void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5292 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005293 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005294 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005295
John McCall92576642012-05-07 06:16:41 +00005296 // When delaying diagnostics to run in the context of a parsed
5297 // declaration, we only want to actually emit anything if parsing
5298 // succeeds.
5299 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005300
John McCall92576642012-05-07 06:16:41 +00005301 // We emit all the active diagnostics in this pool or any of its
5302 // parents. In general, we'll get one pool for the decl spec
5303 // and a child pool for each declarator; in a decl group like:
5304 // deprecated_typedef foo, *bar, baz();
5305 // only the declarator pops will be passed decls. This is correct;
5306 // we really do need to consider delayed diagnostics from the decl spec
5307 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005308 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005309 do {
John McCall13489672012-05-07 06:16:58 +00005310 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005311 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5312 // This const_cast is a bit lame. Really, Triggered should be mutable.
5313 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005314 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005315 continue;
5316
John McCalleee1d542011-02-14 07:13:47 +00005317 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005318 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005319 // Don't bother giving deprecation diagnostics if the decl is invalid.
5320 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005321 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005322 break;
5323
5324 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005325 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005326 break;
John McCallf85e1932011-06-15 23:02:42 +00005327
5328 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005329 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005330 break;
John McCall2f514482010-01-27 03:50:35 +00005331 }
5332 }
John McCall92576642012-05-07 06:16:41 +00005333 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005334}
5335
John McCall13489672012-05-07 06:16:58 +00005336/// Given a set of delayed diagnostics, re-emit them as if they had
5337/// been delayed in the current context instead of in the given pool.
5338/// Essentially, this just moves them to the current pool.
5339void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5340 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5341 assert(curPool && "re-emitting in undelayed context not supported");
5342 curPool->steal(pool);
5343}
5344
John McCall54abf7d2009-11-04 02:18:39 +00005345static bool isDeclDeprecated(Decl *D) {
5346 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005347 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005348 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005349 // A category implicitly has the availability of the interface.
5350 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5351 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005352 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5353 return false;
5354}
5355
Eli Friedmanc3b23082012-08-08 21:52:41 +00005356static void
5357DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5358 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005359 const ObjCInterfaceDecl *UnknownObjCClass,
5360 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005361 DeclarationName Name = D->getDeclName();
5362 if (!Message.empty()) {
5363 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5364 S.Diag(D->getLocation(),
5365 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5366 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005367 if (ObjCPropery)
5368 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5369 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005370 } else if (!UnknownObjCClass) {
5371 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5372 S.Diag(D->getLocation(),
5373 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5374 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005375 if (ObjCPropery)
5376 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5377 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005378 } else {
5379 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5380 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5381 }
5382}
5383
John McCall9c3087b2010-08-26 02:13:20 +00005384void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005385 Decl *Ctx) {
5386 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005387 return;
5388
John McCall2f514482010-01-27 03:50:35 +00005389 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005390 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5391 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005392 DD.getUnknownObjCClass(),
5393 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005394}
5395
Chris Lattner5f9e2722011-07-23 10:55:15 +00005396void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005397 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005398 const ObjCInterfaceDecl *UnknownObjCClass,
5399 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005400 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005401 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005402 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5403 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005404 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005405 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005406 return;
5407 }
5408
5409 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005410 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005411 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005412 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005413}