blob: 7a4a0caf94330661937e1e4335671bceb3d15e95 [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
Aaron Ballman437d43f2013-07-23 14:03:57 +000059/// These constants match the enumerated choices of
60/// err_attribute_argument_n_type.
61enum AttributeArgumentNType {
62 ArgumentIntOrBool,
63 ArgumentIntegerConstant,
64 ArgumentString,
65 ArgumentIdentifier
66};
67
Chris Lattnere5c5ee12008-06-29 00:16:31 +000068//===----------------------------------------------------------------------===//
69// Helper functions
70//===----------------------------------------------------------------------===//
71
Chandler Carruth87c44602011-07-01 23:49:12 +000072static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000073 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000074 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000075 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000076 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000077 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000078 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000079 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000080 Ty = decl->getUnderlyingType();
81 else
82 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000083
Chris Lattner6b6b5372008-06-26 18:38:35 +000084 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000085 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000086 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000087 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000088
John McCall183700f2009-09-21 23:43:11 +000089 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000090}
91
Daniel Dunbar35682492008-09-26 04:12:28 +000092// FIXME: We should provide an abstraction around a method or function
93// to provide the following bits of information.
94
Nuno Lopesd20254f2009-12-20 23:11:08 +000095/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000096/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000097static bool isFunction(const Decl *D) {
98 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000099}
100
101/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000102/// type (function or function-typed variable) or an Objective-C
103/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000105 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +0000106}
107
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000108/// isFunctionOrMethodOrBlock - Return true if the given decl has function
109/// type (function or function-typed variable) or an Objective-C
110/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000111static bool isFunctionOrMethodOrBlock(const Decl *D) {
112 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000113 return true;
114 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000116 QualType Ty = V->getType();
117 return Ty->isBlockPointerType();
118 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000120}
121
John McCall711c52b2011-01-05 12:14:39 +0000122/// Return true if the given decl has a declarator that should have
123/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000124static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000125 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000126 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
127 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000128}
129
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000130/// hasFunctionProto - Return true if the given decl has a argument
131/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000132/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000133static bool hasFunctionProto(const Decl *D) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000136 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000137 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000138 return true;
139 }
140}
141
142/// getFunctionOrMethodNumArgs - Return number of function or method
143/// arguments. It is an error to call this on a K&R function (use
144/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000145static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
146 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000147 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000148 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000149 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000151}
152
Chandler Carruth87c44602011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000155 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000157 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000160}
161
Chandler Carruth87c44602011-07-01 23:49:12 +0000162static QualType getFunctionOrMethodResultType(const Decl *D) {
163 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000164 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000165 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000166}
167
Chandler Carruth87c44602011-07-01 23:49:12 +0000168static bool isFunctionOrMethodVariadic(const Decl *D) {
169 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000170 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000171 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000172 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000173 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000174 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000175 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000176 }
177}
178
Chandler Carruth87c44602011-07-01 23:49:12 +0000179static bool isInstanceMethod(const Decl *D) {
180 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000181 return MethodDecl->isInstance();
182 return false;
183}
184
Chris Lattner6b6b5372008-06-26 18:38:35 +0000185static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000186 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000187 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000188 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000189
John McCall506b57e2010-05-17 21:00:27 +0000190 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
191 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000193
John McCall506b57e2010-05-17 21:00:27 +0000194 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000195
Chris Lattner6b6b5372008-06-26 18:38:35 +0000196 // FIXME: Should we walk the chain of classes?
197 return ClsName == &Ctx.Idents.get("NSString") ||
198 ClsName == &Ctx.Idents.get("NSMutableString");
199}
200
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000201static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000202 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 if (!PT)
204 return false;
205
Ted Kremenek6217b802009-07-29 21:53:49 +0000206 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000207 if (!RT)
208 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000209
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000210 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000211 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000212 return false;
213
214 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
215}
216
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000217/// \brief Check if the attribute has exactly as many args as Num. May
218/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000219static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
220 unsigned int Num) {
221 if (Attr.getNumArgs() != Num) {
222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
223 return false;
224 }
225
226 return true;
227}
228
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000229
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000230/// \brief Check if the attribute has at least as many args as Num. May
231/// output an error.
232static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
233 unsigned int Num) {
234 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000235 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
236 return false;
237 }
238
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000239 return true;
240}
241
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000242/// \brief Check if IdxExpr is a valid argument index for a function or
243/// instance method D. May output an error.
244///
245/// \returns true if IdxExpr is a valid index.
246static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
247 StringRef AttrName,
248 SourceLocation AttrLoc,
249 unsigned AttrArgNum,
250 const Expr *IdxExpr,
251 uint64_t &Idx)
252{
253 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
254
255 // In C++ the implicit 'this' function parameter also counts.
256 // Parameters are counted from one.
257 const bool HasImplicitThisParam = isInstanceMethod(D);
258 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
259 const unsigned FirstIdx = 1;
260
261 llvm::APSInt IdxInt;
262 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
263 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000264 std::string Name = std::string("'") + AttrName.str() + std::string("'");
265 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
266 << AttrArgNum << ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000267 return false;
268 }
269
270 Idx = IdxInt.getLimitedValue();
271 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
272 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
273 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
274 return false;
275 }
276 Idx--; // Convert to zero-based.
277 if (HasImplicitThisParam) {
278 if (Idx == 0) {
279 S.Diag(AttrLoc,
280 diag::err_attribute_invalid_implicit_this_argument)
281 << AttrName << IdxExpr->getSourceRange();
282 return false;
283 }
284 --Idx;
285 }
286
287 return true;
288}
289
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000290///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000291/// \brief Check if passed in Decl is a field or potentially shared global var
292/// \return true if the Decl is a field or potentially shared global variable
293///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000294static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000295 if (isa<FieldDecl>(D))
296 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000297 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000298 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000299
300 return false;
301}
302
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000303/// \brief Check if the passed-in expression is of type int or bool.
304static bool isIntOrBool(Expr *Exp) {
305 QualType QT = Exp->getType();
306 return QT->isBooleanType() || QT->isIntegerType();
307}
308
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000309
310// Check to see if the type is a smart pointer of some kind. We assume
311// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000312static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
313 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
314 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000315 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000316 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000317
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000318 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
319 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000320 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000321 return false;
322
323 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000324}
325
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000326/// \brief Check if passed in Decl is a pointer type.
327/// Note that this function may produce an error message.
328/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000329static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
330 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000331 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000332 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000333 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000334 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000335
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000336 if (const RecordType *RT = QT->getAs<RecordType>()) {
337 // If it's an incomplete type, it could be a smart pointer; skip it.
338 // (We don't want to force template instantiation if we can avoid it,
339 // since that would alter the order in which templates are instantiated.)
340 if (RT->isIncompleteType())
341 return true;
342
343 if (threadSafetyCheckIsSmartPointer(S, RT))
344 return true;
345 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000346
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000347 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000348 << Attr.getName()->getName() << QT;
349 } else {
350 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
351 << Attr.getName();
352 }
353 return false;
354}
355
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000356/// \brief Checks that the passed in QualType either is of RecordType or points
357/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000358static const RecordType *getRecordType(QualType QT) {
359 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000360 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000361
362 // Now check if we point to record type.
363 if (const PointerType *PT = QT->getAs<PointerType>())
364 return PT->getPointeeType()->getAs<RecordType>();
365
366 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000367}
368
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000369
Jordy Rosefad5de92012-05-08 03:27:22 +0000370static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
371 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000372 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
373 if (RT->getDecl()->getAttr<LockableAttr>())
374 return true;
375 return false;
376}
377
378
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000379/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000380/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000381static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
382 QualType Ty) {
383 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000384
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000385 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000386 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000387 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000388 << Attr.getName() << Ty.getAsString();
389 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000390 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000391
Michael Hanf1aae3b2012-08-03 17:40:43 +0000392 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000393 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000394 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000395
396 // Allow smart pointers to be used as lockable objects.
397 // FIXME -- Check the type that the smart pointer points to.
398 if (threadSafetyCheckIsSmartPointer(S, RT))
399 return;
400
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000401 // Check if the type is lockable.
402 RecordDecl *RD = RT->getDecl();
403 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000404 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000405
406 // Else check if any base classes are lockable.
407 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
408 CXXBasePaths BPaths(false, false);
409 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
410 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000411 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000412
413 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
414 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000415}
416
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000417/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000418/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000419/// \param Sidx The attribute argument index to start checking with.
420/// \param ParamIdxOk Whether an argument can be indexing into a function
421/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000422static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000423 const AttributeList &Attr,
424 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000425 int Sidx = 0,
426 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000427 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000428 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000429
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000430 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000431 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000432 Args.push_back(ArgExp);
433 continue;
434 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000435
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000437 if (StrLit->getLength() == 0 ||
438 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000439 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000440 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000441 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000442 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000443 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000444
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000445 // We allow constant strings to be used as a placeholder for expressions
446 // that are not valid C++ syntax, but warn that they are ignored.
447 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
448 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000449 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000450 continue;
451 }
452
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000453 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000454
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000455 // A pointer to member expression of the form &MyClass::mu is treated
456 // specially -- we need to look at the type of the member.
457 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
458 if (UOp->getOpcode() == UO_AddrOf)
459 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
460 if (DRE->getDecl()->isCXXInstanceMember())
461 ArgTy = DRE->getDecl()->getType();
462
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000463 // First see if we can just cast to record type, or point to record type.
464 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000465
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000466 // Now check if we index into a record type function param.
467 if(!RT && ParamIdxOk) {
468 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000469 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
470 if(FD && IL) {
471 unsigned int NumParams = FD->getNumParams();
472 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000473 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
474 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
475 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000476 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
477 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000478 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000479 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000480 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000481 }
482 }
483
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000484 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000485
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000486 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000487 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000488}
489
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000490//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000491// Attribute Implementations
492//===----------------------------------------------------------------------===//
493
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000494// FIXME: All this manual attribute parsing code is gross. At the
495// least add some helper functions to check most argument patterns (#
496// and types of args).
497
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000498enum ThreadAttributeDeclKind {
499 ThreadExpectedFieldOrGlobalVar,
500 ThreadExpectedFunctionOrMethod,
501 ThreadExpectedClassOrStruct
502};
503
Michael Hanf1aae3b2012-08-03 17:40:43 +0000504static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000505 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000506 assert(!Attr.isInvalid());
507
508 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000509 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000510
511 // D must be either a member field or global (potentially shared) variable.
512 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000513 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
514 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000515 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000516 }
517
Michael Handc691572012-07-23 18:48:41 +0000518 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000519}
520
Michael Handc691572012-07-23 18:48:41 +0000521static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
522 if (!checkGuardedVarAttrCommon(S, D, Attr))
523 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000524
Michael Han51d8c522013-01-24 16:46:58 +0000525 D->addAttr(::new (S.Context)
526 GuardedVarAttr(Attr.getRange(), S.Context,
527 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000528}
529
Michael Hanf1aae3b2012-08-03 17:40:43 +0000530static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000531 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000532 if (!checkGuardedVarAttrCommon(S, D, Attr))
533 return;
534
535 if (!threadSafetyCheckIsPointer(S, D, Attr))
536 return;
537
Michael Han51d8c522013-01-24 16:46:58 +0000538 D->addAttr(::new (S.Context)
539 PtGuardedVarAttr(Attr.getRange(), S.Context,
540 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000541}
542
Michael Hanf1aae3b2012-08-03 17:40:43 +0000543static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
544 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000545 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000546 assert(!Attr.isInvalid());
547
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000548 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000549 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000550
551 // D must be either a member field or global (potentially shared) variable.
552 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000553 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
554 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000555 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000556 }
557
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000558 SmallVector<Expr*, 1> Args;
559 // check that all arguments are lockable objects
560 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
561 unsigned Size = Args.size();
562 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000563 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000564
Michael Handc691572012-07-23 18:48:41 +0000565 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000566
Michael Handc691572012-07-23 18:48:41 +0000567 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000568}
569
Michael Handc691572012-07-23 18:48:41 +0000570static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
571 Expr *Arg = 0;
572 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
573 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000574
Michael Handc691572012-07-23 18:48:41 +0000575 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
576}
577
Michael Hanf1aae3b2012-08-03 17:40:43 +0000578static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000579 const AttributeList &Attr) {
580 Expr *Arg = 0;
581 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
582 return;
583
584 if (!threadSafetyCheckIsPointer(S, D, Attr))
585 return;
586
587 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
588 S.Context, Arg));
589}
590
Michael Hanf1aae3b2012-08-03 17:40:43 +0000591static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000592 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000593 assert(!Attr.isInvalid());
594
595 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000596 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000597
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000598 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000599 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000600 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
601 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000602 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000603 }
604
Michael Handc691572012-07-23 18:48:41 +0000605 return true;
606}
607
608static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
609 if (!checkLockableAttrCommon(S, D, Attr))
610 return;
611
612 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
613}
614
Michael Hanf1aae3b2012-08-03 17:40:43 +0000615static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000616 const AttributeList &Attr) {
617 if (!checkLockableAttrCommon(S, D, Attr))
618 return;
619
Michael Han51d8c522013-01-24 16:46:58 +0000620 D->addAttr(::new (S.Context)
621 ScopedLockableAttr(Attr.getRange(), S.Context,
622 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000623}
624
Kostya Serebryany85aee962013-02-26 06:58:27 +0000625static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
626 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000627 assert(!Attr.isInvalid());
628
629 if (!checkAttributeNumArgs(S, Attr, 0))
630 return;
631
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000632 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000633 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
634 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000635 return;
636 }
637
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000638 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000639 S.Context));
640}
641
Kostya Serebryany85aee962013-02-26 06:58:27 +0000642static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000643 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000644 assert(!Attr.isInvalid());
645
646 if (!checkAttributeNumArgs(S, Attr, 0))
647 return;
648
649 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000651 << Attr.getName() << ExpectedFunctionOrMethod;
652 return;
653 }
654
Michael Han51d8c522013-01-24 16:46:58 +0000655 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000656 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
657 Attr.getAttributeSpellingListIndex()));
658}
659
660static void handleNoSanitizeMemory(Sema &S, Decl *D,
661 const AttributeList &Attr) {
662 assert(!Attr.isInvalid());
663
664 if (!checkAttributeNumArgs(S, Attr, 0))
665 return;
666
667 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
669 << Attr.getName() << ExpectedFunctionOrMethod;
670 return;
671 }
672
673 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
674 S.Context));
675}
676
677static void handleNoSanitizeThread(Sema &S, Decl *D,
678 const AttributeList &Attr) {
679 assert(!Attr.isInvalid());
680
681 if (!checkAttributeNumArgs(S, Attr, 0))
682 return;
683
684 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
686 << Attr.getName() << ExpectedFunctionOrMethod;
687 return;
688 }
689
690 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
691 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000692}
693
Michael Hanf1aae3b2012-08-03 17:40:43 +0000694static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
695 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000696 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000697 assert(!Attr.isInvalid());
698
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000699 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000700 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000701
702 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000703 ValueDecl *VD = dyn_cast<ValueDecl>(D);
704 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000705 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
706 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000707 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000708 }
709
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000710 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000711 QualType QT = VD->getType();
712 if (!QT->isDependentType()) {
713 const RecordType *RT = getRecordType(QT);
714 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000715 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000716 << Attr.getName();
717 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000718 }
719 }
720
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000721 // Check that all arguments are lockable objects.
722 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000723 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000724 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000725
Michael Handc691572012-07-23 18:48:41 +0000726 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000727}
728
Michael Hanf1aae3b2012-08-03 17:40:43 +0000729static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 1> Args;
732 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
733 return;
734
735 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 AcquiredAfterAttr(Attr.getRange(), S.Context,
738 StartArg, Args.size(),
739 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000740}
741
Michael Hanf1aae3b2012-08-03 17:40:43 +0000742static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000743 const AttributeList &Attr) {
744 SmallVector<Expr*, 1> Args;
745 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
746 return;
747
748 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000749 D->addAttr(::new (S.Context)
750 AcquiredBeforeAttr(Attr.getRange(), S.Context,
751 StartArg, Args.size(),
752 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000753}
754
Michael Hanf1aae3b2012-08-03 17:40:43 +0000755static bool checkLockFunAttrCommon(Sema &S, Decl *D,
756 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000757 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000758 assert(!Attr.isInvalid());
759
760 // zero or more arguments ok
761
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000762 // check that the attribute is applied to a function
763 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000764 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
765 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000766 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000767 }
768
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000769 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000770 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000771
Michael Handc691572012-07-23 18:48:41 +0000772 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000773}
774
Michael Hanf1aae3b2012-08-03 17:40:43 +0000775static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000776 const AttributeList &Attr) {
777 SmallVector<Expr*, 1> Args;
778 if (!checkLockFunAttrCommon(S, D, Attr, Args))
779 return;
780
781 unsigned Size = Args.size();
782 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000783 D->addAttr(::new (S.Context)
784 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
785 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000786}
787
Michael Hanf1aae3b2012-08-03 17:40:43 +0000788static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000789 const AttributeList &Attr) {
790 SmallVector<Expr*, 1> Args;
791 if (!checkLockFunAttrCommon(S, D, Attr, Args))
792 return;
793
794 unsigned Size = Args.size();
795 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000796 D->addAttr(::new (S.Context)
797 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
798 StartArg, Size,
799 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000800}
801
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000802static void handleAssertSharedLockAttr(Sema &S, Decl *D,
803 const AttributeList &Attr) {
804 SmallVector<Expr*, 1> Args;
805 if (!checkLockFunAttrCommon(S, D, Attr, Args))
806 return;
807
808 unsigned Size = Args.size();
809 Expr **StartArg = Size == 0 ? 0 : &Args[0];
810 D->addAttr(::new (S.Context)
811 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
812 Attr.getAttributeSpellingListIndex()));
813}
814
815static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
816 const AttributeList &Attr) {
817 SmallVector<Expr*, 1> Args;
818 if (!checkLockFunAttrCommon(S, D, Attr, Args))
819 return;
820
821 unsigned Size = Args.size();
822 Expr **StartArg = Size == 0 ? 0 : &Args[0];
823 D->addAttr(::new (S.Context)
824 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
825 StartArg, Size,
826 Attr.getAttributeSpellingListIndex()));
827}
828
829
Michael Hanf1aae3b2012-08-03 17:40:43 +0000830static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
831 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000832 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000833 assert(!Attr.isInvalid());
834
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000835 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000836 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000837
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000839 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
840 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000841 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000842 }
843
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
846 << Attr.getName() << 1 << ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000847 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000848 }
849
850 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000851 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000852
Michael Handc691572012-07-23 18:48:41 +0000853 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000854}
855
Michael Hanf1aae3b2012-08-03 17:40:43 +0000856static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000857 const AttributeList &Attr) {
858 SmallVector<Expr*, 2> Args;
859 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
860 return;
861
862 unsigned Size = Args.size();
863 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000864 D->addAttr(::new (S.Context)
865 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
866 Attr.getArg(0), StartArg, Size,
867 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000868}
869
Michael Hanf1aae3b2012-08-03 17:40:43 +0000870static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000871 const AttributeList &Attr) {
872 SmallVector<Expr*, 2> Args;
873 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
874 return;
875
876 unsigned Size = Args.size();
877 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000878 D->addAttr(::new (S.Context)
879 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
880 Attr.getArg(0), StartArg, Size,
881 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000882}
883
Michael Hanf1aae3b2012-08-03 17:40:43 +0000884static bool checkLocksRequiredCommon(Sema &S, Decl *D,
885 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000886 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000887 assert(!Attr.isInvalid());
888
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000889 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000890 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000893 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
894 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000895 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000896 }
897
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000898 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000899 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000900 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000901 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000902
Michael Handc691572012-07-23 18:48:41 +0000903 return true;
904}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000905
Michael Hanf1aae3b2012-08-03 17:40:43 +0000906static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000907 const AttributeList &Attr) {
908 SmallVector<Expr*, 1> Args;
909 if (!checkLocksRequiredCommon(S, D, Attr, Args))
910 return;
911
912 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000913 D->addAttr(::new (S.Context)
914 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
915 StartArg, Args.size(),
916 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000917}
918
Michael Hanf1aae3b2012-08-03 17:40:43 +0000919static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000920 const AttributeList &Attr) {
921 SmallVector<Expr*, 1> Args;
922 if (!checkLocksRequiredCommon(S, D, Attr, Args))
923 return;
924
925 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000926 D->addAttr(::new (S.Context)
927 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
928 StartArg, Args.size(),
929 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000930}
931
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000932static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000933 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000934 assert(!Attr.isInvalid());
935
936 // zero or more arguments ok
937
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000938 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000939 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
940 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000941 return;
942 }
943
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000944 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000945 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000946 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000947 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000948 Expr **StartArg = Size == 0 ? 0 : &Args[0];
949
Michael Han51d8c522013-01-24 16:46:58 +0000950 D->addAttr(::new (S.Context)
951 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
952 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000953}
954
955static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000956 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000957 assert(!Attr.isInvalid());
958
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000959 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000960 return;
961
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000962 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000963 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
964 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000965 return;
966 }
967
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000968 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000969 SmallVector<Expr*, 1> Args;
970 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
971 unsigned Size = Args.size();
972 if (Size == 0)
973 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000974
Michael Han51d8c522013-01-24 16:46:58 +0000975 D->addAttr(::new (S.Context)
976 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
977 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000978}
979
980static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000981 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000982 assert(!Attr.isInvalid());
983
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000984 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000985 return;
986
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000987 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000988 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
989 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000990 return;
991 }
992
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000993 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000994 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000995 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000996 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000997 if (Size == 0)
998 return;
999 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001000
Michael Han51d8c522013-01-24 16:46:58 +00001001 D->addAttr(::new (S.Context)
1002 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1003 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001004}
1005
1006
Chandler Carruth1b03c872011-07-02 00:01:44 +00001007static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1008 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001009 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1010 if (TD == 0) {
1011 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1012 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001013 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001014 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001015 }
Mike Stumpbf916502009-07-24 19:02:52 +00001016
Richard Smitha4fa9002013-01-13 02:11:23 +00001017 // Remember this typedef decl, we will need it later for diagnostics.
1018 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001019}
1020
Chandler Carruth1b03c872011-07-02 00:01:44 +00001021static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001022 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001023 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001024 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001025
Chandler Carruth87c44602011-07-01 23:49:12 +00001026 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001027 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001028 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001029 // If the alignment is less than or equal to 8 bits, the packed attribute
1030 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001031 if (!FD->getType()->isDependentType() &&
1032 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001033 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001035 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001036 else
Michael Han51d8c522013-01-24 16:46:58 +00001037 FD->addAttr(::new (S.Context)
1038 PackedAttr(Attr.getRange(), S.Context,
1039 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001040 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001041 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001042}
1043
Chandler Carruth1b03c872011-07-02 00:01:44 +00001044static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001045 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001046 RD->addAttr(::new (S.Context)
1047 MsStructAttr(Attr.getRange(), S.Context,
1048 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001049 else
1050 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1051}
1052
Chandler Carruth1b03c872011-07-02 00:01:44 +00001053static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001054 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001055 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001056 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001057
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001058 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001059 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001060 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001061 D->addAttr(::new (S.Context)
1062 IBActionAttr(Attr.getRange(), S.Context,
1063 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001064 return;
1065 }
1066
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001067 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001068}
1069
Ted Kremenek2f041d02011-09-29 07:02:25 +00001070static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1071 // The IBOutlet/IBOutletCollection attributes only apply to instance
1072 // variables or properties of Objective-C classes. The outlet must also
1073 // have an object reference type.
1074 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1075 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001076 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001077 << Attr.getName() << VD->getType() << 0;
1078 return false;
1079 }
1080 }
1081 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1082 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001083 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001084 << Attr.getName() << PD->getType() << 1;
1085 return false;
1086 }
1087 }
1088 else {
1089 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1090 return false;
1091 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001092
Ted Kremenek2f041d02011-09-29 07:02:25 +00001093 return true;
1094}
1095
Chandler Carruth1b03c872011-07-02 00:01:44 +00001096static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001097 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001098 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001099 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001100
1101 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001102 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001103
Michael Han51d8c522013-01-24 16:46:58 +00001104 D->addAttr(::new (S.Context)
1105 IBOutletAttr(Attr.getRange(), S.Context,
1106 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001107}
1108
Chandler Carruth1b03c872011-07-02 00:01:44 +00001109static void handleIBOutletCollection(Sema &S, Decl *D,
1110 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001111
1112 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001113 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001114 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1115 return;
1116 }
1117
Ted Kremenek2f041d02011-09-29 07:02:25 +00001118 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001119 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001120
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001121 IdentifierInfo *II = Attr.getParameterName();
1122 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001123 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001124
John McCallb3d87482010-08-24 05:47:05 +00001125 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001126 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001127 if (!TypeRep) {
1128 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1129 return;
1130 }
John McCallb3d87482010-08-24 05:47:05 +00001131 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001132 // Diagnose use of non-object type in iboutletcollection attribute.
1133 // FIXME. Gnu attribute extension ignores use of builtin types in
1134 // attributes. So, __attribute__((iboutletcollection(char))) will be
1135 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001136 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001137 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1138 return;
1139 }
Michael Han51d8c522013-01-24 16:46:58 +00001140 D->addAttr(::new (S.Context)
1141 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1142 QT, Attr.getParameterLoc(),
1143 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001144}
1145
Chandler Carruthd309c812011-07-01 23:49:16 +00001146static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001147 if (const RecordType *UT = T->getAsUnionType())
1148 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1149 RecordDecl *UD = UT->getDecl();
1150 for (RecordDecl::field_iterator it = UD->field_begin(),
1151 itend = UD->field_end(); it != itend; ++it) {
1152 QualType QT = it->getType();
1153 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1154 T = QT;
1155 return;
1156 }
1157 }
1158 }
1159}
1160
Nuno Lopes587de5b2012-05-24 00:22:00 +00001161static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001162 if (!isFunctionOrMethod(D)) {
1163 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1164 << "alloc_size" << ExpectedFunctionOrMethod;
1165 return;
1166 }
1167
Nuno Lopes587de5b2012-05-24 00:22:00 +00001168 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1169 return;
1170
1171 // In C++ the implicit 'this' function parameter also counts, and they are
1172 // counted from one.
1173 bool HasImplicitThisParam = isInstanceMethod(D);
Argyrios Kyrtzidis28965bf2013-02-22 06:58:28 +00001174 unsigned NumArgs;
1175 if (hasFunctionProto(D))
1176 NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1177 else
1178 NumArgs = 0;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001179
1180 SmallVector<unsigned, 8> SizeArgs;
1181
1182 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1183 E = Attr.arg_end(); I!=E; ++I) {
1184 // The argument must be an integer constant expression.
1185 Expr *Ex = *I;
1186 llvm::APSInt ArgNum;
1187 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1188 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1190 << "alloc_size" << Ex->getSourceRange();
1191 return;
1192 }
1193
1194 uint64_t x = ArgNum.getZExtValue();
1195
1196 if (x < 1 || x > NumArgs) {
1197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1198 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1199 return;
1200 }
1201
1202 --x;
1203 if (HasImplicitThisParam) {
1204 if (x == 0) {
1205 S.Diag(Attr.getLoc(),
1206 diag::err_attribute_invalid_implicit_this_argument)
1207 << "alloc_size" << Ex->getSourceRange();
1208 return;
1209 }
1210 --x;
1211 }
1212
1213 // check if the function argument is of an integer type
1214 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1215 if (!T->isIntegerType()) {
1216 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1217 << "alloc_size" << Ex->getSourceRange();
1218 return;
1219 }
1220
Nuno Lopes587de5b2012-05-24 00:22:00 +00001221 SizeArgs.push_back(x);
1222 }
1223
1224 // check if the function returns a pointer
1225 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1226 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1227 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1228 }
1229
Michael Han51d8c522013-01-24 16:46:58 +00001230 D->addAttr(::new (S.Context)
1231 AllocSizeAttr(Attr.getRange(), S.Context,
1232 SizeArgs.data(), SizeArgs.size(),
1233 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001234}
1235
Chandler Carruth1b03c872011-07-02 00:01:44 +00001236static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001237 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1238 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001239 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001240 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001241 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001242 return;
1243 }
Mike Stumpbf916502009-07-24 19:02:52 +00001244
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001245 // In C++ the implicit 'this' function parameter also counts, and they are
1246 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001247 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001248 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001249
1250 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001251 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001252
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001253 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1254 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001255 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001256 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001257 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001258 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1259 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1261 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001262 return;
1263 }
Mike Stumpbf916502009-07-24 19:02:52 +00001264
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001265 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001266
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001267 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001269 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001270 return;
1271 }
Mike Stumpbf916502009-07-24 19:02:52 +00001272
Ted Kremenek465172f2008-07-21 22:09:15 +00001273 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001274 if (HasImplicitThisParam) {
1275 if (x == 0) {
1276 S.Diag(Attr.getLoc(),
1277 diag::err_attribute_invalid_implicit_this_argument)
1278 << "nonnull" << Ex->getSourceRange();
1279 return;
1280 }
1281 --x;
1282 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001283
1284 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001285 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001286 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001287
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001288 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001289 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001290 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001291 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001292 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001293 }
Mike Stumpbf916502009-07-24 19:02:52 +00001294
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001295 NonNullArgs.push_back(x);
1296 }
Mike Stumpbf916502009-07-24 19:02:52 +00001297
1298 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1299 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001300 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001301 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1302 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001303 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001304 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001305 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001306 }
Mike Stumpbf916502009-07-24 19:02:52 +00001307
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001308 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001309 if (NonNullArgs.empty()) {
1310 // Warn the trivial case only if attribute is not coming from a
1311 // macro instantiation.
1312 if (Attr.getLoc().isFileID())
1313 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001314 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001315 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001316 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001317
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001318 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001319 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001320 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001321 D->addAttr(::new (S.Context)
1322 NonNullAttr(Attr.getRange(), S.Context, start, size,
1323 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001324}
1325
Chandler Carruth1b03c872011-07-02 00:01:44 +00001326static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001327 // This attribute must be applied to a function declaration.
1328 // The first argument to the attribute must be a string,
1329 // the name of the resource, for example "malloc".
1330 // The following arguments must be argument indexes, the arguments must be
1331 // of integer type for Returns, otherwise of pointer type.
1332 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001333 // after being held. free() should be __attribute((ownership_takes)), whereas
1334 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001335
1336 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001337 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1338 << AL.getName()->getName() << 1 << ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001339 return;
1340 }
1341 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001342 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001343 switch (AL.getKind()) {
1344 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001345 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001346 if (AL.getNumArgs() < 1) {
1347 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1348 return;
1349 }
Jordy Rose2a479922010-08-12 08:54:03 +00001350 break;
1351 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001352 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001353 if (AL.getNumArgs() < 1) {
1354 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1355 return;
1356 }
Jordy Rose2a479922010-08-12 08:54:03 +00001357 break;
1358 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001359 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001360 if (AL.getNumArgs() > 1) {
1361 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1362 << AL.getNumArgs() + 1;
1363 return;
1364 }
Jordy Rose2a479922010-08-12 08:54:03 +00001365 break;
1366 default:
1367 // This should never happen given how we are called.
1368 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001369 }
1370
Chandler Carruth87c44602011-07-01 23:49:12 +00001371 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001372 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1373 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001374 return;
1375 }
1376
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001377 // In C++ the implicit 'this' function parameter also counts, and they are
1378 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001379 bool HasImplicitThisParam = isInstanceMethod(D);
1380 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001381
Chris Lattner5f9e2722011-07-23 10:55:15 +00001382 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001383
1384 // Normalize the argument, __foo__ becomes foo.
1385 if (Module.startswith("__") && Module.endswith("__"))
1386 Module = Module.substr(2, Module.size() - 4);
1387
Chris Lattner5f9e2722011-07-23 10:55:15 +00001388 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001389
Jordy Rose2a479922010-08-12 08:54:03 +00001390 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1391 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001392
Peter Collingbourne7a730022010-11-23 20:45:58 +00001393 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001394 llvm::APSInt ArgNum(32);
1395 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1396 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1397 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1398 << AL.getName()->getName() << IdxExpr->getSourceRange();
1399 continue;
1400 }
1401
1402 unsigned x = (unsigned) ArgNum.getZExtValue();
1403
1404 if (x > NumArgs || x < 1) {
1405 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1406 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1407 continue;
1408 }
1409 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001410 if (HasImplicitThisParam) {
1411 if (x == 0) {
1412 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1413 << "ownership" << IdxExpr->getSourceRange();
1414 return;
1415 }
1416 --x;
1417 }
1418
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001419 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001420 case OwnershipAttr::Takes:
1421 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001422 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001423 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001424 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1425 // FIXME: Should also highlight argument in decl.
1426 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001427 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001428 << "pointer"
1429 << IdxExpr->getSourceRange();
1430 continue;
1431 }
1432 break;
1433 }
Sean Huntcf807c42010-08-18 23:23:40 +00001434 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001435 if (AL.getNumArgs() > 1) {
1436 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001437 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001438 llvm::APSInt ArgNum(32);
1439 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1440 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1441 S.Diag(AL.getLoc(), diag::err_ownership_type)
1442 << "ownership_returns" << "integer"
1443 << IdxExpr->getSourceRange();
1444 return;
1445 }
1446 }
1447 break;
1448 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001449 } // switch
1450
1451 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001452 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001453 i = D->specific_attr_begin<OwnershipAttr>(),
1454 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001455 i != e; ++i) {
1456 if ((*i)->getOwnKind() != K) {
1457 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1458 I!=E; ++I) {
1459 if (x == *I) {
1460 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1461 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001462 }
1463 }
1464 }
1465 }
1466 OwnershipArgs.push_back(x);
1467 }
1468
1469 unsigned* start = OwnershipArgs.data();
1470 unsigned size = OwnershipArgs.size();
1471 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001472
1473 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1474 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1475 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001476 }
Sean Huntcf807c42010-08-18 23:23:40 +00001477
Michael Han51d8c522013-01-24 16:46:58 +00001478 D->addAttr(::new (S.Context)
1479 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1480 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001481}
1482
Chandler Carruth1b03c872011-07-02 00:01:44 +00001483static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001484 // Check the attribute arguments.
1485 if (Attr.getNumArgs() > 1) {
1486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1487 return;
1488 }
1489
Chandler Carruth87c44602011-07-01 23:49:12 +00001490 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001492 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001493 return;
1494 }
1495
Chandler Carruth87c44602011-07-01 23:49:12 +00001496 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001497
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001498 // gcc rejects
1499 // class c {
1500 // static int a __attribute__((weakref ("v2")));
1501 // static int b() __attribute__((weakref ("f3")));
1502 // };
1503 // and ignores the attributes of
1504 // void f(void) {
1505 // static int a __attribute__((weakref ("v2")));
1506 // }
1507 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001508 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001509 if (!Ctx->isFileContext()) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001511 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001512 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001513 }
1514
1515 // The GCC manual says
1516 //
1517 // At present, a declaration to which `weakref' is attached can only
1518 // be `static'.
1519 //
1520 // It also says
1521 //
1522 // Without a TARGET,
1523 // given as an argument to `weakref' or to `alias', `weakref' is
1524 // equivalent to `weak'.
1525 //
1526 // gcc 4.4.1 will accept
1527 // int a7 __attribute__((weakref));
1528 // as
1529 // int a7 __attribute__((weak));
1530 // This looks like a bug in gcc. We reject that for now. We should revisit
1531 // it if this behaviour is actually used.
1532
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001533 // GCC rejects
1534 // static ((alias ("y"), weakref)).
1535 // Should we? How to check that weakref is before or after alias?
1536
1537 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001538 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001539 Arg = Arg->IgnoreParenCasts();
1540 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1541
Douglas Gregor5cee1192011-07-27 05:40:30 +00001542 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001543 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1544 << Attr.getName() << 1 << ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001545 return;
1546 }
1547 // GCC will accept anything as the argument of weakref. Should we
1548 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001549 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001550 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001551 }
1552
Michael Han51d8c522013-01-24 16:46:58 +00001553 D->addAttr(::new (S.Context)
1554 WeakRefAttr(Attr.getRange(), S.Context,
1555 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001556}
1557
Chandler Carruth1b03c872011-07-02 00:01:44 +00001558static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001559 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001560 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001561 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001562
Peter Collingbourne7a730022010-11-23 20:45:58 +00001563 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001564 Arg = Arg->IgnoreParenCasts();
1565 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001566
Douglas Gregor5cee1192011-07-27 05:40:30 +00001567 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001568 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1569 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001570 return;
1571 }
Mike Stumpbf916502009-07-24 19:02:52 +00001572
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001573 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001574 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1575 return;
1576 }
1577
Chris Lattner6b6b5372008-06-26 18:38:35 +00001578 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001579
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001580 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001581 Str->getString(),
1582 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001583}
1584
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001585static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1586 // Check the attribute arguments.
1587 if (!checkAttributeNumArgs(S, Attr, 0))
1588 return;
1589
1590 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1591 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1592 << Attr.getName() << ExpectedFunctionOrMethod;
1593 return;
1594 }
1595
Michael Han51d8c522013-01-24 16:46:58 +00001596 D->addAttr(::new (S.Context)
1597 MinSizeAttr(Attr.getRange(), S.Context,
1598 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001599}
1600
Benjamin Krameree409a92012-05-12 21:10:52 +00001601static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1602 // Check the attribute arguments.
1603 if (!checkAttributeNumArgs(S, Attr, 0))
1604 return;
1605
1606 if (!isa<FunctionDecl>(D)) {
1607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1608 << Attr.getName() << ExpectedFunction;
1609 return;
1610 }
1611
1612 if (D->hasAttr<HotAttr>()) {
1613 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1614 << Attr.getName() << "hot";
1615 return;
1616 }
1617
Michael Han51d8c522013-01-24 16:46:58 +00001618 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001620}
1621
1622static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1623 // Check the attribute arguments.
1624 if (!checkAttributeNumArgs(S, Attr, 0))
1625 return;
1626
1627 if (!isa<FunctionDecl>(D)) {
1628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1629 << Attr.getName() << ExpectedFunction;
1630 return;
1631 }
1632
1633 if (D->hasAttr<ColdAttr>()) {
1634 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1635 << Attr.getName() << "cold";
1636 return;
1637 }
1638
Michael Han51d8c522013-01-24 16:46:58 +00001639 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1640 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001641}
1642
Chandler Carruth1b03c872011-07-02 00:01:44 +00001643static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001644 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001645 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001646 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001647
Chandler Carruth87c44602011-07-01 23:49:12 +00001648 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001650 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001651 return;
1652 }
1653
Michael Han51d8c522013-01-24 16:46:58 +00001654 D->addAttr(::new (S.Context)
1655 NakedAttr(Attr.getRange(), S.Context,
1656 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001657}
1658
Chandler Carruth1b03c872011-07-02 00:01:44 +00001659static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1660 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001661 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001662 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1664 return;
1665 }
1666
Chandler Carruth87c44602011-07-01 23:49:12 +00001667 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001669 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001670 return;
1671 }
Mike Stumpbf916502009-07-24 19:02:52 +00001672
Michael Han51d8c522013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 AlwaysInlineAttr(Attr.getRange(), S.Context,
1675 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001676}
1677
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001678static void handleTLSModelAttr(Sema &S, Decl *D,
1679 const AttributeList &Attr) {
1680 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001681 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001682 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001683
1684 Expr *Arg = Attr.getArg(0);
1685 Arg = Arg->IgnoreParenCasts();
1686 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1687
1688 // Check that it is a string.
1689 if (!Str) {
1690 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1691 return;
1692 }
1693
Richard Smith38afbc72013-04-13 02:43:54 +00001694 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1696 << Attr.getName() << ExpectedTLSVar;
1697 return;
1698 }
1699
1700 // Check that the value.
1701 StringRef Model = Str->getString();
1702 if (Model != "global-dynamic" && Model != "local-dynamic"
1703 && Model != "initial-exec" && Model != "local-exec") {
1704 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1705 return;
1706 }
1707
Michael Han51d8c522013-01-24 16:46:58 +00001708 D->addAttr(::new (S.Context)
1709 TLSModelAttr(Attr.getRange(), S.Context, Model,
1710 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001711}
1712
Chandler Carruth1b03c872011-07-02 00:01:44 +00001713static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001714 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001715 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001716 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1717 return;
1718 }
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Chandler Carruth87c44602011-07-01 23:49:12 +00001720 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001721 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001722 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001723 D->addAttr(::new (S.Context)
1724 MallocAttr(Attr.getRange(), S.Context,
1725 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001726 return;
1727 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001728 }
1729
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001730 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001731}
1732
Chandler Carruth1b03c872011-07-02 00:01:44 +00001733static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001734 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001735 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001736 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001737
Michael Han51d8c522013-01-24 16:46:58 +00001738 D->addAttr(::new (S.Context)
1739 MayAliasAttr(Attr.getRange(), S.Context,
1740 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001741}
1742
Chandler Carruth1b03c872011-07-02 00:01:44 +00001743static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001744 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001745 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context)
1747 NoCommonAttr(Attr.getRange(), S.Context,
1748 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001749 else
1750 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001751 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001752}
1753
Chandler Carruth1b03c872011-07-02 00:01:44 +00001754static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001755 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001756
1757 if (S.LangOpts.CPlusPlus) {
1758 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1759 return;
1760 }
1761
Chandler Carruth87c44602011-07-01 23:49:12 +00001762 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001763 D->addAttr(::new (S.Context)
1764 CommonAttr(Attr.getRange(), S.Context,
1765 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001766 else
1767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001768 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001769}
1770
Chandler Carruth1b03c872011-07-02 00:01:44 +00001771static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001772 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001773
1774 if (S.CheckNoReturnAttr(attr)) return;
1775
Chandler Carruth87c44602011-07-01 23:49:12 +00001776 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001777 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001778 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001779 return;
1780 }
1781
Michael Han51d8c522013-01-24 16:46:58 +00001782 D->addAttr(::new (S.Context)
1783 NoReturnAttr(attr.getRange(), S.Context,
1784 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001785}
1786
1787bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001788 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001789 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1790 attr.setInvalid();
1791 return true;
1792 }
1793
1794 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001795}
1796
Chandler Carruth1b03c872011-07-02 00:01:44 +00001797static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1798 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001799
1800 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1801 // because 'analyzer_noreturn' does not impact the type.
1802
Chandler Carruth1731e202011-07-11 23:30:35 +00001803 if(!checkAttributeNumArgs(S, Attr, 0))
1804 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001805
Chandler Carruth87c44602011-07-01 23:49:12 +00001806 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1807 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001808 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1809 && !VD->getType()->isFunctionPointerType())) {
1810 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001811 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001812 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001813 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001814 return;
1815 }
1816 }
1817
Michael Han51d8c522013-01-24 16:46:58 +00001818 D->addAttr(::new (S.Context)
1819 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1820 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001821}
1822
Richard Smithcd8ab512013-01-17 01:30:42 +00001823static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1824 const AttributeList &Attr) {
1825 // C++11 [dcl.attr.noreturn]p1:
1826 // The attribute may be applied to the declarator-id in a function
1827 // declaration.
1828 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1829 if (!FD) {
1830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1831 << Attr.getName() << ExpectedFunctionOrMethod;
1832 return;
1833 }
1834
Michael Han51d8c522013-01-24 16:46:58 +00001835 D->addAttr(::new (S.Context)
1836 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1837 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001838}
1839
John Thompson35cc9622010-08-09 21:53:52 +00001840// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001841static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001842/*
1843 Returning a Vector Class in Registers
1844
Eric Christopherf48f3672010-12-01 22:13:54 +00001845 According to the PPU ABI specifications, a class with a single member of
1846 vector type is returned in memory when used as the return value of a function.
1847 This results in inefficient code when implementing vector classes. To return
1848 the value in a single vector register, add the vecreturn attribute to the
1849 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001850
1851 Example:
1852
1853 struct Vector
1854 {
1855 __vector float xyzw;
1856 } __attribute__((vecreturn));
1857
1858 Vector Add(Vector lhs, Vector rhs)
1859 {
1860 Vector result;
1861 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1862 return result; // This will be returned in a register
1863 }
1864*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001865 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001867 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001868 return;
1869 }
1870
Chandler Carruth87c44602011-07-01 23:49:12 +00001871 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001872 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1873 return;
1874 }
1875
Chandler Carruth87c44602011-07-01 23:49:12 +00001876 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001877 int count = 0;
1878
1879 if (!isa<CXXRecordDecl>(record)) {
1880 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1881 return;
1882 }
1883
1884 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1885 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1886 return;
1887 }
1888
Eric Christopherf48f3672010-12-01 22:13:54 +00001889 for (RecordDecl::field_iterator iter = record->field_begin();
1890 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001891 if ((count == 1) || !iter->getType()->isVectorType()) {
1892 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1893 return;
1894 }
1895 count++;
1896 }
1897
Michael Han51d8c522013-01-24 16:46:58 +00001898 D->addAttr(::new (S.Context)
1899 VecReturnAttr(Attr.getRange(), S.Context,
1900 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001901}
1902
Richard Smith3a2b7a12013-01-28 22:42:45 +00001903static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1904 const AttributeList &Attr) {
1905 if (isa<ParmVarDecl>(D)) {
1906 // [[carries_dependency]] can only be applied to a parameter if it is a
1907 // parameter of a function declaration or lambda.
1908 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1909 S.Diag(Attr.getLoc(),
1910 diag::err_carries_dependency_param_not_function_decl);
1911 return;
1912 }
1913 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001915 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001916 return;
1917 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001918
1919 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1920 Attr.getRange(), S.Context,
1921 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001922}
1923
Chandler Carruth1b03c872011-07-02 00:01:44 +00001924static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001925 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001926 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001928 return;
1929 }
Mike Stumpbf916502009-07-24 19:02:52 +00001930
Chandler Carruth87c44602011-07-01 23:49:12 +00001931 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001932 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001933 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001934 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001935 return;
1936 }
Mike Stumpbf916502009-07-24 19:02:52 +00001937
Michael Han51d8c522013-01-24 16:46:58 +00001938 D->addAttr(::new (S.Context)
1939 UnusedAttr(Attr.getRange(), S.Context,
1940 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001941}
1942
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001943static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1944 const AttributeList &Attr) {
1945 // check the attribute arguments.
1946 if (Attr.hasParameterOrArguments()) {
1947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1948 return;
1949 }
1950
1951 if (!isa<FunctionDecl>(D)) {
1952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1953 << Attr.getName() << ExpectedFunction;
1954 return;
1955 }
1956
Michael Han51d8c522013-01-24 16:46:58 +00001957 D->addAttr(::new (S.Context)
1958 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1959 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001960}
1961
Chandler Carruth1b03c872011-07-02 00:01:44 +00001962static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001963 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001964 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1966 return;
1967 }
Mike Stumpbf916502009-07-24 19:02:52 +00001968
Chandler Carruth87c44602011-07-01 23:49:12 +00001969 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001970 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1972 return;
1973 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001974 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001976 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001977 return;
1978 }
Mike Stumpbf916502009-07-24 19:02:52 +00001979
Michael Han51d8c522013-01-24 16:46:58 +00001980 D->addAttr(::new (S.Context)
1981 UsedAttr(Attr.getRange(), S.Context,
1982 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001983}
1984
Chandler Carruth1b03c872011-07-02 00:01:44 +00001985static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001986 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001987 if (Attr.getNumArgs() > 1) {
1988 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001989 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001990 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001991
1992 int priority = 65535; // FIXME: Do not hardcode such constants.
1993 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001994 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001995 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001996 if (E->isTypeDependent() || E->isValueDependent() ||
1997 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1999 << Attr.getName() << 1 << ArgumentIntegerConstant
2000 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002001 return;
2002 }
2003 priority = Idx.getZExtValue();
2004 }
Mike Stumpbf916502009-07-24 19:02:52 +00002005
Chandler Carruth87c44602011-07-01 23:49:12 +00002006 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002008 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002009 return;
2010 }
2011
Michael Han51d8c522013-01-24 16:46:58 +00002012 D->addAttr(::new (S.Context)
2013 ConstructorAttr(Attr.getRange(), S.Context, priority,
2014 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002015}
2016
Chandler Carruth1b03c872011-07-02 00:01:44 +00002017static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002018 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002019 if (Attr.getNumArgs() > 1) {
2020 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002021 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002022 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002023
2024 int priority = 65535; // FIXME: Do not hardcode such constants.
2025 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002026 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002027 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002028 if (E->isTypeDependent() || E->isValueDependent() ||
2029 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2031 << Attr.getName() << 1 << ArgumentIntegerConstant
2032 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002033 return;
2034 }
2035 priority = Idx.getZExtValue();
2036 }
Mike Stumpbf916502009-07-24 19:02:52 +00002037
Chandler Carruth87c44602011-07-01 23:49:12 +00002038 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002040 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002041 return;
2042 }
2043
Michael Han51d8c522013-01-24 16:46:58 +00002044 D->addAttr(::new (S.Context)
2045 DestructorAttr(Attr.getRange(), S.Context, priority,
2046 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002047}
2048
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002049template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002050static void handleAttrWithMessage(Sema &S, Decl *D,
2051 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002052 unsigned NumArgs = Attr.getNumArgs();
2053 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002054 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002055 return;
2056 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002057
2058 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002059 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002060 if (NumArgs == 1) {
2061 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002062 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002063 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002064 << Attr.getName();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002065 return;
2066 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002067 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002068 }
Mike Stumpbf916502009-07-24 19:02:52 +00002069
Michael Han51d8c522013-01-24 16:46:58 +00002070 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2071 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002072}
2073
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002074static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2075 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002076 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002077 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002078
Michael Han51d8c522013-01-24 16:46:58 +00002079 D->addAttr(::new (S.Context)
2080 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2081 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002082}
2083
Patrick Beardb2f68202012-04-06 18:12:22 +00002084static void handleObjCRootClassAttr(Sema &S, Decl *D,
2085 const AttributeList &Attr) {
2086 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002087 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2088 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002089 return;
2090 }
2091
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002092 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002093 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002094
Michael Han51d8c522013-01-24 16:46:58 +00002095 D->addAttr(::new (S.Context)
2096 ObjCRootClassAttr(Attr.getRange(), S.Context,
2097 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002098}
2099
Michael Han51d8c522013-01-24 16:46:58 +00002100static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2101 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002102 if (!isa<ObjCInterfaceDecl>(D)) {
2103 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2104 return;
2105 }
2106
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002107 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002108 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002109
Michael Han51d8c522013-01-24 16:46:58 +00002110 D->addAttr(::new (S.Context)
2111 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2112 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002113}
2114
Jordy Rosefad5de92012-05-08 03:27:22 +00002115static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2116 IdentifierInfo *Platform,
2117 VersionTuple Introduced,
2118 VersionTuple Deprecated,
2119 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002120 StringRef PlatformName
2121 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2122 if (PlatformName.empty())
2123 PlatformName = Platform->getName();
2124
2125 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2126 // of these steps are needed).
2127 if (!Introduced.empty() && !Deprecated.empty() &&
2128 !(Introduced <= Deprecated)) {
2129 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2130 << 1 << PlatformName << Deprecated.getAsString()
2131 << 0 << Introduced.getAsString();
2132 return true;
2133 }
2134
2135 if (!Introduced.empty() && !Obsoleted.empty() &&
2136 !(Introduced <= Obsoleted)) {
2137 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2138 << 2 << PlatformName << Obsoleted.getAsString()
2139 << 0 << Introduced.getAsString();
2140 return true;
2141 }
2142
2143 if (!Deprecated.empty() && !Obsoleted.empty() &&
2144 !(Deprecated <= Obsoleted)) {
2145 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2146 << 2 << PlatformName << Obsoleted.getAsString()
2147 << 1 << Deprecated.getAsString();
2148 return true;
2149 }
2150
2151 return false;
2152}
2153
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002154/// \brief Check whether the two versions match.
2155///
2156/// If either version tuple is empty, then they are assumed to match. If
2157/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2158static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2159 bool BeforeIsOkay) {
2160 if (X.empty() || Y.empty())
2161 return true;
2162
2163 if (X == Y)
2164 return true;
2165
2166 if (BeforeIsOkay && X < Y)
2167 return true;
2168
2169 return false;
2170}
2171
Rafael Espindola51be6e32013-01-08 22:04:34 +00002172AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002173 IdentifierInfo *Platform,
2174 VersionTuple Introduced,
2175 VersionTuple Deprecated,
2176 VersionTuple Obsoleted,
2177 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002178 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002179 bool Override,
2180 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002181 VersionTuple MergedIntroduced = Introduced;
2182 VersionTuple MergedDeprecated = Deprecated;
2183 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002184 bool FoundAny = false;
2185
Rafael Espindola98ae8342012-05-10 02:50:16 +00002186 if (D->hasAttrs()) {
2187 AttrVec &Attrs = D->getAttrs();
2188 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2189 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2190 if (!OldAA) {
2191 ++i;
2192 continue;
2193 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002194
Rafael Espindola98ae8342012-05-10 02:50:16 +00002195 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2196 if (OldPlatform != Platform) {
2197 ++i;
2198 continue;
2199 }
2200
2201 FoundAny = true;
2202 VersionTuple OldIntroduced = OldAA->getIntroduced();
2203 VersionTuple OldDeprecated = OldAA->getDeprecated();
2204 VersionTuple OldObsoleted = OldAA->getObsoleted();
2205 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002206
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002207 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2208 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2209 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2210 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002211 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002212 if (Override) {
2213 int Which = -1;
2214 VersionTuple FirstVersion;
2215 VersionTuple SecondVersion;
2216 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2217 Which = 0;
2218 FirstVersion = OldIntroduced;
2219 SecondVersion = Introduced;
2220 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2221 Which = 1;
2222 FirstVersion = Deprecated;
2223 SecondVersion = OldDeprecated;
2224 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2225 Which = 2;
2226 FirstVersion = Obsoleted;
2227 SecondVersion = OldObsoleted;
2228 }
2229
2230 if (Which == -1) {
2231 Diag(OldAA->getLocation(),
2232 diag::warn_mismatched_availability_override_unavail)
2233 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2234 } else {
2235 Diag(OldAA->getLocation(),
2236 diag::warn_mismatched_availability_override)
2237 << Which
2238 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2239 << FirstVersion.getAsString() << SecondVersion.getAsString();
2240 }
2241 Diag(Range.getBegin(), diag::note_overridden_method);
2242 } else {
2243 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2244 Diag(Range.getBegin(), diag::note_previous_attribute);
2245 }
2246
Rafael Espindola98ae8342012-05-10 02:50:16 +00002247 Attrs.erase(Attrs.begin() + i);
2248 --e;
2249 continue;
2250 }
2251
2252 VersionTuple MergedIntroduced2 = MergedIntroduced;
2253 VersionTuple MergedDeprecated2 = MergedDeprecated;
2254 VersionTuple MergedObsoleted2 = MergedObsoleted;
2255
2256 if (MergedIntroduced2.empty())
2257 MergedIntroduced2 = OldIntroduced;
2258 if (MergedDeprecated2.empty())
2259 MergedDeprecated2 = OldDeprecated;
2260 if (MergedObsoleted2.empty())
2261 MergedObsoleted2 = OldObsoleted;
2262
2263 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2264 MergedIntroduced2, MergedDeprecated2,
2265 MergedObsoleted2)) {
2266 Attrs.erase(Attrs.begin() + i);
2267 --e;
2268 continue;
2269 }
2270
2271 MergedIntroduced = MergedIntroduced2;
2272 MergedDeprecated = MergedDeprecated2;
2273 MergedObsoleted = MergedObsoleted2;
2274 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002275 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002276 }
2277
2278 if (FoundAny &&
2279 MergedIntroduced == Introduced &&
2280 MergedDeprecated == Deprecated &&
2281 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002282 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002283
Ted Kremenekcb344392013-04-06 00:34:27 +00002284 // Only create a new attribute if !Override, but we want to do
2285 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002286 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002287 MergedDeprecated, MergedObsoleted) &&
2288 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002289 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2290 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002291 Obsoleted, IsUnavailable, Message,
2292 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002293 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002294 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002295}
2296
Chandler Carruth1b03c872011-07-02 00:01:44 +00002297static void handleAvailabilityAttr(Sema &S, Decl *D,
2298 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002299 IdentifierInfo *Platform = Attr.getParameterName();
2300 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002301 unsigned Index = Attr.getAttributeSpellingListIndex();
2302
Rafael Espindola3b294362012-05-06 19:56:25 +00002303 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002304 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2305 << Platform;
2306
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002307 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2308 if (!ND) {
2309 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2310 return;
2311 }
2312
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002313 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2314 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2315 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002316 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002317 StringRef Str;
2318 const StringLiteral *SE =
2319 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2320 if (SE)
2321 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002322
Rafael Espindola51be6e32013-01-08 22:04:34 +00002323 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002324 Platform,
2325 Introduced.Version,
2326 Deprecated.Version,
2327 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002328 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002329 /*Override=*/false,
2330 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002331 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002332 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002333}
2334
John McCalld4c3d662013-02-20 01:54:26 +00002335template <class T>
2336static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2337 typename T::VisibilityType value,
2338 unsigned attrSpellingListIndex) {
2339 T *existingAttr = D->getAttr<T>();
2340 if (existingAttr) {
2341 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2342 if (existingValue == value)
2343 return NULL;
2344 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2345 S.Diag(range.getBegin(), diag::note_previous_attribute);
2346 D->dropAttr<T>();
2347 }
2348 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2349}
2350
Rafael Espindola599f1b72012-05-13 03:25:18 +00002351VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002352 VisibilityAttr::VisibilityType Vis,
2353 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002354 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2355 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002356}
2357
John McCalld4c3d662013-02-20 01:54:26 +00002358TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2359 TypeVisibilityAttr::VisibilityType Vis,
2360 unsigned AttrSpellingListIndex) {
2361 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2362 AttrSpellingListIndex);
2363}
2364
2365static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2366 bool isTypeVisibility) {
2367 // Visibility attributes don't mean anything on a typedef.
2368 if (isa<TypedefNameDecl>(D)) {
2369 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2370 << Attr.getName();
2371 return;
2372 }
2373
2374 // 'type_visibility' can only go on a type or namespace.
2375 if (isTypeVisibility &&
2376 !(isa<TagDecl>(D) ||
2377 isa<ObjCInterfaceDecl>(D) ||
2378 isa<NamespaceDecl>(D))) {
2379 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2380 << Attr.getName() << ExpectedTypeOrNamespace;
2381 return;
2382 }
2383
Chris Lattner6b6b5372008-06-26 18:38:35 +00002384 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002385 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002386 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002387
Peter Collingbourne7a730022010-11-23 20:45:58 +00002388 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002389 Arg = Arg->IgnoreParenCasts();
2390 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002391
Douglas Gregor5cee1192011-07-27 05:40:30 +00002392 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002393 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2394 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002395 return;
2396 }
Mike Stumpbf916502009-07-24 19:02:52 +00002397
Chris Lattner5f9e2722011-07-23 10:55:15 +00002398 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002399 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002400
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002401 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002402 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002403 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002404 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002405 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002406 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002407 else if (TypeStr == "protected") {
2408 // Complain about attempts to use protected visibility on targets
2409 // (like Darwin) that don't support it.
2410 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2411 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2412 type = VisibilityAttr::Default;
2413 } else {
2414 type = VisibilityAttr::Protected;
2415 }
2416 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002417 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002418 return;
2419 }
Mike Stumpbf916502009-07-24 19:02:52 +00002420
Michael Han51d8c522013-01-24 16:46:58 +00002421 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002422 clang::Attr *newAttr;
2423 if (isTypeVisibility) {
2424 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2425 (TypeVisibilityAttr::VisibilityType) type,
2426 Index);
2427 } else {
2428 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2429 }
2430 if (newAttr)
2431 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002432}
2433
Chandler Carruth1b03c872011-07-02 00:01:44 +00002434static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2435 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002436 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2437 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002439 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002440 return;
2441 }
2442
Chandler Carruth87c44602011-07-01 23:49:12 +00002443 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2444 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002445 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2446 << Attr.getName() << 1 << ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002447 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002449 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002450 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002451 return;
2452 }
2453
Chris Lattner5f9e2722011-07-23 10:55:15 +00002454 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002455 ObjCMethodFamilyAttr::FamilyKind family;
2456 if (param == "none")
2457 family = ObjCMethodFamilyAttr::OMF_None;
2458 else if (param == "alloc")
2459 family = ObjCMethodFamilyAttr::OMF_alloc;
2460 else if (param == "copy")
2461 family = ObjCMethodFamilyAttr::OMF_copy;
2462 else if (param == "init")
2463 family = ObjCMethodFamilyAttr::OMF_init;
2464 else if (param == "mutableCopy")
2465 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2466 else if (param == "new")
2467 family = ObjCMethodFamilyAttr::OMF_new;
2468 else {
2469 // Just warn and ignore it. This is future-proof against new
2470 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002471 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002472 return;
2473 }
2474
John McCallf85e1932011-06-15 23:02:42 +00002475 if (family == ObjCMethodFamilyAttr::OMF_init &&
2476 !method->getResultType()->isObjCObjectPointerType()) {
2477 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2478 << method->getResultType();
2479 // Ignore the attribute.
2480 return;
2481 }
2482
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002483 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002484 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002485}
2486
Chandler Carruth1b03c872011-07-02 00:01:44 +00002487static void handleObjCExceptionAttr(Sema &S, Decl *D,
2488 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002489 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002490 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002491
Chris Lattner0db29ec2009-02-14 08:09:34 +00002492 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2493 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2495 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002496 return;
2497 }
Mike Stumpbf916502009-07-24 19:02:52 +00002498
Michael Han51d8c522013-01-24 16:46:58 +00002499 D->addAttr(::new (S.Context)
2500 ObjCExceptionAttr(Attr.getRange(), S.Context,
2501 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002502}
2503
Chandler Carruth1b03c872011-07-02 00:01:44 +00002504static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002505 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002506 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002507 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002508 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002509 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002510 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2511 return;
2512 }
2513 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002514 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2515 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002516 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002517 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2518 return;
2519 }
2520 }
2521 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002522 // It is okay to include this attribute on properties, e.g.:
2523 //
2524 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2525 //
2526 // In this case it follows tradition and suppresses an error in the above
2527 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002528 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002529 }
Michael Han51d8c522013-01-24 16:46:58 +00002530 D->addAttr(::new (S.Context)
2531 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2532 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002533}
2534
Mike Stumpbf916502009-07-24 19:02:52 +00002535static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002536handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002537 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002538 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002539
2540 if (!isa<FunctionDecl>(D)) {
2541 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2542 return;
2543 }
2544
Michael Han51d8c522013-01-24 16:46:58 +00002545 D->addAttr(::new (S.Context)
2546 OverloadableAttr(Attr.getRange(), S.Context,
2547 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002548}
2549
Chandler Carruth1b03c872011-07-02 00:01:44 +00002550static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002551 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2553 << Attr.getName() << 1 << ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002554 return;
2555 }
Mike Stumpbf916502009-07-24 19:02:52 +00002556
Steve Naroff9eae5762008-09-18 16:44:58 +00002557 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002558 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002559 return;
2560 }
Mike Stumpbf916502009-07-24 19:02:52 +00002561
Sean Huntcf807c42010-08-18 23:23:40 +00002562 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002563 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002564 type = BlocksAttr::ByRef;
2565 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002566 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002567 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002568 return;
2569 }
Mike Stumpbf916502009-07-24 19:02:52 +00002570
Michael Han51d8c522013-01-24 16:46:58 +00002571 D->addAttr(::new (S.Context)
2572 BlocksAttr(Attr.getRange(), S.Context, type,
2573 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002574}
2575
Chandler Carruth1b03c872011-07-02 00:01:44 +00002576static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002577 // check the attribute arguments.
2578 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002579 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002580 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002581 }
2582
John McCall3323fad2011-09-09 07:56:05 +00002583 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002584 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002585 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002586 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002587 if (E->isTypeDependent() || E->isValueDependent() ||
2588 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002589 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmand7329282013-07-23 17:35:26 +00002590 << Attr.getName() << 1 << ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002591 << 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)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmand7329282013-07-23 17:35:26 +00002611 << Attr.getName() << 2 << ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002612 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002613 return;
2614 }
2615 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002616
John McCall3323fad2011-09-09 07:56:05 +00002617 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002618 // FIXME: This error message could be improved, it would be nice
2619 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002620 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2621 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002622 return;
2623 }
2624 }
2625
Chandler Carruth87c44602011-07-01 23:49:12 +00002626 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002627 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002628 if (isa<FunctionNoProtoType>(FT)) {
2629 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2630 return;
2631 }
Mike Stumpbf916502009-07-24 19:02:52 +00002632
Chris Lattner897cd902009-03-17 23:03:47 +00002633 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002634 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002635 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002636 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002637 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002638 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002639 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002640 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002641 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002642 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2643 if (!BD->isVariadic()) {
2644 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2645 return;
2646 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002647 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002648 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002649 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002650 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002651 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002652 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002653 int m = Ty->isFunctionPointerType() ? 0 : 1;
2654 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002655 return;
2656 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002657 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002659 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002660 return;
2661 }
Anders Carlsson77091822008-10-05 18:05:59 +00002662 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002664 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002665 return;
2666 }
Michael Han51d8c522013-01-24 16:46:58 +00002667 D->addAttr(::new (S.Context)
2668 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2669 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002670}
2671
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002672static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2673 // Check the attribute arguments.
2674 if (!checkAttributeNumArgs(S, Attr, 0))
2675 return;
2676
2677 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2678 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2679 else
2680 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2681}
2682
Chandler Carruth1b03c872011-07-02 00:01:44 +00002683static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002684 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002685 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002686 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002687
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002688 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002690 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002691 return;
2692 }
Mike Stumpbf916502009-07-24 19:02:52 +00002693
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002694 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2695 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2696 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002697 return;
2698 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002699 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2700 if (MD->getResultType()->isVoidType()) {
2701 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2702 << Attr.getName() << 1;
2703 return;
2704 }
2705
Michael Han51d8c522013-01-24 16:46:58 +00002706 D->addAttr(::new (S.Context)
2707 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2708 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002709}
2710
Chandler Carruth1b03c872011-07-02 00:01:44 +00002711static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002712 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002713 if (Attr.hasParameterOrArguments()) {
2714 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002715 return;
2716 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002717
Chandler Carruth87c44602011-07-01 23:49:12 +00002718 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002719 if (isa<CXXRecordDecl>(D)) {
2720 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2721 return;
2722 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002723 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2724 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002725 return;
2726 }
2727
Chandler Carruth87c44602011-07-01 23:49:12 +00002728 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002729
Michael Han51d8c522013-01-24 16:46:58 +00002730 nd->addAttr(::new (S.Context)
2731 WeakAttr(Attr.getRange(), S.Context,
2732 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002733}
2734
Chandler Carruth1b03c872011-07-02 00:01:44 +00002735static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002736 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002737 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002738 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002739
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002740
2741 // weak_import only applies to variable & function declarations.
2742 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002743 if (!D->canBeWeakImported(isDef)) {
2744 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002745 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2746 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002747 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002748 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002749 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002750 // Nothing to warn about here.
2751 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002752 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002753 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002754
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002755 return;
2756 }
2757
Michael Han51d8c522013-01-24 16:46:58 +00002758 D->addAttr(::new (S.Context)
2759 WeakImportAttr(Attr.getRange(), S.Context,
2760 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002761}
2762
Tanya Lattner0df579e2012-07-09 22:06:01 +00002763// Handles reqd_work_group_size and work_group_size_hint.
2764static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002765 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002766 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2767 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2768
Nate Begeman6f3d8382009-06-26 06:32:41 +00002769 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002770 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002771
2772 unsigned WGSize[3];
2773 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002774 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002775 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002776 if (E->isTypeDependent() || E->isValueDependent() ||
2777 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002778 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002779 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002780 return;
2781 }
2782 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2783 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002784
2785 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2786 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2787 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2788 if (!(A->getXDim() == WGSize[0] &&
2789 A->getYDim() == WGSize[1] &&
2790 A->getZDim() == WGSize[2])) {
2791 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2792 Attr.getName();
2793 }
2794 }
2795
2796 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2797 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2798 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2799 if (!(A->getXDim() == WGSize[0] &&
2800 A->getYDim() == WGSize[1] &&
2801 A->getZDim() == WGSize[2])) {
2802 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2803 Attr.getName();
2804 }
2805 }
2806
2807 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2808 D->addAttr(::new (S.Context)
2809 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002810 WGSize[0], WGSize[1], WGSize[2],
2811 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002812 else
2813 D->addAttr(::new (S.Context)
2814 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002815 WGSize[0], WGSize[1], WGSize[2],
2816 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002817}
2818
Joey Gouly37453b92013-03-08 09:42:32 +00002819static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2820 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2821
2822 // Attribute has 1 argument.
2823 if (!checkAttributeNumArgs(S, Attr, 1))
2824 return;
2825
2826 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2827
2828 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2829 (ParmType->isBooleanType() ||
2830 !ParmType->isIntegralType(S.getASTContext()))) {
2831 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2832 << ParmType;
2833 return;
2834 }
2835
2836 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2837 D->hasAttr<VecTypeHintAttr>()) {
2838 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2839 if (A->getTypeHint() != ParmType) {
2840 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2841 return;
2842 }
2843 }
2844
2845 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2846 ParmType, Attr.getLoc()));
2847}
2848
Joey Gouly96cead52013-03-14 09:54:43 +00002849static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2850 if (!dyn_cast<VarDecl>(D))
2851 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2852 << 9;
2853 StringRef EndianType = Attr.getParameterName()->getName();
2854 if (EndianType != "host" && EndianType != "device")
2855 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2856}
2857
Rafael Espindola599f1b72012-05-13 03:25:18 +00002858SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002859 StringRef Name,
2860 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002861 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2862 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002863 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002864 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2865 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002866 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002867 }
Michael Han51d8c522013-01-24 16:46:58 +00002868 return ::new (Context) SectionAttr(Range, Context, Name,
2869 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002870}
2871
Chandler Carruth1b03c872011-07-02 00:01:44 +00002872static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002873 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002874 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002875 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002876
2877 // Make sure that there is a string literal as the sections's single
2878 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002879 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002880 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002881 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002882 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002883 return;
2884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
Chris Lattner797c3c42009-08-10 19:03:04 +00002886 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002887 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002888 if (!Error.empty()) {
2889 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2890 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002891 return;
2892 }
Mike Stump1eb44332009-09-09 15:08:12 +00002893
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002894 // This attribute cannot be applied to local variables.
2895 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2896 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2897 return;
2898 }
Michael Han51d8c522013-01-24 16:46:58 +00002899
2900 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002901 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002902 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002903 if (NewAttr)
2904 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002905}
2906
Chris Lattner6b6b5372008-06-26 18:38:35 +00002907
Chandler Carruth1b03c872011-07-02 00:01:44 +00002908static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002909 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002910 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002911 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002912 return;
2913 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002914
Chandler Carruth87c44602011-07-01 23:49:12 +00002915 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002916 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002917 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002918 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002919 D->addAttr(::new (S.Context)
2920 NoThrowAttr(Attr.getRange(), S.Context,
2921 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002922 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002923}
2924
Chandler Carruth1b03c872011-07-02 00:01:44 +00002925static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002926 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002927 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002928 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002929 return;
2930 }
Mike Stumpbf916502009-07-24 19:02:52 +00002931
Chandler Carruth87c44602011-07-01 23:49:12 +00002932 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002933 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002934 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002935 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002936 D->addAttr(::new (S.Context)
2937 ConstAttr(Attr.getRange(), S.Context,
2938 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002939 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002940}
2941
Chandler Carruth1b03c872011-07-02 00:01:44 +00002942static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002943 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002944 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002945 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002946
Michael Han51d8c522013-01-24 16:46:58 +00002947 D->addAttr(::new (S.Context)
2948 PureAttr(Attr.getRange(), S.Context,
2949 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002950}
2951
Chandler Carruth1b03c872011-07-02 00:01:44 +00002952static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002953 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2955 return;
2956 }
Mike Stumpbf916502009-07-24 19:02:52 +00002957
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002958 if (Attr.getNumArgs() != 0) {
2959 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2960 return;
2961 }
Mike Stumpbf916502009-07-24 19:02:52 +00002962
Chandler Carruth87c44602011-07-01 23:49:12 +00002963 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002964
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002965 if (!VD || !VD->hasLocalStorage()) {
2966 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2967 return;
2968 }
Mike Stumpbf916502009-07-24 19:02:52 +00002969
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002970 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002971 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002972 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002973 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2974 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002975 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002976 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002977 Attr.getParameterName();
2978 return;
2979 }
Mike Stumpbf916502009-07-24 19:02:52 +00002980
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002981 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2982 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002983 S.Diag(Attr.getParameterLoc(),
2984 diag::err_attribute_cleanup_arg_not_function)
2985 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002986 return;
2987 }
2988
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002989 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002990 S.Diag(Attr.getParameterLoc(),
2991 diag::err_attribute_cleanup_func_must_take_one_arg)
2992 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002993 return;
2994 }
Mike Stumpbf916502009-07-24 19:02:52 +00002995
Anders Carlsson89941c12009-02-07 23:16:50 +00002996 // We're currently more strict than GCC about what function types we accept.
2997 // If this ever proves to be a problem it should be easy to fix.
2998 QualType Ty = S.Context.getPointerType(VD->getType());
2999 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00003000 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3001 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00003002 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00003003 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3004 Attr.getParameterName() << ParamTy << Ty;
3005 return;
3006 }
Mike Stumpbf916502009-07-24 19:02:52 +00003007
Michael Han51d8c522013-01-24 16:46:58 +00003008 D->addAttr(::new (S.Context)
3009 CleanupAttr(Attr.getRange(), S.Context, FD,
3010 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00003011 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00003012 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003013}
3014
Mike Stumpbf916502009-07-24 19:02:52 +00003015/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003016/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003017static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003018 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003019 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003020
Chandler Carruth87c44602011-07-01 23:49:12 +00003021 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003023 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003024 return;
3025 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003026
3027 // In C++ the implicit 'this' function parameter also counts, and they are
3028 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003029 bool HasImplicitThisParam = isInstanceMethod(D);
3030 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003031 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003032
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003033 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003034 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003035 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003036 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3037 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003038 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3039 << Attr.getName() << 2 << ArgumentIntegerConstant
3040 << IdxExpr->getSourceRange();
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003041 return;
3042 }
Mike Stumpbf916502009-07-24 19:02:52 +00003043
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003044 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
3045 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3046 << "format" << 2 << IdxExpr->getSourceRange();
3047 return;
3048 }
Mike Stumpbf916502009-07-24 19:02:52 +00003049
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003050 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003051
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003052 if (HasImplicitThisParam) {
3053 if (ArgIdx == 0) {
3054 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
3055 << "format_arg" << IdxExpr->getSourceRange();
3056 return;
3057 }
3058 ArgIdx--;
3059 }
3060
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003061 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003062 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003063
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003064 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3065 if (not_nsstring_type &&
3066 !isCFStringType(Ty, S.Context) &&
3067 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003068 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003069 // FIXME: Should highlight the actual expression that has the wrong type.
3070 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003071 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003072 << IdxExpr->getSourceRange();
3073 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003074 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003075 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003076 if (!isNSStringType(Ty, S.Context) &&
3077 !isCFStringType(Ty, S.Context) &&
3078 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003079 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003080 // FIXME: Should highlight the actual expression that has the wrong type.
3081 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003082 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003083 << IdxExpr->getSourceRange();
3084 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003085 }
3086
Michael Han51d8c522013-01-24 16:46:58 +00003087 D->addAttr(::new (S.Context)
3088 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
3089 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003090}
3091
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003092enum FormatAttrKind {
3093 CFStringFormat,
3094 NSStringFormat,
3095 StrftimeFormat,
3096 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003097 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003098 InvalidFormat
3099};
3100
3101/// getFormatAttrKind - Map from format attribute names to supported format
3102/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003103static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003104 return llvm::StringSwitch<FormatAttrKind>(Format)
3105 // Check for formats that get handled specially.
3106 .Case("NSString", NSStringFormat)
3107 .Case("CFString", CFStringFormat)
3108 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003109
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003110 // Otherwise, check for supported formats.
3111 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3112 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3113 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003114
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003115 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3116 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003117}
3118
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003119/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003120/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003121static void handleInitPriorityAttr(Sema &S, Decl *D,
3122 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003123 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003124 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3125 return;
3126 }
3127
Chandler Carruth87c44602011-07-01 23:49:12 +00003128 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003129 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3130 Attr.setInvalid();
3131 return;
3132 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003133 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003134 if (S.Context.getAsArrayType(T))
3135 T = S.Context.getBaseElementType(T);
3136 if (!T->getAs<RecordType>()) {
3137 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3138 Attr.setInvalid();
3139 return;
3140 }
3141
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003142 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003143 Attr.setInvalid();
3144 return;
3145 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003146 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003147
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003148 llvm::APSInt priority(32);
3149 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3150 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3151 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3152 << "init_priority" << priorityExpr->getSourceRange();
3153 Attr.setInvalid();
3154 return;
3155 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003156 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003157 if (prioritynum < 101 || prioritynum > 65535) {
3158 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3159 << priorityExpr->getSourceRange();
3160 Attr.setInvalid();
3161 return;
3162 }
Michael Han51d8c522013-01-24 16:46:58 +00003163 D->addAttr(::new (S.Context)
3164 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3165 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003166}
3167
Rafael Espindola599f1b72012-05-13 03:25:18 +00003168FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003169 int FormatIdx, int FirstArg,
3170 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003171 // Check whether we already have an equivalent format attribute.
3172 for (specific_attr_iterator<FormatAttr>
3173 i = D->specific_attr_begin<FormatAttr>(),
3174 e = D->specific_attr_end<FormatAttr>();
3175 i != e ; ++i) {
3176 FormatAttr *f = *i;
3177 if (f->getType() == Format &&
3178 f->getFormatIdx() == FormatIdx &&
3179 f->getFirstArg() == FirstArg) {
3180 // If we don't have a valid location for this attribute, adopt the
3181 // location.
3182 if (f->getLocation().isInvalid())
3183 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003184 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003185 }
3186 }
3187
Michael Han51d8c522013-01-24 16:46:58 +00003188 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3189 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003190}
3191
Mike Stumpbf916502009-07-24 19:02:52 +00003192/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003193/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003194static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003195
Chris Lattner545dd342008-06-28 23:36:30 +00003196 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3198 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003199 return;
3200 }
3201
Chris Lattner545dd342008-06-28 23:36:30 +00003202 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003204 return;
3205 }
3206
Chandler Carruth87c44602011-07-01 23:49:12 +00003207 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003208 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003209 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003210 return;
3211 }
3212
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003213 // In C++ the implicit 'this' function parameter also counts, and they are
3214 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003215 bool HasImplicitThisParam = isInstanceMethod(D);
3216 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003217 unsigned FirstIdx = 1;
3218
Chris Lattner5f9e2722011-07-23 10:55:15 +00003219 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003220
3221 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003222 if (Format.startswith("__") && Format.endswith("__"))
3223 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003224
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003225 // Check for supported formats.
3226 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003227
3228 if (Kind == IgnoredFormat)
3229 return;
3230
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003231 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003232 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003233 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003234 return;
3235 }
3236
3237 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003238 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003239 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003240 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3241 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3243 << Attr.getName() << 2 << ArgumentIntegerConstant
3244 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003245 return;
3246 }
3247
3248 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003250 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003251 return;
3252 }
3253
3254 // FIXME: Do we need to bounds check?
3255 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003256
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003257 if (HasImplicitThisParam) {
3258 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003259 S.Diag(Attr.getLoc(),
3260 diag::err_format_attribute_implicit_this_format_string)
3261 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003262 return;
3263 }
3264 ArgIdx--;
3265 }
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Chris Lattner6b6b5372008-06-26 18:38:35 +00003267 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003268 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003269
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003270 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003271 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003272 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3273 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003274 return;
3275 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003276 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003277 // FIXME: do we need to check if the type is NSString*? What are the
3278 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003279 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003280 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003281 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3282 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003283 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003284 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003285 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003286 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003287 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003288 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3289 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003290 return;
3291 }
3292
3293 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003294 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003295 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003296 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3297 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003298 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3299 << Attr.getName() << 3 << ArgumentIntegerConstant
3300 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003301 return;
3302 }
3303
3304 // check if the function is variadic if the 3rd argument non-zero
3305 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003306 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307 ++NumArgs; // +1 for ...
3308 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003309 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003310 return;
3311 }
3312 }
3313
Chris Lattner3c73c412008-11-19 08:23:25 +00003314 // strftime requires FirstArg to be 0 because it doesn't read from any
3315 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003316 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003317 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003318 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3319 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003320 return;
3321 }
3322 // if 0 it disables parameter checking (to use with e.g. va_list)
3323 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003324 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003325 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003326 return;
3327 }
3328
Rafael Espindola599f1b72012-05-13 03:25:18 +00003329 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3330 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003331 FirstArg.getZExtValue(),
3332 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003333 if (NewAttr)
3334 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003335}
3336
Chandler Carruth1b03c872011-07-02 00:01:44 +00003337static void handleTransparentUnionAttr(Sema &S, Decl *D,
3338 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003339 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003340 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003341 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003342
Chris Lattner6b6b5372008-06-26 18:38:35 +00003343
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003344 // Try to find the underlying union declaration.
3345 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003346 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003347 if (TD && TD->getUnderlyingType()->isUnionType())
3348 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3349 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003350 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003351
3352 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003353 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003354 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003355 return;
3356 }
3357
John McCall5e1cdac2011-10-07 06:10:15 +00003358 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003359 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003360 diag::warn_transparent_union_attribute_not_definition);
3361 return;
3362 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003363
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003364 RecordDecl::field_iterator Field = RD->field_begin(),
3365 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003366 if (Field == FieldEnd) {
3367 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3368 return;
3369 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003370
David Blaikie581deb32012-06-06 20:45:41 +00003371 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003372 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003373 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003374 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003375 diag::warn_transparent_union_attribute_floating)
3376 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003377 return;
3378 }
3379
3380 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3381 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3382 for (; Field != FieldEnd; ++Field) {
3383 QualType FieldType = Field->getType();
3384 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3385 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3386 // Warn if we drop the attribute.
3387 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003388 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003389 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003390 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003391 diag::warn_transparent_union_attribute_field_size_align)
3392 << isSize << Field->getDeclName() << FieldBits;
3393 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003394 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003395 diag::note_transparent_union_first_field_size_align)
3396 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003397 return;
3398 }
3399 }
3400
Michael Han51d8c522013-01-24 16:46:58 +00003401 RD->addAttr(::new (S.Context)
3402 TransparentUnionAttr(Attr.getRange(), S.Context,
3403 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003404}
3405
Chandler Carruth1b03c872011-07-02 00:01:44 +00003406static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003407 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003408 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003409 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003410
Peter Collingbourne7a730022010-11-23 20:45:58 +00003411 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003412 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003413
Chris Lattner6b6b5372008-06-26 18:38:35 +00003414 // Make sure that there is a string literal as the annotation's single
3415 // argument.
3416 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003417 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003418 return;
3419 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003420
3421 // Don't duplicate annotations that are already set.
3422 for (specific_attr_iterator<AnnotateAttr>
3423 i = D->specific_attr_begin<AnnotateAttr>(),
3424 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3425 if ((*i)->getAnnotation() == SE->getString())
3426 return;
3427 }
Michael Han51d8c522013-01-24 16:46:58 +00003428
3429 D->addAttr(::new (S.Context)
3430 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3431 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003432}
3433
Chandler Carruth1b03c872011-07-02 00:01:44 +00003434static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003435 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003436 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003438 return;
3439 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003440
Richard Smithbe507b62013-02-01 08:12:08 +00003441 if (Attr.getNumArgs() == 0) {
3442 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3443 true, 0, Attr.getAttributeSpellingListIndex()));
3444 return;
3445 }
3446
Richard Smithf6565a92013-02-22 08:32:16 +00003447 Expr *E = Attr.getArg(0);
3448 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3449 S.Diag(Attr.getEllipsisLoc(),
3450 diag::err_pack_expansion_without_parameter_packs);
3451 return;
3452 }
3453
3454 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3455 return;
3456
3457 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3458 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003459}
3460
3461void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003462 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003463 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3464 SourceLocation AttrLoc = AttrRange.getBegin();
3465
Richard Smith4cd81c52013-01-29 09:02:09 +00003466 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003467 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003468 // C++11 [dcl.align]p1:
3469 // An alignment-specifier may be applied to a variable or to a class
3470 // data member, but it shall not be applied to a bit-field, a function
3471 // parameter, the formal parameter of a catch clause, or a variable
3472 // declared with the register storage class specifier. An
3473 // alignment-specifier may also be applied to the declaration of a class
3474 // or enumeration type.
3475 // C11 6.7.5/2:
3476 // An alignment attribute shall not be specified in a declaration of
3477 // a typedef, or a bit-field, or a function, or a parameter, or an
3478 // object declared with the register storage-class specifier.
3479 int DiagKind = -1;
3480 if (isa<ParmVarDecl>(D)) {
3481 DiagKind = 0;
3482 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3483 if (VD->getStorageClass() == SC_Register)
3484 DiagKind = 1;
3485 if (VD->isExceptionVariable())
3486 DiagKind = 2;
3487 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3488 if (FD->isBitField())
3489 DiagKind = 3;
3490 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003491 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3492 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003493 << (TmpAttr.isC11() ? ExpectedVariableOrField
3494 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003495 return;
3496 }
3497 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003498 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003499 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003500 return;
3501 }
3502 }
3503
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003504 if (E->isTypeDependent() || E->isValueDependent()) {
3505 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003506 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3507 AA->setPackExpansion(IsPackExpansion);
3508 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003509 return;
3510 }
Michael Hana31f65b2013-02-01 01:19:17 +00003511
Sean Huntcf807c42010-08-18 23:23:40 +00003512 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003513 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003514 ExprResult ICE
3515 = VerifyIntegerConstantExpression(E, &Alignment,
3516 diag::err_aligned_attribute_argument_not_int,
3517 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003518 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003519 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003520
3521 // C++11 [dcl.align]p2:
3522 // -- if the constant expression evaluates to zero, the alignment
3523 // specifier shall have no effect
3524 // C11 6.7.5p6:
3525 // An alignment specification of zero has no effect.
3526 if (!(TmpAttr.isAlignas() && !Alignment) &&
3527 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003528 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3529 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003530 return;
3531 }
Michael Hana31f65b2013-02-01 01:19:17 +00003532
Richard Smithbe507b62013-02-01 08:12:08 +00003533 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003534 // We've already verified it's a power of 2, now let's make sure it's
3535 // 8192 or less.
3536 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003537 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003538 << E->getSourceRange();
3539 return;
3540 }
3541 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003542
Richard Smithf6565a92013-02-22 08:32:16 +00003543 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3544 ICE.take(), SpellingListIndex);
3545 AA->setPackExpansion(IsPackExpansion);
3546 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003547}
3548
Michael Hana31f65b2013-02-01 01:19:17 +00003549void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003550 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003551 // FIXME: Cache the number on the Attr object if non-dependent?
3552 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003553 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3554 SpellingListIndex);
3555 AA->setPackExpansion(IsPackExpansion);
3556 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003557}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003558
Richard Smithbe507b62013-02-01 08:12:08 +00003559void Sema::CheckAlignasUnderalignment(Decl *D) {
3560 assert(D->hasAttrs() && "no attributes on decl");
3561
3562 QualType Ty;
3563 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3564 Ty = VD->getType();
3565 else
3566 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003567 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003568 return;
3569
3570 // C++11 [dcl.align]p5, C11 6.7.5/4:
3571 // The combined effect of all alignment attributes in a declaration shall
3572 // not specify an alignment that is less strict than the alignment that
3573 // would otherwise be required for the entity being declared.
3574 AlignedAttr *AlignasAttr = 0;
3575 unsigned Align = 0;
3576 for (specific_attr_iterator<AlignedAttr>
3577 I = D->specific_attr_begin<AlignedAttr>(),
3578 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3579 if (I->isAlignmentDependent())
3580 return;
3581 if (I->isAlignas())
3582 AlignasAttr = *I;
3583 Align = std::max(Align, I->getAlignment(Context));
3584 }
3585
3586 if (AlignasAttr && Align) {
3587 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3588 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3589 if (NaturalAlign > RequestedAlign)
3590 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3591 << Ty << (unsigned)NaturalAlign.getQuantity();
3592 }
3593}
3594
Chandler Carruthd309c812011-07-01 23:49:16 +00003595/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003596/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003597///
Mike Stumpbf916502009-07-24 19:02:52 +00003598/// Despite what would be logical, the mode attribute is a decl attribute, not a
3599/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3600/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003601static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003602 // This attribute isn't documented, but glibc uses it. It changes
3603 // the width of an int or unsigned int to the specified size.
3604
3605 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003606 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003607 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003608
Chris Lattnerfbf13472008-06-27 22:18:37 +00003609
3610 IdentifierInfo *Name = Attr.getParameterName();
3611 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003612 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003613 return;
3614 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003615
Chris Lattner5f9e2722011-07-23 10:55:15 +00003616 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003617
3618 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003619 if (Str.startswith("__") && Str.endswith("__"))
3620 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003621
3622 unsigned DestWidth = 0;
3623 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003624 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003625 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003626 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003627 switch (Str[0]) {
3628 case 'Q': DestWidth = 8; break;
3629 case 'H': DestWidth = 16; break;
3630 case 'S': DestWidth = 32; break;
3631 case 'D': DestWidth = 64; break;
3632 case 'X': DestWidth = 96; break;
3633 case 'T': DestWidth = 128; break;
3634 }
3635 if (Str[1] == 'F') {
3636 IntegerMode = false;
3637 } else if (Str[1] == 'C') {
3638 IntegerMode = false;
3639 ComplexMode = true;
3640 } else if (Str[1] != 'I') {
3641 DestWidth = 0;
3642 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003643 break;
3644 case 4:
3645 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3646 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003647 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003648 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003649 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003650 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003651 break;
3652 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003653 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003654 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003655 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003656 case 11:
3657 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003658 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003659 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003660 }
3661
3662 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003663 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003664 OldTy = TD->getUnderlyingType();
3665 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3666 OldTy = VD->getType();
3667 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003668 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003669 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003670 return;
3671 }
Eli Friedman73397492009-03-03 06:41:03 +00003672
John McCall183700f2009-09-21 23:43:11 +00003673 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003674 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3675 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003676 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003677 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3678 } else if (ComplexMode) {
3679 if (!OldTy->isComplexType())
3680 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3681 } else {
3682 if (!OldTy->isFloatingType())
3683 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3684 }
3685
Mike Stump390b4cc2009-05-16 07:39:55 +00003686 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3687 // and friends, at least with glibc.
3688 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3689 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003690 // FIXME: Make sure floating-point mappings are accurate
3691 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003692 QualType NewTy;
3693 switch (DestWidth) {
3694 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003695 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003696 return;
3697 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003698 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003699 return;
3700 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003701 if (!IntegerMode) {
3702 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3703 return;
3704 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003705 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003706 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003707 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003708 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003709 break;
3710 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003711 if (!IntegerMode) {
3712 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3713 return;
3714 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003715 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003716 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003717 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003718 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003719 break;
3720 case 32:
3721 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003722 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003723 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003724 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003725 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003726 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003727 break;
3728 case 64:
3729 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003730 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003731 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003732 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003733 NewTy = S.Context.LongTy;
3734 else
3735 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003736 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003737 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003738 NewTy = S.Context.UnsignedLongTy;
3739 else
3740 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003741 break;
Eli Friedman73397492009-03-03 06:41:03 +00003742 case 96:
3743 NewTy = S.Context.LongDoubleTy;
3744 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003745 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003746 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3747 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003748 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3749 return;
3750 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003751 if (IntegerMode) {
3752 if (OldTy->isSignedIntegerType())
3753 NewTy = S.Context.Int128Ty;
3754 else
3755 NewTy = S.Context.UnsignedInt128Ty;
3756 } else
3757 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003758 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003759 }
3760
Eli Friedman73397492009-03-03 06:41:03 +00003761 if (ComplexMode) {
3762 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003763 }
3764
3765 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003766 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3767 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3768 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003769 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003770
3771 D->addAttr(::new (S.Context)
3772 ModeAttr(Attr.getRange(), S.Context, Name,
3773 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003774}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003775
Chandler Carruth1b03c872011-07-02 00:01:44 +00003776static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003777 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003778 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003779 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003780
Nick Lewycky78d1a102012-07-24 01:40:49 +00003781 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3782 if (!VD->hasGlobalStorage())
3783 S.Diag(Attr.getLoc(),
3784 diag::warn_attribute_requires_functions_or_static_globals)
3785 << Attr.getName();
3786 } else if (!isFunctionOrMethod(D)) {
3787 S.Diag(Attr.getLoc(),
3788 diag::warn_attribute_requires_functions_or_static_globals)
3789 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003790 return;
3791 }
Mike Stumpbf916502009-07-24 19:02:52 +00003792
Michael Han51d8c522013-01-24 16:46:58 +00003793 D->addAttr(::new (S.Context)
3794 NoDebugAttr(Attr.getRange(), S.Context,
3795 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003796}
3797
Chandler Carruth1b03c872011-07-02 00:01:44 +00003798static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003799 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003800 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003801 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003802
Mike Stumpbf916502009-07-24 19:02:52 +00003803
Chandler Carruth87c44602011-07-01 23:49:12 +00003804 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003805 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003806 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003807 return;
3808 }
Mike Stumpbf916502009-07-24 19:02:52 +00003809
Michael Han51d8c522013-01-24 16:46:58 +00003810 D->addAttr(::new (S.Context)
3811 NoInlineAttr(Attr.getRange(), S.Context,
3812 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003813}
3814
Chandler Carruth1b03c872011-07-02 00:01:44 +00003815static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3816 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003817 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003818 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003819 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003820
Chris Lattner7255a2d2010-06-22 00:03:40 +00003821
Chandler Carruth87c44602011-07-01 23:49:12 +00003822 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003824 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003825 return;
3826 }
3827
Michael Han51d8c522013-01-24 16:46:58 +00003828 D->addAttr(::new (S.Context)
3829 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3830 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003831}
3832
Chandler Carruth1b03c872011-07-02 00:01:44 +00003833static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003834 if (S.LangOpts.CUDA) {
3835 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003836 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003837 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3838 return;
3839 }
3840
Chandler Carruth87c44602011-07-01 23:49:12 +00003841 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003843 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003844 return;
3845 }
3846
Michael Han51d8c522013-01-24 16:46:58 +00003847 D->addAttr(::new (S.Context)
3848 CUDAConstantAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003850 } else {
3851 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3852 }
3853}
3854
Chandler Carruth1b03c872011-07-02 00:01:44 +00003855static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003856 if (S.LangOpts.CUDA) {
3857 // check the attribute arguments.
3858 if (Attr.getNumArgs() != 0) {
3859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3860 return;
3861 }
3862
Chandler Carruth87c44602011-07-01 23:49:12 +00003863 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003864 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003865 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003866 return;
3867 }
3868
Michael Han51d8c522013-01-24 16:46:58 +00003869 D->addAttr(::new (S.Context)
3870 CUDADeviceAttr(Attr.getRange(), S.Context,
3871 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003872 } else {
3873 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3874 }
3875}
3876
Chandler Carruth1b03c872011-07-02 00:01:44 +00003877static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 if (S.LangOpts.CUDA) {
3879 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003880 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003881 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003882
Chandler Carruth87c44602011-07-01 23:49:12 +00003883 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003885 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003886 return;
3887 }
3888
Chandler Carruth87c44602011-07-01 23:49:12 +00003889 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003890 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003891 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003892 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003893 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3894 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003895 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003896 "void");
3897 } else {
3898 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3899 << FD->getType();
3900 }
3901 return;
3902 }
3903
Michael Han51d8c522013-01-24 16:46:58 +00003904 D->addAttr(::new (S.Context)
3905 CUDAGlobalAttr(Attr.getRange(), S.Context,
3906 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003907 } else {
3908 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3909 }
3910}
3911
Chandler Carruth1b03c872011-07-02 00:01:44 +00003912static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003913 if (S.LangOpts.CUDA) {
3914 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003915 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003916 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003917
Peter Collingbourneced76712010-12-01 03:15:31 +00003918
Chandler Carruth87c44602011-07-01 23:49:12 +00003919 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003921 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003922 return;
3923 }
3924
Michael Han51d8c522013-01-24 16:46:58 +00003925 D->addAttr(::new (S.Context)
3926 CUDAHostAttr(Attr.getRange(), S.Context,
3927 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003928 } else {
3929 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3930 }
3931}
3932
Chandler Carruth1b03c872011-07-02 00:01:44 +00003933static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003934 if (S.LangOpts.CUDA) {
3935 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003936 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003937 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003938
Chandler Carruth87c44602011-07-01 23:49:12 +00003939 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003941 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003942 return;
3943 }
3944
Michael Han51d8c522013-01-24 16:46:58 +00003945 D->addAttr(::new (S.Context)
3946 CUDASharedAttr(Attr.getRange(), S.Context,
3947 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003948 } else {
3949 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3950 }
3951}
3952
Chandler Carruth1b03c872011-07-02 00:01:44 +00003953static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003954 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003955 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003956 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003957
Chandler Carruth87c44602011-07-01 23:49:12 +00003958 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003959 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003960 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003961 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003962 return;
3963 }
Mike Stumpbf916502009-07-24 19:02:52 +00003964
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003965 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003966 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003967 return;
3968 }
Mike Stumpbf916502009-07-24 19:02:52 +00003969
Michael Han51d8c522013-01-24 16:46:58 +00003970 D->addAttr(::new (S.Context)
3971 GNUInlineAttr(Attr.getRange(), S.Context,
3972 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003973}
3974
Chandler Carruth1b03c872011-07-02 00:01:44 +00003975static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003976 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003977
Aaron Ballmanfff32482012-12-09 17:45:41 +00003978 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003979 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003980 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3981 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003982 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003983 return;
3984
Chandler Carruth87c44602011-07-01 23:49:12 +00003985 if (!isa<ObjCMethodDecl>(D)) {
3986 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3987 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003988 return;
3989 }
3990
Chandler Carruth87c44602011-07-01 23:49:12 +00003991 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003992 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003993 D->addAttr(::new (S.Context)
3994 FastCallAttr(Attr.getRange(), S.Context,
3995 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003996 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003997 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003998 D->addAttr(::new (S.Context)
3999 StdCallAttr(Attr.getRange(), S.Context,
4000 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004001 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004002 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00004003 D->addAttr(::new (S.Context)
4004 ThisCallAttr(Attr.getRange(), S.Context,
4005 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00004006 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004007 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00004008 D->addAttr(::new (S.Context)
4009 CDeclAttr(Attr.getRange(), S.Context,
4010 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004011 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004012 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004013 D->addAttr(::new (S.Context)
4014 PascalAttr(Attr.getRange(), S.Context,
4015 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004016 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004017 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004018 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004019 switch (CC) {
4020 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004021 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004022 break;
4023 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004024 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004025 break;
4026 default:
4027 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004028 }
4029
Michael Han51d8c522013-01-24 16:46:58 +00004030 D->addAttr(::new (S.Context)
4031 PcsAttr(Attr.getRange(), S.Context, PCS,
4032 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004033 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004034 }
Derek Schuff263366f2012-10-16 22:30:41 +00004035 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004036 D->addAttr(::new (S.Context)
4037 PnaclCallAttr(Attr.getRange(), S.Context,
4038 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004039 return;
Guy Benyei38980082012-12-25 08:53:55 +00004040 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004041 D->addAttr(::new (S.Context)
4042 IntelOclBiccAttr(Attr.getRange(), S.Context,
4043 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004044 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004045
Abramo Bagnarae215f722010-04-30 13:10:51 +00004046 default:
4047 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004048 }
4049}
4050
Chandler Carruth1b03c872011-07-02 00:01:44 +00004051static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00004052 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004053 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004054}
4055
Guy Benyei1db70402013-03-24 13:58:12 +00004056static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
4057 assert(!Attr.isInvalid());
4058
4059 Expr *E = Attr.getArg(0);
4060 llvm::APSInt ArgNum(32);
4061 if (E->isTypeDependent() || E->isValueDependent() ||
4062 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
4063 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4064 << Attr.getName()->getName() << E->getSourceRange();
4065 return;
4066 }
4067
4068 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4069 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4070}
4071
Aaron Ballmanfff32482012-12-09 17:45:41 +00004072bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4073 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004074 if (attr.isInvalid())
4075 return true;
4076
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004077 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4078 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
4079 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004080 attr.setInvalid();
4081 return true;
4082 }
4083
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004084 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4085 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004086 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004087 case AttributeList::AT_CDecl: CC = CC_C; break;
4088 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4089 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4090 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4091 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4092 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004093 Expr *Arg = attr.getArg(0);
4094 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004095 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004096 Diag(attr.getLoc(), diag::err_attribute_argument_n_type)
4097 << attr.getName() << 1 << ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004098 attr.setInvalid();
4099 return true;
4100 }
4101
Chris Lattner5f9e2722011-07-23 10:55:15 +00004102 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004103 if (StrRef == "aapcs") {
4104 CC = CC_AAPCS;
4105 break;
4106 } else if (StrRef == "aapcs-vfp") {
4107 CC = CC_AAPCS_VFP;
4108 break;
4109 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004110
4111 attr.setInvalid();
4112 Diag(attr.getLoc(), diag::err_invalid_pcs);
4113 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004114 }
Derek Schuff263366f2012-10-16 22:30:41 +00004115 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004116 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004117 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004118 }
4119
Aaron Ballman82bfa192012-10-02 14:26:08 +00004120 const TargetInfo &TI = Context.getTargetInfo();
4121 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4122 if (A == TargetInfo::CCCR_Warning) {
4123 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004124
4125 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4126 if (FD)
4127 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4128 TargetInfo::CCMT_NonMember;
4129 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004130 }
4131
John McCall711c52b2011-01-05 12:14:39 +00004132 return false;
4133}
4134
Chandler Carruth1b03c872011-07-02 00:01:44 +00004135static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004136 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004137
4138 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004139 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004140 return;
4141
Chandler Carruth87c44602011-07-01 23:49:12 +00004142 if (!isa<ObjCMethodDecl>(D)) {
4143 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4144 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004145 return;
4146 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004147
Michael Han51d8c522013-01-24 16:46:58 +00004148 D->addAttr(::new (S.Context)
4149 RegparmAttr(Attr.getRange(), S.Context, numParams,
4150 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004151}
4152
4153/// Checks a regparm attribute, returning true if it is ill-formed and
4154/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004155bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4156 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004157 return true;
4158
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004159 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004160 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004161 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004162 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004163
Chandler Carruth87c44602011-07-01 23:49:12 +00004164 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004165 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004166 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004167 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004168 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004169 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004170 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004171 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004172 }
4173
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004174 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004175 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004176 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004177 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004178 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004179 }
4180
John McCall711c52b2011-01-05 12:14:39 +00004181 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004182 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004183 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004184 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004185 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004186 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004187 }
4188
John McCall711c52b2011-01-05 12:14:39 +00004189 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004190}
4191
Chandler Carruth1b03c872011-07-02 00:01:44 +00004192static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004193 if (S.LangOpts.CUDA) {
4194 // check the attribute arguments.
4195 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004196 // FIXME: 0 is not okay.
4197 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004198 return;
4199 }
4200
Chandler Carruth87c44602011-07-01 23:49:12 +00004201 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004202 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004203 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004204 return;
4205 }
4206
4207 Expr *MaxThreadsExpr = Attr.getArg(0);
4208 llvm::APSInt MaxThreads(32);
4209 if (MaxThreadsExpr->isTypeDependent() ||
4210 MaxThreadsExpr->isValueDependent() ||
4211 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004212 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4213 << Attr.getName() << 1 << ArgumentIntegerConstant
4214 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004215 return;
4216 }
4217
4218 llvm::APSInt MinBlocks(32);
4219 if (Attr.getNumArgs() > 1) {
4220 Expr *MinBlocksExpr = Attr.getArg(1);
4221 if (MinBlocksExpr->isTypeDependent() ||
4222 MinBlocksExpr->isValueDependent() ||
4223 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4225 << Attr.getName() << 2 << ArgumentIntegerConstant
4226 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004227 return;
4228 }
4229 }
4230
Michael Han51d8c522013-01-24 16:46:58 +00004231 D->addAttr(::new (S.Context)
4232 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4233 MaxThreads.getZExtValue(),
4234 MinBlocks.getZExtValue(),
4235 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004236 } else {
4237 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4238 }
4239}
4240
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004241static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4242 const AttributeList &Attr) {
4243 StringRef AttrName = Attr.getName()->getName();
4244 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4246 << Attr.getName() << /* arg num = */ 1 << ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004247 return;
4248 }
4249
4250 if (Attr.getNumArgs() != 2) {
4251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4252 << /* required args = */ 3;
4253 return;
4254 }
4255
4256 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4257
4258 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4259 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4260 << Attr.getName() << ExpectedFunctionOrMethod;
4261 return;
4262 }
4263
4264 uint64_t ArgumentIdx;
4265 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4266 Attr.getLoc(), 2,
4267 Attr.getArg(0), ArgumentIdx))
4268 return;
4269
4270 uint64_t TypeTagIdx;
4271 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4272 Attr.getLoc(), 3,
4273 Attr.getArg(1), TypeTagIdx))
4274 return;
4275
4276 bool IsPointer = (AttrName == "pointer_with_type_tag");
4277 if (IsPointer) {
4278 // Ensure that buffer has a pointer type.
4279 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4280 if (!BufferTy->isPointerType()) {
4281 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004282 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004283 }
4284 }
4285
Michael Han51d8c522013-01-24 16:46:58 +00004286 D->addAttr(::new (S.Context)
4287 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4288 ArgumentIdx, TypeTagIdx, IsPointer,
4289 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004290}
4291
4292static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4293 const AttributeList &Attr) {
4294 IdentifierInfo *PointerKind = Attr.getParameterName();
4295 if (!PointerKind) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004296 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4297 << Attr.getName() << 1 << ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004298 return;
4299 }
4300
4301 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4302
Michael Han51d8c522013-01-24 16:46:58 +00004303 D->addAttr(::new (S.Context)
4304 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4305 MatchingCType,
4306 Attr.getLayoutCompatible(),
4307 Attr.getMustBeNull(),
4308 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004309}
4310
Chris Lattner0744e5f2008-06-29 00:23:49 +00004311//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004312// Checker-specific attribute handlers.
4313//===----------------------------------------------------------------------===//
4314
John McCallc7ad3812011-01-25 03:31:58 +00004315static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004316 return type->isDependentType() ||
4317 type->isObjCObjectPointerType() ||
4318 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004319}
4320static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004321 return type->isDependentType() ||
4322 type->isPointerType() ||
4323 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004324}
4325
Chandler Carruth1b03c872011-07-02 00:01:44 +00004326static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004327 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004328 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004329 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004330 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004331 return;
4332 }
4333
4334 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004335 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004336 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4337 cf = false;
4338 } else {
4339 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4340 cf = true;
4341 }
4342
4343 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004344 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004345 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004346 return;
4347 }
4348
4349 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004350 param->addAttr(::new (S.Context)
4351 CFConsumedAttr(Attr.getRange(), S.Context,
4352 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004353 else
Michael Han51d8c522013-01-24 16:46:58 +00004354 param->addAttr(::new (S.Context)
4355 NSConsumedAttr(Attr.getRange(), S.Context,
4356 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004357}
4358
Chandler Carruth1b03c872011-07-02 00:01:44 +00004359static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4360 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004361 if (!isa<ObjCMethodDecl>(D)) {
4362 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004363 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004364 return;
4365 }
4366
Michael Han51d8c522013-01-24 16:46:58 +00004367 D->addAttr(::new (S.Context)
4368 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4369 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004370}
4371
Chandler Carruth1b03c872011-07-02 00:01:44 +00004372static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4373 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004374
John McCallc7ad3812011-01-25 03:31:58 +00004375 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004376
Chandler Carruth87c44602011-07-01 23:49:12 +00004377 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004378 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004379 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004380 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004381 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004382 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4383 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004384 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004385 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004386 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004387 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004388 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004389 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004390 return;
4391 }
Mike Stumpbf916502009-07-24 19:02:52 +00004392
John McCallc7ad3812011-01-25 03:31:58 +00004393 bool typeOK;
4394 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004395 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004396 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004397 case AttributeList::AT_NSReturnsAutoreleased:
4398 case AttributeList::AT_NSReturnsRetained:
4399 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004400 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4401 cf = false;
4402 break;
4403
Sean Hunt8e083e72012-06-19 23:57:03 +00004404 case AttributeList::AT_CFReturnsRetained:
4405 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004406 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4407 cf = true;
4408 break;
4409 }
4410
4411 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004412 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004413 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004414 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004415 }
Mike Stumpbf916502009-07-24 19:02:52 +00004416
Chandler Carruth87c44602011-07-01 23:49:12 +00004417 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004418 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004419 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004420 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004421 D->addAttr(::new (S.Context)
4422 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4423 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004424 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004425 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004426 D->addAttr(::new (S.Context)
4427 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4428 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004429 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004430 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004431 D->addAttr(::new (S.Context)
4432 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4433 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004434 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004435 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004436 D->addAttr(::new (S.Context)
4437 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4438 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004439 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004440 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004441 D->addAttr(::new (S.Context)
4442 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4443 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004444 return;
4445 };
4446}
4447
John McCalldc7c5ad2011-07-22 08:53:00 +00004448static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4449 const AttributeList &attr) {
4450 SourceLocation loc = attr.getLoc();
4451
4452 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4453
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004454 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004455 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004456 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004457 return;
4458 }
4459
4460 // Check that the method returns a normal pointer.
4461 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004462
4463 if (!resultType->isReferenceType() &&
4464 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004465 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4466 << SourceRange(loc)
4467 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4468
4469 // Drop the attribute.
4470 return;
4471 }
4472
Michael Han51d8c522013-01-24 16:46:58 +00004473 method->addAttr(::new (S.Context)
4474 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4475 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004476}
4477
Fariborz Jahanian84101132012-09-07 23:46:23 +00004478static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4479 const AttributeList &attr) {
4480 SourceLocation loc = attr.getLoc();
4481 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4482
4483 if (!method) {
4484 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4485 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4486 return;
4487 }
4488 DeclContext *DC = method->getDeclContext();
4489 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4490 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4491 << attr.getName() << 0;
4492 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4493 return;
4494 }
4495 if (method->getMethodFamily() == OMF_dealloc) {
4496 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4497 << attr.getName() << 1;
4498 return;
4499 }
4500
Michael Han51d8c522013-01-24 16:46:58 +00004501 method->addAttr(::new (S.Context)
4502 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4503 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004504}
4505
John McCall8dfac0b2011-09-30 05:12:12 +00004506/// Handle cf_audited_transfer and cf_unknown_transfer.
4507static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4508 if (!isa<FunctionDecl>(D)) {
4509 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004510 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004511 return;
4512 }
4513
Sean Hunt8e083e72012-06-19 23:57:03 +00004514 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004515
4516 // Check whether there's a conflicting attribute already present.
4517 Attr *Existing;
4518 if (IsAudited) {
4519 Existing = D->getAttr<CFUnknownTransferAttr>();
4520 } else {
4521 Existing = D->getAttr<CFAuditedTransferAttr>();
4522 }
4523 if (Existing) {
4524 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4525 << A.getName()
4526 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4527 << A.getRange() << Existing->getRange();
4528 return;
4529 }
4530
4531 // All clear; add the attribute.
4532 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004533 D->addAttr(::new (S.Context)
4534 CFAuditedTransferAttr(A.getRange(), S.Context,
4535 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004536 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004537 D->addAttr(::new (S.Context)
4538 CFUnknownTransferAttr(A.getRange(), S.Context,
4539 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004540 }
4541}
4542
John McCallfe98da02011-09-29 07:17:38 +00004543static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4544 const AttributeList &Attr) {
4545 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4546 if (!RD || RD->isUnion()) {
4547 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004548 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004549 }
4550
4551 IdentifierInfo *ParmName = Attr.getParameterName();
4552
4553 // In Objective-C, verify that the type names an Objective-C type.
4554 // We don't want to check this outside of ObjC because people sometimes
4555 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004556 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004557 // Check for an existing type with this name.
4558 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4559 Sema::LookupOrdinaryName);
4560 if (S.LookupName(R, Sc)) {
4561 NamedDecl *Target = R.getFoundDecl();
4562 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4563 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4564 S.Diag(Target->getLocStart(), diag::note_declared_at);
4565 }
4566 }
4567 }
4568
Michael Han51d8c522013-01-24 16:46:58 +00004569 D->addAttr(::new (S.Context)
4570 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4571 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004572}
4573
Chandler Carruth1b03c872011-07-02 00:01:44 +00004574static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4575 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004576 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004577
Chandler Carruth87c44602011-07-01 23:49:12 +00004578 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004579 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004580}
4581
Chandler Carruth1b03c872011-07-02 00:01:44 +00004582static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4583 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004584 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004585 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004586 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004587 return;
4588 }
4589
Chandler Carruth87c44602011-07-01 23:49:12 +00004590 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004591 QualType type = vd->getType();
4592
4593 if (!type->isDependentType() &&
4594 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004595 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004596 << type;
4597 return;
4598 }
4599
4600 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4601
4602 // If we have no lifetime yet, check the lifetime we're presumably
4603 // going to infer.
4604 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4605 lifetime = type->getObjCARCImplicitLifetime();
4606
4607 switch (lifetime) {
4608 case Qualifiers::OCL_None:
4609 assert(type->isDependentType() &&
4610 "didn't infer lifetime for non-dependent type?");
4611 break;
4612
4613 case Qualifiers::OCL_Weak: // meaningful
4614 case Qualifiers::OCL_Strong: // meaningful
4615 break;
4616
4617 case Qualifiers::OCL_ExplicitNone:
4618 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004619 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004620 << (lifetime == Qualifiers::OCL_Autoreleasing);
4621 break;
4622 }
4623
Chandler Carruth87c44602011-07-01 23:49:12 +00004624 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004625 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4626 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004627}
4628
Francois Pichet11542142010-12-19 06:50:37 +00004629//===----------------------------------------------------------------------===//
4630// Microsoft specific attribute handlers.
4631//===----------------------------------------------------------------------===//
4632
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004633// Check if MS extensions or some other language extensions are enabled. If
4634// not, issue a diagnostic that the given attribute is unused.
4635static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4636 bool OtherExtension = false) {
4637 if (S.LangOpts.MicrosoftExt || OtherExtension)
4638 return true;
4639 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4640 return false;
4641}
4642
Chandler Carruth1b03c872011-07-02 00:01:44 +00004643static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004644 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4645 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004646
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004647 // check the attribute arguments.
4648 if (!checkAttributeNumArgs(S, Attr, 1))
4649 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004650
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004651 Expr *Arg = Attr.getArg(0);
4652 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4653 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004654 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4655 << Attr.getName() << 1 << ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004656 return;
4657 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004658
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004659 StringRef StrRef = Str->getString();
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004660
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004661 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4662 StrRef.back() == '}';
Francois Pichetd3d3be92010-12-20 01:41:49 +00004663
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004664 // Validate GUID length.
4665 if (IsCurly && StrRef.size() != 38) {
4666 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4667 return;
4668 }
4669 if (!IsCurly && StrRef.size() != 36) {
4670 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4671 return;
4672 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004673
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004674 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4675 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4676 StringRef::iterator I = StrRef.begin();
4677 if (IsCurly) // Skip the optional '{'
4678 ++I;
4679
4680 for (int i = 0; i < 36; ++i) {
4681 if (i == 8 || i == 13 || i == 18 || i == 23) {
4682 if (*I != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004683 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4684 return;
4685 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004686 } else if (!isHexDigit(*I)) {
4687 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4688 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004689 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004690 I++;
4691 }
Francois Pichet11542142010-12-19 06:50:37 +00004692
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004693 D->addAttr(::new (S.Context)
4694 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4695 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004696}
4697
John McCallc052dbb2012-05-22 21:28:12 +00004698static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004699 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004700 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004701
4702 AttributeList::Kind Kind = Attr.getKind();
4703 if (Kind == AttributeList::AT_SingleInheritance)
4704 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004705 ::new (S.Context)
4706 SingleInheritanceAttr(Attr.getRange(), S.Context,
4707 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004708 else if (Kind == AttributeList::AT_MultipleInheritance)
4709 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004710 ::new (S.Context)
4711 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4712 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004713 else if (Kind == AttributeList::AT_VirtualInheritance)
4714 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004715 ::new (S.Context)
4716 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4717 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004718}
4719
4720static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004721 if (!checkMicrosoftExt(S, Attr))
4722 return;
4723
4724 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004725 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004726 D->addAttr(
4727 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4728 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004729}
4730
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004731static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004732 if (!checkMicrosoftExt(S, Attr))
4733 return;
4734 D->addAttr(::new (S.Context)
4735 ForceInlineAttr(Attr.getRange(), S.Context,
4736 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004737}
4738
Reid Klecknera7225342013-05-20 14:02:37 +00004739static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4740 if (!checkMicrosoftExt(S, Attr))
4741 return;
4742 // Check linkage after possibly merging declaratinos. See
4743 // checkAttributesAfterMerging().
4744 D->addAttr(::new (S.Context)
4745 SelectAnyAttr(Attr.getRange(), S.Context,
4746 Attr.getAttributeSpellingListIndex()));
4747}
4748
Ted Kremenekb71368d2009-05-09 02:44:38 +00004749//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004750// Top Level Sema Entry Points
4751//===----------------------------------------------------------------------===//
4752
Chandler Carruth1b03c872011-07-02 00:01:44 +00004753static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4754 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004755 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4757 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4758 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004759 default:
4760 break;
4761 }
4762}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004763
Chandler Carruth1b03c872011-07-02 00:01:44 +00004764static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4765 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004766 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004767 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4768 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4769 case AttributeList::AT_IBOutletCollection:
4770 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004771 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004772 case AttributeList::AT_ObjCGC:
4773 case AttributeList::AT_VectorSize:
4774 case AttributeList::AT_NeonVectorType:
4775 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004776 case AttributeList::AT_Ptr32:
4777 case AttributeList::AT_Ptr64:
4778 case AttributeList::AT_SPtr:
4779 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004780 // Ignore these, these are type attributes, handled by
4781 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004782 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_CUDADevice:
4784 case AttributeList::AT_CUDAHost:
4785 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004786 // Ignore, this is a non-inheritable attribute, handled
4787 // by ProcessNonInheritableDeclAttr.
4788 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4790 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4791 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4792 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004793 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004794 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004795 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004796 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4798 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4799 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004800 handleDependencyAttr(S, scope, D, Attr);
4801 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4803 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4804 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004805 case AttributeList::AT_CXX11NoReturn:
4806 handleCXX11NoReturnAttr(S, D, Attr);
4807 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004809 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004810 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004811 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4812 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004813 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004814 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004815 case AttributeList::AT_MinSize:
4816 handleMinSizeAttr(S, D, Attr);
4817 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004818 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4819 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4820 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4821 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4822 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004823 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004824 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004825 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4826 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4827 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4828 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4829 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004830 case AttributeList::AT_ownership_returns:
4831 case AttributeList::AT_ownership_takes:
4832 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004833 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004834 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4835 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4836 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4837 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4838 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4839 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4840 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004841
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004843 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004845 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004846
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004848 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4849
Fariborz Jahanian84101132012-09-07 23:46:23 +00004850 case AttributeList::AT_ObjCRequiresSuper:
4851 handleObjCRequiresSuperAttr(S, D, Attr); break;
4852
Sean Hunt8e083e72012-06-19 23:57:03 +00004853 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004854 handleNSBridgedAttr(S, scope, D, Attr); break;
4855
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_CFAuditedTransfer:
4857 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004858 handleCFTransferAttr(S, D, Attr); break;
4859
Ted Kremenekb71368d2009-05-09 02:44:38 +00004860 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004861 case AttributeList::AT_CFConsumed:
4862 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4863 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004864 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004865
Sean Hunt8e083e72012-06-19 23:57:03 +00004866 case AttributeList::AT_NSReturnsAutoreleased:
4867 case AttributeList::AT_NSReturnsNotRetained:
4868 case AttributeList::AT_CFReturnsNotRetained:
4869 case AttributeList::AT_NSReturnsRetained:
4870 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004871 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004872
Tanya Lattner0df579e2012-07-09 22:06:01 +00004873 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004875 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004876
Joey Gouly37453b92013-03-08 09:42:32 +00004877 case AttributeList::AT_VecTypeHint:
4878 handleVecTypeHint(S, D, Attr); break;
4879
Joey Gouly96cead52013-03-14 09:54:43 +00004880 case AttributeList::AT_Endian:
4881 handleEndianAttr(S, D, Attr);
4882 break;
4883
Sean Hunt8e083e72012-06-19 23:57:03 +00004884 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004885 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004886
Sean Hunt8e083e72012-06-19 23:57:03 +00004887 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4888 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4889 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004890 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004891 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004892 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004893 handleArcWeakrefUnavailableAttr (S, D, Attr);
4894 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004895 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004896 handleObjCRootClassAttr(S, D, Attr);
4897 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004898 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004899 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004900 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004901 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4902 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004903 handleReturnsTwiceAttr(S, D, Attr);
4904 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004905 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004906 case AttributeList::AT_Visibility:
4907 handleVisibilityAttr(S, D, Attr, false);
4908 break;
4909 case AttributeList::AT_TypeVisibility:
4910 handleVisibilityAttr(S, D, Attr, true);
4911 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004912 case AttributeList::AT_WarnUnused:
4913 handleWarnUnusedAttr(S, D, Attr);
4914 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004915 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004916 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004917 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4918 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4919 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4920 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004921 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004922 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004923 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004924 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004925 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004926 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004927 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004928 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004929 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4930 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4931 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4932 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4933 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4934 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4935 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4936 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4937 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004938 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004939 // Just ignore
4940 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004941 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004942 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004943 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004944 case AttributeList::AT_StdCall:
4945 case AttributeList::AT_CDecl:
4946 case AttributeList::AT_FastCall:
4947 case AttributeList::AT_ThisCall:
4948 case AttributeList::AT_Pascal:
4949 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004950 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004951 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004952 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004953 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004954 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004955 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004956 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004957 case AttributeList::AT_OpenCLImageAccess:
4958 handleOpenCLImageAccessAttr(S, D, Attr);
4959 break;
John McCallc052dbb2012-05-22 21:28:12 +00004960
4961 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004962 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004963 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004964 handleMsStructAttr(S, D, Attr);
4965 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004966 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004967 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004968 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004969 case AttributeList::AT_SingleInheritance:
4970 case AttributeList::AT_MultipleInheritance:
4971 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004972 handleInheritanceAttr(S, D, Attr);
4973 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004974 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004975 handlePortabilityAttr(S, D, Attr);
4976 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004977 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004978 handleForceInlineAttr(S, D, Attr);
4979 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004980 case AttributeList::AT_SelectAny:
4981 handleSelectAnyAttr(S, D, Attr);
4982 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004983
4984 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004985 case AttributeList::AT_AssertExclusiveLock:
4986 handleAssertExclusiveLockAttr(S, D, Attr);
4987 break;
4988 case AttributeList::AT_AssertSharedLock:
4989 handleAssertSharedLockAttr(S, D, Attr);
4990 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004991 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004992 handleGuardedVarAttr(S, D, Attr);
4993 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004994 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004995 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004996 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004997 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004998 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004999 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00005000 case AttributeList::AT_NoSanitizeAddress:
5001 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00005002 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005003 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00005004 handleNoThreadSafetyAnalysis(S, D, Attr);
5005 break;
5006 case AttributeList::AT_NoSanitizeThread:
5007 handleNoSanitizeThread(S, D, Attr);
5008 break;
5009 case AttributeList::AT_NoSanitizeMemory:
5010 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005011 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005012 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005013 handleLockableAttr(S, D, Attr);
5014 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005015 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005016 handleGuardedByAttr(S, D, Attr);
5017 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005018 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005019 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005020 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005021 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005022 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005023 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005024 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005025 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005026 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005027 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005028 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005029 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005030 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005031 handleLockReturnedAttr(S, D, Attr);
5032 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005033 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005034 handleLocksExcludedAttr(S, D, Attr);
5035 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005036 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005037 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005038 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005039 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005040 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005041 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005042 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005043 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005044 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005045 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005046 handleUnlockFunAttr(S, D, Attr);
5047 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005048 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005049 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005050 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005051 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005052 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005053 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005054
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005055 // Type safety attributes.
5056 case AttributeList::AT_ArgumentWithTypeTag:
5057 handleArgumentWithTypeTagAttr(S, D, Attr);
5058 break;
5059 case AttributeList::AT_TypeTagForDatatype:
5060 handleTypeTagForDatatypeAttr(S, D, Attr);
5061 break;
5062
Chris Lattner803d0802008-06-29 00:43:07 +00005063 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005064 // Ask target about the attribute.
5065 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5066 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005067 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5068 diag::warn_unhandled_ms_attribute_ignored :
5069 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005070 break;
5071 }
5072}
5073
Peter Collingbourne60700392011-01-21 02:08:45 +00005074/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5075/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00005076/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00005077static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5078 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00005079 bool NonInheritable, bool Inheritable,
5080 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00005081 if (Attr.isInvalid())
5082 return;
5083
Richard Smithcd8ab512013-01-17 01:30:42 +00005084 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5085 // instead.
5086 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5087 return;
5088
Peter Collingbourne60700392011-01-21 02:08:45 +00005089 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005090 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005091
5092 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005093 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005094}
5095
Chris Lattner803d0802008-06-29 00:43:07 +00005096/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5097/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005098void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005099 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005100 bool NonInheritable, bool Inheritable,
5101 bool IncludeCXX11Attributes) {
5102 for (const AttributeList* l = AttrList; l; l = l->getNext())
5103 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5104 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005105
5106 // GCC accepts
5107 // static int a9 __attribute__((weakref));
5108 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005109 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005110 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005111 cast<NamedDecl>(D)->getNameAsString();
5112 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005113 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005114 }
5115}
5116
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005117// Annotation attributes are the only attributes allowed after an access
5118// specifier.
5119bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5120 const AttributeList *AttrList) {
5121 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005122 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005123 handleAnnotateAttr(*this, ASDecl, *l);
5124 } else {
5125 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5126 return true;
5127 }
5128 }
5129
5130 return false;
5131}
5132
John McCalle82247a2011-10-01 05:17:03 +00005133/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5134/// contains any decl attributes that we should warn about.
5135static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5136 for ( ; A; A = A->getNext()) {
5137 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005138 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005139 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5140
5141 if (A->getKind() == AttributeList::UnknownAttribute) {
5142 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5143 << A->getName() << A->getRange();
5144 } else {
5145 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5146 << A->getName() << A->getRange();
5147 }
5148 }
5149}
5150
5151/// checkUnusedDeclAttributes - Given a declarator which is not being
5152/// used to build a declaration, complain about any decl attributes
5153/// which might be lying around on it.
5154void Sema::checkUnusedDeclAttributes(Declarator &D) {
5155 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5156 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5157 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5158 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5159}
5160
Ryan Flynne25ff832009-07-30 03:15:39 +00005161/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005162/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005163NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5164 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005165 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005166 NamedDecl *NewD = 0;
5167 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005168 FunctionDecl *NewFD;
5169 // FIXME: Missing call to CheckFunctionDeclaration().
5170 // FIXME: Mangling?
5171 // FIXME: Is the qualifier info correct?
5172 // FIXME: Is the DeclContext correct?
5173 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5174 Loc, Loc, DeclarationName(II),
5175 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005176 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005177 FD->hasPrototype(),
5178 false/*isConstexprSpecified*/);
5179 NewD = NewFD;
5180
5181 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005182 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005183
5184 // Fake up parameter variables; they are declared as if this were
5185 // a typedef.
5186 QualType FDTy = FD->getType();
5187 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5188 SmallVector<ParmVarDecl*, 16> Params;
5189 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5190 AE = FT->arg_type_end(); AI != AE; ++AI) {
5191 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5192 Param->setScopeInfo(0, Params.size());
5193 Params.push_back(Param);
5194 }
David Blaikie4278c652011-09-21 18:16:56 +00005195 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005196 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005197 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5198 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005199 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005200 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005201 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005202 if (VD->getQualifier()) {
5203 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005204 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005205 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005206 }
5207 return NewD;
5208}
5209
James Dennett1dfbd922012-06-14 21:40:34 +00005210/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005211/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005212void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005213 if (W.getUsed()) return; // only do this once
5214 W.setUsed(true);
5215 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5216 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005217 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005218 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5219 NDId->getName()));
5220 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005221 WeakTopLevelDecl.push_back(NewD);
5222 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5223 // to insert Decl at TU scope, sorry.
5224 DeclContext *SavedContext = CurContext;
5225 CurContext = Context.getTranslationUnitDecl();
5226 PushOnScopeChains(NewD, S);
5227 CurContext = SavedContext;
5228 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005229 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005230 }
5231}
5232
Rafael Espindola65611bf2013-03-02 21:41:48 +00005233void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5234 // It's valid to "forward-declare" #pragma weak, in which case we
5235 // have to do this.
5236 LoadExternalWeakUndeclaredIdentifiers();
5237 if (!WeakUndeclaredIdentifiers.empty()) {
5238 NamedDecl *ND = NULL;
5239 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5240 if (VD->isExternC())
5241 ND = VD;
5242 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5243 if (FD->isExternC())
5244 ND = FD;
5245 if (ND) {
5246 if (IdentifierInfo *Id = ND->getIdentifier()) {
5247 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5248 = WeakUndeclaredIdentifiers.find(Id);
5249 if (I != WeakUndeclaredIdentifiers.end()) {
5250 WeakInfo W = I->second;
5251 DeclApplyPragmaWeak(S, ND, W);
5252 WeakUndeclaredIdentifiers[Id] = W;
5253 }
5254 }
5255 }
5256 }
5257}
5258
Chris Lattner0744e5f2008-06-29 00:23:49 +00005259/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5260/// it, apply them to D. This is a bit tricky because PD can have attributes
5261/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005262void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5263 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005264 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005265 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005266 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005267
Chris Lattner0744e5f2008-06-29 00:23:49 +00005268 // Walk the declarator structure, applying decl attributes that were in a type
5269 // position to the decl itself. This handles cases like:
5270 // int *__attr__(x)** D;
5271 // when X is a decl attribute.
5272 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5273 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005274 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5275 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005276
Chris Lattner0744e5f2008-06-29 00:23:49 +00005277 // Finally, apply any attributes on the decl itself.
5278 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005279 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005280}
John McCall54abf7d2009-11-04 02:18:39 +00005281
John McCallf85e1932011-06-15 23:02:42 +00005282/// Is the given declaration allowed to use a forbidden type?
5283static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5284 // Private ivars are always okay. Unfortunately, people don't
5285 // always properly make their ivars private, even in system headers.
5286 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005287 // Function declarations in sys headers will be marked unavailable.
5288 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5289 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005290 return false;
5291
5292 // Require it to be declared in a system header.
5293 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5294}
5295
5296/// Handle a delayed forbidden-type diagnostic.
5297static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5298 Decl *decl) {
5299 if (decl && isForbiddenTypeAllowed(S, decl)) {
5300 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5301 "this system declaration uses an unsupported type"));
5302 return;
5303 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005304 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005305 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005306 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005307 // kind of forbidden type messages on unavailable functions.
5308 if (FD->hasAttr<UnavailableAttr>() &&
5309 diag.getForbiddenTypeDiagnostic() ==
5310 diag::err_arc_array_param_no_ownership) {
5311 diag.Triggered = true;
5312 return;
5313 }
5314 }
John McCallf85e1932011-06-15 23:02:42 +00005315
5316 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5317 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5318 diag.Triggered = true;
5319}
5320
John McCall92576642012-05-07 06:16:41 +00005321void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5322 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005323 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005324 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005325
John McCall92576642012-05-07 06:16:41 +00005326 // When delaying diagnostics to run in the context of a parsed
5327 // declaration, we only want to actually emit anything if parsing
5328 // succeeds.
5329 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005330
John McCall92576642012-05-07 06:16:41 +00005331 // We emit all the active diagnostics in this pool or any of its
5332 // parents. In general, we'll get one pool for the decl spec
5333 // and a child pool for each declarator; in a decl group like:
5334 // deprecated_typedef foo, *bar, baz();
5335 // only the declarator pops will be passed decls. This is correct;
5336 // we really do need to consider delayed diagnostics from the decl spec
5337 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005338 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005339 do {
John McCall13489672012-05-07 06:16:58 +00005340 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005341 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5342 // This const_cast is a bit lame. Really, Triggered should be mutable.
5343 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005344 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005345 continue;
5346
John McCalleee1d542011-02-14 07:13:47 +00005347 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005348 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005349 // Don't bother giving deprecation diagnostics if the decl is invalid.
5350 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005351 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005352 break;
5353
5354 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005355 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005356 break;
John McCallf85e1932011-06-15 23:02:42 +00005357
5358 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005359 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005360 break;
John McCall2f514482010-01-27 03:50:35 +00005361 }
5362 }
John McCall92576642012-05-07 06:16:41 +00005363 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005364}
5365
John McCall13489672012-05-07 06:16:58 +00005366/// Given a set of delayed diagnostics, re-emit them as if they had
5367/// been delayed in the current context instead of in the given pool.
5368/// Essentially, this just moves them to the current pool.
5369void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5370 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5371 assert(curPool && "re-emitting in undelayed context not supported");
5372 curPool->steal(pool);
5373}
5374
John McCall54abf7d2009-11-04 02:18:39 +00005375static bool isDeclDeprecated(Decl *D) {
5376 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005377 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005378 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005379 // A category implicitly has the availability of the interface.
5380 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5381 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005382 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5383 return false;
5384}
5385
Eli Friedmanc3b23082012-08-08 21:52:41 +00005386static void
5387DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5388 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005389 const ObjCInterfaceDecl *UnknownObjCClass,
5390 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005391 DeclarationName Name = D->getDeclName();
5392 if (!Message.empty()) {
5393 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5394 S.Diag(D->getLocation(),
5395 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5396 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005397 if (ObjCPropery)
5398 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5399 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005400 } else if (!UnknownObjCClass) {
5401 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5402 S.Diag(D->getLocation(),
5403 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5404 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005405 if (ObjCPropery)
5406 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5407 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005408 } else {
5409 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5410 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5411 }
5412}
5413
John McCall9c3087b2010-08-26 02:13:20 +00005414void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005415 Decl *Ctx) {
5416 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005417 return;
5418
John McCall2f514482010-01-27 03:50:35 +00005419 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005420 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5421 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005422 DD.getUnknownObjCClass(),
5423 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005424}
5425
Chris Lattner5f9e2722011-07-23 10:55:15 +00005426void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005427 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005428 const ObjCInterfaceDecl *UnknownObjCClass,
5429 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005430 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005431 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005432 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5433 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005434 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005435 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005436 return;
5437 }
5438
5439 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005440 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005441 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005442 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005443}