blob: a3e05e69d2a732474f6b852caf3d591e0c6d1b63 [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.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001550 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001551 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001552
Peter Collingbourne7a730022010-11-23 20:45:58 +00001553 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001554 Arg = Arg->IgnoreParenCasts();
1555 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001556
Douglas Gregor5cee1192011-07-27 05:40:30 +00001557 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001558 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001559 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001560 return;
1561 }
Mike Stumpbf916502009-07-24 19:02:52 +00001562
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001563 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001564 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1565 return;
1566 }
1567
Chris Lattner6b6b5372008-06-26 18:38:35 +00001568 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001569
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001570 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001571 Str->getString(),
1572 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001573}
1574
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001575static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1576 // Check the attribute arguments.
1577 if (!checkAttributeNumArgs(S, Attr, 0))
1578 return;
1579
1580 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1582 << Attr.getName() << ExpectedFunctionOrMethod;
1583 return;
1584 }
1585
Michael Han51d8c522013-01-24 16:46:58 +00001586 D->addAttr(::new (S.Context)
1587 MinSizeAttr(Attr.getRange(), S.Context,
1588 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001589}
1590
Benjamin Krameree409a92012-05-12 21:10:52 +00001591static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1592 // Check the attribute arguments.
1593 if (!checkAttributeNumArgs(S, Attr, 0))
1594 return;
1595
1596 if (!isa<FunctionDecl>(D)) {
1597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1598 << Attr.getName() << ExpectedFunction;
1599 return;
1600 }
1601
1602 if (D->hasAttr<HotAttr>()) {
1603 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1604 << Attr.getName() << "hot";
1605 return;
1606 }
1607
Michael Han51d8c522013-01-24 16:46:58 +00001608 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1609 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001610}
1611
1612static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1613 // Check the attribute arguments.
1614 if (!checkAttributeNumArgs(S, Attr, 0))
1615 return;
1616
1617 if (!isa<FunctionDecl>(D)) {
1618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1619 << Attr.getName() << ExpectedFunction;
1620 return;
1621 }
1622
1623 if (D->hasAttr<ColdAttr>()) {
1624 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1625 << Attr.getName() << "cold";
1626 return;
1627 }
1628
Michael Han51d8c522013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1630 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001631}
1632
Chandler Carruth1b03c872011-07-02 00:01:44 +00001633static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001634 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001635 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001636 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001637
Chandler Carruth87c44602011-07-01 23:49:12 +00001638 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001639 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001640 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001641 return;
1642 }
1643
Michael Han51d8c522013-01-24 16:46:58 +00001644 D->addAttr(::new (S.Context)
1645 NakedAttr(Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001647}
1648
Chandler Carruth1b03c872011-07-02 00:01:44 +00001649static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1650 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001651 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001652 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1654 return;
1655 }
1656
Chandler Carruth87c44602011-07-01 23:49:12 +00001657 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001659 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001660 return;
1661 }
Mike Stumpbf916502009-07-24 19:02:52 +00001662
Michael Han51d8c522013-01-24 16:46:58 +00001663 D->addAttr(::new (S.Context)
1664 AlwaysInlineAttr(Attr.getRange(), S.Context,
1665 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001666}
1667
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001668static void handleTLSModelAttr(Sema &S, Decl *D,
1669 const AttributeList &Attr) {
1670 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001671 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001672 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001673
1674 Expr *Arg = Attr.getArg(0);
1675 Arg = Arg->IgnoreParenCasts();
1676 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1677
1678 // Check that it is a string.
1679 if (!Str) {
1680 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1681 return;
1682 }
1683
Richard Smith38afbc72013-04-13 02:43:54 +00001684 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1686 << Attr.getName() << ExpectedTLSVar;
1687 return;
1688 }
1689
1690 // Check that the value.
1691 StringRef Model = Str->getString();
1692 if (Model != "global-dynamic" && Model != "local-dynamic"
1693 && Model != "initial-exec" && Model != "local-exec") {
1694 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1695 return;
1696 }
1697
Michael Han51d8c522013-01-24 16:46:58 +00001698 D->addAttr(::new (S.Context)
1699 TLSModelAttr(Attr.getRange(), S.Context, Model,
1700 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001701}
1702
Chandler Carruth1b03c872011-07-02 00:01:44 +00001703static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001704 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001705 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1707 return;
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Chandler Carruth87c44602011-07-01 23:49:12 +00001710 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001711 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001712 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001713 D->addAttr(::new (S.Context)
1714 MallocAttr(Attr.getRange(), S.Context,
1715 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001716 return;
1717 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001718 }
1719
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001720 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001721}
1722
Chandler Carruth1b03c872011-07-02 00:01:44 +00001723static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001724 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001725 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001726 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001727
Michael Han51d8c522013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
1729 MayAliasAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001731}
1732
Chandler Carruth1b03c872011-07-02 00:01:44 +00001733static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001734 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001735 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001736 D->addAttr(::new (S.Context)
1737 NoCommonAttr(Attr.getRange(), S.Context,
1738 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001739 else
1740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001741 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001742}
1743
Chandler Carruth1b03c872011-07-02 00:01:44 +00001744static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001745 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001746
1747 if (S.LangOpts.CPlusPlus) {
1748 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1749 return;
1750 }
1751
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context)
1754 CommonAttr(Attr.getRange(), S.Context,
1755 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001756 else
1757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001758 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001759}
1760
Chandler Carruth1b03c872011-07-02 00:01:44 +00001761static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001762 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001763
1764 if (S.CheckNoReturnAttr(attr)) return;
1765
Chandler Carruth87c44602011-07-01 23:49:12 +00001766 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001767 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001768 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001769 return;
1770 }
1771
Michael Han51d8c522013-01-24 16:46:58 +00001772 D->addAttr(::new (S.Context)
1773 NoReturnAttr(attr.getRange(), S.Context,
1774 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001775}
1776
1777bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001778 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001779 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1780 attr.setInvalid();
1781 return true;
1782 }
1783
1784 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001785}
1786
Chandler Carruth1b03c872011-07-02 00:01:44 +00001787static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1788 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001789
1790 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1791 // because 'analyzer_noreturn' does not impact the type.
1792
Chandler Carruth1731e202011-07-11 23:30:35 +00001793 if(!checkAttributeNumArgs(S, Attr, 0))
1794 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001795
Chandler Carruth87c44602011-07-01 23:49:12 +00001796 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1797 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001798 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1799 && !VD->getType()->isFunctionPointerType())) {
1800 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001801 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001802 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001803 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001804 return;
1805 }
1806 }
1807
Michael Han51d8c522013-01-24 16:46:58 +00001808 D->addAttr(::new (S.Context)
1809 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1810 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001811}
1812
Richard Smithcd8ab512013-01-17 01:30:42 +00001813static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1814 const AttributeList &Attr) {
1815 // C++11 [dcl.attr.noreturn]p1:
1816 // The attribute may be applied to the declarator-id in a function
1817 // declaration.
1818 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1819 if (!FD) {
1820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1821 << Attr.getName() << ExpectedFunctionOrMethod;
1822 return;
1823 }
1824
Michael Han51d8c522013-01-24 16:46:58 +00001825 D->addAttr(::new (S.Context)
1826 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1827 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001828}
1829
John Thompson35cc9622010-08-09 21:53:52 +00001830// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001831static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001832/*
1833 Returning a Vector Class in Registers
1834
Eric Christopherf48f3672010-12-01 22:13:54 +00001835 According to the PPU ABI specifications, a class with a single member of
1836 vector type is returned in memory when used as the return value of a function.
1837 This results in inefficient code when implementing vector classes. To return
1838 the value in a single vector register, add the vecreturn attribute to the
1839 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001840
1841 Example:
1842
1843 struct Vector
1844 {
1845 __vector float xyzw;
1846 } __attribute__((vecreturn));
1847
1848 Vector Add(Vector lhs, Vector rhs)
1849 {
1850 Vector result;
1851 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1852 return result; // This will be returned in a register
1853 }
1854*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001855 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001856 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001857 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001858 return;
1859 }
1860
Chandler Carruth87c44602011-07-01 23:49:12 +00001861 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001862 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1863 return;
1864 }
1865
Chandler Carruth87c44602011-07-01 23:49:12 +00001866 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001867 int count = 0;
1868
1869 if (!isa<CXXRecordDecl>(record)) {
1870 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1871 return;
1872 }
1873
1874 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1875 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1876 return;
1877 }
1878
Eric Christopherf48f3672010-12-01 22:13:54 +00001879 for (RecordDecl::field_iterator iter = record->field_begin();
1880 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001881 if ((count == 1) || !iter->getType()->isVectorType()) {
1882 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1883 return;
1884 }
1885 count++;
1886 }
1887
Michael Han51d8c522013-01-24 16:46:58 +00001888 D->addAttr(::new (S.Context)
1889 VecReturnAttr(Attr.getRange(), S.Context,
1890 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001891}
1892
Richard Smith3a2b7a12013-01-28 22:42:45 +00001893static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1894 const AttributeList &Attr) {
1895 if (isa<ParmVarDecl>(D)) {
1896 // [[carries_dependency]] can only be applied to a parameter if it is a
1897 // parameter of a function declaration or lambda.
1898 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1899 S.Diag(Attr.getLoc(),
1900 diag::err_carries_dependency_param_not_function_decl);
1901 return;
1902 }
1903 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001904 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001905 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001906 return;
1907 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001908
1909 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1910 Attr.getRange(), S.Context,
1911 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001912}
1913
Chandler Carruth1b03c872011-07-02 00:01:44 +00001914static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001915 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001916 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001918 return;
1919 }
Mike Stumpbf916502009-07-24 19:02:52 +00001920
Chandler Carruth87c44602011-07-01 23:49:12 +00001921 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001922 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001924 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001925 return;
1926 }
Mike Stumpbf916502009-07-24 19:02:52 +00001927
Michael Han51d8c522013-01-24 16:46:58 +00001928 D->addAttr(::new (S.Context)
1929 UnusedAttr(Attr.getRange(), S.Context,
1930 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001931}
1932
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001933static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1934 const AttributeList &Attr) {
1935 // check the attribute arguments.
1936 if (Attr.hasParameterOrArguments()) {
1937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1938 return;
1939 }
1940
1941 if (!isa<FunctionDecl>(D)) {
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1943 << Attr.getName() << ExpectedFunction;
1944 return;
1945 }
1946
Michael Han51d8c522013-01-24 16:46:58 +00001947 D->addAttr(::new (S.Context)
1948 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1949 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001950}
1951
Chandler Carruth1b03c872011-07-02 00:01:44 +00001952static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001953 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001954 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001955 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1956 return;
1957 }
Mike Stumpbf916502009-07-24 19:02:52 +00001958
Chandler Carruth87c44602011-07-01 23:49:12 +00001959 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001960 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001961 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1962 return;
1963 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001964 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001966 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001967 return;
1968 }
Mike Stumpbf916502009-07-24 19:02:52 +00001969
Michael Han51d8c522013-01-24 16:46:58 +00001970 D->addAttr(::new (S.Context)
1971 UsedAttr(Attr.getRange(), S.Context,
1972 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001973}
1974
Chandler Carruth1b03c872011-07-02 00:01:44 +00001975static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001976 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001977 if (Attr.getNumArgs() > 1) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001979 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001980 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001981
1982 int priority = 65535; // FIXME: Do not hardcode such constants.
1983 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001984 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001985 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001986 if (E->isTypeDependent() || E->isValueDependent() ||
1987 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001989 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001990 return;
1991 }
1992 priority = Idx.getZExtValue();
1993 }
Mike Stumpbf916502009-07-24 19:02:52 +00001994
Chandler Carruth87c44602011-07-01 23:49:12 +00001995 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001997 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001998 return;
1999 }
2000
Michael Han51d8c522013-01-24 16:46:58 +00002001 D->addAttr(::new (S.Context)
2002 ConstructorAttr(Attr.getRange(), S.Context, priority,
2003 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002004}
2005
Chandler Carruth1b03c872011-07-02 00:01:44 +00002006static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002007 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002008 if (Attr.getNumArgs() > 1) {
2009 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002010 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002011 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002012
2013 int priority = 65535; // FIXME: Do not hardcode such constants.
2014 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002015 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002016 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002017 if (E->isTypeDependent() || E->isValueDependent() ||
2018 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002019 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002020 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002021 return;
2022 }
2023 priority = Idx.getZExtValue();
2024 }
Mike Stumpbf916502009-07-24 19:02:52 +00002025
Chandler Carruth87c44602011-07-01 23:49:12 +00002026 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002027 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002028 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002029 return;
2030 }
2031
Michael Han51d8c522013-01-24 16:46:58 +00002032 D->addAttr(::new (S.Context)
2033 DestructorAttr(Attr.getRange(), S.Context, priority,
2034 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002035}
2036
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002037template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002038static void handleAttrWithMessage(Sema &S, Decl *D,
2039 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002040 unsigned NumArgs = Attr.getNumArgs();
2041 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002042 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002043 return;
2044 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002045
2046 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002047 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002048 if (NumArgs == 1) {
2049 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002050 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002051 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002052 << Attr.getName();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002053 return;
2054 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002055 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002056 }
Mike Stumpbf916502009-07-24 19:02:52 +00002057
Michael Han51d8c522013-01-24 16:46:58 +00002058 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2059 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002060}
2061
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002062static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2063 const AttributeList &Attr) {
2064 unsigned NumArgs = Attr.getNumArgs();
2065 if (NumArgs > 0) {
2066 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2067 return;
2068 }
2069
Michael Han51d8c522013-01-24 16:46:58 +00002070 D->addAttr(::new (S.Context)
2071 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2072 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002073}
2074
Patrick Beardb2f68202012-04-06 18:12:22 +00002075static void handleObjCRootClassAttr(Sema &S, Decl *D,
2076 const AttributeList &Attr) {
2077 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2079 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002080 return;
2081 }
2082
2083 unsigned NumArgs = Attr.getNumArgs();
2084 if (NumArgs > 0) {
2085 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2086 return;
2087 }
2088
Michael Han51d8c522013-01-24 16:46:58 +00002089 D->addAttr(::new (S.Context)
2090 ObjCRootClassAttr(Attr.getRange(), S.Context,
2091 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002092}
2093
Michael Han51d8c522013-01-24 16:46:58 +00002094static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2095 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002096 if (!isa<ObjCInterfaceDecl>(D)) {
2097 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2098 return;
2099 }
2100
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002101 unsigned NumArgs = Attr.getNumArgs();
2102 if (NumArgs > 0) {
2103 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2104 return;
2105 }
2106
Michael Han51d8c522013-01-24 16:46:58 +00002107 D->addAttr(::new (S.Context)
2108 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2109 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002110}
2111
Jordy Rosefad5de92012-05-08 03:27:22 +00002112static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2113 IdentifierInfo *Platform,
2114 VersionTuple Introduced,
2115 VersionTuple Deprecated,
2116 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002117 StringRef PlatformName
2118 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2119 if (PlatformName.empty())
2120 PlatformName = Platform->getName();
2121
2122 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2123 // of these steps are needed).
2124 if (!Introduced.empty() && !Deprecated.empty() &&
2125 !(Introduced <= Deprecated)) {
2126 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2127 << 1 << PlatformName << Deprecated.getAsString()
2128 << 0 << Introduced.getAsString();
2129 return true;
2130 }
2131
2132 if (!Introduced.empty() && !Obsoleted.empty() &&
2133 !(Introduced <= Obsoleted)) {
2134 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2135 << 2 << PlatformName << Obsoleted.getAsString()
2136 << 0 << Introduced.getAsString();
2137 return true;
2138 }
2139
2140 if (!Deprecated.empty() && !Obsoleted.empty() &&
2141 !(Deprecated <= Obsoleted)) {
2142 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2143 << 2 << PlatformName << Obsoleted.getAsString()
2144 << 1 << Deprecated.getAsString();
2145 return true;
2146 }
2147
2148 return false;
2149}
2150
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002151/// \brief Check whether the two versions match.
2152///
2153/// If either version tuple is empty, then they are assumed to match. If
2154/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2155static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2156 bool BeforeIsOkay) {
2157 if (X.empty() || Y.empty())
2158 return true;
2159
2160 if (X == Y)
2161 return true;
2162
2163 if (BeforeIsOkay && X < Y)
2164 return true;
2165
2166 return false;
2167}
2168
Rafael Espindola51be6e32013-01-08 22:04:34 +00002169AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002170 IdentifierInfo *Platform,
2171 VersionTuple Introduced,
2172 VersionTuple Deprecated,
2173 VersionTuple Obsoleted,
2174 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002175 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002176 bool Override,
2177 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002178 VersionTuple MergedIntroduced = Introduced;
2179 VersionTuple MergedDeprecated = Deprecated;
2180 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002181 bool FoundAny = false;
2182
Rafael Espindola98ae8342012-05-10 02:50:16 +00002183 if (D->hasAttrs()) {
2184 AttrVec &Attrs = D->getAttrs();
2185 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2186 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2187 if (!OldAA) {
2188 ++i;
2189 continue;
2190 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002191
Rafael Espindola98ae8342012-05-10 02:50:16 +00002192 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2193 if (OldPlatform != Platform) {
2194 ++i;
2195 continue;
2196 }
2197
2198 FoundAny = true;
2199 VersionTuple OldIntroduced = OldAA->getIntroduced();
2200 VersionTuple OldDeprecated = OldAA->getDeprecated();
2201 VersionTuple OldObsoleted = OldAA->getObsoleted();
2202 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002203
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002204 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2205 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2206 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2207 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002208 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002209 if (Override) {
2210 int Which = -1;
2211 VersionTuple FirstVersion;
2212 VersionTuple SecondVersion;
2213 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2214 Which = 0;
2215 FirstVersion = OldIntroduced;
2216 SecondVersion = Introduced;
2217 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2218 Which = 1;
2219 FirstVersion = Deprecated;
2220 SecondVersion = OldDeprecated;
2221 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2222 Which = 2;
2223 FirstVersion = Obsoleted;
2224 SecondVersion = OldObsoleted;
2225 }
2226
2227 if (Which == -1) {
2228 Diag(OldAA->getLocation(),
2229 diag::warn_mismatched_availability_override_unavail)
2230 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2231 } else {
2232 Diag(OldAA->getLocation(),
2233 diag::warn_mismatched_availability_override)
2234 << Which
2235 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2236 << FirstVersion.getAsString() << SecondVersion.getAsString();
2237 }
2238 Diag(Range.getBegin(), diag::note_overridden_method);
2239 } else {
2240 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2241 Diag(Range.getBegin(), diag::note_previous_attribute);
2242 }
2243
Rafael Espindola98ae8342012-05-10 02:50:16 +00002244 Attrs.erase(Attrs.begin() + i);
2245 --e;
2246 continue;
2247 }
2248
2249 VersionTuple MergedIntroduced2 = MergedIntroduced;
2250 VersionTuple MergedDeprecated2 = MergedDeprecated;
2251 VersionTuple MergedObsoleted2 = MergedObsoleted;
2252
2253 if (MergedIntroduced2.empty())
2254 MergedIntroduced2 = OldIntroduced;
2255 if (MergedDeprecated2.empty())
2256 MergedDeprecated2 = OldDeprecated;
2257 if (MergedObsoleted2.empty())
2258 MergedObsoleted2 = OldObsoleted;
2259
2260 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2261 MergedIntroduced2, MergedDeprecated2,
2262 MergedObsoleted2)) {
2263 Attrs.erase(Attrs.begin() + i);
2264 --e;
2265 continue;
2266 }
2267
2268 MergedIntroduced = MergedIntroduced2;
2269 MergedDeprecated = MergedDeprecated2;
2270 MergedObsoleted = MergedObsoleted2;
2271 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002272 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002273 }
2274
2275 if (FoundAny &&
2276 MergedIntroduced == Introduced &&
2277 MergedDeprecated == Deprecated &&
2278 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002279 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002280
Ted Kremenekcb344392013-04-06 00:34:27 +00002281 // Only create a new attribute if !Override, but we want to do
2282 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002283 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002284 MergedDeprecated, MergedObsoleted) &&
2285 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002286 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2287 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002288 Obsoleted, IsUnavailable, Message,
2289 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002290 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002291 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002292}
2293
Chandler Carruth1b03c872011-07-02 00:01:44 +00002294static void handleAvailabilityAttr(Sema &S, Decl *D,
2295 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002296 IdentifierInfo *Platform = Attr.getParameterName();
2297 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002298 unsigned Index = Attr.getAttributeSpellingListIndex();
2299
Rafael Espindola3b294362012-05-06 19:56:25 +00002300 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002301 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2302 << Platform;
2303
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002304 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2305 if (!ND) {
2306 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2307 return;
2308 }
2309
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002310 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2311 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2312 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002313 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002314 StringRef Str;
2315 const StringLiteral *SE =
2316 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2317 if (SE)
2318 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002319
Rafael Espindola51be6e32013-01-08 22:04:34 +00002320 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002321 Platform,
2322 Introduced.Version,
2323 Deprecated.Version,
2324 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002325 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002326 /*Override=*/false,
2327 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002328 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002329 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002330}
2331
John McCalld4c3d662013-02-20 01:54:26 +00002332template <class T>
2333static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2334 typename T::VisibilityType value,
2335 unsigned attrSpellingListIndex) {
2336 T *existingAttr = D->getAttr<T>();
2337 if (existingAttr) {
2338 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2339 if (existingValue == value)
2340 return NULL;
2341 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2342 S.Diag(range.getBegin(), diag::note_previous_attribute);
2343 D->dropAttr<T>();
2344 }
2345 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2346}
2347
Rafael Espindola599f1b72012-05-13 03:25:18 +00002348VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002349 VisibilityAttr::VisibilityType Vis,
2350 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002351 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2352 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002353}
2354
John McCalld4c3d662013-02-20 01:54:26 +00002355TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2356 TypeVisibilityAttr::VisibilityType Vis,
2357 unsigned AttrSpellingListIndex) {
2358 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2359 AttrSpellingListIndex);
2360}
2361
2362static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2363 bool isTypeVisibility) {
2364 // Visibility attributes don't mean anything on a typedef.
2365 if (isa<TypedefNameDecl>(D)) {
2366 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2367 << Attr.getName();
2368 return;
2369 }
2370
2371 // 'type_visibility' can only go on a type or namespace.
2372 if (isTypeVisibility &&
2373 !(isa<TagDecl>(D) ||
2374 isa<ObjCInterfaceDecl>(D) ||
2375 isa<NamespaceDecl>(D))) {
2376 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2377 << Attr.getName() << ExpectedTypeOrNamespace;
2378 return;
2379 }
2380
Chris Lattner6b6b5372008-06-26 18:38:35 +00002381 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002382 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002383 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002384
Peter Collingbourne7a730022010-11-23 20:45:58 +00002385 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002386 Arg = Arg->IgnoreParenCasts();
2387 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002388
Douglas Gregor5cee1192011-07-27 05:40:30 +00002389 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002390 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld4c3d662013-02-20 01:54:26 +00002391 << (isTypeVisibility ? "type_visibility" : "visibility") << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002392 return;
2393 }
Mike Stumpbf916502009-07-24 19:02:52 +00002394
Chris Lattner5f9e2722011-07-23 10:55:15 +00002395 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002396 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002397
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002398 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002399 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002400 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002401 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002402 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002403 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002404 else if (TypeStr == "protected") {
2405 // Complain about attempts to use protected visibility on targets
2406 // (like Darwin) that don't support it.
2407 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2408 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2409 type = VisibilityAttr::Default;
2410 } else {
2411 type = VisibilityAttr::Protected;
2412 }
2413 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002414 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002415 return;
2416 }
Mike Stumpbf916502009-07-24 19:02:52 +00002417
Michael Han51d8c522013-01-24 16:46:58 +00002418 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002419 clang::Attr *newAttr;
2420 if (isTypeVisibility) {
2421 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2422 (TypeVisibilityAttr::VisibilityType) type,
2423 Index);
2424 } else {
2425 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2426 }
2427 if (newAttr)
2428 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002429}
2430
Chandler Carruth1b03c872011-07-02 00:01:44 +00002431static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2432 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002433 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2434 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002435 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002436 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002437 return;
2438 }
2439
Chandler Carruth87c44602011-07-01 23:49:12 +00002440 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2441 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002443 << "objc_method_family" << 1;
2444 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002445 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002446 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002447 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002448 return;
2449 }
2450
Chris Lattner5f9e2722011-07-23 10:55:15 +00002451 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002452 ObjCMethodFamilyAttr::FamilyKind family;
2453 if (param == "none")
2454 family = ObjCMethodFamilyAttr::OMF_None;
2455 else if (param == "alloc")
2456 family = ObjCMethodFamilyAttr::OMF_alloc;
2457 else if (param == "copy")
2458 family = ObjCMethodFamilyAttr::OMF_copy;
2459 else if (param == "init")
2460 family = ObjCMethodFamilyAttr::OMF_init;
2461 else if (param == "mutableCopy")
2462 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2463 else if (param == "new")
2464 family = ObjCMethodFamilyAttr::OMF_new;
2465 else {
2466 // Just warn and ignore it. This is future-proof against new
2467 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002468 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002469 return;
2470 }
2471
John McCallf85e1932011-06-15 23:02:42 +00002472 if (family == ObjCMethodFamilyAttr::OMF_init &&
2473 !method->getResultType()->isObjCObjectPointerType()) {
2474 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2475 << method->getResultType();
2476 // Ignore the attribute.
2477 return;
2478 }
2479
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002480 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002481 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002482}
2483
Chandler Carruth1b03c872011-07-02 00:01:44 +00002484static void handleObjCExceptionAttr(Sema &S, Decl *D,
2485 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002486 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002487 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002488
Chris Lattner0db29ec2009-02-14 08:09:34 +00002489 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2490 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2492 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002493 return;
2494 }
Mike Stumpbf916502009-07-24 19:02:52 +00002495
Michael Han51d8c522013-01-24 16:46:58 +00002496 D->addAttr(::new (S.Context)
2497 ObjCExceptionAttr(Attr.getRange(), S.Context,
2498 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002499}
2500
Chandler Carruth1b03c872011-07-02 00:01:44 +00002501static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002502 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002503 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002504 return;
2505 }
Richard Smith162e1c12011-04-15 14:24:37 +00002506 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002507 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002508 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002509 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2510 return;
2511 }
2512 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002513 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2514 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002515 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002516 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2517 return;
2518 }
2519 }
2520 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002521 // It is okay to include this attribute on properties, e.g.:
2522 //
2523 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2524 //
2525 // In this case it follows tradition and suppresses an error in the above
2526 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002527 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002528 }
Michael Han51d8c522013-01-24 16:46:58 +00002529 D->addAttr(::new (S.Context)
2530 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2531 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002532}
2533
Mike Stumpbf916502009-07-24 19:02:52 +00002534static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002535handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002536 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002538 return;
2539 }
2540
2541 if (!isa<FunctionDecl>(D)) {
2542 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2543 return;
2544 }
2545
Michael Han51d8c522013-01-24 16:46:58 +00002546 D->addAttr(::new (S.Context)
2547 OverloadableAttr(Attr.getRange(), S.Context,
2548 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002549}
2550
Chandler Carruth1b03c872011-07-02 00:01:44 +00002551static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002552 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002553 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002554 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002555 return;
2556 }
Mike Stumpbf916502009-07-24 19:02:52 +00002557
Steve Naroff9eae5762008-09-18 16:44:58 +00002558 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002560 return;
2561 }
Mike Stumpbf916502009-07-24 19:02:52 +00002562
Sean Huntcf807c42010-08-18 23:23:40 +00002563 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002564 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002565 type = BlocksAttr::ByRef;
2566 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002568 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002569 return;
2570 }
Mike Stumpbf916502009-07-24 19:02:52 +00002571
Michael Han51d8c522013-01-24 16:46:58 +00002572 D->addAttr(::new (S.Context)
2573 BlocksAttr(Attr.getRange(), S.Context, type,
2574 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002575}
2576
Chandler Carruth1b03c872011-07-02 00:01:44 +00002577static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002578 // check the attribute arguments.
2579 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002580 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002581 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002582 }
2583
John McCall3323fad2011-09-09 07:56:05 +00002584 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002585 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002586 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002587 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002588 if (E->isTypeDependent() || E->isValueDependent() ||
2589 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002591 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002592 return;
2593 }
Mike Stumpbf916502009-07-24 19:02:52 +00002594
John McCall3323fad2011-09-09 07:56:05 +00002595 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2597 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002598 return;
2599 }
John McCall3323fad2011-09-09 07:56:05 +00002600
2601 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002602 }
2603
John McCall3323fad2011-09-09 07:56:05 +00002604 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002605 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002606 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002607 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002608 if (E->isTypeDependent() || E->isValueDependent() ||
2609 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002611 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002612 return;
2613 }
2614 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002615
John McCall3323fad2011-09-09 07:56:05 +00002616 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002617 // FIXME: This error message could be improved, it would be nice
2618 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2620 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002621 return;
2622 }
2623 }
2624
Chandler Carruth87c44602011-07-01 23:49:12 +00002625 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002626 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002627 if (isa<FunctionNoProtoType>(FT)) {
2628 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2629 return;
2630 }
Mike Stumpbf916502009-07-24 19:02:52 +00002631
Chris Lattner897cd902009-03-17 23:03:47 +00002632 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002633 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002634 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002635 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002636 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002637 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002638 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002639 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002640 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002641 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2642 if (!BD->isVariadic()) {
2643 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2644 return;
2645 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002646 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002647 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002648 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002649 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002650 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002651 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002652 int m = Ty->isFunctionPointerType() ? 0 : 1;
2653 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002654 return;
2655 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002656 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002658 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002659 return;
2660 }
Anders Carlsson77091822008-10-05 18:05:59 +00002661 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002663 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002664 return;
2665 }
Michael Han51d8c522013-01-24 16:46:58 +00002666 D->addAttr(::new (S.Context)
2667 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2668 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002669}
2670
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002671static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2672 // Check the attribute arguments.
2673 if (!checkAttributeNumArgs(S, Attr, 0))
2674 return;
2675
2676 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2677 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2678 else
2679 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2680}
2681
Chandler Carruth1b03c872011-07-02 00:01:44 +00002682static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002683 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002684 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002685 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002686
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002687 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002689 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002690 return;
2691 }
Mike Stumpbf916502009-07-24 19:02:52 +00002692
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002693 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2694 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2695 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002696 return;
2697 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002698 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2699 if (MD->getResultType()->isVoidType()) {
2700 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2701 << Attr.getName() << 1;
2702 return;
2703 }
2704
Michael Han51d8c522013-01-24 16:46:58 +00002705 D->addAttr(::new (S.Context)
2706 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2707 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002708}
2709
Chandler Carruth1b03c872011-07-02 00:01:44 +00002710static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002711 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002712 if (Attr.hasParameterOrArguments()) {
2713 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002714 return;
2715 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002716
Chandler Carruth87c44602011-07-01 23:49:12 +00002717 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002718 if (isa<CXXRecordDecl>(D)) {
2719 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2720 return;
2721 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002722 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2723 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002724 return;
2725 }
2726
Chandler Carruth87c44602011-07-01 23:49:12 +00002727 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002728
Michael Han51d8c522013-01-24 16:46:58 +00002729 nd->addAttr(::new (S.Context)
2730 WeakAttr(Attr.getRange(), S.Context,
2731 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002732}
2733
Chandler Carruth1b03c872011-07-02 00:01:44 +00002734static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002735 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002736 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002737 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002738
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002739
2740 // weak_import only applies to variable & function declarations.
2741 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002742 if (!D->canBeWeakImported(isDef)) {
2743 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002744 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2745 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002746 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002747 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002748 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002749 // Nothing to warn about here.
2750 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002752 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002753
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002754 return;
2755 }
2756
Michael Han51d8c522013-01-24 16:46:58 +00002757 D->addAttr(::new (S.Context)
2758 WeakImportAttr(Attr.getRange(), S.Context,
2759 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002760}
2761
Tanya Lattner0df579e2012-07-09 22:06:01 +00002762// Handles reqd_work_group_size and work_group_size_hint.
2763static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002764 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002765 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2766 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2767
Nate Begeman6f3d8382009-06-26 06:32:41 +00002768 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002769 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002770
2771 unsigned WGSize[3];
2772 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002773 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002774 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002775 if (E->isTypeDependent() || E->isValueDependent() ||
2776 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002778 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002779 return;
2780 }
2781 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2782 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002783
2784 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2785 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2786 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2787 if (!(A->getXDim() == WGSize[0] &&
2788 A->getYDim() == WGSize[1] &&
2789 A->getZDim() == WGSize[2])) {
2790 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2791 Attr.getName();
2792 }
2793 }
2794
2795 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2796 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2797 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2798 if (!(A->getXDim() == WGSize[0] &&
2799 A->getYDim() == WGSize[1] &&
2800 A->getZDim() == WGSize[2])) {
2801 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2802 Attr.getName();
2803 }
2804 }
2805
2806 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2807 D->addAttr(::new (S.Context)
2808 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002809 WGSize[0], WGSize[1], WGSize[2],
2810 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002811 else
2812 D->addAttr(::new (S.Context)
2813 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002814 WGSize[0], WGSize[1], WGSize[2],
2815 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002816}
2817
Joey Gouly37453b92013-03-08 09:42:32 +00002818static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2819 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2820
2821 // Attribute has 1 argument.
2822 if (!checkAttributeNumArgs(S, Attr, 1))
2823 return;
2824
2825 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2826
2827 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2828 (ParmType->isBooleanType() ||
2829 !ParmType->isIntegralType(S.getASTContext()))) {
2830 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2831 << ParmType;
2832 return;
2833 }
2834
2835 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2836 D->hasAttr<VecTypeHintAttr>()) {
2837 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2838 if (A->getTypeHint() != ParmType) {
2839 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2840 return;
2841 }
2842 }
2843
2844 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2845 ParmType, Attr.getLoc()));
2846}
2847
Joey Gouly96cead52013-03-14 09:54:43 +00002848static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2849 if (!dyn_cast<VarDecl>(D))
2850 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2851 << 9;
2852 StringRef EndianType = Attr.getParameterName()->getName();
2853 if (EndianType != "host" && EndianType != "device")
2854 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2855}
2856
Rafael Espindola599f1b72012-05-13 03:25:18 +00002857SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002858 StringRef Name,
2859 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002860 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2861 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002862 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002863 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2864 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002865 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002866 }
Michael Han51d8c522013-01-24 16:46:58 +00002867 return ::new (Context) SectionAttr(Range, Context, Name,
2868 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002869}
2870
Chandler Carruth1b03c872011-07-02 00:01:44 +00002871static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002872 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002873 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002874 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002875
2876 // Make sure that there is a string literal as the sections's single
2877 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002878 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002879 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002880 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002881 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002882 return;
2883 }
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Chris Lattner797c3c42009-08-10 19:03:04 +00002885 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002886 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002887 if (!Error.empty()) {
2888 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2889 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002890 return;
2891 }
Mike Stump1eb44332009-09-09 15:08:12 +00002892
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002893 // This attribute cannot be applied to local variables.
2894 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2895 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2896 return;
2897 }
Michael Han51d8c522013-01-24 16:46:58 +00002898
2899 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002900 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002901 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002902 if (NewAttr)
2903 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002904}
2905
Chris Lattner6b6b5372008-06-26 18:38:35 +00002906
Chandler Carruth1b03c872011-07-02 00:01:44 +00002907static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002908 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002909 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002910 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002911 return;
2912 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002913
Chandler Carruth87c44602011-07-01 23:49:12 +00002914 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002915 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002916 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002917 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002918 D->addAttr(::new (S.Context)
2919 NoThrowAttr(Attr.getRange(), S.Context,
2920 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002921 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002922}
2923
Chandler Carruth1b03c872011-07-02 00:01:44 +00002924static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002925 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002926 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002928 return;
2929 }
Mike Stumpbf916502009-07-24 19:02:52 +00002930
Chandler Carruth87c44602011-07-01 23:49:12 +00002931 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002932 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002933 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002934 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002935 D->addAttr(::new (S.Context)
2936 ConstAttr(Attr.getRange(), S.Context,
2937 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002938 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002939}
2940
Chandler Carruth1b03c872011-07-02 00:01:44 +00002941static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002942 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002943 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002944 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002945
Michael Han51d8c522013-01-24 16:46:58 +00002946 D->addAttr(::new (S.Context)
2947 PureAttr(Attr.getRange(), S.Context,
2948 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002949}
2950
Chandler Carruth1b03c872011-07-02 00:01:44 +00002951static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002952 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002953 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2954 return;
2955 }
Mike Stumpbf916502009-07-24 19:02:52 +00002956
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002957 if (Attr.getNumArgs() != 0) {
2958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2959 return;
2960 }
Mike Stumpbf916502009-07-24 19:02:52 +00002961
Chandler Carruth87c44602011-07-01 23:49:12 +00002962 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002963
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002964 if (!VD || !VD->hasLocalStorage()) {
2965 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2966 return;
2967 }
Mike Stumpbf916502009-07-24 19:02:52 +00002968
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002969 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002970 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002971 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002972 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2973 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002974 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002975 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002976 Attr.getParameterName();
2977 return;
2978 }
Mike Stumpbf916502009-07-24 19:02:52 +00002979
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002980 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2981 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002982 S.Diag(Attr.getParameterLoc(),
2983 diag::err_attribute_cleanup_arg_not_function)
2984 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002985 return;
2986 }
2987
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002988 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002989 S.Diag(Attr.getParameterLoc(),
2990 diag::err_attribute_cleanup_func_must_take_one_arg)
2991 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002992 return;
2993 }
Mike Stumpbf916502009-07-24 19:02:52 +00002994
Anders Carlsson89941c12009-02-07 23:16:50 +00002995 // We're currently more strict than GCC about what function types we accept.
2996 // If this ever proves to be a problem it should be easy to fix.
2997 QualType Ty = S.Context.getPointerType(VD->getType());
2998 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002999 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3000 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00003001 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00003002 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3003 Attr.getParameterName() << ParamTy << Ty;
3004 return;
3005 }
Mike Stumpbf916502009-07-24 19:02:52 +00003006
Michael Han51d8c522013-01-24 16:46:58 +00003007 D->addAttr(::new (S.Context)
3008 CleanupAttr(Attr.getRange(), S.Context, FD,
3009 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00003010 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00003011 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003012}
3013
Mike Stumpbf916502009-07-24 19:02:52 +00003014/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003015/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003016static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003017 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003018 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003019
Chandler Carruth87c44602011-07-01 23:49:12 +00003020 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003021 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003022 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003023 return;
3024 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003025
3026 // In C++ the implicit 'this' function parameter also counts, and they are
3027 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003028 bool HasImplicitThisParam = isInstanceMethod(D);
3029 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003030 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003031
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003032 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003033 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003034 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003035 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3036 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003037 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3038 << "format" << 2 << IdxExpr->getSourceRange();
3039 return;
3040 }
Mike Stumpbf916502009-07-24 19:02:52 +00003041
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003042 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3044 << "format" << 2 << IdxExpr->getSourceRange();
3045 return;
3046 }
Mike Stumpbf916502009-07-24 19:02:52 +00003047
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003048 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003049
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003050 if (HasImplicitThisParam) {
3051 if (ArgIdx == 0) {
3052 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3053 << "format_arg" << IdxExpr->getSourceRange();
3054 return;
3055 }
3056 ArgIdx--;
3057 }
3058
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003059 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003060 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003061
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003062 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3063 if (not_nsstring_type &&
3064 !isCFStringType(Ty, S.Context) &&
3065 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003066 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003067 // FIXME: Should highlight the actual expression that has the wrong type.
3068 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003069 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003070 << IdxExpr->getSourceRange();
3071 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003072 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003073 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003074 if (!isNSStringType(Ty, S.Context) &&
3075 !isCFStringType(Ty, S.Context) &&
3076 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003077 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003078 // FIXME: Should highlight the actual expression that has the wrong type.
3079 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003080 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003081 << IdxExpr->getSourceRange();
3082 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003083 }
3084
Michael Han51d8c522013-01-24 16:46:58 +00003085 D->addAttr(::new (S.Context)
3086 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3087 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003088}
3089
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003090enum FormatAttrKind {
3091 CFStringFormat,
3092 NSStringFormat,
3093 StrftimeFormat,
3094 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003095 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003096 InvalidFormat
3097};
3098
3099/// getFormatAttrKind - Map from format attribute names to supported format
3100/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003101static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003102 return llvm::StringSwitch<FormatAttrKind>(Format)
3103 // Check for formats that get handled specially.
3104 .Case("NSString", NSStringFormat)
3105 .Case("CFString", CFStringFormat)
3106 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003107
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003108 // Otherwise, check for supported formats.
3109 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3110 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3111 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003112
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003113 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3114 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003115}
3116
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003117/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003118/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003119static void handleInitPriorityAttr(Sema &S, Decl *D,
3120 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003121 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003122 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3123 return;
3124 }
3125
Chandler Carruth87c44602011-07-01 23:49:12 +00003126 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003127 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3128 Attr.setInvalid();
3129 return;
3130 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003131 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003132 if (S.Context.getAsArrayType(T))
3133 T = S.Context.getBaseElementType(T);
3134 if (!T->getAs<RecordType>()) {
3135 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3136 Attr.setInvalid();
3137 return;
3138 }
3139
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003140 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003141 Attr.setInvalid();
3142 return;
3143 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003144 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003145
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003146 llvm::APSInt priority(32);
3147 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3148 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3150 << "init_priority" << priorityExpr->getSourceRange();
3151 Attr.setInvalid();
3152 return;
3153 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003154 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003155 if (prioritynum < 101 || prioritynum > 65535) {
3156 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3157 << priorityExpr->getSourceRange();
3158 Attr.setInvalid();
3159 return;
3160 }
Michael Han51d8c522013-01-24 16:46:58 +00003161 D->addAttr(::new (S.Context)
3162 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3163 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003164}
3165
Rafael Espindola599f1b72012-05-13 03:25:18 +00003166FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003167 int FormatIdx, int FirstArg,
3168 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003169 // Check whether we already have an equivalent format attribute.
3170 for (specific_attr_iterator<FormatAttr>
3171 i = D->specific_attr_begin<FormatAttr>(),
3172 e = D->specific_attr_end<FormatAttr>();
3173 i != e ; ++i) {
3174 FormatAttr *f = *i;
3175 if (f->getType() == Format &&
3176 f->getFormatIdx() == FormatIdx &&
3177 f->getFirstArg() == FirstArg) {
3178 // If we don't have a valid location for this attribute, adopt the
3179 // location.
3180 if (f->getLocation().isInvalid())
3181 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003182 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003183 }
3184 }
3185
Michael Han51d8c522013-01-24 16:46:58 +00003186 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3187 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003188}
3189
Mike Stumpbf916502009-07-24 19:02:52 +00003190/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003191/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003192static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003193
Chris Lattner545dd342008-06-28 23:36:30 +00003194 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00003196 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197 return;
3198 }
3199
Chris Lattner545dd342008-06-28 23:36:30 +00003200 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003202 return;
3203 }
3204
Chandler Carruth87c44602011-07-01 23:49:12 +00003205 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003206 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003207 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003208 return;
3209 }
3210
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003211 // In C++ the implicit 'this' function parameter also counts, and they are
3212 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003213 bool HasImplicitThisParam = isInstanceMethod(D);
3214 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003215 unsigned FirstIdx = 1;
3216
Chris Lattner5f9e2722011-07-23 10:55:15 +00003217 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003218
3219 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003220 if (Format.startswith("__") && Format.endswith("__"))
3221 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003222
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003223 // Check for supported formats.
3224 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003225
3226 if (Kind == IgnoredFormat)
3227 return;
3228
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003229 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003230 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003231 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003232 return;
3233 }
3234
3235 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003236 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003237 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003238 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3239 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003241 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242 return;
3243 }
3244
3245 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003246 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003247 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003248 return;
3249 }
3250
3251 // FIXME: Do we need to bounds check?
3252 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003253
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003254 if (HasImplicitThisParam) {
3255 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003256 S.Diag(Attr.getLoc(),
3257 diag::err_format_attribute_implicit_this_format_string)
3258 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003259 return;
3260 }
3261 ArgIdx--;
3262 }
Mike Stump1eb44332009-09-09 15:08:12 +00003263
Chris Lattner6b6b5372008-06-26 18:38:35 +00003264 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003265 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003266
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003267 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003268 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003269 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3270 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003271 return;
3272 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003273 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003274 // FIXME: do we need to check if the type is NSString*? What are the
3275 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003276 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003277 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003278 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3279 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003280 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003281 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003282 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003283 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003284 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003285 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3286 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003287 return;
3288 }
3289
3290 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003291 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003292 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003293 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3294 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003295 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00003296 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003297 return;
3298 }
3299
3300 // check if the function is variadic if the 3rd argument non-zero
3301 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003302 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003303 ++NumArgs; // +1 for ...
3304 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003305 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003306 return;
3307 }
3308 }
3309
Chris Lattner3c73c412008-11-19 08:23:25 +00003310 // strftime requires FirstArg to be 0 because it doesn't read from any
3311 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003312 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003313 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003314 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3315 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003316 return;
3317 }
3318 // if 0 it disables parameter checking (to use with e.g. va_list)
3319 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003321 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003322 return;
3323 }
3324
Rafael Espindola599f1b72012-05-13 03:25:18 +00003325 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3326 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003327 FirstArg.getZExtValue(),
3328 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003329 if (NewAttr)
3330 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003331}
3332
Chandler Carruth1b03c872011-07-02 00:01:44 +00003333static void handleTransparentUnionAttr(Sema &S, Decl *D,
3334 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003335 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003336 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003337 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003338
Chris Lattner6b6b5372008-06-26 18:38:35 +00003339
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003340 // Try to find the underlying union declaration.
3341 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003342 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003343 if (TD && TD->getUnderlyingType()->isUnionType())
3344 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3345 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003346 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003347
3348 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003349 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003350 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003351 return;
3352 }
3353
John McCall5e1cdac2011-10-07 06:10:15 +00003354 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003355 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003356 diag::warn_transparent_union_attribute_not_definition);
3357 return;
3358 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003359
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003360 RecordDecl::field_iterator Field = RD->field_begin(),
3361 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003362 if (Field == FieldEnd) {
3363 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3364 return;
3365 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003366
David Blaikie581deb32012-06-06 20:45:41 +00003367 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003368 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003369 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003370 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003371 diag::warn_transparent_union_attribute_floating)
3372 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003373 return;
3374 }
3375
3376 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3377 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3378 for (; Field != FieldEnd; ++Field) {
3379 QualType FieldType = Field->getType();
3380 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3381 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3382 // Warn if we drop the attribute.
3383 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003384 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003385 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003386 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003387 diag::warn_transparent_union_attribute_field_size_align)
3388 << isSize << Field->getDeclName() << FieldBits;
3389 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003390 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003391 diag::note_transparent_union_first_field_size_align)
3392 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003393 return;
3394 }
3395 }
3396
Michael Han51d8c522013-01-24 16:46:58 +00003397 RD->addAttr(::new (S.Context)
3398 TransparentUnionAttr(Attr.getRange(), S.Context,
3399 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003400}
3401
Chandler Carruth1b03c872011-07-02 00:01:44 +00003402static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003403 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003404 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003405 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003406
Peter Collingbourne7a730022010-11-23 20:45:58 +00003407 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003408 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003409
Chris Lattner6b6b5372008-06-26 18:38:35 +00003410 // Make sure that there is a string literal as the annotation's single
3411 // argument.
3412 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003413 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003414 return;
3415 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003416
3417 // Don't duplicate annotations that are already set.
3418 for (specific_attr_iterator<AnnotateAttr>
3419 i = D->specific_attr_begin<AnnotateAttr>(),
3420 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3421 if ((*i)->getAnnotation() == SE->getString())
3422 return;
3423 }
Michael Han51d8c522013-01-24 16:46:58 +00003424
3425 D->addAttr(::new (S.Context)
3426 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3427 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003428}
3429
Chandler Carruth1b03c872011-07-02 00:01:44 +00003430static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003431 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003432 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003434 return;
3435 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003436
Richard Smithbe507b62013-02-01 08:12:08 +00003437 if (Attr.getNumArgs() == 0) {
3438 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3439 true, 0, Attr.getAttributeSpellingListIndex()));
3440 return;
3441 }
3442
Richard Smithf6565a92013-02-22 08:32:16 +00003443 Expr *E = Attr.getArg(0);
3444 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3445 S.Diag(Attr.getEllipsisLoc(),
3446 diag::err_pack_expansion_without_parameter_packs);
3447 return;
3448 }
3449
3450 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3451 return;
3452
3453 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3454 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003455}
3456
3457void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003458 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003459 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3460 SourceLocation AttrLoc = AttrRange.getBegin();
3461
Richard Smith4cd81c52013-01-29 09:02:09 +00003462 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003463 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003464 // C++11 [dcl.align]p1:
3465 // An alignment-specifier may be applied to a variable or to a class
3466 // data member, but it shall not be applied to a bit-field, a function
3467 // parameter, the formal parameter of a catch clause, or a variable
3468 // declared with the register storage class specifier. An
3469 // alignment-specifier may also be applied to the declaration of a class
3470 // or enumeration type.
3471 // C11 6.7.5/2:
3472 // An alignment attribute shall not be specified in a declaration of
3473 // a typedef, or a bit-field, or a function, or a parameter, or an
3474 // object declared with the register storage-class specifier.
3475 int DiagKind = -1;
3476 if (isa<ParmVarDecl>(D)) {
3477 DiagKind = 0;
3478 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3479 if (VD->getStorageClass() == SC_Register)
3480 DiagKind = 1;
3481 if (VD->isExceptionVariable())
3482 DiagKind = 2;
3483 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3484 if (FD->isBitField())
3485 DiagKind = 3;
3486 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003487 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3488 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003489 << (TmpAttr.isC11() ? ExpectedVariableOrField
3490 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003491 return;
3492 }
3493 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003494 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003495 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003496 return;
3497 }
3498 }
3499
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003500 if (E->isTypeDependent() || E->isValueDependent()) {
3501 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003502 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3503 AA->setPackExpansion(IsPackExpansion);
3504 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003505 return;
3506 }
Michael Hana31f65b2013-02-01 01:19:17 +00003507
Sean Huntcf807c42010-08-18 23:23:40 +00003508 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003509 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003510 ExprResult ICE
3511 = VerifyIntegerConstantExpression(E, &Alignment,
3512 diag::err_aligned_attribute_argument_not_int,
3513 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003514 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003515 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003516
3517 // C++11 [dcl.align]p2:
3518 // -- if the constant expression evaluates to zero, the alignment
3519 // specifier shall have no effect
3520 // C11 6.7.5p6:
3521 // An alignment specification of zero has no effect.
3522 if (!(TmpAttr.isAlignas() && !Alignment) &&
3523 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003524 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3525 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003526 return;
3527 }
Michael Hana31f65b2013-02-01 01:19:17 +00003528
Richard Smithbe507b62013-02-01 08:12:08 +00003529 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003530 // We've already verified it's a power of 2, now let's make sure it's
3531 // 8192 or less.
3532 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003533 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003534 << E->getSourceRange();
3535 return;
3536 }
3537 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003538
Richard Smithf6565a92013-02-22 08:32:16 +00003539 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3540 ICE.take(), SpellingListIndex);
3541 AA->setPackExpansion(IsPackExpansion);
3542 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003543}
3544
Michael Hana31f65b2013-02-01 01:19:17 +00003545void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003546 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003547 // FIXME: Cache the number on the Attr object if non-dependent?
3548 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003549 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3550 SpellingListIndex);
3551 AA->setPackExpansion(IsPackExpansion);
3552 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003553}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003554
Richard Smithbe507b62013-02-01 08:12:08 +00003555void Sema::CheckAlignasUnderalignment(Decl *D) {
3556 assert(D->hasAttrs() && "no attributes on decl");
3557
3558 QualType Ty;
3559 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3560 Ty = VD->getType();
3561 else
3562 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003563 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003564 return;
3565
3566 // C++11 [dcl.align]p5, C11 6.7.5/4:
3567 // The combined effect of all alignment attributes in a declaration shall
3568 // not specify an alignment that is less strict than the alignment that
3569 // would otherwise be required for the entity being declared.
3570 AlignedAttr *AlignasAttr = 0;
3571 unsigned Align = 0;
3572 for (specific_attr_iterator<AlignedAttr>
3573 I = D->specific_attr_begin<AlignedAttr>(),
3574 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3575 if (I->isAlignmentDependent())
3576 return;
3577 if (I->isAlignas())
3578 AlignasAttr = *I;
3579 Align = std::max(Align, I->getAlignment(Context));
3580 }
3581
3582 if (AlignasAttr && Align) {
3583 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3584 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3585 if (NaturalAlign > RequestedAlign)
3586 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3587 << Ty << (unsigned)NaturalAlign.getQuantity();
3588 }
3589}
3590
Chandler Carruthd309c812011-07-01 23:49:16 +00003591/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003592/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003593///
Mike Stumpbf916502009-07-24 19:02:52 +00003594/// Despite what would be logical, the mode attribute is a decl attribute, not a
3595/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3596/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003597static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003598 // This attribute isn't documented, but glibc uses it. It changes
3599 // the width of an int or unsigned int to the specified size.
3600
3601 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003602 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003603 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003604
Chris Lattnerfbf13472008-06-27 22:18:37 +00003605
3606 IdentifierInfo *Name = Attr.getParameterName();
3607 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003608 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003609 return;
3610 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003611
Chris Lattner5f9e2722011-07-23 10:55:15 +00003612 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003613
3614 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003615 if (Str.startswith("__") && Str.endswith("__"))
3616 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003617
3618 unsigned DestWidth = 0;
3619 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003620 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003621 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003622 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003623 switch (Str[0]) {
3624 case 'Q': DestWidth = 8; break;
3625 case 'H': DestWidth = 16; break;
3626 case 'S': DestWidth = 32; break;
3627 case 'D': DestWidth = 64; break;
3628 case 'X': DestWidth = 96; break;
3629 case 'T': DestWidth = 128; break;
3630 }
3631 if (Str[1] == 'F') {
3632 IntegerMode = false;
3633 } else if (Str[1] == 'C') {
3634 IntegerMode = false;
3635 ComplexMode = true;
3636 } else if (Str[1] != 'I') {
3637 DestWidth = 0;
3638 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003639 break;
3640 case 4:
3641 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3642 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003643 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003644 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003645 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003646 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003647 break;
3648 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003649 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003650 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003651 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003652 case 11:
3653 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003654 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003655 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003656 }
3657
3658 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003659 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003660 OldTy = TD->getUnderlyingType();
3661 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3662 OldTy = VD->getType();
3663 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003664 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003665 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003666 return;
3667 }
Eli Friedman73397492009-03-03 06:41:03 +00003668
John McCall183700f2009-09-21 23:43:11 +00003669 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003670 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3671 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003672 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003673 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3674 } else if (ComplexMode) {
3675 if (!OldTy->isComplexType())
3676 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3677 } else {
3678 if (!OldTy->isFloatingType())
3679 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3680 }
3681
Mike Stump390b4cc2009-05-16 07:39:55 +00003682 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3683 // and friends, at least with glibc.
3684 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3685 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003686 // FIXME: Make sure floating-point mappings are accurate
3687 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003688 QualType NewTy;
3689 switch (DestWidth) {
3690 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003691 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003692 return;
3693 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003694 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003695 return;
3696 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003697 if (!IntegerMode) {
3698 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3699 return;
3700 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003701 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003702 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003703 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003704 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003705 break;
3706 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003707 if (!IntegerMode) {
3708 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3709 return;
3710 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003711 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003712 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003713 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003714 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003715 break;
3716 case 32:
3717 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003718 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003719 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003720 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003721 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003722 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003723 break;
3724 case 64:
3725 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003726 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003727 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003728 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003729 NewTy = S.Context.LongTy;
3730 else
3731 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003732 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003733 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003734 NewTy = S.Context.UnsignedLongTy;
3735 else
3736 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003737 break;
Eli Friedman73397492009-03-03 06:41:03 +00003738 case 96:
3739 NewTy = S.Context.LongDoubleTy;
3740 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003741 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003742 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3743 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003744 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3745 return;
3746 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003747 if (IntegerMode) {
3748 if (OldTy->isSignedIntegerType())
3749 NewTy = S.Context.Int128Ty;
3750 else
3751 NewTy = S.Context.UnsignedInt128Ty;
3752 } else
3753 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003754 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003755 }
3756
Eli Friedman73397492009-03-03 06:41:03 +00003757 if (ComplexMode) {
3758 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003759 }
3760
3761 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003762 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3763 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3764 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003765 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003766
3767 D->addAttr(::new (S.Context)
3768 ModeAttr(Attr.getRange(), S.Context, Name,
3769 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003770}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003771
Chandler Carruth1b03c872011-07-02 00:01:44 +00003772static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003773 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003774 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003775 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003776
Nick Lewycky78d1a102012-07-24 01:40:49 +00003777 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3778 if (!VD->hasGlobalStorage())
3779 S.Diag(Attr.getLoc(),
3780 diag::warn_attribute_requires_functions_or_static_globals)
3781 << Attr.getName();
3782 } else if (!isFunctionOrMethod(D)) {
3783 S.Diag(Attr.getLoc(),
3784 diag::warn_attribute_requires_functions_or_static_globals)
3785 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003786 return;
3787 }
Mike Stumpbf916502009-07-24 19:02:52 +00003788
Michael Han51d8c522013-01-24 16:46:58 +00003789 D->addAttr(::new (S.Context)
3790 NoDebugAttr(Attr.getRange(), S.Context,
3791 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003792}
3793
Chandler Carruth1b03c872011-07-02 00:01:44 +00003794static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003795 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003796 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003797 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003798
Mike Stumpbf916502009-07-24 19:02:52 +00003799
Chandler Carruth87c44602011-07-01 23:49:12 +00003800 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003802 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003803 return;
3804 }
Mike Stumpbf916502009-07-24 19:02:52 +00003805
Michael Han51d8c522013-01-24 16:46:58 +00003806 D->addAttr(::new (S.Context)
3807 NoInlineAttr(Attr.getRange(), S.Context,
3808 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003809}
3810
Chandler Carruth1b03c872011-07-02 00:01:44 +00003811static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3812 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003813 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003814 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003815 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003816
Chris Lattner7255a2d2010-06-22 00:03:40 +00003817
Chandler Carruth87c44602011-07-01 23:49:12 +00003818 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003820 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003821 return;
3822 }
3823
Michael Han51d8c522013-01-24 16:46:58 +00003824 D->addAttr(::new (S.Context)
3825 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3826 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003827}
3828
Chandler Carruth1b03c872011-07-02 00:01:44 +00003829static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003830 if (S.LangOpts.CUDA) {
3831 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003832 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3834 return;
3835 }
3836
Chandler Carruth87c44602011-07-01 23:49:12 +00003837 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003838 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003839 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003840 return;
3841 }
3842
Michael Han51d8c522013-01-24 16:46:58 +00003843 D->addAttr(::new (S.Context)
3844 CUDAConstantAttr(Attr.getRange(), S.Context,
3845 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003846 } else {
3847 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3848 }
3849}
3850
Chandler Carruth1b03c872011-07-02 00:01:44 +00003851static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003852 if (S.LangOpts.CUDA) {
3853 // check the attribute arguments.
3854 if (Attr.getNumArgs() != 0) {
3855 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3856 return;
3857 }
3858
Chandler Carruth87c44602011-07-01 23:49:12 +00003859 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003860 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003861 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003862 return;
3863 }
3864
Michael Han51d8c522013-01-24 16:46:58 +00003865 D->addAttr(::new (S.Context)
3866 CUDADeviceAttr(Attr.getRange(), S.Context,
3867 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003868 } else {
3869 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3870 }
3871}
3872
Chandler Carruth1b03c872011-07-02 00:01:44 +00003873static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003874 if (S.LangOpts.CUDA) {
3875 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003876 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003877 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003878
Chandler Carruth87c44602011-07-01 23:49:12 +00003879 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003880 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003881 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003882 return;
3883 }
3884
Chandler Carruth87c44602011-07-01 23:49:12 +00003885 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003886 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003887 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003888 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003889 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3890 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003891 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003892 "void");
3893 } else {
3894 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3895 << FD->getType();
3896 }
3897 return;
3898 }
3899
Michael Han51d8c522013-01-24 16:46:58 +00003900 D->addAttr(::new (S.Context)
3901 CUDAGlobalAttr(Attr.getRange(), S.Context,
3902 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003903 } else {
3904 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3905 }
3906}
3907
Chandler Carruth1b03c872011-07-02 00:01:44 +00003908static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003909 if (S.LangOpts.CUDA) {
3910 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003911 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003912 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003913
Peter Collingbourneced76712010-12-01 03:15:31 +00003914
Chandler Carruth87c44602011-07-01 23:49:12 +00003915 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003916 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003917 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003918 return;
3919 }
3920
Michael Han51d8c522013-01-24 16:46:58 +00003921 D->addAttr(::new (S.Context)
3922 CUDAHostAttr(Attr.getRange(), S.Context,
3923 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003924 } else {
3925 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3926 }
3927}
3928
Chandler Carruth1b03c872011-07-02 00:01:44 +00003929static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003930 if (S.LangOpts.CUDA) {
3931 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003932 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003933 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003934
Chandler Carruth87c44602011-07-01 23:49:12 +00003935 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003936 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003937 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003938 return;
3939 }
3940
Michael Han51d8c522013-01-24 16:46:58 +00003941 D->addAttr(::new (S.Context)
3942 CUDASharedAttr(Attr.getRange(), S.Context,
3943 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003944 } else {
3945 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3946 }
3947}
3948
Chandler Carruth1b03c872011-07-02 00:01:44 +00003949static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003950 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003951 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003952 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003953
Chandler Carruth87c44602011-07-01 23:49:12 +00003954 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003955 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003957 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003958 return;
3959 }
Mike Stumpbf916502009-07-24 19:02:52 +00003960
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003961 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003962 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003963 return;
3964 }
Mike Stumpbf916502009-07-24 19:02:52 +00003965
Michael Han51d8c522013-01-24 16:46:58 +00003966 D->addAttr(::new (S.Context)
3967 GNUInlineAttr(Attr.getRange(), S.Context,
3968 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003969}
3970
Chandler Carruth1b03c872011-07-02 00:01:44 +00003971static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003972 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003973
Aaron Ballmanfff32482012-12-09 17:45:41 +00003974 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003975 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003976 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3977 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003978 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003979 return;
3980
Chandler Carruth87c44602011-07-01 23:49:12 +00003981 if (!isa<ObjCMethodDecl>(D)) {
3982 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3983 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003984 return;
3985 }
3986
Chandler Carruth87c44602011-07-01 23:49:12 +00003987 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003988 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003989 D->addAttr(::new (S.Context)
3990 FastCallAttr(Attr.getRange(), S.Context,
3991 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003992 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003993 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003994 D->addAttr(::new (S.Context)
3995 StdCallAttr(Attr.getRange(), S.Context,
3996 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003997 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003998 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003999 D->addAttr(::new (S.Context)
4000 ThisCallAttr(Attr.getRange(), S.Context,
4001 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00004002 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004003 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00004004 D->addAttr(::new (S.Context)
4005 CDeclAttr(Attr.getRange(), S.Context,
4006 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004007 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004008 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004009 D->addAttr(::new (S.Context)
4010 PascalAttr(Attr.getRange(), S.Context,
4011 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004012 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004013 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004014 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004015 switch (CC) {
4016 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004017 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004018 break;
4019 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004020 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004021 break;
4022 default:
4023 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004024 }
4025
Michael Han51d8c522013-01-24 16:46:58 +00004026 D->addAttr(::new (S.Context)
4027 PcsAttr(Attr.getRange(), S.Context, PCS,
4028 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004029 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004030 }
Derek Schuff263366f2012-10-16 22:30:41 +00004031 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004032 D->addAttr(::new (S.Context)
4033 PnaclCallAttr(Attr.getRange(), S.Context,
4034 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004035 return;
Guy Benyei38980082012-12-25 08:53:55 +00004036 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004037 D->addAttr(::new (S.Context)
4038 IntelOclBiccAttr(Attr.getRange(), S.Context,
4039 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004040 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004041
Abramo Bagnarae215f722010-04-30 13:10:51 +00004042 default:
4043 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004044 }
4045}
4046
Chandler Carruth1b03c872011-07-02 00:01:44 +00004047static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00004048 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004049 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004050}
4051
Guy Benyei1db70402013-03-24 13:58:12 +00004052static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4053 assert(!Attr.isInvalid());
4054
4055 Expr *E = Attr.getArg(0);
4056 llvm::APSInt ArgNum(32);
4057 if (E->isTypeDependent() || E->isValueDependent() ||
4058 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4059 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4060 << Attr.getName()->getName() << E->getSourceRange();
4061 return;
4062 }
4063
4064 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4065 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4066}
4067
Aaron Ballmanfff32482012-12-09 17:45:41 +00004068bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4069 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004070 if (attr.isInvalid())
4071 return true;
4072
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004073 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4074 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4075 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004076 attr.setInvalid();
4077 return true;
4078 }
4079
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004080 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4081 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004082 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004083 case AttributeList::AT_CDecl: CC = CC_C; break;
4084 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4085 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4086 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4087 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4088 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004089 Expr *Arg = attr.getArg(0);
4090 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004091 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004092 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
4093 << "pcs" << 1;
4094 attr.setInvalid();
4095 return true;
4096 }
4097
Chris Lattner5f9e2722011-07-23 10:55:15 +00004098 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004099 if (StrRef == "aapcs") {
4100 CC = CC_AAPCS;
4101 break;
4102 } else if (StrRef == "aapcs-vfp") {
4103 CC = CC_AAPCS_VFP;
4104 break;
4105 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004106
4107 attr.setInvalid();
4108 Diag(attr.getLoc(), diag::err_invalid_pcs);
4109 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004110 }
Derek Schuff263366f2012-10-16 22:30:41 +00004111 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004112 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004113 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004114 }
4115
Aaron Ballman82bfa192012-10-02 14:26:08 +00004116 const TargetInfo &TI = Context.getTargetInfo();
4117 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4118 if (A == TargetInfo::CCCR_Warning) {
4119 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004120
4121 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4122 if (FD)
4123 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4124 TargetInfo::CCMT_NonMember;
4125 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004126 }
4127
John McCall711c52b2011-01-05 12:14:39 +00004128 return false;
4129}
4130
Chandler Carruth1b03c872011-07-02 00:01:44 +00004131static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004132 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004133
4134 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004135 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004136 return;
4137
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 if (!isa<ObjCMethodDecl>(D)) {
4139 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4140 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004141 return;
4142 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004143
Michael Han51d8c522013-01-24 16:46:58 +00004144 D->addAttr(::new (S.Context)
4145 RegparmAttr(Attr.getRange(), S.Context, numParams,
4146 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004147}
4148
4149/// Checks a regparm attribute, returning true if it is ill-formed and
4150/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004151bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4152 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004153 return true;
4154
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004155 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004156 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004157 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004158 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004159
Chandler Carruth87c44602011-07-01 23:49:12 +00004160 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004161 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004162 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004163 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004164 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004165 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004166 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004167 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004168 }
4169
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004170 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004171 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004172 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004173 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004174 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004175 }
4176
John McCall711c52b2011-01-05 12:14:39 +00004177 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004178 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004179 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004180 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004181 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004182 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004183 }
4184
John McCall711c52b2011-01-05 12:14:39 +00004185 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004186}
4187
Chandler Carruth1b03c872011-07-02 00:01:44 +00004188static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004189 if (S.LangOpts.CUDA) {
4190 // check the attribute arguments.
4191 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004192 // FIXME: 0 is not okay.
4193 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004194 return;
4195 }
4196
Chandler Carruth87c44602011-07-01 23:49:12 +00004197 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004198 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004199 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004200 return;
4201 }
4202
4203 Expr *MaxThreadsExpr = Attr.getArg(0);
4204 llvm::APSInt MaxThreads(32);
4205 if (MaxThreadsExpr->isTypeDependent() ||
4206 MaxThreadsExpr->isValueDependent() ||
4207 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4209 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4210 return;
4211 }
4212
4213 llvm::APSInt MinBlocks(32);
4214 if (Attr.getNumArgs() > 1) {
4215 Expr *MinBlocksExpr = Attr.getArg(1);
4216 if (MinBlocksExpr->isTypeDependent() ||
4217 MinBlocksExpr->isValueDependent() ||
4218 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4220 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4221 return;
4222 }
4223 }
4224
Michael Han51d8c522013-01-24 16:46:58 +00004225 D->addAttr(::new (S.Context)
4226 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4227 MaxThreads.getZExtValue(),
4228 MinBlocks.getZExtValue(),
4229 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004230 } else {
4231 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4232 }
4233}
4234
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004235static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4236 const AttributeList &Attr) {
4237 StringRef AttrName = Attr.getName()->getName();
4238 if (!Attr.getParameterName()) {
4239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4240 << Attr.getName() << /* arg num = */ 1;
4241 return;
4242 }
4243
4244 if (Attr.getNumArgs() != 2) {
4245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4246 << /* required args = */ 3;
4247 return;
4248 }
4249
4250 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4251
4252 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4254 << Attr.getName() << ExpectedFunctionOrMethod;
4255 return;
4256 }
4257
4258 uint64_t ArgumentIdx;
4259 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4260 Attr.getLoc(), 2,
4261 Attr.getArg(0), ArgumentIdx))
4262 return;
4263
4264 uint64_t TypeTagIdx;
4265 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4266 Attr.getLoc(), 3,
4267 Attr.getArg(1), TypeTagIdx))
4268 return;
4269
4270 bool IsPointer = (AttrName == "pointer_with_type_tag");
4271 if (IsPointer) {
4272 // Ensure that buffer has a pointer type.
4273 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4274 if (!BufferTy->isPointerType()) {
4275 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004276 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004277 }
4278 }
4279
Michael Han51d8c522013-01-24 16:46:58 +00004280 D->addAttr(::new (S.Context)
4281 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4282 ArgumentIdx, TypeTagIdx, IsPointer,
4283 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004284}
4285
4286static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4287 const AttributeList &Attr) {
4288 IdentifierInfo *PointerKind = Attr.getParameterName();
4289 if (!PointerKind) {
4290 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4291 << "type_tag_for_datatype" << 1;
4292 return;
4293 }
4294
4295 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4296
Michael Han51d8c522013-01-24 16:46:58 +00004297 D->addAttr(::new (S.Context)
4298 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4299 MatchingCType,
4300 Attr.getLayoutCompatible(),
4301 Attr.getMustBeNull(),
4302 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004303}
4304
Chris Lattner0744e5f2008-06-29 00:23:49 +00004305//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004306// Checker-specific attribute handlers.
4307//===----------------------------------------------------------------------===//
4308
John McCallc7ad3812011-01-25 03:31:58 +00004309static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004310 return type->isDependentType() ||
4311 type->isObjCObjectPointerType() ||
4312 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004313}
4314static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004315 return type->isDependentType() ||
4316 type->isPointerType() ||
4317 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004318}
4319
Chandler Carruth1b03c872011-07-02 00:01:44 +00004320static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004321 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004322 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004323 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004324 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004325 return;
4326 }
4327
4328 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004329 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004330 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4331 cf = false;
4332 } else {
4333 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4334 cf = true;
4335 }
4336
4337 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004338 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004339 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004340 return;
4341 }
4342
4343 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004344 param->addAttr(::new (S.Context)
4345 CFConsumedAttr(Attr.getRange(), S.Context,
4346 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004347 else
Michael Han51d8c522013-01-24 16:46:58 +00004348 param->addAttr(::new (S.Context)
4349 NSConsumedAttr(Attr.getRange(), S.Context,
4350 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004351}
4352
Chandler Carruth1b03c872011-07-02 00:01:44 +00004353static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4354 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004355 if (!isa<ObjCMethodDecl>(D)) {
4356 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004357 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004358 return;
4359 }
4360
Michael Han51d8c522013-01-24 16:46:58 +00004361 D->addAttr(::new (S.Context)
4362 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4363 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004364}
4365
Chandler Carruth1b03c872011-07-02 00:01:44 +00004366static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4367 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004368
John McCallc7ad3812011-01-25 03:31:58 +00004369 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004370
Chandler Carruth87c44602011-07-01 23:49:12 +00004371 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004372 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004373 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004374 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004375 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004376 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4377 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004378 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004379 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004380 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004381 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004382 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004383 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004384 return;
4385 }
Mike Stumpbf916502009-07-24 19:02:52 +00004386
John McCallc7ad3812011-01-25 03:31:58 +00004387 bool typeOK;
4388 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004389 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004390 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004391 case AttributeList::AT_NSReturnsAutoreleased:
4392 case AttributeList::AT_NSReturnsRetained:
4393 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004394 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4395 cf = false;
4396 break;
4397
Sean Hunt8e083e72012-06-19 23:57:03 +00004398 case AttributeList::AT_CFReturnsRetained:
4399 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004400 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4401 cf = true;
4402 break;
4403 }
4404
4405 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004406 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004407 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004408 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004409 }
Mike Stumpbf916502009-07-24 19:02:52 +00004410
Chandler Carruth87c44602011-07-01 23:49:12 +00004411 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004412 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004413 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004414 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004415 D->addAttr(::new (S.Context)
4416 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4417 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004418 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004419 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004420 D->addAttr(::new (S.Context)
4421 CFReturnsNotRetainedAttr(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_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004425 D->addAttr(::new (S.Context)
4426 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4427 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004428 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004429 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004430 D->addAttr(::new (S.Context)
4431 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4432 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004433 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004434 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004435 D->addAttr(::new (S.Context)
4436 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4437 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004438 return;
4439 };
4440}
4441
John McCalldc7c5ad2011-07-22 08:53:00 +00004442static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4443 const AttributeList &attr) {
4444 SourceLocation loc = attr.getLoc();
4445
4446 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4447
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004448 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004449 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004450 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004451 return;
4452 }
4453
4454 // Check that the method returns a normal pointer.
4455 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004456
4457 if (!resultType->isReferenceType() &&
4458 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004459 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4460 << SourceRange(loc)
4461 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4462
4463 // Drop the attribute.
4464 return;
4465 }
4466
Michael Han51d8c522013-01-24 16:46:58 +00004467 method->addAttr(::new (S.Context)
4468 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4469 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004470}
4471
Fariborz Jahanian84101132012-09-07 23:46:23 +00004472static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4473 const AttributeList &attr) {
4474 SourceLocation loc = attr.getLoc();
4475 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4476
4477 if (!method) {
4478 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4479 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4480 return;
4481 }
4482 DeclContext *DC = method->getDeclContext();
4483 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4484 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4485 << attr.getName() << 0;
4486 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4487 return;
4488 }
4489 if (method->getMethodFamily() == OMF_dealloc) {
4490 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4491 << attr.getName() << 1;
4492 return;
4493 }
4494
Michael Han51d8c522013-01-24 16:46:58 +00004495 method->addAttr(::new (S.Context)
4496 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4497 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004498}
4499
John McCall8dfac0b2011-09-30 05:12:12 +00004500/// Handle cf_audited_transfer and cf_unknown_transfer.
4501static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4502 if (!isa<FunctionDecl>(D)) {
4503 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004504 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004505 return;
4506 }
4507
Sean Hunt8e083e72012-06-19 23:57:03 +00004508 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004509
4510 // Check whether there's a conflicting attribute already present.
4511 Attr *Existing;
4512 if (IsAudited) {
4513 Existing = D->getAttr<CFUnknownTransferAttr>();
4514 } else {
4515 Existing = D->getAttr<CFAuditedTransferAttr>();
4516 }
4517 if (Existing) {
4518 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4519 << A.getName()
4520 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4521 << A.getRange() << Existing->getRange();
4522 return;
4523 }
4524
4525 // All clear; add the attribute.
4526 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004527 D->addAttr(::new (S.Context)
4528 CFAuditedTransferAttr(A.getRange(), S.Context,
4529 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004530 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004531 D->addAttr(::new (S.Context)
4532 CFUnknownTransferAttr(A.getRange(), S.Context,
4533 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004534 }
4535}
4536
John McCallfe98da02011-09-29 07:17:38 +00004537static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4538 const AttributeList &Attr) {
4539 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4540 if (!RD || RD->isUnion()) {
4541 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004542 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004543 }
4544
4545 IdentifierInfo *ParmName = Attr.getParameterName();
4546
4547 // In Objective-C, verify that the type names an Objective-C type.
4548 // We don't want to check this outside of ObjC because people sometimes
4549 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004550 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004551 // Check for an existing type with this name.
4552 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4553 Sema::LookupOrdinaryName);
4554 if (S.LookupName(R, Sc)) {
4555 NamedDecl *Target = R.getFoundDecl();
4556 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4557 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4558 S.Diag(Target->getLocStart(), diag::note_declared_at);
4559 }
4560 }
4561 }
4562
Michael Han51d8c522013-01-24 16:46:58 +00004563 D->addAttr(::new (S.Context)
4564 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4565 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004566}
4567
Chandler Carruth1b03c872011-07-02 00:01:44 +00004568static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4569 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004570 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004571
Chandler Carruth87c44602011-07-01 23:49:12 +00004572 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004573 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004574}
4575
Chandler Carruth1b03c872011-07-02 00:01:44 +00004576static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4577 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004578 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004579 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004580 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004581 return;
4582 }
4583
Chandler Carruth87c44602011-07-01 23:49:12 +00004584 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004585 QualType type = vd->getType();
4586
4587 if (!type->isDependentType() &&
4588 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004589 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004590 << type;
4591 return;
4592 }
4593
4594 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4595
4596 // If we have no lifetime yet, check the lifetime we're presumably
4597 // going to infer.
4598 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4599 lifetime = type->getObjCARCImplicitLifetime();
4600
4601 switch (lifetime) {
4602 case Qualifiers::OCL_None:
4603 assert(type->isDependentType() &&
4604 "didn't infer lifetime for non-dependent type?");
4605 break;
4606
4607 case Qualifiers::OCL_Weak: // meaningful
4608 case Qualifiers::OCL_Strong: // meaningful
4609 break;
4610
4611 case Qualifiers::OCL_ExplicitNone:
4612 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004613 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004614 << (lifetime == Qualifiers::OCL_Autoreleasing);
4615 break;
4616 }
4617
Chandler Carruth87c44602011-07-01 23:49:12 +00004618 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004619 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4620 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004621}
4622
Francois Pichet11542142010-12-19 06:50:37 +00004623//===----------------------------------------------------------------------===//
4624// Microsoft specific attribute handlers.
4625//===----------------------------------------------------------------------===//
4626
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004627// Check if MS extensions or some other language extensions are enabled. If
4628// not, issue a diagnostic that the given attribute is unused.
4629static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4630 bool OtherExtension = false) {
4631 if (S.LangOpts.MicrosoftExt || OtherExtension)
4632 return true;
4633 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4634 return false;
4635}
4636
Chandler Carruth1b03c872011-07-02 00:01:44 +00004637static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004638 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4639 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004640
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004641 // check the attribute arguments.
4642 if (!checkAttributeNumArgs(S, Attr, 1))
4643 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004644
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004645 Expr *Arg = Attr.getArg(0);
4646 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4647 if (!Str || !Str->isAscii()) {
4648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4649 << "uuid" << 1;
4650 return;
4651 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004652
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004653 StringRef StrRef = Str->getString();
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004654
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004655 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4656 StrRef.back() == '}';
Francois Pichetd3d3be92010-12-20 01:41:49 +00004657
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004658 // Validate GUID length.
4659 if (IsCurly && StrRef.size() != 38) {
4660 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4661 return;
4662 }
4663 if (!IsCurly && StrRef.size() != 36) {
4664 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4665 return;
4666 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004667
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004668 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4669 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4670 StringRef::iterator I = StrRef.begin();
4671 if (IsCurly) // Skip the optional '{'
4672 ++I;
4673
4674 for (int i = 0; i < 36; ++i) {
4675 if (i == 8 || i == 13 || i == 18 || i == 23) {
4676 if (*I != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004677 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4678 return;
4679 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004680 } else if (!isHexDigit(*I)) {
4681 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4682 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004683 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004684 I++;
4685 }
Francois Pichet11542142010-12-19 06:50:37 +00004686
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004687 D->addAttr(::new (S.Context)
4688 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4689 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004690}
4691
John McCallc052dbb2012-05-22 21:28:12 +00004692static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004693 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004694 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004695
4696 AttributeList::Kind Kind = Attr.getKind();
4697 if (Kind == AttributeList::AT_SingleInheritance)
4698 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004699 ::new (S.Context)
4700 SingleInheritanceAttr(Attr.getRange(), S.Context,
4701 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004702 else if (Kind == AttributeList::AT_MultipleInheritance)
4703 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004704 ::new (S.Context)
4705 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4706 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004707 else if (Kind == AttributeList::AT_VirtualInheritance)
4708 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004709 ::new (S.Context)
4710 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4711 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004712}
4713
4714static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004715 if (!checkMicrosoftExt(S, Attr))
4716 return;
4717
4718 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004719 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004720 D->addAttr(
4721 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4722 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004723}
4724
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004725static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004726 if (!checkMicrosoftExt(S, Attr))
4727 return;
4728 D->addAttr(::new (S.Context)
4729 ForceInlineAttr(Attr.getRange(), S.Context,
4730 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004731}
4732
Reid Klecknera7225342013-05-20 14:02:37 +00004733static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4734 if (!checkMicrosoftExt(S, Attr))
4735 return;
4736 // Check linkage after possibly merging declaratinos. See
4737 // checkAttributesAfterMerging().
4738 D->addAttr(::new (S.Context)
4739 SelectAnyAttr(Attr.getRange(), S.Context,
4740 Attr.getAttributeSpellingListIndex()));
4741}
4742
Ted Kremenekb71368d2009-05-09 02:44:38 +00004743//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004744// Top Level Sema Entry Points
4745//===----------------------------------------------------------------------===//
4746
Chandler Carruth1b03c872011-07-02 00:01:44 +00004747static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4748 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004749 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004750 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4751 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4752 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004753 default:
4754 break;
4755 }
4756}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004757
Chandler Carruth1b03c872011-07-02 00:01:44 +00004758static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4759 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004760 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004761 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4762 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4763 case AttributeList::AT_IBOutletCollection:
4764 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004765 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004766 case AttributeList::AT_ObjCGC:
4767 case AttributeList::AT_VectorSize:
4768 case AttributeList::AT_NeonVectorType:
4769 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004770 case AttributeList::AT_Ptr32:
4771 case AttributeList::AT_Ptr64:
4772 case AttributeList::AT_SPtr:
4773 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004774 // Ignore these, these are type attributes, handled by
4775 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004776 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_CUDADevice:
4778 case AttributeList::AT_CUDAHost:
4779 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004780 // Ignore, this is a non-inheritable attribute, handled
4781 // by ProcessNonInheritableDeclAttr.
4782 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4784 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4785 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4786 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004787 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004788 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004789 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004790 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4792 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4793 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004794 handleDependencyAttr(S, scope, D, Attr);
4795 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004796 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4797 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4798 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004799 case AttributeList::AT_CXX11NoReturn:
4800 handleCXX11NoReturnAttr(S, D, Attr);
4801 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004803 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004804 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004805 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4806 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004807 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004808 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004809 case AttributeList::AT_MinSize:
4810 handleMinSizeAttr(S, D, Attr);
4811 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004812 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4813 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4814 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4815 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4816 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004817 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004818 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004819 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4820 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4821 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4822 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4823 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004824 case AttributeList::AT_ownership_returns:
4825 case AttributeList::AT_ownership_takes:
4826 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004827 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004828 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4829 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4830 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4831 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4832 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4833 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4834 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004835
Sean Hunt8e083e72012-06-19 23:57:03 +00004836 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004837 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004838 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004839 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004840
Sean Hunt8e083e72012-06-19 23:57:03 +00004841 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004842 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4843
Fariborz Jahanian84101132012-09-07 23:46:23 +00004844 case AttributeList::AT_ObjCRequiresSuper:
4845 handleObjCRequiresSuperAttr(S, D, Attr); break;
4846
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004848 handleNSBridgedAttr(S, scope, D, Attr); break;
4849
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_CFAuditedTransfer:
4851 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004852 handleCFTransferAttr(S, D, Attr); break;
4853
Ted Kremenekb71368d2009-05-09 02:44:38 +00004854 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004855 case AttributeList::AT_CFConsumed:
4856 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4857 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004858 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004859
Sean Hunt8e083e72012-06-19 23:57:03 +00004860 case AttributeList::AT_NSReturnsAutoreleased:
4861 case AttributeList::AT_NSReturnsNotRetained:
4862 case AttributeList::AT_CFReturnsNotRetained:
4863 case AttributeList::AT_NSReturnsRetained:
4864 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004865 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004866
Tanya Lattner0df579e2012-07-09 22:06:01 +00004867 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004869 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004870
Joey Gouly37453b92013-03-08 09:42:32 +00004871 case AttributeList::AT_VecTypeHint:
4872 handleVecTypeHint(S, D, Attr); break;
4873
Joey Gouly96cead52013-03-14 09:54:43 +00004874 case AttributeList::AT_Endian:
4875 handleEndianAttr(S, D, Attr);
4876 break;
4877
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004879 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004880
Sean Hunt8e083e72012-06-19 23:57:03 +00004881 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4882 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4883 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004884 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004885 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004886 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004887 handleArcWeakrefUnavailableAttr (S, D, Attr);
4888 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004889 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004890 handleObjCRootClassAttr(S, D, Attr);
4891 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004892 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004893 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004894 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004895 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4896 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004897 handleReturnsTwiceAttr(S, D, Attr);
4898 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004899 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004900 case AttributeList::AT_Visibility:
4901 handleVisibilityAttr(S, D, Attr, false);
4902 break;
4903 case AttributeList::AT_TypeVisibility:
4904 handleVisibilityAttr(S, D, Attr, true);
4905 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004906 case AttributeList::AT_WarnUnused:
4907 handleWarnUnusedAttr(S, D, Attr);
4908 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004909 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004910 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004911 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4912 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4913 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4914 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004915 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004916 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004917 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004918 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004919 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004920 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004921 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004922 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004923 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4924 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4925 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4926 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4927 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4928 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4929 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4930 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4931 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004932 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004933 // Just ignore
4934 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004935 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004936 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004937 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004938 case AttributeList::AT_StdCall:
4939 case AttributeList::AT_CDecl:
4940 case AttributeList::AT_FastCall:
4941 case AttributeList::AT_ThisCall:
4942 case AttributeList::AT_Pascal:
4943 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004944 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004945 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004946 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004947 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004948 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004949 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004950 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004951 case AttributeList::AT_OpenCLImageAccess:
4952 handleOpenCLImageAccessAttr(S, D, Attr);
4953 break;
John McCallc052dbb2012-05-22 21:28:12 +00004954
4955 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004956 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004957 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004958 handleMsStructAttr(S, D, Attr);
4959 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004960 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004961 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004962 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004963 case AttributeList::AT_SingleInheritance:
4964 case AttributeList::AT_MultipleInheritance:
4965 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004966 handleInheritanceAttr(S, D, Attr);
4967 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004968 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004969 handlePortabilityAttr(S, D, Attr);
4970 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004971 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004972 handleForceInlineAttr(S, D, Attr);
4973 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004974 case AttributeList::AT_SelectAny:
4975 handleSelectAnyAttr(S, D, Attr);
4976 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004977
4978 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004979 case AttributeList::AT_AssertExclusiveLock:
4980 handleAssertExclusiveLockAttr(S, D, Attr);
4981 break;
4982 case AttributeList::AT_AssertSharedLock:
4983 handleAssertSharedLockAttr(S, D, Attr);
4984 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004985 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004986 handleGuardedVarAttr(S, D, Attr);
4987 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004988 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004989 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004990 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004991 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004992 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004993 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004994 case AttributeList::AT_NoSanitizeAddress:
4995 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004996 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004997 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004998 handleNoThreadSafetyAnalysis(S, D, Attr);
4999 break;
5000 case AttributeList::AT_NoSanitizeThread:
5001 handleNoSanitizeThread(S, D, Attr);
5002 break;
5003 case AttributeList::AT_NoSanitizeMemory:
5004 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005005 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005006 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005007 handleLockableAttr(S, D, Attr);
5008 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005009 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005010 handleGuardedByAttr(S, D, Attr);
5011 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005012 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005013 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005014 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005015 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005016 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005017 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005018 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005019 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005020 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005021 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005022 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005023 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005024 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005025 handleLockReturnedAttr(S, D, Attr);
5026 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005027 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005028 handleLocksExcludedAttr(S, D, Attr);
5029 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005030 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005031 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005032 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005033 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005034 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005035 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005036 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005037 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005038 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005039 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005040 handleUnlockFunAttr(S, D, Attr);
5041 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005042 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005043 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005044 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005045 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005046 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005047 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005048
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005049 // Type safety attributes.
5050 case AttributeList::AT_ArgumentWithTypeTag:
5051 handleArgumentWithTypeTagAttr(S, D, Attr);
5052 break;
5053 case AttributeList::AT_TypeTagForDatatype:
5054 handleTypeTagForDatatypeAttr(S, D, Attr);
5055 break;
5056
Chris Lattner803d0802008-06-29 00:43:07 +00005057 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005058 // Ask target about the attribute.
5059 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5060 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005061 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5062 diag::warn_unhandled_ms_attribute_ignored :
5063 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005064 break;
5065 }
5066}
5067
Peter Collingbourne60700392011-01-21 02:08:45 +00005068/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5069/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00005070/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00005071static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5072 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00005073 bool NonInheritable, bool Inheritable,
5074 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005075 if (Attr.isInvalid())
5076 return;
5077
Richard Smithcd8ab512013-01-17 01:30:42 +00005078 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5079 // instead.
5080 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5081 return;
5082
Peter Collingbourne60700392011-01-21 02:08:45 +00005083 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005084 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005085
5086 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005087 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005088}
5089
Chris Lattner803d0802008-06-29 00:43:07 +00005090/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5091/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005092void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005093 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005094 bool NonInheritable, bool Inheritable,
5095 bool IncludeCXX11Attributes) {
5096 for (const AttributeList* l = AttrList; l; l = l->getNext())
5097 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5098 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005099
5100 // GCC accepts
5101 // static int a9 __attribute__((weakref));
5102 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005103 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005104 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005105 cast<NamedDecl>(D)->getNameAsString();
5106 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005107 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005108 }
5109}
5110
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005111// Annotation attributes are the only attributes allowed after an access
5112// specifier.
5113bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5114 const AttributeList *AttrList) {
5115 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005116 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005117 handleAnnotateAttr(*this, ASDecl, *l);
5118 } else {
5119 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5120 return true;
5121 }
5122 }
5123
5124 return false;
5125}
5126
John McCalle82247a2011-10-01 05:17:03 +00005127/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5128/// contains any decl attributes that we should warn about.
5129static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5130 for ( ; A; A = A->getNext()) {
5131 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005132 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005133 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5134
5135 if (A->getKind() == AttributeList::UnknownAttribute) {
5136 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5137 << A->getName() << A->getRange();
5138 } else {
5139 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5140 << A->getName() << A->getRange();
5141 }
5142 }
5143}
5144
5145/// checkUnusedDeclAttributes - Given a declarator which is not being
5146/// used to build a declaration, complain about any decl attributes
5147/// which might be lying around on it.
5148void Sema::checkUnusedDeclAttributes(Declarator &D) {
5149 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5150 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5151 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5152 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5153}
5154
Ryan Flynne25ff832009-07-30 03:15:39 +00005155/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005156/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005157NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5158 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005159 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005160 NamedDecl *NewD = 0;
5161 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005162 FunctionDecl *NewFD;
5163 // FIXME: Missing call to CheckFunctionDeclaration().
5164 // FIXME: Mangling?
5165 // FIXME: Is the qualifier info correct?
5166 // FIXME: Is the DeclContext correct?
5167 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5168 Loc, Loc, DeclarationName(II),
5169 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005170 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005171 FD->hasPrototype(),
5172 false/*isConstexprSpecified*/);
5173 NewD = NewFD;
5174
5175 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005176 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005177
5178 // Fake up parameter variables; they are declared as if this were
5179 // a typedef.
5180 QualType FDTy = FD->getType();
5181 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5182 SmallVector<ParmVarDecl*, 16> Params;
5183 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5184 AE = FT->arg_type_end(); AI != AE; ++AI) {
5185 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5186 Param->setScopeInfo(0, Params.size());
5187 Params.push_back(Param);
5188 }
David Blaikie4278c652011-09-21 18:16:56 +00005189 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005190 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005191 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5192 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005193 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005194 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005195 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005196 if (VD->getQualifier()) {
5197 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005198 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005199 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005200 }
5201 return NewD;
5202}
5203
James Dennett1dfbd922012-06-14 21:40:34 +00005204/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005205/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005206void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005207 if (W.getUsed()) return; // only do this once
5208 W.setUsed(true);
5209 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5210 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005211 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005212 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5213 NDId->getName()));
5214 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005215 WeakTopLevelDecl.push_back(NewD);
5216 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5217 // to insert Decl at TU scope, sorry.
5218 DeclContext *SavedContext = CurContext;
5219 CurContext = Context.getTranslationUnitDecl();
5220 PushOnScopeChains(NewD, S);
5221 CurContext = SavedContext;
5222 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005223 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005224 }
5225}
5226
Rafael Espindola65611bf2013-03-02 21:41:48 +00005227void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5228 // It's valid to "forward-declare" #pragma weak, in which case we
5229 // have to do this.
5230 LoadExternalWeakUndeclaredIdentifiers();
5231 if (!WeakUndeclaredIdentifiers.empty()) {
5232 NamedDecl *ND = NULL;
5233 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5234 if (VD->isExternC())
5235 ND = VD;
5236 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5237 if (FD->isExternC())
5238 ND = FD;
5239 if (ND) {
5240 if (IdentifierInfo *Id = ND->getIdentifier()) {
5241 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5242 = WeakUndeclaredIdentifiers.find(Id);
5243 if (I != WeakUndeclaredIdentifiers.end()) {
5244 WeakInfo W = I->second;
5245 DeclApplyPragmaWeak(S, ND, W);
5246 WeakUndeclaredIdentifiers[Id] = W;
5247 }
5248 }
5249 }
5250 }
5251}
5252
Chris Lattner0744e5f2008-06-29 00:23:49 +00005253/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5254/// it, apply them to D. This is a bit tricky because PD can have attributes
5255/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005256void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5257 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005258 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005259 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005260 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005261
Chris Lattner0744e5f2008-06-29 00:23:49 +00005262 // Walk the declarator structure, applying decl attributes that were in a type
5263 // position to the decl itself. This handles cases like:
5264 // int *__attr__(x)** D;
5265 // when X is a decl attribute.
5266 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5267 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005268 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5269 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005270
Chris Lattner0744e5f2008-06-29 00:23:49 +00005271 // Finally, apply any attributes on the decl itself.
5272 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005273 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005274}
John McCall54abf7d2009-11-04 02:18:39 +00005275
John McCallf85e1932011-06-15 23:02:42 +00005276/// Is the given declaration allowed to use a forbidden type?
5277static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5278 // Private ivars are always okay. Unfortunately, people don't
5279 // always properly make their ivars private, even in system headers.
5280 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005281 // Function declarations in sys headers will be marked unavailable.
5282 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5283 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005284 return false;
5285
5286 // Require it to be declared in a system header.
5287 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5288}
5289
5290/// Handle a delayed forbidden-type diagnostic.
5291static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5292 Decl *decl) {
5293 if (decl && isForbiddenTypeAllowed(S, decl)) {
5294 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5295 "this system declaration uses an unsupported type"));
5296 return;
5297 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005298 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005299 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005300 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005301 // kind of forbidden type messages on unavailable functions.
5302 if (FD->hasAttr<UnavailableAttr>() &&
5303 diag.getForbiddenTypeDiagnostic() ==
5304 diag::err_arc_array_param_no_ownership) {
5305 diag.Triggered = true;
5306 return;
5307 }
5308 }
John McCallf85e1932011-06-15 23:02:42 +00005309
5310 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5311 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5312 diag.Triggered = true;
5313}
5314
John McCall92576642012-05-07 06:16:41 +00005315void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5316 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005317 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005318 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005319
John McCall92576642012-05-07 06:16:41 +00005320 // When delaying diagnostics to run in the context of a parsed
5321 // declaration, we only want to actually emit anything if parsing
5322 // succeeds.
5323 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005324
John McCall92576642012-05-07 06:16:41 +00005325 // We emit all the active diagnostics in this pool or any of its
5326 // parents. In general, we'll get one pool for the decl spec
5327 // and a child pool for each declarator; in a decl group like:
5328 // deprecated_typedef foo, *bar, baz();
5329 // only the declarator pops will be passed decls. This is correct;
5330 // we really do need to consider delayed diagnostics from the decl spec
5331 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005332 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005333 do {
John McCall13489672012-05-07 06:16:58 +00005334 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005335 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5336 // This const_cast is a bit lame. Really, Triggered should be mutable.
5337 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005338 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005339 continue;
5340
John McCalleee1d542011-02-14 07:13:47 +00005341 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005342 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005343 // Don't bother giving deprecation diagnostics if the decl is invalid.
5344 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005345 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005346 break;
5347
5348 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005349 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005350 break;
John McCallf85e1932011-06-15 23:02:42 +00005351
5352 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005353 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005354 break;
John McCall2f514482010-01-27 03:50:35 +00005355 }
5356 }
John McCall92576642012-05-07 06:16:41 +00005357 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005358}
5359
John McCall13489672012-05-07 06:16:58 +00005360/// Given a set of delayed diagnostics, re-emit them as if they had
5361/// been delayed in the current context instead of in the given pool.
5362/// Essentially, this just moves them to the current pool.
5363void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5364 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5365 assert(curPool && "re-emitting in undelayed context not supported");
5366 curPool->steal(pool);
5367}
5368
John McCall54abf7d2009-11-04 02:18:39 +00005369static bool isDeclDeprecated(Decl *D) {
5370 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005371 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005372 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005373 // A category implicitly has the availability of the interface.
5374 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5375 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005376 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5377 return false;
5378}
5379
Eli Friedmanc3b23082012-08-08 21:52:41 +00005380static void
5381DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5382 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005383 const ObjCInterfaceDecl *UnknownObjCClass,
5384 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005385 DeclarationName Name = D->getDeclName();
5386 if (!Message.empty()) {
5387 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
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 if (!UnknownObjCClass) {
5395 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5396 S.Diag(D->getLocation(),
5397 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5398 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005399 if (ObjCPropery)
5400 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5401 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005402 } else {
5403 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5404 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5405 }
5406}
5407
John McCall9c3087b2010-08-26 02:13:20 +00005408void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005409 Decl *Ctx) {
5410 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005411 return;
5412
John McCall2f514482010-01-27 03:50:35 +00005413 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005414 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5415 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005416 DD.getUnknownObjCClass(),
5417 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005418}
5419
Chris Lattner5f9e2722011-07-23 10:55:15 +00005420void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005421 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005422 const ObjCInterfaceDecl *UnknownObjCClass,
5423 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005424 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005425 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005426 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5427 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005428 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005429 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005430 return;
5431 }
5432
5433 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005434 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005435 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005436 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005437}