blob: 9d73ca3e48dbf32e81288f2b427c4401fec1949e [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,
Aaron Ballman37a89532013-07-18 14:56:42 +000055 ExpectedTypeOrNamespace,
56 ExpectedObjectiveCInterface
John McCall883cc2c2011-03-02 12:29:23 +000057};
58
Chris Lattnere5c5ee12008-06-29 00:16:31 +000059//===----------------------------------------------------------------------===//
60// Helper functions
61//===----------------------------------------------------------------------===//
62
Chandler Carruth87c44602011-07-01 23:49:12 +000063static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000064 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000065 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000066 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000068 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000069 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000070 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000071 Ty = decl->getUnderlyingType();
72 else
73 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000074
Chris Lattner6b6b5372008-06-26 18:38:35 +000075 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000076 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000077 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000078 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000079
John McCall183700f2009-09-21 23:43:11 +000080 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000081}
82
Daniel Dunbar35682492008-09-26 04:12:28 +000083// FIXME: We should provide an abstraction around a method or function
84// to provide the following bits of information.
85
Nuno Lopesd20254f2009-12-20 23:11:08 +000086/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000087/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000088static bool isFunction(const Decl *D) {
89 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000090}
91
92/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000093/// type (function or function-typed variable) or an Objective-C
94/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000095static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000096 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000097}
98
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000099/// isFunctionOrMethodOrBlock - Return true if the given decl has function
100/// type (function or function-typed variable) or an Objective-C
101/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000102static bool isFunctionOrMethodOrBlock(const Decl *D) {
103 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000104 return true;
105 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000107 QualType Ty = V->getType();
108 return Ty->isBlockPointerType();
109 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000110 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000111}
112
John McCall711c52b2011-01-05 12:14:39 +0000113/// Return true if the given decl has a declarator that should have
114/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000116 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
118 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000119}
120
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000121/// hasFunctionProto - Return true if the given decl has a argument
122/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000123/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000124static bool hasFunctionProto(const Decl *D) {
125 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000126 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000127 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000129 return true;
130 }
131}
132
133/// getFunctionOrMethodNumArgs - Return number of function or method
134/// arguments. It is an error to call this on a K&R function (use
135/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000136static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
137 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000138 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000140 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000146 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000148 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000151}
152
Chandler Carruth87c44602011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodResultType(const Decl *D) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000155 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isFunctionOrMethodVariadic(const Decl *D) {
160 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000161 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000162 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000163 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000164 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000165 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000166 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000167 }
168}
169
Chandler Carruth87c44602011-07-01 23:49:12 +0000170static bool isInstanceMethod(const Decl *D) {
171 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000172 return MethodDecl->isInstance();
173 return false;
174}
175
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000177 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000178 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000179 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000180
John McCall506b57e2010-05-17 21:00:27 +0000181 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
182 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000184
John McCall506b57e2010-05-17 21:00:27 +0000185 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000186
Chris Lattner6b6b5372008-06-26 18:38:35 +0000187 // FIXME: Should we walk the chain of classes?
188 return ClsName == &Ctx.Idents.get("NSString") ||
189 ClsName == &Ctx.Idents.get("NSMutableString");
190}
191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000193 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 if (!PT)
195 return false;
196
Ted Kremenek6217b802009-07-29 21:53:49 +0000197 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000198 if (!RT)
199 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000201 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000202 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 return false;
204
205 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
206}
207
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000208/// \brief Check if the attribute has exactly as many args as Num. May
209/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000210static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
211 unsigned int Num) {
212 if (Attr.getNumArgs() != Num) {
213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
214 return false;
215 }
216
217 return true;
218}
219
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000220
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000221/// \brief Check if the attribute has at least as many args as Num. May
222/// output an error.
223static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
224 unsigned int Num) {
225 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000226 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
227 return false;
228 }
229
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000230 return true;
231}
232
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000233/// \brief Check if IdxExpr is a valid argument index for a function or
234/// instance method D. May output an error.
235///
236/// \returns true if IdxExpr is a valid index.
237static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
238 StringRef AttrName,
239 SourceLocation AttrLoc,
240 unsigned AttrArgNum,
241 const Expr *IdxExpr,
242 uint64_t &Idx)
243{
244 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
245
246 // In C++ the implicit 'this' function parameter also counts.
247 // Parameters are counted from one.
248 const bool HasImplicitThisParam = isInstanceMethod(D);
249 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
250 const unsigned FirstIdx = 1;
251
252 llvm::APSInt IdxInt;
253 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
254 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
255 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
256 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
257 return false;
258 }
259
260 Idx = IdxInt.getLimitedValue();
261 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
262 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
263 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
264 return false;
265 }
266 Idx--; // Convert to zero-based.
267 if (HasImplicitThisParam) {
268 if (Idx == 0) {
269 S.Diag(AttrLoc,
270 diag::err_attribute_invalid_implicit_this_argument)
271 << AttrName << IdxExpr->getSourceRange();
272 return false;
273 }
274 --Idx;
275 }
276
277 return true;
278}
279
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000280///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000281/// \brief Check if passed in Decl is a field or potentially shared global var
282/// \return true if the Decl is a field or potentially shared global variable
283///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000284static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000285 if (isa<FieldDecl>(D))
286 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000287 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000288 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000289
290 return false;
291}
292
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000293/// \brief Check if the passed-in expression is of type int or bool.
294static bool isIntOrBool(Expr *Exp) {
295 QualType QT = Exp->getType();
296 return QT->isBooleanType() || QT->isIntegerType();
297}
298
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000299
300// Check to see if the type is a smart pointer of some kind. We assume
301// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000302static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
303 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
304 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000305 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000306 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000307
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000308 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
309 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000310 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000311 return false;
312
313 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000314}
315
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000316/// \brief Check if passed in Decl is a pointer type.
317/// Note that this function may produce an error message.
318/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000319static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
320 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000321 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000322 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000323 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000324 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000325
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000326 if (const RecordType *RT = QT->getAs<RecordType>()) {
327 // If it's an incomplete type, it could be a smart pointer; skip it.
328 // (We don't want to force template instantiation if we can avoid it,
329 // since that would alter the order in which templates are instantiated.)
330 if (RT->isIncompleteType())
331 return true;
332
333 if (threadSafetyCheckIsSmartPointer(S, RT))
334 return true;
335 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000336
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000337 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000338 << Attr.getName()->getName() << QT;
339 } else {
340 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
341 << Attr.getName();
342 }
343 return false;
344}
345
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000346/// \brief Checks that the passed in QualType either is of RecordType or points
347/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000348static const RecordType *getRecordType(QualType QT) {
349 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000351
352 // Now check if we point to record type.
353 if (const PointerType *PT = QT->getAs<PointerType>())
354 return PT->getPointeeType()->getAs<RecordType>();
355
356 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000357}
358
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000359
Jordy Rosefad5de92012-05-08 03:27:22 +0000360static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
361 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000362 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
363 if (RT->getDecl()->getAttr<LockableAttr>())
364 return true;
365 return false;
366}
367
368
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000369/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000370/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000371static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
372 QualType Ty) {
373 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000374
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000375 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000376 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000377 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000378 << Attr.getName() << Ty.getAsString();
379 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000380 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000381
Michael Hanf1aae3b2012-08-03 17:40:43 +0000382 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000383 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000384 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000385
386 // Allow smart pointers to be used as lockable objects.
387 // FIXME -- Check the type that the smart pointer points to.
388 if (threadSafetyCheckIsSmartPointer(S, RT))
389 return;
390
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000391 // Check if the type is lockable.
392 RecordDecl *RD = RT->getDecl();
393 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000394 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000395
396 // Else check if any base classes are lockable.
397 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
398 CXXBasePaths BPaths(false, false);
399 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
400 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000401 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000402
403 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
404 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000405}
406
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000407/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000408/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000409/// \param Sidx The attribute argument index to start checking with.
410/// \param ParamIdxOk Whether an argument can be indexing into a function
411/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000413 const AttributeList &Attr,
414 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000415 int Sidx = 0,
416 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000417 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000418 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000419
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000420 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000421 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000422 Args.push_back(ArgExp);
423 continue;
424 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000425
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000426 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000427 if (StrLit->getLength() == 0 ||
428 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000429 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000430 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000431 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000432 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000433 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000434
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000435 // We allow constant strings to be used as a placeholder for expressions
436 // that are not valid C++ syntax, but warn that they are ignored.
437 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
438 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000439 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000440 continue;
441 }
442
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000443 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000444
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000445 // A pointer to member expression of the form &MyClass::mu is treated
446 // specially -- we need to look at the type of the member.
447 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
448 if (UOp->getOpcode() == UO_AddrOf)
449 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
450 if (DRE->getDecl()->isCXXInstanceMember())
451 ArgTy = DRE->getDecl()->getType();
452
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000453 // First see if we can just cast to record type, or point to record type.
454 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000455
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456 // Now check if we index into a record type function param.
457 if(!RT && ParamIdxOk) {
458 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
460 if(FD && IL) {
461 unsigned int NumParams = FD->getNumParams();
462 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000463 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
464 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
465 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000466 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
467 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000468 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000469 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000470 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000471 }
472 }
473
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000474 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000475
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000476 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000477 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000478}
479
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000480//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000481// Attribute Implementations
482//===----------------------------------------------------------------------===//
483
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000484// FIXME: All this manual attribute parsing code is gross. At the
485// least add some helper functions to check most argument patterns (#
486// and types of args).
487
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000488enum ThreadAttributeDeclKind {
489 ThreadExpectedFieldOrGlobalVar,
490 ThreadExpectedFunctionOrMethod,
491 ThreadExpectedClassOrStruct
492};
493
Michael Hanf1aae3b2012-08-03 17:40:43 +0000494static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000495 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000496 assert(!Attr.isInvalid());
497
498 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000499 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500
501 // D must be either a member field or global (potentially shared) variable.
502 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000503 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
504 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000505 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000506 }
507
Michael Handc691572012-07-23 18:48:41 +0000508 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000509}
510
Michael Handc691572012-07-23 18:48:41 +0000511static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
512 if (!checkGuardedVarAttrCommon(S, D, Attr))
513 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000514
Michael Han51d8c522013-01-24 16:46:58 +0000515 D->addAttr(::new (S.Context)
516 GuardedVarAttr(Attr.getRange(), S.Context,
517 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000518}
519
Michael Hanf1aae3b2012-08-03 17:40:43 +0000520static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000521 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000522 if (!checkGuardedVarAttrCommon(S, D, Attr))
523 return;
524
525 if (!threadSafetyCheckIsPointer(S, D, Attr))
526 return;
527
Michael Han51d8c522013-01-24 16:46:58 +0000528 D->addAttr(::new (S.Context)
529 PtGuardedVarAttr(Attr.getRange(), S.Context,
530 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000531}
532
Michael Hanf1aae3b2012-08-03 17:40:43 +0000533static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
534 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000535 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000536 assert(!Attr.isInvalid());
537
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000538 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000539 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540
541 // D must be either a member field or global (potentially shared) variable.
542 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000543 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
544 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000545 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000546 }
547
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000548 SmallVector<Expr*, 1> Args;
549 // check that all arguments are lockable objects
550 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
551 unsigned Size = Args.size();
552 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000553 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000554
Michael Handc691572012-07-23 18:48:41 +0000555 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000556
Michael Handc691572012-07-23 18:48:41 +0000557 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000558}
559
Michael Handc691572012-07-23 18:48:41 +0000560static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
561 Expr *Arg = 0;
562 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
563 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000564
Michael Handc691572012-07-23 18:48:41 +0000565 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
570 Expr *Arg = 0;
571 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
572 return;
573
574 if (!threadSafetyCheckIsPointer(S, D, Attr))
575 return;
576
577 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
578 S.Context, Arg));
579}
580
Michael Hanf1aae3b2012-08-03 17:40:43 +0000581static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000582 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000583 assert(!Attr.isInvalid());
584
585 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000586 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000587
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000588 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000589 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000590 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
591 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000592 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000593 }
594
Michael Handc691572012-07-23 18:48:41 +0000595 return true;
596}
597
598static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
599 if (!checkLockableAttrCommon(S, D, Attr))
600 return;
601
602 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
603}
604
Michael Hanf1aae3b2012-08-03 17:40:43 +0000605static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000606 const AttributeList &Attr) {
607 if (!checkLockableAttrCommon(S, D, Attr))
608 return;
609
Michael Han51d8c522013-01-24 16:46:58 +0000610 D->addAttr(::new (S.Context)
611 ScopedLockableAttr(Attr.getRange(), S.Context,
612 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000613}
614
Kostya Serebryany85aee962013-02-26 06:58:27 +0000615static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
616 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000617 assert(!Attr.isInvalid());
618
619 if (!checkAttributeNumArgs(S, Attr, 0))
620 return;
621
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000622 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000623 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
624 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000625 return;
626 }
627
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000628 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000629 S.Context));
630}
631
Kostya Serebryany85aee962013-02-26 06:58:27 +0000632static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000633 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000634 assert(!Attr.isInvalid());
635
636 if (!checkAttributeNumArgs(S, Attr, 0))
637 return;
638
639 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000641 << Attr.getName() << ExpectedFunctionOrMethod;
642 return;
643 }
644
Michael Han51d8c522013-01-24 16:46:58 +0000645 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000646 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
647 Attr.getAttributeSpellingListIndex()));
648}
649
650static void handleNoSanitizeMemory(Sema &S, Decl *D,
651 const AttributeList &Attr) {
652 assert(!Attr.isInvalid());
653
654 if (!checkAttributeNumArgs(S, Attr, 0))
655 return;
656
657 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
659 << Attr.getName() << ExpectedFunctionOrMethod;
660 return;
661 }
662
663 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
664 S.Context));
665}
666
667static void handleNoSanitizeThread(Sema &S, Decl *D,
668 const AttributeList &Attr) {
669 assert(!Attr.isInvalid());
670
671 if (!checkAttributeNumArgs(S, Attr, 0))
672 return;
673
674 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
676 << Attr.getName() << ExpectedFunctionOrMethod;
677 return;
678 }
679
680 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
681 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000682}
683
Michael Hanf1aae3b2012-08-03 17:40:43 +0000684static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
685 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000686 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000687 assert(!Attr.isInvalid());
688
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000689 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000690 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000691
692 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000693 ValueDecl *VD = dyn_cast<ValueDecl>(D);
694 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000695 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
696 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000697 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000698 }
699
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000700 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000701 QualType QT = VD->getType();
702 if (!QT->isDependentType()) {
703 const RecordType *RT = getRecordType(QT);
704 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000705 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000706 << Attr.getName();
707 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000708 }
709 }
710
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000711 // Check that all arguments are lockable objects.
712 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000713 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000714 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000715
Michael Handc691572012-07-23 18:48:41 +0000716 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000717}
718
Michael Hanf1aae3b2012-08-03 17:40:43 +0000719static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000720 const AttributeList &Attr) {
721 SmallVector<Expr*, 1> Args;
722 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
723 return;
724
725 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000726 D->addAttr(::new (S.Context)
727 AcquiredAfterAttr(Attr.getRange(), S.Context,
728 StartArg, Args.size(),
729 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000730}
731
Michael Hanf1aae3b2012-08-03 17:40:43 +0000732static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000733 const AttributeList &Attr) {
734 SmallVector<Expr*, 1> Args;
735 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
736 return;
737
738 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000739 D->addAttr(::new (S.Context)
740 AcquiredBeforeAttr(Attr.getRange(), S.Context,
741 StartArg, Args.size(),
742 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000743}
744
Michael Hanf1aae3b2012-08-03 17:40:43 +0000745static bool checkLockFunAttrCommon(Sema &S, Decl *D,
746 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000747 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000748 assert(!Attr.isInvalid());
749
750 // zero or more arguments ok
751
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000752 // check that the attribute is applied to a function
753 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000754 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
755 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000756 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000757 }
758
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000759 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000761
Michael Handc691572012-07-23 18:48:41 +0000762 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000763}
764
Michael Hanf1aae3b2012-08-03 17:40:43 +0000765static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000766 const AttributeList &Attr) {
767 SmallVector<Expr*, 1> Args;
768 if (!checkLockFunAttrCommon(S, D, Attr, Args))
769 return;
770
771 unsigned Size = Args.size();
772 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000773 D->addAttr(::new (S.Context)
774 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
775 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000776}
777
Michael Hanf1aae3b2012-08-03 17:40:43 +0000778static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000779 const AttributeList &Attr) {
780 SmallVector<Expr*, 1> Args;
781 if (!checkLockFunAttrCommon(S, D, Attr, Args))
782 return;
783
784 unsigned Size = Args.size();
785 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000786 D->addAttr(::new (S.Context)
787 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
788 StartArg, Size,
789 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000790}
791
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000792static void handleAssertSharedLockAttr(Sema &S, Decl *D,
793 const AttributeList &Attr) {
794 SmallVector<Expr*, 1> Args;
795 if (!checkLockFunAttrCommon(S, D, Attr, Args))
796 return;
797
798 unsigned Size = Args.size();
799 Expr **StartArg = Size == 0 ? 0 : &Args[0];
800 D->addAttr(::new (S.Context)
801 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
803}
804
805static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
806 const AttributeList &Attr) {
807 SmallVector<Expr*, 1> Args;
808 if (!checkLockFunAttrCommon(S, D, Attr, Args))
809 return;
810
811 unsigned Size = Args.size();
812 Expr **StartArg = Size == 0 ? 0 : &Args[0];
813 D->addAttr(::new (S.Context)
814 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
815 StartArg, Size,
816 Attr.getAttributeSpellingListIndex()));
817}
818
819
Michael Hanf1aae3b2012-08-03 17:40:43 +0000820static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
821 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000822 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000823 assert(!Attr.isInvalid());
824
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000825 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000826 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000827
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000828 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000829 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
830 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000831 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000832 }
833
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000834 if (!isIntOrBool(Attr.getArg(0))) {
835 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000836 << Attr.getName();
837 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 }
839
840 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000841 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000842
Michael Handc691572012-07-23 18:48:41 +0000843 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000844}
845
Michael Hanf1aae3b2012-08-03 17:40:43 +0000846static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000847 const AttributeList &Attr) {
848 SmallVector<Expr*, 2> Args;
849 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
850 return;
851
852 unsigned Size = Args.size();
853 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000854 D->addAttr(::new (S.Context)
855 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
856 Attr.getArg(0), StartArg, Size,
857 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000858}
859
Michael Hanf1aae3b2012-08-03 17:40:43 +0000860static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000861 const AttributeList &Attr) {
862 SmallVector<Expr*, 2> Args;
863 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
864 return;
865
866 unsigned Size = Args.size();
867 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000868 D->addAttr(::new (S.Context)
869 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
870 Attr.getArg(0), StartArg, Size,
871 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000872}
873
Michael Hanf1aae3b2012-08-03 17:40:43 +0000874static bool checkLocksRequiredCommon(Sema &S, Decl *D,
875 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000876 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000877 assert(!Attr.isInvalid());
878
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000879 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000880 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000881
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000882 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000883 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
884 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000885 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000886 }
887
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000888 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000889 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000890 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000891 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000892
Michael Handc691572012-07-23 18:48:41 +0000893 return true;
894}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000895
Michael Hanf1aae3b2012-08-03 17:40:43 +0000896static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000897 const AttributeList &Attr) {
898 SmallVector<Expr*, 1> Args;
899 if (!checkLocksRequiredCommon(S, D, Attr, Args))
900 return;
901
902 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000903 D->addAttr(::new (S.Context)
904 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
905 StartArg, Args.size(),
906 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000907}
908
Michael Hanf1aae3b2012-08-03 17:40:43 +0000909static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000910 const AttributeList &Attr) {
911 SmallVector<Expr*, 1> Args;
912 if (!checkLocksRequiredCommon(S, D, Attr, Args))
913 return;
914
915 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000916 D->addAttr(::new (S.Context)
917 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
918 StartArg, Args.size(),
919 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000920}
921
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000922static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000923 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000924 assert(!Attr.isInvalid());
925
926 // zero or more arguments ok
927
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000928 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000929 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
930 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000931 return;
932 }
933
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000934 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000935 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000936 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000937 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000938 Expr **StartArg = Size == 0 ? 0 : &Args[0];
939
Michael Han51d8c522013-01-24 16:46:58 +0000940 D->addAttr(::new (S.Context)
941 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
942 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000943}
944
945static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000946 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000947 assert(!Attr.isInvalid());
948
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000949 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000950 return;
951
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000952 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000953 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
954 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000955 return;
956 }
957
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000958 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000959 SmallVector<Expr*, 1> Args;
960 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
961 unsigned Size = Args.size();
962 if (Size == 0)
963 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000964
Michael Han51d8c522013-01-24 16:46:58 +0000965 D->addAttr(::new (S.Context)
966 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
967 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968}
969
970static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000971 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000972 assert(!Attr.isInvalid());
973
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000974 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000975 return;
976
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000977 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000978 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
979 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000980 return;
981 }
982
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000983 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000984 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000985 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000986 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000987 if (Size == 0)
988 return;
989 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000990
Michael Han51d8c522013-01-24 16:46:58 +0000991 D->addAttr(::new (S.Context)
992 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
993 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000994}
995
996
Chandler Carruth1b03c872011-07-02 00:01:44 +0000997static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
998 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +0000999 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1000 if (TD == 0) {
1001 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1002 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001003 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001004 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001005 }
Mike Stumpbf916502009-07-24 19:02:52 +00001006
Richard Smitha4fa9002013-01-13 02:11:23 +00001007 // Remember this typedef decl, we will need it later for diagnostics.
1008 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001009}
1010
Chandler Carruth1b03c872011-07-02 00:01:44 +00001011static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001012 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001013 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001014 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001015
Chandler Carruth87c44602011-07-01 23:49:12 +00001016 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001017 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001018 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001019 // If the alignment is less than or equal to 8 bits, the packed attribute
1020 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001021 if (!FD->getType()->isDependentType() &&
1022 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001023 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001024 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001025 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001026 else
Michael Han51d8c522013-01-24 16:46:58 +00001027 FD->addAttr(::new (S.Context)
1028 PackedAttr(Attr.getRange(), S.Context,
1029 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001030 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001031 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001032}
1033
Chandler Carruth1b03c872011-07-02 00:01:44 +00001034static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001035 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001036 RD->addAttr(::new (S.Context)
1037 MsStructAttr(Attr.getRange(), S.Context,
1038 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001039 else
1040 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1041}
1042
Chandler Carruth1b03c872011-07-02 00:01:44 +00001043static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001044 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001045 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001046 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001047
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001048 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001049 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001050 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001051 D->addAttr(::new (S.Context)
1052 IBActionAttr(Attr.getRange(), S.Context,
1053 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001054 return;
1055 }
1056
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001057 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001058}
1059
Ted Kremenek2f041d02011-09-29 07:02:25 +00001060static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1061 // The IBOutlet/IBOutletCollection attributes only apply to instance
1062 // variables or properties of Objective-C classes. The outlet must also
1063 // have an object reference type.
1064 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1065 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001066 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001067 << Attr.getName() << VD->getType() << 0;
1068 return false;
1069 }
1070 }
1071 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1072 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001073 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001074 << Attr.getName() << PD->getType() << 1;
1075 return false;
1076 }
1077 }
1078 else {
1079 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1080 return false;
1081 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001082
Ted Kremenek2f041d02011-09-29 07:02:25 +00001083 return true;
1084}
1085
Chandler Carruth1b03c872011-07-02 00:01:44 +00001086static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001087 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001088 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001089 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001090
1091 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001092 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001093
Michael Han51d8c522013-01-24 16:46:58 +00001094 D->addAttr(::new (S.Context)
1095 IBOutletAttr(Attr.getRange(), S.Context,
1096 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001097}
1098
Chandler Carruth1b03c872011-07-02 00:01:44 +00001099static void handleIBOutletCollection(Sema &S, Decl *D,
1100 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001101
1102 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001103 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001104 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1105 return;
1106 }
1107
Ted Kremenek2f041d02011-09-29 07:02:25 +00001108 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001109 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001110
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001111 IdentifierInfo *II = Attr.getParameterName();
1112 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001113 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001114
John McCallb3d87482010-08-24 05:47:05 +00001115 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001116 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001117 if (!TypeRep) {
1118 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1119 return;
1120 }
John McCallb3d87482010-08-24 05:47:05 +00001121 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001122 // Diagnose use of non-object type in iboutletcollection attribute.
1123 // FIXME. Gnu attribute extension ignores use of builtin types in
1124 // attributes. So, __attribute__((iboutletcollection(char))) will be
1125 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001126 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001127 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1128 return;
1129 }
Michael Han51d8c522013-01-24 16:46:58 +00001130 D->addAttr(::new (S.Context)
1131 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1132 QT, Attr.getParameterLoc(),
1133 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001134}
1135
Chandler Carruthd309c812011-07-01 23:49:16 +00001136static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001137 if (const RecordType *UT = T->getAsUnionType())
1138 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1139 RecordDecl *UD = UT->getDecl();
1140 for (RecordDecl::field_iterator it = UD->field_begin(),
1141 itend = UD->field_end(); it != itend; ++it) {
1142 QualType QT = it->getType();
1143 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1144 T = QT;
1145 return;
1146 }
1147 }
1148 }
1149}
1150
Nuno Lopes587de5b2012-05-24 00:22:00 +00001151static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001152 if (!isFunctionOrMethod(D)) {
1153 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1154 << "alloc_size" << ExpectedFunctionOrMethod;
1155 return;
1156 }
1157
Nuno Lopes587de5b2012-05-24 00:22:00 +00001158 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1159 return;
1160
1161 // In C++ the implicit 'this' function parameter also counts, and they are
1162 // counted from one.
1163 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidis28965bf2013-02-22 06:58:28 +00001164 unsigned NumArgs;
1165 if (hasFunctionProto(D))
1166 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1167 else
1168 NumArgs = 0;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001169
1170 SmallVector<unsigned, 8> SizeArgs;
1171
1172 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1173 E = Attr.arg_end(); I!=E; ++I) {
1174 // The argument must be an integer constant expression.
1175 Expr *Ex = *I;
1176 llvm::APSInt ArgNum;
1177 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1178 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1179 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1180 << "alloc_size" << Ex->getSourceRange();
1181 return;
1182 }
1183
1184 uint64_t x = ArgNum.getZExtValue();
1185
1186 if (x < 1 || x > NumArgs) {
1187 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1188 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1189 return;
1190 }
1191
1192 --x;
1193 if (HasImplicitThisParam) {
1194 if (x == 0) {
1195 S.Diag(Attr.getLoc(),
1196 diag::err_attribute_invalid_implicit_this_argument)
1197 << "alloc_size" << Ex->getSourceRange();
1198 return;
1199 }
1200 --x;
1201 }
1202
1203 // check if the function argument is of an integer type
1204 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1205 if (!T->isIntegerType()) {
1206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1207 << "alloc_size" << Ex->getSourceRange();
1208 return;
1209 }
1210
Nuno Lopes587de5b2012-05-24 00:22:00 +00001211 SizeArgs.push_back(x);
1212 }
1213
1214 // check if the function returns a pointer
1215 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1216 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1217 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1218 }
1219
Michael Han51d8c522013-01-24 16:46:58 +00001220 D->addAttr(::new (S.Context)
1221 AllocSizeAttr(Attr.getRange(), S.Context,
1222 SizeArgs.data(), SizeArgs.size(),
1223 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001224}
1225
Chandler Carruth1b03c872011-07-02 00:01:44 +00001226static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001227 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1228 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001229 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001230 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001231 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001232 return;
1233 }
Mike Stumpbf916502009-07-24 19:02:52 +00001234
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001235 // In C++ the implicit 'this' function parameter also counts, and they are
1236 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001237 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001238 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001239
1240 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001241 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001242
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001243 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1244 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001245 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001246 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001247 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001248 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1249 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1251 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001252 return;
1253 }
Mike Stumpbf916502009-07-24 19:02:52 +00001254
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001255 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001256
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001257 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001259 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001260 return;
1261 }
Mike Stumpbf916502009-07-24 19:02:52 +00001262
Ted Kremenek465172f2008-07-21 22:09:15 +00001263 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001264 if (HasImplicitThisParam) {
1265 if (x == 0) {
1266 S.Diag(Attr.getLoc(),
1267 diag::err_attribute_invalid_implicit_this_argument)
1268 << "nonnull" << Ex->getSourceRange();
1269 return;
1270 }
1271 --x;
1272 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001273
1274 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001275 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001276 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001277
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001278 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001279 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001280 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001281 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001282 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001283 }
Mike Stumpbf916502009-07-24 19:02:52 +00001284
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001285 NonNullArgs.push_back(x);
1286 }
Mike Stumpbf916502009-07-24 19:02:52 +00001287
1288 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1289 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001290 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001291 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1292 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001293 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001294 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001295 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001296 }
Mike Stumpbf916502009-07-24 19:02:52 +00001297
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001298 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001299 if (NonNullArgs.empty()) {
1300 // Warn the trivial case only if attribute is not coming from a
1301 // macro instantiation.
1302 if (Attr.getLoc().isFileID())
1303 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001304 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001305 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001306 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001307
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001308 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001309 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001310 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001311 D->addAttr(::new (S.Context)
1312 NonNullAttr(Attr.getRange(), S.Context, start, size,
1313 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001314}
1315
Chandler Carruth1b03c872011-07-02 00:01:44 +00001316static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001317 // This attribute must be applied to a function declaration.
1318 // The first argument to the attribute must be a string,
1319 // the name of the resource, for example "malloc".
1320 // The following arguments must be argument indexes, the arguments must be
1321 // of integer type for Returns, otherwise of pointer type.
1322 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001323 // after being held. free() should be __attribute((ownership_takes)), whereas
1324 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001325
1326 if (!AL.getParameterName()) {
1327 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1328 << AL.getName()->getName() << 1;
1329 return;
1330 }
1331 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001332 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001333 switch (AL.getKind()) {
1334 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001335 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001336 if (AL.getNumArgs() < 1) {
1337 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1338 return;
1339 }
Jordy Rose2a479922010-08-12 08:54:03 +00001340 break;
1341 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001342 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001343 if (AL.getNumArgs() < 1) {
1344 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1345 return;
1346 }
Jordy Rose2a479922010-08-12 08:54:03 +00001347 break;
1348 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001349 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001350 if (AL.getNumArgs() > 1) {
1351 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1352 << AL.getNumArgs() + 1;
1353 return;
1354 }
Jordy Rose2a479922010-08-12 08:54:03 +00001355 break;
1356 default:
1357 // This should never happen given how we are called.
1358 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001359 }
1360
Chandler Carruth87c44602011-07-01 23:49:12 +00001361 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001362 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1363 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001364 return;
1365 }
1366
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001367 // In C++ the implicit 'this' function parameter also counts, and they are
1368 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001369 bool HasImplicitThisParam = isInstanceMethod(D);
1370 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001371
Chris Lattner5f9e2722011-07-23 10:55:15 +00001372 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001373
1374 // Normalize the argument, __foo__ becomes foo.
1375 if (Module.startswith("__") && Module.endswith("__"))
1376 Module = Module.substr(2, Module.size() - 4);
1377
Chris Lattner5f9e2722011-07-23 10:55:15 +00001378 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001379
Jordy Rose2a479922010-08-12 08:54:03 +00001380 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1381 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001382
Peter Collingbourne7a730022010-11-23 20:45:58 +00001383 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001384 llvm::APSInt ArgNum(32);
1385 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1386 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1387 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1388 << AL.getName()->getName() << IdxExpr->getSourceRange();
1389 continue;
1390 }
1391
1392 unsigned x = (unsigned) ArgNum.getZExtValue();
1393
1394 if (x > NumArgs || x < 1) {
1395 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1396 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1397 continue;
1398 }
1399 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001400 if (HasImplicitThisParam) {
1401 if (x == 0) {
1402 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1403 << "ownership" << IdxExpr->getSourceRange();
1404 return;
1405 }
1406 --x;
1407 }
1408
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001409 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001410 case OwnershipAttr::Takes:
1411 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001412 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001413 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001414 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1415 // FIXME: Should also highlight argument in decl.
1416 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001417 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001418 << "pointer"
1419 << IdxExpr->getSourceRange();
1420 continue;
1421 }
1422 break;
1423 }
Sean Huntcf807c42010-08-18 23:23:40 +00001424 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001425 if (AL.getNumArgs() > 1) {
1426 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001427 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001428 llvm::APSInt ArgNum(32);
1429 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1430 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1431 S.Diag(AL.getLoc(), diag::err_ownership_type)
1432 << "ownership_returns" << "integer"
1433 << IdxExpr->getSourceRange();
1434 return;
1435 }
1436 }
1437 break;
1438 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001439 } // switch
1440
1441 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001442 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001443 i = D->specific_attr_begin<OwnershipAttr>(),
1444 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001445 i != e; ++i) {
1446 if ((*i)->getOwnKind() != K) {
1447 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1448 I!=E; ++I) {
1449 if (x == *I) {
1450 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1451 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001452 }
1453 }
1454 }
1455 }
1456 OwnershipArgs.push_back(x);
1457 }
1458
1459 unsigned* start = OwnershipArgs.data();
1460 unsigned size = OwnershipArgs.size();
1461 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001462
1463 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1464 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1465 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001466 }
Sean Huntcf807c42010-08-18 23:23:40 +00001467
Michael Han51d8c522013-01-24 16:46:58 +00001468 D->addAttr(::new (S.Context)
1469 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1470 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001471}
1472
Chandler Carruth1b03c872011-07-02 00:01:44 +00001473static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001474 // Check the attribute arguments.
1475 if (Attr.getNumArgs() > 1) {
1476 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1477 return;
1478 }
1479
Chandler Carruth87c44602011-07-01 23:49:12 +00001480 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001482 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001483 return;
1484 }
1485
Chandler Carruth87c44602011-07-01 23:49:12 +00001486 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001487
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001488 // gcc rejects
1489 // class c {
1490 // static int a __attribute__((weakref ("v2")));
1491 // static int b() __attribute__((weakref ("f3")));
1492 // };
1493 // and ignores the attributes of
1494 // void f(void) {
1495 // static int a __attribute__((weakref ("v2")));
1496 // }
1497 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001498 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001499 if (!Ctx->isFileContext()) {
1500 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001501 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001502 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001503 }
1504
1505 // The GCC manual says
1506 //
1507 // At present, a declaration to which `weakref' is attached can only
1508 // be `static'.
1509 //
1510 // It also says
1511 //
1512 // Without a TARGET,
1513 // given as an argument to `weakref' or to `alias', `weakref' is
1514 // equivalent to `weak'.
1515 //
1516 // gcc 4.4.1 will accept
1517 // int a7 __attribute__((weakref));
1518 // as
1519 // int a7 __attribute__((weak));
1520 // This looks like a bug in gcc. We reject that for now. We should revisit
1521 // it if this behaviour is actually used.
1522
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001523 // GCC rejects
1524 // static ((alias ("y"), weakref)).
1525 // Should we? How to check that weakref is before or after alias?
1526
1527 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001528 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001529 Arg = Arg->IgnoreParenCasts();
1530 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1531
Douglas Gregor5cee1192011-07-27 05:40:30 +00001532 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1534 << "weakref" << 1;
1535 return;
1536 }
1537 // GCC will accept anything as the argument of weakref. Should we
1538 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001539 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001540 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001541 }
1542
Michael Han51d8c522013-01-24 16:46:58 +00001543 D->addAttr(::new (S.Context)
1544 WeakRefAttr(Attr.getRange(), S.Context,
1545 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001546}
1547
Chandler Carruth1b03c872011-07-02 00:01:44 +00001548static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001549 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001550 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001551 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001552 return;
1553 }
Mike Stumpbf916502009-07-24 19:02:52 +00001554
Peter Collingbourne7a730022010-11-23 20:45:58 +00001555 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001556 Arg = Arg->IgnoreParenCasts();
1557 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001558
Douglas Gregor5cee1192011-07-27 05:40:30 +00001559 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001561 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001562 return;
1563 }
Mike Stumpbf916502009-07-24 19:02:52 +00001564
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001565 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001566 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1567 return;
1568 }
1569
Chris Lattner6b6b5372008-06-26 18:38:35 +00001570 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001571
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001572 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001573 Str->getString(),
1574 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001575}
1576
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001577static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1578 // Check the attribute arguments.
1579 if (!checkAttributeNumArgs(S, Attr, 0))
1580 return;
1581
1582 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1584 << Attr.getName() << ExpectedFunctionOrMethod;
1585 return;
1586 }
1587
Michael Han51d8c522013-01-24 16:46:58 +00001588 D->addAttr(::new (S.Context)
1589 MinSizeAttr(Attr.getRange(), S.Context,
1590 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001591}
1592
Benjamin Krameree409a92012-05-12 21:10:52 +00001593static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1594 // Check the attribute arguments.
1595 if (!checkAttributeNumArgs(S, Attr, 0))
1596 return;
1597
1598 if (!isa<FunctionDecl>(D)) {
1599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1600 << Attr.getName() << ExpectedFunction;
1601 return;
1602 }
1603
1604 if (D->hasAttr<HotAttr>()) {
1605 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1606 << Attr.getName() << "hot";
1607 return;
1608 }
1609
Michael Han51d8c522013-01-24 16:46:58 +00001610 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1611 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001612}
1613
1614static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1615 // Check the attribute arguments.
1616 if (!checkAttributeNumArgs(S, Attr, 0))
1617 return;
1618
1619 if (!isa<FunctionDecl>(D)) {
1620 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1621 << Attr.getName() << ExpectedFunction;
1622 return;
1623 }
1624
1625 if (D->hasAttr<ColdAttr>()) {
1626 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1627 << Attr.getName() << "cold";
1628 return;
1629 }
1630
Michael Han51d8c522013-01-24 16:46:58 +00001631 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1632 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001633}
1634
Chandler Carruth1b03c872011-07-02 00:01:44 +00001635static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001636 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001637 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001638 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001639
Chandler Carruth87c44602011-07-01 23:49:12 +00001640 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001642 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001643 return;
1644 }
1645
Michael Han51d8c522013-01-24 16:46:58 +00001646 D->addAttr(::new (S.Context)
1647 NakedAttr(Attr.getRange(), S.Context,
1648 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001649}
1650
Chandler Carruth1b03c872011-07-02 00:01:44 +00001651static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1652 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001653 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001654 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001655 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1656 return;
1657 }
1658
Chandler Carruth87c44602011-07-01 23:49:12 +00001659 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001661 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001662 return;
1663 }
Mike Stumpbf916502009-07-24 19:02:52 +00001664
Michael Han51d8c522013-01-24 16:46:58 +00001665 D->addAttr(::new (S.Context)
1666 AlwaysInlineAttr(Attr.getRange(), S.Context,
1667 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001668}
1669
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001670static void handleTLSModelAttr(Sema &S, Decl *D,
1671 const AttributeList &Attr) {
1672 // Check the attribute arguments.
1673 if (Attr.getNumArgs() != 1) {
1674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1675 return;
1676 }
1677
1678 Expr *Arg = Attr.getArg(0);
1679 Arg = Arg->IgnoreParenCasts();
1680 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1681
1682 // Check that it is a string.
1683 if (!Str) {
1684 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1685 return;
1686 }
1687
Richard Smith38afbc72013-04-13 02:43:54 +00001688 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1690 << Attr.getName() << ExpectedTLSVar;
1691 return;
1692 }
1693
1694 // Check that the value.
1695 StringRef Model = Str->getString();
1696 if (Model != "global-dynamic" && Model != "local-dynamic"
1697 && Model != "initial-exec" && Model != "local-exec") {
1698 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1699 return;
1700 }
1701
Michael Han51d8c522013-01-24 16:46:58 +00001702 D->addAttr(::new (S.Context)
1703 TLSModelAttr(Attr.getRange(), S.Context, Model,
1704 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001705}
1706
Chandler Carruth1b03c872011-07-02 00:01:44 +00001707static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001708 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001709 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1711 return;
1712 }
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Chandler Carruth87c44602011-07-01 23:49:12 +00001714 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001715 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001716 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001717 D->addAttr(::new (S.Context)
1718 MallocAttr(Attr.getRange(), S.Context,
1719 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001720 return;
1721 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001722 }
1723
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001724 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001725}
1726
Chandler Carruth1b03c872011-07-02 00:01:44 +00001727static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001728 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001729 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001730 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001731
Michael Han51d8c522013-01-24 16:46:58 +00001732 D->addAttr(::new (S.Context)
1733 MayAliasAttr(Attr.getRange(), S.Context,
1734 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001735}
1736
Chandler Carruth1b03c872011-07-02 00:01:44 +00001737static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001738 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001739 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001740 D->addAttr(::new (S.Context)
1741 NoCommonAttr(Attr.getRange(), S.Context,
1742 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001743 else
1744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001745 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001746}
1747
Chandler Carruth1b03c872011-07-02 00:01:44 +00001748static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001749 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001750
1751 if (S.LangOpts.CPlusPlus) {
1752 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1753 return;
1754 }
1755
Chandler Carruth87c44602011-07-01 23:49:12 +00001756 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001757 D->addAttr(::new (S.Context)
1758 CommonAttr(Attr.getRange(), S.Context,
1759 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001760 else
1761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001762 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001763}
1764
Chandler Carruth1b03c872011-07-02 00:01:44 +00001765static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001766 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001767
1768 if (S.CheckNoReturnAttr(attr)) return;
1769
Chandler Carruth87c44602011-07-01 23:49:12 +00001770 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001771 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001772 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001773 return;
1774 }
1775
Michael Han51d8c522013-01-24 16:46:58 +00001776 D->addAttr(::new (S.Context)
1777 NoReturnAttr(attr.getRange(), S.Context,
1778 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001779}
1780
1781bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001782 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001783 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1784 attr.setInvalid();
1785 return true;
1786 }
1787
1788 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001789}
1790
Chandler Carruth1b03c872011-07-02 00:01:44 +00001791static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1792 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001793
1794 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1795 // because 'analyzer_noreturn' does not impact the type.
1796
Chandler Carruth1731e202011-07-11 23:30:35 +00001797 if(!checkAttributeNumArgs(S, Attr, 0))
1798 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001799
Chandler Carruth87c44602011-07-01 23:49:12 +00001800 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1801 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001802 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1803 && !VD->getType()->isFunctionPointerType())) {
1804 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001805 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001806 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001807 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001808 return;
1809 }
1810 }
1811
Michael Han51d8c522013-01-24 16:46:58 +00001812 D->addAttr(::new (S.Context)
1813 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1814 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001815}
1816
Richard Smithcd8ab512013-01-17 01:30:42 +00001817static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1818 const AttributeList &Attr) {
1819 // C++11 [dcl.attr.noreturn]p1:
1820 // The attribute may be applied to the declarator-id in a function
1821 // declaration.
1822 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1823 if (!FD) {
1824 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1825 << Attr.getName() << ExpectedFunctionOrMethod;
1826 return;
1827 }
1828
Michael Han51d8c522013-01-24 16:46:58 +00001829 D->addAttr(::new (S.Context)
1830 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1831 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001832}
1833
John Thompson35cc9622010-08-09 21:53:52 +00001834// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001835static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001836/*
1837 Returning a Vector Class in Registers
1838
Eric Christopherf48f3672010-12-01 22:13:54 +00001839 According to the PPU ABI specifications, a class with a single member of
1840 vector type is returned in memory when used as the return value of a function.
1841 This results in inefficient code when implementing vector classes. To return
1842 the value in a single vector register, add the vecreturn attribute to the
1843 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001844
1845 Example:
1846
1847 struct Vector
1848 {
1849 __vector float xyzw;
1850 } __attribute__((vecreturn));
1851
1852 Vector Add(Vector lhs, Vector rhs)
1853 {
1854 Vector result;
1855 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1856 return result; // This will be returned in a register
1857 }
1858*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001859 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001861 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001862 return;
1863 }
1864
Chandler Carruth87c44602011-07-01 23:49:12 +00001865 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001866 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1867 return;
1868 }
1869
Chandler Carruth87c44602011-07-01 23:49:12 +00001870 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001871 int count = 0;
1872
1873 if (!isa<CXXRecordDecl>(record)) {
1874 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1875 return;
1876 }
1877
1878 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1879 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1880 return;
1881 }
1882
Eric Christopherf48f3672010-12-01 22:13:54 +00001883 for (RecordDecl::field_iterator iter = record->field_begin();
1884 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001885 if ((count == 1) || !iter->getType()->isVectorType()) {
1886 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1887 return;
1888 }
1889 count++;
1890 }
1891
Michael Han51d8c522013-01-24 16:46:58 +00001892 D->addAttr(::new (S.Context)
1893 VecReturnAttr(Attr.getRange(), S.Context,
1894 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001895}
1896
Richard Smith3a2b7a12013-01-28 22:42:45 +00001897static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1898 const AttributeList &Attr) {
1899 if (isa<ParmVarDecl>(D)) {
1900 // [[carries_dependency]] can only be applied to a parameter if it is a
1901 // parameter of a function declaration or lambda.
1902 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1903 S.Diag(Attr.getLoc(),
1904 diag::err_carries_dependency_param_not_function_decl);
1905 return;
1906 }
1907 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001909 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001910 return;
1911 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001912
1913 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1914 Attr.getRange(), S.Context,
1915 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001916}
1917
Chandler Carruth1b03c872011-07-02 00:01:44 +00001918static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001919 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001920 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001922 return;
1923 }
Mike Stumpbf916502009-07-24 19:02:52 +00001924
Chandler Carruth87c44602011-07-01 23:49:12 +00001925 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001926 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001927 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001928 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001929 return;
1930 }
Mike Stumpbf916502009-07-24 19:02:52 +00001931
Michael Han51d8c522013-01-24 16:46:58 +00001932 D->addAttr(::new (S.Context)
1933 UnusedAttr(Attr.getRange(), S.Context,
1934 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001935}
1936
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001937static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1938 const AttributeList &Attr) {
1939 // check the attribute arguments.
1940 if (Attr.hasParameterOrArguments()) {
1941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1942 return;
1943 }
1944
1945 if (!isa<FunctionDecl>(D)) {
1946 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1947 << Attr.getName() << ExpectedFunction;
1948 return;
1949 }
1950
Michael Han51d8c522013-01-24 16:46:58 +00001951 D->addAttr(::new (S.Context)
1952 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1953 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001954}
1955
Chandler Carruth1b03c872011-07-02 00:01:44 +00001956static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001957 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001958 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001959 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1960 return;
1961 }
Mike Stumpbf916502009-07-24 19:02:52 +00001962
Chandler Carruth87c44602011-07-01 23:49:12 +00001963 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001964 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1966 return;
1967 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001968 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001969 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001970 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001971 return;
1972 }
Mike Stumpbf916502009-07-24 19:02:52 +00001973
Michael Han51d8c522013-01-24 16:46:58 +00001974 D->addAttr(::new (S.Context)
1975 UsedAttr(Attr.getRange(), S.Context,
1976 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001977}
1978
Chandler Carruth1b03c872011-07-02 00:01:44 +00001979static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001980 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001981 if (Attr.getNumArgs() > 1) {
1982 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001983 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001984 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001985
1986 int priority = 65535; // FIXME: Do not hardcode such constants.
1987 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001988 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001989 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001990 if (E->isTypeDependent() || E->isValueDependent() ||
1991 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001993 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001994 return;
1995 }
1996 priority = Idx.getZExtValue();
1997 }
Mike Stumpbf916502009-07-24 19:02:52 +00001998
Chandler Carruth87c44602011-07-01 23:49:12 +00001999 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002000 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002001 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002002 return;
2003 }
2004
Michael Han51d8c522013-01-24 16:46:58 +00002005 D->addAttr(::new (S.Context)
2006 ConstructorAttr(Attr.getRange(), S.Context, priority,
2007 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002008}
2009
Chandler Carruth1b03c872011-07-02 00:01:44 +00002010static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002011 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002012 if (Attr.getNumArgs() > 1) {
2013 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002014 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002015 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002016
2017 int priority = 65535; // FIXME: Do not hardcode such constants.
2018 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002019 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002020 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002021 if (E->isTypeDependent() || E->isValueDependent() ||
2022 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002023 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002024 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002025 return;
2026 }
2027 priority = Idx.getZExtValue();
2028 }
Mike Stumpbf916502009-07-24 19:02:52 +00002029
Chandler Carruth87c44602011-07-01 23:49:12 +00002030 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002031 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002032 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002033 return;
2034 }
2035
Michael Han51d8c522013-01-24 16:46:58 +00002036 D->addAttr(::new (S.Context)
2037 DestructorAttr(Attr.getRange(), S.Context, priority,
2038 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002039}
2040
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002041template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002042static void handleAttrWithMessage(Sema &S, Decl *D,
2043 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002044 unsigned NumArgs = Attr.getNumArgs();
2045 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002046 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002047 return;
2048 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002049
2050 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002051 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002052 if (NumArgs == 1) {
2053 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002054 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002055 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002056 << Attr.getName();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002057 return;
2058 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002059 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002060 }
Mike Stumpbf916502009-07-24 19:02:52 +00002061
Michael Han51d8c522013-01-24 16:46:58 +00002062 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2063 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002064}
2065
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002066static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2067 const AttributeList &Attr) {
2068 unsigned NumArgs = Attr.getNumArgs();
2069 if (NumArgs > 0) {
2070 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2071 return;
2072 }
2073
Michael Han51d8c522013-01-24 16:46:58 +00002074 D->addAttr(::new (S.Context)
2075 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2076 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002077}
2078
Patrick Beardb2f68202012-04-06 18:12:22 +00002079static void handleObjCRootClassAttr(Sema &S, Decl *D,
2080 const AttributeList &Attr) {
2081 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2083 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002084 return;
2085 }
2086
2087 unsigned NumArgs = Attr.getNumArgs();
2088 if (NumArgs > 0) {
2089 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2090 return;
2091 }
2092
Michael Han51d8c522013-01-24 16:46:58 +00002093 D->addAttr(::new (S.Context)
2094 ObjCRootClassAttr(Attr.getRange(), S.Context,
2095 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002096}
2097
Michael Han51d8c522013-01-24 16:46:58 +00002098static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2099 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002100 if (!isa<ObjCInterfaceDecl>(D)) {
2101 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2102 return;
2103 }
2104
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002105 unsigned NumArgs = Attr.getNumArgs();
2106 if (NumArgs > 0) {
2107 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2108 return;
2109 }
2110
Michael Han51d8c522013-01-24 16:46:58 +00002111 D->addAttr(::new (S.Context)
2112 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2113 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002114}
2115
Jordy Rosefad5de92012-05-08 03:27:22 +00002116static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2117 IdentifierInfo *Platform,
2118 VersionTuple Introduced,
2119 VersionTuple Deprecated,
2120 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002121 StringRef PlatformName
2122 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2123 if (PlatformName.empty())
2124 PlatformName = Platform->getName();
2125
2126 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2127 // of these steps are needed).
2128 if (!Introduced.empty() && !Deprecated.empty() &&
2129 !(Introduced <= Deprecated)) {
2130 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2131 << 1 << PlatformName << Deprecated.getAsString()
2132 << 0 << Introduced.getAsString();
2133 return true;
2134 }
2135
2136 if (!Introduced.empty() && !Obsoleted.empty() &&
2137 !(Introduced <= Obsoleted)) {
2138 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2139 << 2 << PlatformName << Obsoleted.getAsString()
2140 << 0 << Introduced.getAsString();
2141 return true;
2142 }
2143
2144 if (!Deprecated.empty() && !Obsoleted.empty() &&
2145 !(Deprecated <= Obsoleted)) {
2146 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2147 << 2 << PlatformName << Obsoleted.getAsString()
2148 << 1 << Deprecated.getAsString();
2149 return true;
2150 }
2151
2152 return false;
2153}
2154
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002155/// \brief Check whether the two versions match.
2156///
2157/// If either version tuple is empty, then they are assumed to match. If
2158/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2159static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2160 bool BeforeIsOkay) {
2161 if (X.empty() || Y.empty())
2162 return true;
2163
2164 if (X == Y)
2165 return true;
2166
2167 if (BeforeIsOkay && X < Y)
2168 return true;
2169
2170 return false;
2171}
2172
Rafael Espindola51be6e32013-01-08 22:04:34 +00002173AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002174 IdentifierInfo *Platform,
2175 VersionTuple Introduced,
2176 VersionTuple Deprecated,
2177 VersionTuple Obsoleted,
2178 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002179 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002180 bool Override,
2181 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002182 VersionTuple MergedIntroduced = Introduced;
2183 VersionTuple MergedDeprecated = Deprecated;
2184 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002185 bool FoundAny = false;
2186
Rafael Espindola98ae8342012-05-10 02:50:16 +00002187 if (D->hasAttrs()) {
2188 AttrVec &Attrs = D->getAttrs();
2189 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2190 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2191 if (!OldAA) {
2192 ++i;
2193 continue;
2194 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002195
Rafael Espindola98ae8342012-05-10 02:50:16 +00002196 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2197 if (OldPlatform != Platform) {
2198 ++i;
2199 continue;
2200 }
2201
2202 FoundAny = true;
2203 VersionTuple OldIntroduced = OldAA->getIntroduced();
2204 VersionTuple OldDeprecated = OldAA->getDeprecated();
2205 VersionTuple OldObsoleted = OldAA->getObsoleted();
2206 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002207
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002208 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2209 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2210 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2211 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002212 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002213 if (Override) {
2214 int Which = -1;
2215 VersionTuple FirstVersion;
2216 VersionTuple SecondVersion;
2217 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2218 Which = 0;
2219 FirstVersion = OldIntroduced;
2220 SecondVersion = Introduced;
2221 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2222 Which = 1;
2223 FirstVersion = Deprecated;
2224 SecondVersion = OldDeprecated;
2225 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2226 Which = 2;
2227 FirstVersion = Obsoleted;
2228 SecondVersion = OldObsoleted;
2229 }
2230
2231 if (Which == -1) {
2232 Diag(OldAA->getLocation(),
2233 diag::warn_mismatched_availability_override_unavail)
2234 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2235 } else {
2236 Diag(OldAA->getLocation(),
2237 diag::warn_mismatched_availability_override)
2238 << Which
2239 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2240 << FirstVersion.getAsString() << SecondVersion.getAsString();
2241 }
2242 Diag(Range.getBegin(), diag::note_overridden_method);
2243 } else {
2244 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2245 Diag(Range.getBegin(), diag::note_previous_attribute);
2246 }
2247
Rafael Espindola98ae8342012-05-10 02:50:16 +00002248 Attrs.erase(Attrs.begin() + i);
2249 --e;
2250 continue;
2251 }
2252
2253 VersionTuple MergedIntroduced2 = MergedIntroduced;
2254 VersionTuple MergedDeprecated2 = MergedDeprecated;
2255 VersionTuple MergedObsoleted2 = MergedObsoleted;
2256
2257 if (MergedIntroduced2.empty())
2258 MergedIntroduced2 = OldIntroduced;
2259 if (MergedDeprecated2.empty())
2260 MergedDeprecated2 = OldDeprecated;
2261 if (MergedObsoleted2.empty())
2262 MergedObsoleted2 = OldObsoleted;
2263
2264 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2265 MergedIntroduced2, MergedDeprecated2,
2266 MergedObsoleted2)) {
2267 Attrs.erase(Attrs.begin() + i);
2268 --e;
2269 continue;
2270 }
2271
2272 MergedIntroduced = MergedIntroduced2;
2273 MergedDeprecated = MergedDeprecated2;
2274 MergedObsoleted = MergedObsoleted2;
2275 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002276 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002277 }
2278
2279 if (FoundAny &&
2280 MergedIntroduced == Introduced &&
2281 MergedDeprecated == Deprecated &&
2282 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002283 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002284
Ted Kremenekcb344392013-04-06 00:34:27 +00002285 // Only create a new attribute if !Override, but we want to do
2286 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002287 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002288 MergedDeprecated, MergedObsoleted) &&
2289 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002290 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2291 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002292 Obsoleted, IsUnavailable, Message,
2293 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002294 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002295 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002296}
2297
Chandler Carruth1b03c872011-07-02 00:01:44 +00002298static void handleAvailabilityAttr(Sema &S, Decl *D,
2299 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002300 IdentifierInfo *Platform = Attr.getParameterName();
2301 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002302 unsigned Index = Attr.getAttributeSpellingListIndex();
2303
Rafael Espindola3b294362012-05-06 19:56:25 +00002304 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002305 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2306 << Platform;
2307
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002308 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2309 if (!ND) {
2310 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2311 return;
2312 }
2313
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002314 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2315 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2316 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002317 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002318 StringRef Str;
2319 const StringLiteral *SE =
2320 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2321 if (SE)
2322 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002323
Rafael Espindola51be6e32013-01-08 22:04:34 +00002324 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002325 Platform,
2326 Introduced.Version,
2327 Deprecated.Version,
2328 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002329 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002330 /*Override=*/false,
2331 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002332 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002333 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002334}
2335
John McCalld4c3d662013-02-20 01:54:26 +00002336template <class T>
2337static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2338 typename T::VisibilityType value,
2339 unsigned attrSpellingListIndex) {
2340 T *existingAttr = D->getAttr<T>();
2341 if (existingAttr) {
2342 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2343 if (existingValue == value)
2344 return NULL;
2345 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2346 S.Diag(range.getBegin(), diag::note_previous_attribute);
2347 D->dropAttr<T>();
2348 }
2349 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2350}
2351
Rafael Espindola599f1b72012-05-13 03:25:18 +00002352VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002353 VisibilityAttr::VisibilityType Vis,
2354 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002355 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2356 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002357}
2358
John McCalld4c3d662013-02-20 01:54:26 +00002359TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2360 TypeVisibilityAttr::VisibilityType Vis,
2361 unsigned AttrSpellingListIndex) {
2362 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2363 AttrSpellingListIndex);
2364}
2365
2366static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2367 bool isTypeVisibility) {
2368 // Visibility attributes don't mean anything on a typedef.
2369 if (isa<TypedefNameDecl>(D)) {
2370 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2371 << Attr.getName();
2372 return;
2373 }
2374
2375 // 'type_visibility' can only go on a type or namespace.
2376 if (isTypeVisibility &&
2377 !(isa<TagDecl>(D) ||
2378 isa<ObjCInterfaceDecl>(D) ||
2379 isa<NamespaceDecl>(D))) {
2380 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2381 << Attr.getName() << ExpectedTypeOrNamespace;
2382 return;
2383 }
2384
Chris Lattner6b6b5372008-06-26 18:38:35 +00002385 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002386 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002387 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002388
Peter Collingbourne7a730022010-11-23 20:45:58 +00002389 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002390 Arg = Arg->IgnoreParenCasts();
2391 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002392
Douglas Gregor5cee1192011-07-27 05:40:30 +00002393 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002394 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld4c3d662013-02-20 01:54:26 +00002395 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002396 return;
2397 }
Mike Stumpbf916502009-07-24 19:02:52 +00002398
Chris Lattner5f9e2722011-07-23 10:55:15 +00002399 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002400 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002401
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002402 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002403 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002404 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002405 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002406 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002407 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002408 else if (TypeStr == "protected") {
2409 // Complain about attempts to use protected visibility on targets
2410 // (like Darwin) that don't support it.
2411 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2412 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2413 type = VisibilityAttr::Default;
2414 } else {
2415 type = VisibilityAttr::Protected;
2416 }
2417 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002418 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002419 return;
2420 }
Mike Stumpbf916502009-07-24 19:02:52 +00002421
Michael Han51d8c522013-01-24 16:46:58 +00002422 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002423 clang::Attr *newAttr;
2424 if (isTypeVisibility) {
2425 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2426 (TypeVisibilityAttr::VisibilityType) type,
2427 Index);
2428 } else {
2429 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2430 }
2431 if (newAttr)
2432 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002433}
2434
Chandler Carruth1b03c872011-07-02 00:01:44 +00002435static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2436 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002437 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2438 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002440 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002441 return;
2442 }
2443
Chandler Carruth87c44602011-07-01 23:49:12 +00002444 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2445 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002447 << "objc_method_family" << 1;
2448 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002449 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002450 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002451 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002452 return;
2453 }
2454
Chris Lattner5f9e2722011-07-23 10:55:15 +00002455 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002456 ObjCMethodFamilyAttr::FamilyKind family;
2457 if (param == "none")
2458 family = ObjCMethodFamilyAttr::OMF_None;
2459 else if (param == "alloc")
2460 family = ObjCMethodFamilyAttr::OMF_alloc;
2461 else if (param == "copy")
2462 family = ObjCMethodFamilyAttr::OMF_copy;
2463 else if (param == "init")
2464 family = ObjCMethodFamilyAttr::OMF_init;
2465 else if (param == "mutableCopy")
2466 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2467 else if (param == "new")
2468 family = ObjCMethodFamilyAttr::OMF_new;
2469 else {
2470 // Just warn and ignore it. This is future-proof against new
2471 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002472 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002473 return;
2474 }
2475
John McCallf85e1932011-06-15 23:02:42 +00002476 if (family == ObjCMethodFamilyAttr::OMF_init &&
2477 !method->getResultType()->isObjCObjectPointerType()) {
2478 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2479 << method->getResultType();
2480 // Ignore the attribute.
2481 return;
2482 }
2483
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002484 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002485 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002486}
2487
Chandler Carruth1b03c872011-07-02 00:01:44 +00002488static void handleObjCExceptionAttr(Sema &S, Decl *D,
2489 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002490 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002491 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002492
Chris Lattner0db29ec2009-02-14 08:09:34 +00002493 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2494 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002495 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2496 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002497 return;
2498 }
Mike Stumpbf916502009-07-24 19:02:52 +00002499
Michael Han51d8c522013-01-24 16:46:58 +00002500 D->addAttr(::new (S.Context)
2501 ObjCExceptionAttr(Attr.getRange(), S.Context,
2502 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002503}
2504
Chandler Carruth1b03c872011-07-02 00:01:44 +00002505static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002506 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002507 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002508 return;
2509 }
Richard Smith162e1c12011-04-15 14:24:37 +00002510 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002511 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002512 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002513 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2514 return;
2515 }
2516 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002517 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2518 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002519 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002520 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2521 return;
2522 }
2523 }
2524 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002525 // It is okay to include this attribute on properties, e.g.:
2526 //
2527 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2528 //
2529 // In this case it follows tradition and suppresses an error in the above
2530 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002531 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002532 }
Michael Han51d8c522013-01-24 16:46:58 +00002533 D->addAttr(::new (S.Context)
2534 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2535 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002536}
2537
Mike Stumpbf916502009-07-24 19:02:52 +00002538static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002539handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002540 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002541 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002542 return;
2543 }
2544
2545 if (!isa<FunctionDecl>(D)) {
2546 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2547 return;
2548 }
2549
Michael Han51d8c522013-01-24 16:46:58 +00002550 D->addAttr(::new (S.Context)
2551 OverloadableAttr(Attr.getRange(), S.Context,
2552 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002553}
2554
Chandler Carruth1b03c872011-07-02 00:01:44 +00002555static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002556 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002557 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002558 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002559 return;
2560 }
Mike Stumpbf916502009-07-24 19:02:52 +00002561
Steve Naroff9eae5762008-09-18 16:44:58 +00002562 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002563 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002564 return;
2565 }
Mike Stumpbf916502009-07-24 19:02:52 +00002566
Sean Huntcf807c42010-08-18 23:23:40 +00002567 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002568 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002569 type = BlocksAttr::ByRef;
2570 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002571 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002572 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002573 return;
2574 }
Mike Stumpbf916502009-07-24 19:02:52 +00002575
Michael Han51d8c522013-01-24 16:46:58 +00002576 D->addAttr(::new (S.Context)
2577 BlocksAttr(Attr.getRange(), S.Context, type,
2578 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002579}
2580
Chandler Carruth1b03c872011-07-02 00:01:44 +00002581static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002582 // check the attribute arguments.
2583 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002584 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002585 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002586 }
2587
John McCall3323fad2011-09-09 07:56:05 +00002588 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002589 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002590 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002591 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002592 if (E->isTypeDependent() || E->isValueDependent() ||
2593 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002594 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002595 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002596 return;
2597 }
Mike Stumpbf916502009-07-24 19:02:52 +00002598
John McCall3323fad2011-09-09 07:56:05 +00002599 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002600 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2601 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002602 return;
2603 }
John McCall3323fad2011-09-09 07:56:05 +00002604
2605 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002606 }
2607
John McCall3323fad2011-09-09 07:56:05 +00002608 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002609 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002610 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002611 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002612 if (E->isTypeDependent() || E->isValueDependent() ||
2613 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002614 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002615 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002616 return;
2617 }
2618 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002619
John McCall3323fad2011-09-09 07:56:05 +00002620 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002621 // FIXME: This error message could be improved, it would be nice
2622 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002623 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2624 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002625 return;
2626 }
2627 }
2628
Chandler Carruth87c44602011-07-01 23:49:12 +00002629 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002630 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002631 if (isa<FunctionNoProtoType>(FT)) {
2632 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2633 return;
2634 }
Mike Stumpbf916502009-07-24 19:02:52 +00002635
Chris Lattner897cd902009-03-17 23:03:47 +00002636 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002637 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002638 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002639 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002640 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002641 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002642 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002643 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002644 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002645 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2646 if (!BD->isVariadic()) {
2647 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2648 return;
2649 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002650 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002651 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002652 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002653 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002654 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002655 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002656 int m = Ty->isFunctionPointerType() ? 0 : 1;
2657 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002658 return;
2659 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002660 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002662 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002663 return;
2664 }
Anders Carlsson77091822008-10-05 18:05:59 +00002665 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002667 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002668 return;
2669 }
Michael Han51d8c522013-01-24 16:46:58 +00002670 D->addAttr(::new (S.Context)
2671 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2672 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002673}
2674
Chandler Carruth1b03c872011-07-02 00:01:44 +00002675static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002676 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002677 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002678 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002679
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002680 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002682 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002683 return;
2684 }
Mike Stumpbf916502009-07-24 19:02:52 +00002685
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002686 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2687 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2688 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002689 return;
2690 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002691 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2692 if (MD->getResultType()->isVoidType()) {
2693 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2694 << Attr.getName() << 1;
2695 return;
2696 }
2697
Michael Han51d8c522013-01-24 16:46:58 +00002698 D->addAttr(::new (S.Context)
2699 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2700 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002701}
2702
Chandler Carruth1b03c872011-07-02 00:01:44 +00002703static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002704 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002705 if (Attr.hasParameterOrArguments()) {
2706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002707 return;
2708 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002709
Chandler Carruth87c44602011-07-01 23:49:12 +00002710 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002711 if (isa<CXXRecordDecl>(D)) {
2712 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2713 return;
2714 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2716 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002717 return;
2718 }
2719
Chandler Carruth87c44602011-07-01 23:49:12 +00002720 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002721
Michael Han51d8c522013-01-24 16:46:58 +00002722 nd->addAttr(::new (S.Context)
2723 WeakAttr(Attr.getRange(), S.Context,
2724 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002725}
2726
Chandler Carruth1b03c872011-07-02 00:01:44 +00002727static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002728 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002729 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002730 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002731
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002732
2733 // weak_import only applies to variable & function declarations.
2734 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002735 if (!D->canBeWeakImported(isDef)) {
2736 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002737 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2738 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002739 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002740 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002741 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002742 // Nothing to warn about here.
2743 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002745 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002746
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002747 return;
2748 }
2749
Michael Han51d8c522013-01-24 16:46:58 +00002750 D->addAttr(::new (S.Context)
2751 WeakImportAttr(Attr.getRange(), S.Context,
2752 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002753}
2754
Tanya Lattner0df579e2012-07-09 22:06:01 +00002755// Handles reqd_work_group_size and work_group_size_hint.
2756static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002757 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002758 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2759 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2760
Nate Begeman6f3d8382009-06-26 06:32:41 +00002761 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002762 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002763
2764 unsigned WGSize[3];
2765 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002766 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002767 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002768 if (E->isTypeDependent() || E->isValueDependent() ||
2769 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002771 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002772 return;
2773 }
2774 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2775 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002776
2777 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2778 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2779 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2780 if (!(A->getXDim() == WGSize[0] &&
2781 A->getYDim() == WGSize[1] &&
2782 A->getZDim() == WGSize[2])) {
2783 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2784 Attr.getName();
2785 }
2786 }
2787
2788 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2789 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2790 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2791 if (!(A->getXDim() == WGSize[0] &&
2792 A->getYDim() == WGSize[1] &&
2793 A->getZDim() == WGSize[2])) {
2794 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2795 Attr.getName();
2796 }
2797 }
2798
2799 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2800 D->addAttr(::new (S.Context)
2801 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002802 WGSize[0], WGSize[1], WGSize[2],
2803 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002804 else
2805 D->addAttr(::new (S.Context)
2806 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002807 WGSize[0], WGSize[1], WGSize[2],
2808 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002809}
2810
Joey Gouly37453b92013-03-08 09:42:32 +00002811static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2812 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2813
2814 // Attribute has 1 argument.
2815 if (!checkAttributeNumArgs(S, Attr, 1))
2816 return;
2817
2818 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2819
2820 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2821 (ParmType->isBooleanType() ||
2822 !ParmType->isIntegralType(S.getASTContext()))) {
2823 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2824 << ParmType;
2825 return;
2826 }
2827
2828 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2829 D->hasAttr<VecTypeHintAttr>()) {
2830 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2831 if (A->getTypeHint() != ParmType) {
2832 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2833 return;
2834 }
2835 }
2836
2837 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2838 ParmType, Attr.getLoc()));
2839}
2840
Joey Gouly96cead52013-03-14 09:54:43 +00002841static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2842 if (!dyn_cast<VarDecl>(D))
2843 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2844 << 9;
2845 StringRef EndianType = Attr.getParameterName()->getName();
2846 if (EndianType != "host" && EndianType != "device")
2847 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2848}
2849
Rafael Espindola599f1b72012-05-13 03:25:18 +00002850SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002851 StringRef Name,
2852 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002853 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2854 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002855 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002856 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2857 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002858 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002859 }
Michael Han51d8c522013-01-24 16:46:58 +00002860 return ::new (Context) SectionAttr(Range, Context, Name,
2861 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002862}
2863
Chandler Carruth1b03c872011-07-02 00:01:44 +00002864static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002865 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002866 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002867 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002868
2869 // Make sure that there is a string literal as the sections's single
2870 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002871 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002872 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002873 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002874 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002875 return;
2876 }
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Chris Lattner797c3c42009-08-10 19:03:04 +00002878 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002879 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002880 if (!Error.empty()) {
2881 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2882 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002883 return;
2884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002886 // This attribute cannot be applied to local variables.
2887 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2888 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2889 return;
2890 }
Michael Han51d8c522013-01-24 16:46:58 +00002891
2892 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002893 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002894 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002895 if (NewAttr)
2896 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002897}
2898
Chris Lattner6b6b5372008-06-26 18:38:35 +00002899
Chandler Carruth1b03c872011-07-02 00:01:44 +00002900static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002901 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002902 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002903 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002904 return;
2905 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002906
Chandler Carruth87c44602011-07-01 23:49:12 +00002907 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002908 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002909 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002910 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002911 D->addAttr(::new (S.Context)
2912 NoThrowAttr(Attr.getRange(), S.Context,
2913 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002914 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002915}
2916
Chandler Carruth1b03c872011-07-02 00:01:44 +00002917static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002918 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002919 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002920 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002921 return;
2922 }
Mike Stumpbf916502009-07-24 19:02:52 +00002923
Chandler Carruth87c44602011-07-01 23:49:12 +00002924 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002925 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002926 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002927 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002928 D->addAttr(::new (S.Context)
2929 ConstAttr(Attr.getRange(), S.Context,
2930 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002931 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002932}
2933
Chandler Carruth1b03c872011-07-02 00:01:44 +00002934static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002935 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002936 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002937 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002938
Michael Han51d8c522013-01-24 16:46:58 +00002939 D->addAttr(::new (S.Context)
2940 PureAttr(Attr.getRange(), S.Context,
2941 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002942}
2943
Chandler Carruth1b03c872011-07-02 00:01:44 +00002944static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002945 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2947 return;
2948 }
Mike Stumpbf916502009-07-24 19:02:52 +00002949
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002950 if (Attr.getNumArgs() != 0) {
2951 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2952 return;
2953 }
Mike Stumpbf916502009-07-24 19:02:52 +00002954
Chandler Carruth87c44602011-07-01 23:49:12 +00002955 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002956
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002957 if (!VD || !VD->hasLocalStorage()) {
2958 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2959 return;
2960 }
Mike Stumpbf916502009-07-24 19:02:52 +00002961
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002962 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002963 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002964 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002965 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2966 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002967 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002968 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002969 Attr.getParameterName();
2970 return;
2971 }
Mike Stumpbf916502009-07-24 19:02:52 +00002972
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002973 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2974 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002975 S.Diag(Attr.getParameterLoc(),
2976 diag::err_attribute_cleanup_arg_not_function)
2977 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002978 return;
2979 }
2980
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002981 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002982 S.Diag(Attr.getParameterLoc(),
2983 diag::err_attribute_cleanup_func_must_take_one_arg)
2984 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002985 return;
2986 }
Mike Stumpbf916502009-07-24 19:02:52 +00002987
Anders Carlsson89941c12009-02-07 23:16:50 +00002988 // We're currently more strict than GCC about what function types we accept.
2989 // If this ever proves to be a problem it should be easy to fix.
2990 QualType Ty = S.Context.getPointerType(VD->getType());
2991 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002992 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2993 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002994 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002995 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2996 Attr.getParameterName() << ParamTy << Ty;
2997 return;
2998 }
Mike Stumpbf916502009-07-24 19:02:52 +00002999
Michael Han51d8c522013-01-24 16:46:58 +00003000 D->addAttr(::new (S.Context)
3001 CleanupAttr(Attr.getRange(), S.Context, FD,
3002 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00003003 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00003004 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003005}
3006
Mike Stumpbf916502009-07-24 19:02:52 +00003007/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003008/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003009static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003010 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003011 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003012
Chandler Carruth87c44602011-07-01 23:49:12 +00003013 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003015 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003016 return;
3017 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003018
3019 // In C++ the implicit 'this' function parameter also counts, and they are
3020 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003021 bool HasImplicitThisParam = isInstanceMethod(D);
3022 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003023 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003024
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003025 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003026 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003027 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003028 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3029 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3031 << "format" << 2 << IdxExpr->getSourceRange();
3032 return;
3033 }
Mike Stumpbf916502009-07-24 19:02:52 +00003034
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003035 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3036 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3037 << "format" << 2 << IdxExpr->getSourceRange();
3038 return;
3039 }
Mike Stumpbf916502009-07-24 19:02:52 +00003040
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003041 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003042
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003043 if (HasImplicitThisParam) {
3044 if (ArgIdx == 0) {
3045 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3046 << "format_arg" << IdxExpr->getSourceRange();
3047 return;
3048 }
3049 ArgIdx--;
3050 }
3051
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003052 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003053 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003054
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003055 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3056 if (not_nsstring_type &&
3057 !isCFStringType(Ty, S.Context) &&
3058 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003059 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003060 // FIXME: Should highlight the actual expression that has the wrong type.
3061 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003062 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003063 << IdxExpr->getSourceRange();
3064 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003065 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003066 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003067 if (!isNSStringType(Ty, S.Context) &&
3068 !isCFStringType(Ty, S.Context) &&
3069 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003070 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003071 // FIXME: Should highlight the actual expression that has the wrong type.
3072 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003073 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003074 << IdxExpr->getSourceRange();
3075 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003076 }
3077
Michael Han51d8c522013-01-24 16:46:58 +00003078 D->addAttr(::new (S.Context)
3079 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3080 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003081}
3082
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003083enum FormatAttrKind {
3084 CFStringFormat,
3085 NSStringFormat,
3086 StrftimeFormat,
3087 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003088 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003089 InvalidFormat
3090};
3091
3092/// getFormatAttrKind - Map from format attribute names to supported format
3093/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003094static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003095 return llvm::StringSwitch<FormatAttrKind>(Format)
3096 // Check for formats that get handled specially.
3097 .Case("NSString", NSStringFormat)
3098 .Case("CFString", CFStringFormat)
3099 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003100
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003101 // Otherwise, check for supported formats.
3102 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3103 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3104 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003105
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003106 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3107 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003108}
3109
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003110/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003111/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003112static void handleInitPriorityAttr(Sema &S, Decl *D,
3113 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003114 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003115 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3116 return;
3117 }
3118
Chandler Carruth87c44602011-07-01 23:49:12 +00003119 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003120 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3121 Attr.setInvalid();
3122 return;
3123 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003124 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003125 if (S.Context.getAsArrayType(T))
3126 T = S.Context.getBaseElementType(T);
3127 if (!T->getAs<RecordType>()) {
3128 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3129 Attr.setInvalid();
3130 return;
3131 }
3132
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003133 if (Attr.getNumArgs() != 1) {
3134 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3135 Attr.setInvalid();
3136 return;
3137 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003138 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003139
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003140 llvm::APSInt priority(32);
3141 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3142 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3144 << "init_priority" << priorityExpr->getSourceRange();
3145 Attr.setInvalid();
3146 return;
3147 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003148 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003149 if (prioritynum < 101 || prioritynum > 65535) {
3150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3151 << priorityExpr->getSourceRange();
3152 Attr.setInvalid();
3153 return;
3154 }
Michael Han51d8c522013-01-24 16:46:58 +00003155 D->addAttr(::new (S.Context)
3156 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3157 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003158}
3159
Rafael Espindola599f1b72012-05-13 03:25:18 +00003160FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003161 int FormatIdx, int FirstArg,
3162 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003163 // Check whether we already have an equivalent format attribute.
3164 for (specific_attr_iterator<FormatAttr>
3165 i = D->specific_attr_begin<FormatAttr>(),
3166 e = D->specific_attr_end<FormatAttr>();
3167 i != e ; ++i) {
3168 FormatAttr *f = *i;
3169 if (f->getType() == Format &&
3170 f->getFormatIdx() == FormatIdx &&
3171 f->getFirstArg() == FirstArg) {
3172 // If we don't have a valid location for this attribute, adopt the
3173 // location.
3174 if (f->getLocation().isInvalid())
3175 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003176 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003177 }
3178 }
3179
Michael Han51d8c522013-01-24 16:46:58 +00003180 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3181 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003182}
3183
Mike Stumpbf916502009-07-24 19:02:52 +00003184/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003185/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003186static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003187
Chris Lattner545dd342008-06-28 23:36:30 +00003188 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00003190 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003191 return;
3192 }
3193
Chris Lattner545dd342008-06-28 23:36:30 +00003194 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003195 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003196 return;
3197 }
3198
Chandler Carruth87c44602011-07-01 23:49:12 +00003199 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003200 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003201 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003202 return;
3203 }
3204
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003205 // In C++ the implicit 'this' function parameter also counts, and they are
3206 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003207 bool HasImplicitThisParam = isInstanceMethod(D);
3208 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003209 unsigned FirstIdx = 1;
3210
Chris Lattner5f9e2722011-07-23 10:55:15 +00003211 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003212
3213 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003214 if (Format.startswith("__") && Format.endswith("__"))
3215 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003216
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003217 // Check for supported formats.
3218 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003219
3220 if (Kind == IgnoredFormat)
3221 return;
3222
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003223 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003224 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003225 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003226 return;
3227 }
3228
3229 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003230 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003231 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003232 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3233 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003235 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003236 return;
3237 }
3238
3239 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003241 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242 return;
3243 }
3244
3245 // FIXME: Do we need to bounds check?
3246 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003247
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003248 if (HasImplicitThisParam) {
3249 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003250 S.Diag(Attr.getLoc(),
3251 diag::err_format_attribute_implicit_this_format_string)
3252 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003253 return;
3254 }
3255 ArgIdx--;
3256 }
Mike Stump1eb44332009-09-09 15:08:12 +00003257
Chris Lattner6b6b5372008-06-26 18:38:35 +00003258 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003259 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003260
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003261 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003262 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003263 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3264 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003265 return;
3266 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003267 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003268 // FIXME: do we need to check if the type is NSString*? What are the
3269 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003270 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003271 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003272 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3273 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003274 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003275 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003276 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003277 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003278 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003279 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3280 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003281 return;
3282 }
3283
3284 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003285 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003286 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003287 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3288 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003289 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003290 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003291 return;
3292 }
3293
3294 // check if the function is variadic if the 3rd argument non-zero
3295 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003296 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003297 ++NumArgs; // +1 for ...
3298 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003299 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003300 return;
3301 }
3302 }
3303
Chris Lattner3c73c412008-11-19 08:23:25 +00003304 // strftime requires FirstArg to be 0 because it doesn't read from any
3305 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003306 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003308 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3309 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003310 return;
3311 }
3312 // if 0 it disables parameter checking (to use with e.g. va_list)
3313 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003315 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003316 return;
3317 }
3318
Rafael Espindola599f1b72012-05-13 03:25:18 +00003319 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3320 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003321 FirstArg.getZExtValue(),
3322 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003323 if (NewAttr)
3324 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003325}
3326
Chandler Carruth1b03c872011-07-02 00:01:44 +00003327static void handleTransparentUnionAttr(Sema &S, Decl *D,
3328 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003329 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003330 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003331 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003332
Chris Lattner6b6b5372008-06-26 18:38:35 +00003333
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003334 // Try to find the underlying union declaration.
3335 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003336 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003337 if (TD && TD->getUnderlyingType()->isUnionType())
3338 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3339 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003340 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003341
3342 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003343 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003344 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003345 return;
3346 }
3347
John McCall5e1cdac2011-10-07 06:10:15 +00003348 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003349 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003350 diag::warn_transparent_union_attribute_not_definition);
3351 return;
3352 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003353
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003354 RecordDecl::field_iterator Field = RD->field_begin(),
3355 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003356 if (Field == FieldEnd) {
3357 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3358 return;
3359 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003360
David Blaikie581deb32012-06-06 20:45:41 +00003361 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003362 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003363 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003364 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003365 diag::warn_transparent_union_attribute_floating)
3366 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003367 return;
3368 }
3369
3370 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3371 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3372 for (; Field != FieldEnd; ++Field) {
3373 QualType FieldType = Field->getType();
3374 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3375 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3376 // Warn if we drop the attribute.
3377 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003378 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003379 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003380 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003381 diag::warn_transparent_union_attribute_field_size_align)
3382 << isSize << Field->getDeclName() << FieldBits;
3383 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003384 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003385 diag::note_transparent_union_first_field_size_align)
3386 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003387 return;
3388 }
3389 }
3390
Michael Han51d8c522013-01-24 16:46:58 +00003391 RD->addAttr(::new (S.Context)
3392 TransparentUnionAttr(Attr.getRange(), S.Context,
3393 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003394}
3395
Chandler Carruth1b03c872011-07-02 00:01:44 +00003396static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003397 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003398 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003399 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003400
Peter Collingbourne7a730022010-11-23 20:45:58 +00003401 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003402 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003403
Chris Lattner6b6b5372008-06-26 18:38:35 +00003404 // Make sure that there is a string literal as the annotation's single
3405 // argument.
3406 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003407 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003408 return;
3409 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003410
3411 // Don't duplicate annotations that are already set.
3412 for (specific_attr_iterator<AnnotateAttr>
3413 i = D->specific_attr_begin<AnnotateAttr>(),
3414 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3415 if ((*i)->getAnnotation() == SE->getString())
3416 return;
3417 }
Michael Han51d8c522013-01-24 16:46:58 +00003418
3419 D->addAttr(::new (S.Context)
3420 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3421 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003422}
3423
Chandler Carruth1b03c872011-07-02 00:01:44 +00003424static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003425 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003426 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003428 return;
3429 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003430
Richard Smithbe507b62013-02-01 08:12:08 +00003431 if (Attr.getNumArgs() == 0) {
3432 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3433 true, 0, Attr.getAttributeSpellingListIndex()));
3434 return;
3435 }
3436
Richard Smithf6565a92013-02-22 08:32:16 +00003437 Expr *E = Attr.getArg(0);
3438 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3439 S.Diag(Attr.getEllipsisLoc(),
3440 diag::err_pack_expansion_without_parameter_packs);
3441 return;
3442 }
3443
3444 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3445 return;
3446
3447 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3448 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003449}
3450
3451void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003452 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003453 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3454 SourceLocation AttrLoc = AttrRange.getBegin();
3455
Richard Smith4cd81c52013-01-29 09:02:09 +00003456 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003457 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003458 // C++11 [dcl.align]p1:
3459 // An alignment-specifier may be applied to a variable or to a class
3460 // data member, but it shall not be applied to a bit-field, a function
3461 // parameter, the formal parameter of a catch clause, or a variable
3462 // declared with the register storage class specifier. An
3463 // alignment-specifier may also be applied to the declaration of a class
3464 // or enumeration type.
3465 // C11 6.7.5/2:
3466 // An alignment attribute shall not be specified in a declaration of
3467 // a typedef, or a bit-field, or a function, or a parameter, or an
3468 // object declared with the register storage-class specifier.
3469 int DiagKind = -1;
3470 if (isa<ParmVarDecl>(D)) {
3471 DiagKind = 0;
3472 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3473 if (VD->getStorageClass() == SC_Register)
3474 DiagKind = 1;
3475 if (VD->isExceptionVariable())
3476 DiagKind = 2;
3477 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3478 if (FD->isBitField())
3479 DiagKind = 3;
3480 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003481 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3482 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003483 << (TmpAttr.isC11() ? ExpectedVariableOrField
3484 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003485 return;
3486 }
3487 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003488 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003489 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003490 return;
3491 }
3492 }
3493
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003494 if (E->isTypeDependent() || E->isValueDependent()) {
3495 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003496 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3497 AA->setPackExpansion(IsPackExpansion);
3498 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003499 return;
3500 }
Michael Hana31f65b2013-02-01 01:19:17 +00003501
Sean Huntcf807c42010-08-18 23:23:40 +00003502 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003503 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003504 ExprResult ICE
3505 = VerifyIntegerConstantExpression(E, &Alignment,
3506 diag::err_aligned_attribute_argument_not_int,
3507 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003508 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003509 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003510
3511 // C++11 [dcl.align]p2:
3512 // -- if the constant expression evaluates to zero, the alignment
3513 // specifier shall have no effect
3514 // C11 6.7.5p6:
3515 // An alignment specification of zero has no effect.
3516 if (!(TmpAttr.isAlignas() && !Alignment) &&
3517 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003518 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3519 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003520 return;
3521 }
Michael Hana31f65b2013-02-01 01:19:17 +00003522
Richard Smithbe507b62013-02-01 08:12:08 +00003523 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003524 // We've already verified it's a power of 2, now let's make sure it's
3525 // 8192 or less.
3526 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003527 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003528 << E->getSourceRange();
3529 return;
3530 }
3531 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003532
Richard Smithf6565a92013-02-22 08:32:16 +00003533 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3534 ICE.take(), SpellingListIndex);
3535 AA->setPackExpansion(IsPackExpansion);
3536 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003537}
3538
Michael Hana31f65b2013-02-01 01:19:17 +00003539void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003540 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003541 // FIXME: Cache the number on the Attr object if non-dependent?
3542 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003543 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3544 SpellingListIndex);
3545 AA->setPackExpansion(IsPackExpansion);
3546 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003547}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003548
Richard Smithbe507b62013-02-01 08:12:08 +00003549void Sema::CheckAlignasUnderalignment(Decl *D) {
3550 assert(D->hasAttrs() && "no attributes on decl");
3551
3552 QualType Ty;
3553 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3554 Ty = VD->getType();
3555 else
3556 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003557 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003558 return;
3559
3560 // C++11 [dcl.align]p5, C11 6.7.5/4:
3561 // The combined effect of all alignment attributes in a declaration shall
3562 // not specify an alignment that is less strict than the alignment that
3563 // would otherwise be required for the entity being declared.
3564 AlignedAttr *AlignasAttr = 0;
3565 unsigned Align = 0;
3566 for (specific_attr_iterator<AlignedAttr>
3567 I = D->specific_attr_begin<AlignedAttr>(),
3568 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3569 if (I->isAlignmentDependent())
3570 return;
3571 if (I->isAlignas())
3572 AlignasAttr = *I;
3573 Align = std::max(Align, I->getAlignment(Context));
3574 }
3575
3576 if (AlignasAttr && Align) {
3577 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3578 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3579 if (NaturalAlign > RequestedAlign)
3580 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3581 << Ty << (unsigned)NaturalAlign.getQuantity();
3582 }
3583}
3584
Chandler Carruthd309c812011-07-01 23:49:16 +00003585/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003586/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003587///
Mike Stumpbf916502009-07-24 19:02:52 +00003588/// Despite what would be logical, the mode attribute is a decl attribute, not a
3589/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3590/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003591static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003592 // This attribute isn't documented, but glibc uses it. It changes
3593 // the width of an int or unsigned int to the specified size.
3594
3595 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003596 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003597 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003598
Chris Lattnerfbf13472008-06-27 22:18:37 +00003599
3600 IdentifierInfo *Name = Attr.getParameterName();
3601 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003602 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003603 return;
3604 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003605
Chris Lattner5f9e2722011-07-23 10:55:15 +00003606 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003607
3608 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003609 if (Str.startswith("__") && Str.endswith("__"))
3610 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003611
3612 unsigned DestWidth = 0;
3613 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003614 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003615 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003616 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003617 switch (Str[0]) {
3618 case 'Q': DestWidth = 8; break;
3619 case 'H': DestWidth = 16; break;
3620 case 'S': DestWidth = 32; break;
3621 case 'D': DestWidth = 64; break;
3622 case 'X': DestWidth = 96; break;
3623 case 'T': DestWidth = 128; break;
3624 }
3625 if (Str[1] == 'F') {
3626 IntegerMode = false;
3627 } else if (Str[1] == 'C') {
3628 IntegerMode = false;
3629 ComplexMode = true;
3630 } else if (Str[1] != 'I') {
3631 DestWidth = 0;
3632 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003633 break;
3634 case 4:
3635 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3636 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003637 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003638 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003639 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003640 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003641 break;
3642 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003643 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003644 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003645 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003646 case 11:
3647 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003648 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003649 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003650 }
3651
3652 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003653 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003654 OldTy = TD->getUnderlyingType();
3655 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3656 OldTy = VD->getType();
3657 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003658 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003659 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003660 return;
3661 }
Eli Friedman73397492009-03-03 06:41:03 +00003662
John McCall183700f2009-09-21 23:43:11 +00003663 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003664 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3665 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003666 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003667 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3668 } else if (ComplexMode) {
3669 if (!OldTy->isComplexType())
3670 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3671 } else {
3672 if (!OldTy->isFloatingType())
3673 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3674 }
3675
Mike Stump390b4cc2009-05-16 07:39:55 +00003676 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3677 // and friends, at least with glibc.
3678 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3679 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003680 // FIXME: Make sure floating-point mappings are accurate
3681 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003682 QualType NewTy;
3683 switch (DestWidth) {
3684 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003685 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003686 return;
3687 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003688 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003689 return;
3690 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003691 if (!IntegerMode) {
3692 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3693 return;
3694 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003695 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003696 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003697 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003698 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003699 break;
3700 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003701 if (!IntegerMode) {
3702 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3703 return;
3704 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003705 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003706 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003707 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003708 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003709 break;
3710 case 32:
3711 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003712 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003713 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003714 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003715 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003716 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003717 break;
3718 case 64:
3719 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003720 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003721 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003722 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003723 NewTy = S.Context.LongTy;
3724 else
3725 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003726 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003727 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003728 NewTy = S.Context.UnsignedLongTy;
3729 else
3730 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003731 break;
Eli Friedman73397492009-03-03 06:41:03 +00003732 case 96:
3733 NewTy = S.Context.LongDoubleTy;
3734 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003735 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003736 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3737 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003738 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3739 return;
3740 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003741 if (IntegerMode) {
3742 if (OldTy->isSignedIntegerType())
3743 NewTy = S.Context.Int128Ty;
3744 else
3745 NewTy = S.Context.UnsignedInt128Ty;
3746 } else
3747 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003748 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003749 }
3750
Eli Friedman73397492009-03-03 06:41:03 +00003751 if (ComplexMode) {
3752 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003753 }
3754
3755 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003756 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3757 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3758 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003759 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003760
3761 D->addAttr(::new (S.Context)
3762 ModeAttr(Attr.getRange(), S.Context, Name,
3763 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003764}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003765
Chandler Carruth1b03c872011-07-02 00:01:44 +00003766static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003767 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003768 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003769 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003770
Nick Lewycky78d1a102012-07-24 01:40:49 +00003771 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3772 if (!VD->hasGlobalStorage())
3773 S.Diag(Attr.getLoc(),
3774 diag::warn_attribute_requires_functions_or_static_globals)
3775 << Attr.getName();
3776 } else if (!isFunctionOrMethod(D)) {
3777 S.Diag(Attr.getLoc(),
3778 diag::warn_attribute_requires_functions_or_static_globals)
3779 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003780 return;
3781 }
Mike Stumpbf916502009-07-24 19:02:52 +00003782
Michael Han51d8c522013-01-24 16:46:58 +00003783 D->addAttr(::new (S.Context)
3784 NoDebugAttr(Attr.getRange(), S.Context,
3785 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003786}
3787
Chandler Carruth1b03c872011-07-02 00:01:44 +00003788static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003789 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003790 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003791 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003792
Mike Stumpbf916502009-07-24 19:02:52 +00003793
Chandler Carruth87c44602011-07-01 23:49:12 +00003794 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003795 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003796 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003797 return;
3798 }
Mike Stumpbf916502009-07-24 19:02:52 +00003799
Michael Han51d8c522013-01-24 16:46:58 +00003800 D->addAttr(::new (S.Context)
3801 NoInlineAttr(Attr.getRange(), S.Context,
3802 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003803}
3804
Chandler Carruth1b03c872011-07-02 00:01:44 +00003805static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3806 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003807 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003808 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003809 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003810
Chris Lattner7255a2d2010-06-22 00:03:40 +00003811
Chandler Carruth87c44602011-07-01 23:49:12 +00003812 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003813 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003814 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003815 return;
3816 }
3817
Michael Han51d8c522013-01-24 16:46:58 +00003818 D->addAttr(::new (S.Context)
3819 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3820 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003821}
3822
Chandler Carruth1b03c872011-07-02 00:01:44 +00003823static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003824 if (S.LangOpts.CUDA) {
3825 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003826 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003827 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3828 return;
3829 }
3830
Chandler Carruth87c44602011-07-01 23:49:12 +00003831 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003832 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003833 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003834 return;
3835 }
3836
Michael Han51d8c522013-01-24 16:46:58 +00003837 D->addAttr(::new (S.Context)
3838 CUDAConstantAttr(Attr.getRange(), S.Context,
3839 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003840 } else {
3841 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3842 }
3843}
3844
Chandler Carruth1b03c872011-07-02 00:01:44 +00003845static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003846 if (S.LangOpts.CUDA) {
3847 // check the attribute arguments.
3848 if (Attr.getNumArgs() != 0) {
3849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3850 return;
3851 }
3852
Chandler Carruth87c44602011-07-01 23:49:12 +00003853 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003854 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003855 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003856 return;
3857 }
3858
Michael Han51d8c522013-01-24 16:46:58 +00003859 D->addAttr(::new (S.Context)
3860 CUDADeviceAttr(Attr.getRange(), S.Context,
3861 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003862 } else {
3863 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3864 }
3865}
3866
Chandler Carruth1b03c872011-07-02 00:01:44 +00003867static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003868 if (S.LangOpts.CUDA) {
3869 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003870 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003871 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003872
Chandler Carruth87c44602011-07-01 23:49:12 +00003873 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003874 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003875 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003876 return;
3877 }
3878
Chandler Carruth87c44602011-07-01 23:49:12 +00003879 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003880 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003881 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003882 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003883 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3884 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003885 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003886 "void");
3887 } else {
3888 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3889 << FD->getType();
3890 }
3891 return;
3892 }
3893
Michael Han51d8c522013-01-24 16:46:58 +00003894 D->addAttr(::new (S.Context)
3895 CUDAGlobalAttr(Attr.getRange(), S.Context,
3896 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003897 } else {
3898 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3899 }
3900}
3901
Chandler Carruth1b03c872011-07-02 00:01:44 +00003902static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003903 if (S.LangOpts.CUDA) {
3904 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003905 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003906 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003907
Peter Collingbourneced76712010-12-01 03:15:31 +00003908
Chandler Carruth87c44602011-07-01 23:49:12 +00003909 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003910 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003911 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003912 return;
3913 }
3914
Michael Han51d8c522013-01-24 16:46:58 +00003915 D->addAttr(::new (S.Context)
3916 CUDAHostAttr(Attr.getRange(), S.Context,
3917 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003918 } else {
3919 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3920 }
3921}
3922
Chandler Carruth1b03c872011-07-02 00:01:44 +00003923static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003924 if (S.LangOpts.CUDA) {
3925 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003926 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003927 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003928
Chandler Carruth87c44602011-07-01 23:49:12 +00003929 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003930 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003931 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003932 return;
3933 }
3934
Michael Han51d8c522013-01-24 16:46:58 +00003935 D->addAttr(::new (S.Context)
3936 CUDASharedAttr(Attr.getRange(), S.Context,
3937 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003938 } else {
3939 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3940 }
3941}
3942
Chandler Carruth1b03c872011-07-02 00:01:44 +00003943static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003944 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003945 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003946 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003947
Chandler Carruth87c44602011-07-01 23:49:12 +00003948 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003949 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003950 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003951 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003952 return;
3953 }
Mike Stumpbf916502009-07-24 19:02:52 +00003954
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003955 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003956 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003957 return;
3958 }
Mike Stumpbf916502009-07-24 19:02:52 +00003959
Michael Han51d8c522013-01-24 16:46:58 +00003960 D->addAttr(::new (S.Context)
3961 GNUInlineAttr(Attr.getRange(), S.Context,
3962 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003963}
3964
Chandler Carruth1b03c872011-07-02 00:01:44 +00003965static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003966 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003967
Aaron Ballmanfff32482012-12-09 17:45:41 +00003968 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003969 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003970 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3971 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003972 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003973 return;
3974
Chandler Carruth87c44602011-07-01 23:49:12 +00003975 if (!isa<ObjCMethodDecl>(D)) {
3976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3977 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003978 return;
3979 }
3980
Chandler Carruth87c44602011-07-01 23:49:12 +00003981 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003982 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003983 D->addAttr(::new (S.Context)
3984 FastCallAttr(Attr.getRange(), S.Context,
3985 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003986 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003987 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003988 D->addAttr(::new (S.Context)
3989 StdCallAttr(Attr.getRange(), S.Context,
3990 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003991 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003992 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003993 D->addAttr(::new (S.Context)
3994 ThisCallAttr(Attr.getRange(), S.Context,
3995 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003996 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003997 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003998 D->addAttr(::new (S.Context)
3999 CDeclAttr(Attr.getRange(), S.Context,
4000 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004001 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004002 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004003 D->addAttr(::new (S.Context)
4004 PascalAttr(Attr.getRange(), S.Context,
4005 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004006 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004007 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004008 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004009 switch (CC) {
4010 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004011 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004012 break;
4013 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004014 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004015 break;
4016 default:
4017 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004018 }
4019
Michael Han51d8c522013-01-24 16:46:58 +00004020 D->addAttr(::new (S.Context)
4021 PcsAttr(Attr.getRange(), S.Context, PCS,
4022 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004023 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004024 }
Derek Schuff263366f2012-10-16 22:30:41 +00004025 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004026 D->addAttr(::new (S.Context)
4027 PnaclCallAttr(Attr.getRange(), S.Context,
4028 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004029 return;
Guy Benyei38980082012-12-25 08:53:55 +00004030 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004031 D->addAttr(::new (S.Context)
4032 IntelOclBiccAttr(Attr.getRange(), S.Context,
4033 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004034 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004035
Abramo Bagnarae215f722010-04-30 13:10:51 +00004036 default:
4037 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004038 }
4039}
4040
Chandler Carruth1b03c872011-07-02 00:01:44 +00004041static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00004042 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004043 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004044}
4045
Guy Benyei1db70402013-03-24 13:58:12 +00004046static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4047 assert(!Attr.isInvalid());
4048
4049 Expr *E = Attr.getArg(0);
4050 llvm::APSInt ArgNum(32);
4051 if (E->isTypeDependent() || E->isValueDependent() ||
4052 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4054 << Attr.getName()->getName() << E->getSourceRange();
4055 return;
4056 }
4057
4058 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4059 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4060}
4061
Aaron Ballmanfff32482012-12-09 17:45:41 +00004062bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4063 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004064 if (attr.isInvalid())
4065 return true;
4066
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004067 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4068 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4069 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004070 attr.setInvalid();
4071 return true;
4072 }
4073
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004074 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4075 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004076 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004077 case AttributeList::AT_CDecl: CC = CC_C; break;
4078 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4079 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4080 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4081 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4082 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004083 Expr *Arg = attr.getArg(0);
4084 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004085 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004086 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4087 << "pcs" << 1;
4088 attr.setInvalid();
4089 return true;
4090 }
4091
Chris Lattner5f9e2722011-07-23 10:55:15 +00004092 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004093 if (StrRef == "aapcs") {
4094 CC = CC_AAPCS;
4095 break;
4096 } else if (StrRef == "aapcs-vfp") {
4097 CC = CC_AAPCS_VFP;
4098 break;
4099 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004100
4101 attr.setInvalid();
4102 Diag(attr.getLoc(), diag::err_invalid_pcs);
4103 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004104 }
Derek Schuff263366f2012-10-16 22:30:41 +00004105 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004106 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004107 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004108 }
4109
Aaron Ballman82bfa192012-10-02 14:26:08 +00004110 const TargetInfo &TI = Context.getTargetInfo();
4111 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4112 if (A == TargetInfo::CCCR_Warning) {
4113 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004114
4115 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4116 if (FD)
4117 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4118 TargetInfo::CCMT_NonMember;
4119 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004120 }
4121
John McCall711c52b2011-01-05 12:14:39 +00004122 return false;
4123}
4124
Chandler Carruth1b03c872011-07-02 00:01:44 +00004125static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004126 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004127
4128 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004129 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004130 return;
4131
Chandler Carruth87c44602011-07-01 23:49:12 +00004132 if (!isa<ObjCMethodDecl>(D)) {
4133 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4134 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004135 return;
4136 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004137
Michael Han51d8c522013-01-24 16:46:58 +00004138 D->addAttr(::new (S.Context)
4139 RegparmAttr(Attr.getRange(), S.Context, numParams,
4140 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004141}
4142
4143/// Checks a regparm attribute, returning true if it is ill-formed and
4144/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004145bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4146 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004147 return true;
4148
Chandler Carruth87c44602011-07-01 23:49:12 +00004149 if (Attr.getNumArgs() != 1) {
4150 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4151 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004152 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004153 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004154
Chandler Carruth87c44602011-07-01 23:49:12 +00004155 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004156 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004157 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004158 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004159 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004160 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004161 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004162 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004163 }
4164
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004165 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004166 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004167 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004168 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004169 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004170 }
4171
John McCall711c52b2011-01-05 12:14:39 +00004172 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004173 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004174 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004175 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004176 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004177 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004178 }
4179
John McCall711c52b2011-01-05 12:14:39 +00004180 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004181}
4182
Chandler Carruth1b03c872011-07-02 00:01:44 +00004183static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004184 if (S.LangOpts.CUDA) {
4185 // check the attribute arguments.
4186 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004187 // FIXME: 0 is not okay.
4188 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004189 return;
4190 }
4191
Chandler Carruth87c44602011-07-01 23:49:12 +00004192 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004193 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004194 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004195 return;
4196 }
4197
4198 Expr *MaxThreadsExpr = Attr.getArg(0);
4199 llvm::APSInt MaxThreads(32);
4200 if (MaxThreadsExpr->isTypeDependent() ||
4201 MaxThreadsExpr->isValueDependent() ||
4202 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4203 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4204 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4205 return;
4206 }
4207
4208 llvm::APSInt MinBlocks(32);
4209 if (Attr.getNumArgs() > 1) {
4210 Expr *MinBlocksExpr = Attr.getArg(1);
4211 if (MinBlocksExpr->isTypeDependent() ||
4212 MinBlocksExpr->isValueDependent() ||
4213 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4214 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4215 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4216 return;
4217 }
4218 }
4219
Michael Han51d8c522013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
4221 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4222 MaxThreads.getZExtValue(),
4223 MinBlocks.getZExtValue(),
4224 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004225 } else {
4226 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4227 }
4228}
4229
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004230static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4231 const AttributeList &Attr) {
4232 StringRef AttrName = Attr.getName()->getName();
4233 if (!Attr.getParameterName()) {
4234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4235 << Attr.getName() << /* arg num = */ 1;
4236 return;
4237 }
4238
4239 if (Attr.getNumArgs() != 2) {
4240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4241 << /* required args = */ 3;
4242 return;
4243 }
4244
4245 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4246
4247 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4249 << Attr.getName() << ExpectedFunctionOrMethod;
4250 return;
4251 }
4252
4253 uint64_t ArgumentIdx;
4254 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4255 Attr.getLoc(), 2,
4256 Attr.getArg(0), ArgumentIdx))
4257 return;
4258
4259 uint64_t TypeTagIdx;
4260 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4261 Attr.getLoc(), 3,
4262 Attr.getArg(1), TypeTagIdx))
4263 return;
4264
4265 bool IsPointer = (AttrName == "pointer_with_type_tag");
4266 if (IsPointer) {
4267 // Ensure that buffer has a pointer type.
4268 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4269 if (!BufferTy->isPointerType()) {
4270 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004271 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004272 }
4273 }
4274
Michael Han51d8c522013-01-24 16:46:58 +00004275 D->addAttr(::new (S.Context)
4276 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4277 ArgumentIdx, TypeTagIdx, IsPointer,
4278 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004279}
4280
4281static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4282 const AttributeList &Attr) {
4283 IdentifierInfo *PointerKind = Attr.getParameterName();
4284 if (!PointerKind) {
4285 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4286 << "type_tag_for_datatype" << 1;
4287 return;
4288 }
4289
4290 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4291
Michael Han51d8c522013-01-24 16:46:58 +00004292 D->addAttr(::new (S.Context)
4293 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4294 MatchingCType,
4295 Attr.getLayoutCompatible(),
4296 Attr.getMustBeNull(),
4297 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004298}
4299
Chris Lattner0744e5f2008-06-29 00:23:49 +00004300//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004301// Checker-specific attribute handlers.
4302//===----------------------------------------------------------------------===//
4303
John McCallc7ad3812011-01-25 03:31:58 +00004304static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004305 return type->isDependentType() ||
4306 type->isObjCObjectPointerType() ||
4307 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004308}
4309static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004310 return type->isDependentType() ||
4311 type->isPointerType() ||
4312 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004313}
4314
Chandler Carruth1b03c872011-07-02 00:01:44 +00004315static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004316 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004317 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004318 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004319 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004320 return;
4321 }
4322
4323 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004324 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004325 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4326 cf = false;
4327 } else {
4328 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4329 cf = true;
4330 }
4331
4332 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004333 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004334 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004335 return;
4336 }
4337
4338 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004339 param->addAttr(::new (S.Context)
4340 CFConsumedAttr(Attr.getRange(), S.Context,
4341 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004342 else
Michael Han51d8c522013-01-24 16:46:58 +00004343 param->addAttr(::new (S.Context)
4344 NSConsumedAttr(Attr.getRange(), S.Context,
4345 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004346}
4347
Chandler Carruth1b03c872011-07-02 00:01:44 +00004348static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4349 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004350 if (!isa<ObjCMethodDecl>(D)) {
4351 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004352 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004353 return;
4354 }
4355
Michael Han51d8c522013-01-24 16:46:58 +00004356 D->addAttr(::new (S.Context)
4357 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4358 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004359}
4360
Chandler Carruth1b03c872011-07-02 00:01:44 +00004361static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4362 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004363
John McCallc7ad3812011-01-25 03:31:58 +00004364 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004365
Chandler Carruth87c44602011-07-01 23:49:12 +00004366 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004367 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004368 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004369 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004370 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004371 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4372 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004373 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004374 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004375 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004376 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004377 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004378 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004379 return;
4380 }
Mike Stumpbf916502009-07-24 19:02:52 +00004381
John McCallc7ad3812011-01-25 03:31:58 +00004382 bool typeOK;
4383 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004384 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004385 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004386 case AttributeList::AT_NSReturnsAutoreleased:
4387 case AttributeList::AT_NSReturnsRetained:
4388 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004389 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4390 cf = false;
4391 break;
4392
Sean Hunt8e083e72012-06-19 23:57:03 +00004393 case AttributeList::AT_CFReturnsRetained:
4394 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004395 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4396 cf = true;
4397 break;
4398 }
4399
4400 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004401 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004402 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004403 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004404 }
Mike Stumpbf916502009-07-24 19:02:52 +00004405
Chandler Carruth87c44602011-07-01 23:49:12 +00004406 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004407 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004408 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004409 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004410 D->addAttr(::new (S.Context)
4411 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4412 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004413 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004414 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004415 D->addAttr(::new (S.Context)
4416 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4417 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004418 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004419 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004420 D->addAttr(::new (S.Context)
4421 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4422 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004423 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004424 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004425 D->addAttr(::new (S.Context)
4426 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4427 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004428 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004429 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004430 D->addAttr(::new (S.Context)
4431 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4432 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004433 return;
4434 };
4435}
4436
John McCalldc7c5ad2011-07-22 08:53:00 +00004437static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4438 const AttributeList &attr) {
4439 SourceLocation loc = attr.getLoc();
4440
4441 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4442
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004443 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004444 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004445 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004446 return;
4447 }
4448
4449 // Check that the method returns a normal pointer.
4450 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004451
4452 if (!resultType->isReferenceType() &&
4453 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004454 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4455 << SourceRange(loc)
4456 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4457
4458 // Drop the attribute.
4459 return;
4460 }
4461
Michael Han51d8c522013-01-24 16:46:58 +00004462 method->addAttr(::new (S.Context)
4463 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4464 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004465}
4466
Fariborz Jahanian84101132012-09-07 23:46:23 +00004467static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4468 const AttributeList &attr) {
4469 SourceLocation loc = attr.getLoc();
4470 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4471
4472 if (!method) {
4473 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4474 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4475 return;
4476 }
4477 DeclContext *DC = method->getDeclContext();
4478 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4479 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4480 << attr.getName() << 0;
4481 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4482 return;
4483 }
4484 if (method->getMethodFamily() == OMF_dealloc) {
4485 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4486 << attr.getName() << 1;
4487 return;
4488 }
4489
Michael Han51d8c522013-01-24 16:46:58 +00004490 method->addAttr(::new (S.Context)
4491 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4492 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004493}
4494
John McCall8dfac0b2011-09-30 05:12:12 +00004495/// Handle cf_audited_transfer and cf_unknown_transfer.
4496static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4497 if (!isa<FunctionDecl>(D)) {
4498 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004499 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004500 return;
4501 }
4502
Sean Hunt8e083e72012-06-19 23:57:03 +00004503 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004504
4505 // Check whether there's a conflicting attribute already present.
4506 Attr *Existing;
4507 if (IsAudited) {
4508 Existing = D->getAttr<CFUnknownTransferAttr>();
4509 } else {
4510 Existing = D->getAttr<CFAuditedTransferAttr>();
4511 }
4512 if (Existing) {
4513 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4514 << A.getName()
4515 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4516 << A.getRange() << Existing->getRange();
4517 return;
4518 }
4519
4520 // All clear; add the attribute.
4521 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004522 D->addAttr(::new (S.Context)
4523 CFAuditedTransferAttr(A.getRange(), S.Context,
4524 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004525 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004526 D->addAttr(::new (S.Context)
4527 CFUnknownTransferAttr(A.getRange(), S.Context,
4528 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004529 }
4530}
4531
John McCallfe98da02011-09-29 07:17:38 +00004532static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4533 const AttributeList &Attr) {
4534 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4535 if (!RD || RD->isUnion()) {
4536 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004537 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004538 }
4539
4540 IdentifierInfo *ParmName = Attr.getParameterName();
4541
4542 // In Objective-C, verify that the type names an Objective-C type.
4543 // We don't want to check this outside of ObjC because people sometimes
4544 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004545 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004546 // Check for an existing type with this name.
4547 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4548 Sema::LookupOrdinaryName);
4549 if (S.LookupName(R, Sc)) {
4550 NamedDecl *Target = R.getFoundDecl();
4551 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4552 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4553 S.Diag(Target->getLocStart(), diag::note_declared_at);
4554 }
4555 }
4556 }
4557
Michael Han51d8c522013-01-24 16:46:58 +00004558 D->addAttr(::new (S.Context)
4559 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4560 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004561}
4562
Chandler Carruth1b03c872011-07-02 00:01:44 +00004563static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4564 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004565 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004566
Chandler Carruth87c44602011-07-01 23:49:12 +00004567 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004568 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004569}
4570
Chandler Carruth1b03c872011-07-02 00:01:44 +00004571static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4572 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004573 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004574 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004575 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004576 return;
4577 }
4578
Chandler Carruth87c44602011-07-01 23:49:12 +00004579 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004580 QualType type = vd->getType();
4581
4582 if (!type->isDependentType() &&
4583 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004584 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004585 << type;
4586 return;
4587 }
4588
4589 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4590
4591 // If we have no lifetime yet, check the lifetime we're presumably
4592 // going to infer.
4593 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4594 lifetime = type->getObjCARCImplicitLifetime();
4595
4596 switch (lifetime) {
4597 case Qualifiers::OCL_None:
4598 assert(type->isDependentType() &&
4599 "didn't infer lifetime for non-dependent type?");
4600 break;
4601
4602 case Qualifiers::OCL_Weak: // meaningful
4603 case Qualifiers::OCL_Strong: // meaningful
4604 break;
4605
4606 case Qualifiers::OCL_ExplicitNone:
4607 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004608 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004609 << (lifetime == Qualifiers::OCL_Autoreleasing);
4610 break;
4611 }
4612
Chandler Carruth87c44602011-07-01 23:49:12 +00004613 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004614 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4615 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004616}
4617
Francois Pichet11542142010-12-19 06:50:37 +00004618//===----------------------------------------------------------------------===//
4619// Microsoft specific attribute handlers.
4620//===----------------------------------------------------------------------===//
4621
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004622// Check if MS extensions or some other language extensions are enabled. If
4623// not, issue a diagnostic that the given attribute is unused.
4624static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4625 bool OtherExtension = false) {
4626 if (S.LangOpts.MicrosoftExt || OtherExtension)
4627 return true;
4628 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4629 return false;
4630}
4631
Chandler Carruth1b03c872011-07-02 00:01:44 +00004632static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004633 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4634 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004635
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004636 // check the attribute arguments.
4637 if (!checkAttributeNumArgs(S, Attr, 1))
4638 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004639
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004640 Expr *Arg = Attr.getArg(0);
4641 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4642 if (!Str || !Str->isAscii()) {
4643 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4644 << "uuid" << 1;
4645 return;
4646 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004647
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004648 StringRef StrRef = Str->getString();
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004649
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004650 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4651 StrRef.back() == '}';
Francois Pichetd3d3be92010-12-20 01:41:49 +00004652
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004653 // Validate GUID length.
4654 if (IsCurly && StrRef.size() != 38) {
4655 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4656 return;
4657 }
4658 if (!IsCurly && StrRef.size() != 36) {
4659 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4660 return;
4661 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004662
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004663 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4664 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4665 StringRef::iterator I = StrRef.begin();
4666 if (IsCurly) // Skip the optional '{'
4667 ++I;
4668
4669 for (int i = 0; i < 36; ++i) {
4670 if (i == 8 || i == 13 || i == 18 || i == 23) {
4671 if (*I != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004672 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4673 return;
4674 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004675 } else if (!isHexDigit(*I)) {
4676 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4677 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004678 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004679 I++;
4680 }
Francois Pichet11542142010-12-19 06:50:37 +00004681
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004682 D->addAttr(::new (S.Context)
4683 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4684 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004685}
4686
John McCallc052dbb2012-05-22 21:28:12 +00004687static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004688 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004689 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004690
4691 AttributeList::Kind Kind = Attr.getKind();
4692 if (Kind == AttributeList::AT_SingleInheritance)
4693 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004694 ::new (S.Context)
4695 SingleInheritanceAttr(Attr.getRange(), S.Context,
4696 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004697 else if (Kind == AttributeList::AT_MultipleInheritance)
4698 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004699 ::new (S.Context)
4700 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4701 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004702 else if (Kind == AttributeList::AT_VirtualInheritance)
4703 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004704 ::new (S.Context)
4705 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4706 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004707}
4708
4709static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004710 if (!checkMicrosoftExt(S, Attr))
4711 return;
4712
4713 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004714 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004715 D->addAttr(
4716 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4717 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004718}
4719
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004720static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004721 if (!checkMicrosoftExt(S, Attr))
4722 return;
4723 D->addAttr(::new (S.Context)
4724 ForceInlineAttr(Attr.getRange(), S.Context,
4725 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004726}
4727
Reid Klecknera7225342013-05-20 14:02:37 +00004728static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4729 if (!checkMicrosoftExt(S, Attr))
4730 return;
4731 // Check linkage after possibly merging declaratinos. See
4732 // checkAttributesAfterMerging().
4733 D->addAttr(::new (S.Context)
4734 SelectAnyAttr(Attr.getRange(), S.Context,
4735 Attr.getAttributeSpellingListIndex()));
4736}
4737
Ted Kremenekb71368d2009-05-09 02:44:38 +00004738//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004739// Top Level Sema Entry Points
4740//===----------------------------------------------------------------------===//
4741
Chandler Carruth1b03c872011-07-02 00:01:44 +00004742static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4743 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004744 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4746 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4747 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004748 default:
4749 break;
4750 }
4751}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004752
Chandler Carruth1b03c872011-07-02 00:01:44 +00004753static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4754 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004755 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004756 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4757 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4758 case AttributeList::AT_IBOutletCollection:
4759 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004761 case AttributeList::AT_ObjCGC:
4762 case AttributeList::AT_VectorSize:
4763 case AttributeList::AT_NeonVectorType:
4764 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004765 case AttributeList::AT_Ptr32:
4766 case AttributeList::AT_Ptr64:
4767 case AttributeList::AT_SPtr:
4768 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004769 // Ignore these, these are type attributes, handled by
4770 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004771 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004772 case AttributeList::AT_CUDADevice:
4773 case AttributeList::AT_CUDAHost:
4774 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004775 // Ignore, this is a non-inheritable attribute, handled
4776 // by ProcessNonInheritableDeclAttr.
4777 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004778 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4779 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4780 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4781 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004782 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004784 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004785 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004786 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4787 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4788 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004789 handleDependencyAttr(S, scope, D, Attr);
4790 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4792 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4793 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004794 case AttributeList::AT_CXX11NoReturn:
4795 handleCXX11NoReturnAttr(S, D, Attr);
4796 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004798 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004799 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004800 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4801 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004802 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004803 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004804 case AttributeList::AT_MinSize:
4805 handleMinSizeAttr(S, D, Attr);
4806 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4808 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4809 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4810 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4811 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004812 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004813 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4815 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4816 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4817 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4818 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004819 case AttributeList::AT_ownership_returns:
4820 case AttributeList::AT_ownership_takes:
4821 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004822 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004823 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4824 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4825 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4826 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4827 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4828 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4829 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004830
Sean Hunt8e083e72012-06-19 23:57:03 +00004831 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004832 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004833 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004834 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004835
Sean Hunt8e083e72012-06-19 23:57:03 +00004836 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004837 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4838
Fariborz Jahanian84101132012-09-07 23:46:23 +00004839 case AttributeList::AT_ObjCRequiresSuper:
4840 handleObjCRequiresSuperAttr(S, D, Attr); break;
4841
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004843 handleNSBridgedAttr(S, scope, D, Attr); break;
4844
Sean Hunt8e083e72012-06-19 23:57:03 +00004845 case AttributeList::AT_CFAuditedTransfer:
4846 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004847 handleCFTransferAttr(S, D, Attr); break;
4848
Ted Kremenekb71368d2009-05-09 02:44:38 +00004849 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_CFConsumed:
4851 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4852 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004853 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004854
Sean Hunt8e083e72012-06-19 23:57:03 +00004855 case AttributeList::AT_NSReturnsAutoreleased:
4856 case AttributeList::AT_NSReturnsNotRetained:
4857 case AttributeList::AT_CFReturnsNotRetained:
4858 case AttributeList::AT_NSReturnsRetained:
4859 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004860 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004861
Tanya Lattner0df579e2012-07-09 22:06:01 +00004862 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004863 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004864 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004865
Joey Gouly37453b92013-03-08 09:42:32 +00004866 case AttributeList::AT_VecTypeHint:
4867 handleVecTypeHint(S, D, Attr); break;
4868
Joey Gouly96cead52013-03-14 09:54:43 +00004869 case AttributeList::AT_Endian:
4870 handleEndianAttr(S, D, Attr);
4871 break;
4872
Sean Hunt8e083e72012-06-19 23:57:03 +00004873 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004874 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004875
Sean Hunt8e083e72012-06-19 23:57:03 +00004876 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4877 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4878 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004879 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004880 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004881 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004882 handleArcWeakrefUnavailableAttr (S, D, Attr);
4883 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004884 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004885 handleObjCRootClassAttr(S, D, Attr);
4886 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004887 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004888 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004889 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004890 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4891 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004892 handleReturnsTwiceAttr(S, D, Attr);
4893 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004894 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004895 case AttributeList::AT_Visibility:
4896 handleVisibilityAttr(S, D, Attr, false);
4897 break;
4898 case AttributeList::AT_TypeVisibility:
4899 handleVisibilityAttr(S, D, Attr, true);
4900 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004901 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004902 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004903 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4904 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4905 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4906 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004907 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004908 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004909 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004910 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004911 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004912 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004913 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004914 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004915 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4916 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4917 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4918 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4919 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4920 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4921 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4922 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4923 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004924 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004925 // Just ignore
4926 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004927 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004928 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004929 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004930 case AttributeList::AT_StdCall:
4931 case AttributeList::AT_CDecl:
4932 case AttributeList::AT_FastCall:
4933 case AttributeList::AT_ThisCall:
4934 case AttributeList::AT_Pascal:
4935 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004936 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004937 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004938 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004939 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004940 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004941 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004942 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004943 case AttributeList::AT_OpenCLImageAccess:
4944 handleOpenCLImageAccessAttr(S, D, Attr);
4945 break;
John McCallc052dbb2012-05-22 21:28:12 +00004946
4947 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004948 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004949 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004950 handleMsStructAttr(S, D, Attr);
4951 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004952 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004953 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004954 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004955 case AttributeList::AT_SingleInheritance:
4956 case AttributeList::AT_MultipleInheritance:
4957 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004958 handleInheritanceAttr(S, D, Attr);
4959 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004960 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004961 handlePortabilityAttr(S, D, Attr);
4962 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004963 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004964 handleForceInlineAttr(S, D, Attr);
4965 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004966 case AttributeList::AT_SelectAny:
4967 handleSelectAnyAttr(S, D, Attr);
4968 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004969
4970 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004971 case AttributeList::AT_AssertExclusiveLock:
4972 handleAssertExclusiveLockAttr(S, D, Attr);
4973 break;
4974 case AttributeList::AT_AssertSharedLock:
4975 handleAssertSharedLockAttr(S, D, Attr);
4976 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004977 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004978 handleGuardedVarAttr(S, D, Attr);
4979 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004980 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004981 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004982 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004983 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004984 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004985 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004986 case AttributeList::AT_NoSanitizeAddress:
4987 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004988 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004989 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004990 handleNoThreadSafetyAnalysis(S, D, Attr);
4991 break;
4992 case AttributeList::AT_NoSanitizeThread:
4993 handleNoSanitizeThread(S, D, Attr);
4994 break;
4995 case AttributeList::AT_NoSanitizeMemory:
4996 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004997 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004998 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004999 handleLockableAttr(S, D, Attr);
5000 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005001 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005002 handleGuardedByAttr(S, D, Attr);
5003 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005004 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005005 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005006 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005007 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005008 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005009 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005010 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005011 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005012 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005013 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005014 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005015 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005016 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005017 handleLockReturnedAttr(S, D, Attr);
5018 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005019 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005020 handleLocksExcludedAttr(S, D, Attr);
5021 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005022 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005023 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005024 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005025 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005026 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005027 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005028 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005029 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005030 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005031 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005032 handleUnlockFunAttr(S, D, Attr);
5033 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005034 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005035 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005036 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005037 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005038 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005039 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005040
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005041 // Type safety attributes.
5042 case AttributeList::AT_ArgumentWithTypeTag:
5043 handleArgumentWithTypeTagAttr(S, D, Attr);
5044 break;
5045 case AttributeList::AT_TypeTagForDatatype:
5046 handleTypeTagForDatatypeAttr(S, D, Attr);
5047 break;
5048
Chris Lattner803d0802008-06-29 00:43:07 +00005049 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005050 // Ask target about the attribute.
5051 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5052 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005053 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5054 diag::warn_unhandled_ms_attribute_ignored :
5055 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005056 break;
5057 }
5058}
5059
Peter Collingbourne60700392011-01-21 02:08:45 +00005060/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5061/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00005062/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00005063static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5064 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00005065 bool NonInheritable, bool Inheritable,
5066 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005067 if (Attr.isInvalid())
5068 return;
5069
Richard Smithcd8ab512013-01-17 01:30:42 +00005070 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5071 // instead.
5072 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5073 return;
5074
Peter Collingbourne60700392011-01-21 02:08:45 +00005075 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005076 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005077
5078 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005079 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005080}
5081
Chris Lattner803d0802008-06-29 00:43:07 +00005082/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5083/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005084void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005085 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005086 bool NonInheritable, bool Inheritable,
5087 bool IncludeCXX11Attributes) {
5088 for (const AttributeList* l = AttrList; l; l = l->getNext())
5089 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5090 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005091
5092 // GCC accepts
5093 // static int a9 __attribute__((weakref));
5094 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005095 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005096 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005097 cast<NamedDecl>(D)->getNameAsString();
5098 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005099 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005100 }
5101}
5102
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005103// Annotation attributes are the only attributes allowed after an access
5104// specifier.
5105bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5106 const AttributeList *AttrList) {
5107 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005108 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005109 handleAnnotateAttr(*this, ASDecl, *l);
5110 } else {
5111 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5112 return true;
5113 }
5114 }
5115
5116 return false;
5117}
5118
John McCalle82247a2011-10-01 05:17:03 +00005119/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5120/// contains any decl attributes that we should warn about.
5121static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5122 for ( ; A; A = A->getNext()) {
5123 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005124 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005125 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5126
5127 if (A->getKind() == AttributeList::UnknownAttribute) {
5128 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5129 << A->getName() << A->getRange();
5130 } else {
5131 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5132 << A->getName() << A->getRange();
5133 }
5134 }
5135}
5136
5137/// checkUnusedDeclAttributes - Given a declarator which is not being
5138/// used to build a declaration, complain about any decl attributes
5139/// which might be lying around on it.
5140void Sema::checkUnusedDeclAttributes(Declarator &D) {
5141 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5142 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5143 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5144 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5145}
5146
Ryan Flynne25ff832009-07-30 03:15:39 +00005147/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005148/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005149NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5150 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005151 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005152 NamedDecl *NewD = 0;
5153 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005154 FunctionDecl *NewFD;
5155 // FIXME: Missing call to CheckFunctionDeclaration().
5156 // FIXME: Mangling?
5157 // FIXME: Is the qualifier info correct?
5158 // FIXME: Is the DeclContext correct?
5159 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5160 Loc, Loc, DeclarationName(II),
5161 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005162 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005163 FD->hasPrototype(),
5164 false/*isConstexprSpecified*/);
5165 NewD = NewFD;
5166
5167 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005168 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005169
5170 // Fake up parameter variables; they are declared as if this were
5171 // a typedef.
5172 QualType FDTy = FD->getType();
5173 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5174 SmallVector<ParmVarDecl*, 16> Params;
5175 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5176 AE = FT->arg_type_end(); AI != AE; ++AI) {
5177 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5178 Param->setScopeInfo(0, Params.size());
5179 Params.push_back(Param);
5180 }
David Blaikie4278c652011-09-21 18:16:56 +00005181 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005182 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005183 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5184 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005185 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005186 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005187 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005188 if (VD->getQualifier()) {
5189 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005190 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005191 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005192 }
5193 return NewD;
5194}
5195
James Dennett1dfbd922012-06-14 21:40:34 +00005196/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005197/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005198void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005199 if (W.getUsed()) return; // only do this once
5200 W.setUsed(true);
5201 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5202 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005203 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005204 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5205 NDId->getName()));
5206 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005207 WeakTopLevelDecl.push_back(NewD);
5208 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5209 // to insert Decl at TU scope, sorry.
5210 DeclContext *SavedContext = CurContext;
5211 CurContext = Context.getTranslationUnitDecl();
5212 PushOnScopeChains(NewD, S);
5213 CurContext = SavedContext;
5214 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005215 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005216 }
5217}
5218
Rafael Espindola65611bf2013-03-02 21:41:48 +00005219void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5220 // It's valid to "forward-declare" #pragma weak, in which case we
5221 // have to do this.
5222 LoadExternalWeakUndeclaredIdentifiers();
5223 if (!WeakUndeclaredIdentifiers.empty()) {
5224 NamedDecl *ND = NULL;
5225 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5226 if (VD->isExternC())
5227 ND = VD;
5228 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5229 if (FD->isExternC())
5230 ND = FD;
5231 if (ND) {
5232 if (IdentifierInfo *Id = ND->getIdentifier()) {
5233 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5234 = WeakUndeclaredIdentifiers.find(Id);
5235 if (I != WeakUndeclaredIdentifiers.end()) {
5236 WeakInfo W = I->second;
5237 DeclApplyPragmaWeak(S, ND, W);
5238 WeakUndeclaredIdentifiers[Id] = W;
5239 }
5240 }
5241 }
5242 }
5243}
5244
Chris Lattner0744e5f2008-06-29 00:23:49 +00005245/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5246/// it, apply them to D. This is a bit tricky because PD can have attributes
5247/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005248void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5249 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005250 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005251 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005252 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005253
Chris Lattner0744e5f2008-06-29 00:23:49 +00005254 // Walk the declarator structure, applying decl attributes that were in a type
5255 // position to the decl itself. This handles cases like:
5256 // int *__attr__(x)** D;
5257 // when X is a decl attribute.
5258 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5259 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005260 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5261 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005262
Chris Lattner0744e5f2008-06-29 00:23:49 +00005263 // Finally, apply any attributes on the decl itself.
5264 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005265 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005266}
John McCall54abf7d2009-11-04 02:18:39 +00005267
John McCallf85e1932011-06-15 23:02:42 +00005268/// Is the given declaration allowed to use a forbidden type?
5269static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5270 // Private ivars are always okay. Unfortunately, people don't
5271 // always properly make their ivars private, even in system headers.
5272 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005273 // Function declarations in sys headers will be marked unavailable.
5274 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5275 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005276 return false;
5277
5278 // Require it to be declared in a system header.
5279 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5280}
5281
5282/// Handle a delayed forbidden-type diagnostic.
5283static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5284 Decl *decl) {
5285 if (decl && isForbiddenTypeAllowed(S, decl)) {
5286 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5287 "this system declaration uses an unsupported type"));
5288 return;
5289 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005290 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005291 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005292 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005293 // kind of forbidden type messages on unavailable functions.
5294 if (FD->hasAttr<UnavailableAttr>() &&
5295 diag.getForbiddenTypeDiagnostic() ==
5296 diag::err_arc_array_param_no_ownership) {
5297 diag.Triggered = true;
5298 return;
5299 }
5300 }
John McCallf85e1932011-06-15 23:02:42 +00005301
5302 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5303 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5304 diag.Triggered = true;
5305}
5306
John McCall92576642012-05-07 06:16:41 +00005307void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5308 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005309 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005310 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005311
John McCall92576642012-05-07 06:16:41 +00005312 // When delaying diagnostics to run in the context of a parsed
5313 // declaration, we only want to actually emit anything if parsing
5314 // succeeds.
5315 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005316
John McCall92576642012-05-07 06:16:41 +00005317 // We emit all the active diagnostics in this pool or any of its
5318 // parents. In general, we'll get one pool for the decl spec
5319 // and a child pool for each declarator; in a decl group like:
5320 // deprecated_typedef foo, *bar, baz();
5321 // only the declarator pops will be passed decls. This is correct;
5322 // we really do need to consider delayed diagnostics from the decl spec
5323 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005324 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005325 do {
John McCall13489672012-05-07 06:16:58 +00005326 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005327 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5328 // This const_cast is a bit lame. Really, Triggered should be mutable.
5329 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005330 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005331 continue;
5332
John McCalleee1d542011-02-14 07:13:47 +00005333 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005334 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005335 // Don't bother giving deprecation diagnostics if the decl is invalid.
5336 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005337 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005338 break;
5339
5340 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005341 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005342 break;
John McCallf85e1932011-06-15 23:02:42 +00005343
5344 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005345 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005346 break;
John McCall2f514482010-01-27 03:50:35 +00005347 }
5348 }
John McCall92576642012-05-07 06:16:41 +00005349 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005350}
5351
John McCall13489672012-05-07 06:16:58 +00005352/// Given a set of delayed diagnostics, re-emit them as if they had
5353/// been delayed in the current context instead of in the given pool.
5354/// Essentially, this just moves them to the current pool.
5355void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5356 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5357 assert(curPool && "re-emitting in undelayed context not supported");
5358 curPool->steal(pool);
5359}
5360
John McCall54abf7d2009-11-04 02:18:39 +00005361static bool isDeclDeprecated(Decl *D) {
5362 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005363 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005364 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005365 // A category implicitly has the availability of the interface.
5366 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5367 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005368 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5369 return false;
5370}
5371
Eli Friedmanc3b23082012-08-08 21:52:41 +00005372static void
5373DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5374 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005375 const ObjCInterfaceDecl *UnknownObjCClass,
5376 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005377 DeclarationName Name = D->getDeclName();
5378 if (!Message.empty()) {
5379 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5380 S.Diag(D->getLocation(),
5381 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5382 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005383 if (ObjCPropery)
5384 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5385 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005386 } else if (!UnknownObjCClass) {
5387 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5388 S.Diag(D->getLocation(),
5389 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5390 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005391 if (ObjCPropery)
5392 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5393 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005394 } else {
5395 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5396 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5397 }
5398}
5399
John McCall9c3087b2010-08-26 02:13:20 +00005400void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005401 Decl *Ctx) {
5402 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005403 return;
5404
John McCall2f514482010-01-27 03:50:35 +00005405 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005406 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5407 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005408 DD.getUnknownObjCClass(),
5409 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005410}
5411
Chris Lattner5f9e2722011-07-23 10:55:15 +00005412void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005413 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005414 const ObjCInterfaceDecl *UnknownObjCClass,
5415 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005416 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005417 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005418 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5419 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005420 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005421 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005422 return;
5423 }
5424
5425 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005426 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005427 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005428 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005429}