blob: e925a70af9e9015a9a42e8ac7d28e9d530bb8a05 [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) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
223 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000224 return false;
225 }
226
227 return true;
228}
229
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000230
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000231/// \brief Check if the attribute has at least as many args as Num. May
232/// output an error.
233static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
234 unsigned int Num) {
235 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000236 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
237 return false;
238 }
239
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000240 return true;
241}
242
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000243/// \brief Check if IdxExpr is a valid argument index for a function or
244/// instance method D. May output an error.
245///
246/// \returns true if IdxExpr is a valid index.
247static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
248 StringRef AttrName,
249 SourceLocation AttrLoc,
250 unsigned AttrArgNum,
251 const Expr *IdxExpr,
252 uint64_t &Idx)
253{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000254 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000255
256 // In C++ the implicit 'this' function parameter also counts.
257 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000258 bool HP = hasFunctionProto(D);
259 bool HasImplicitThisParam = isInstanceMethod(D);
260 bool IV = HP && isFunctionOrMethodVariadic(D);
261 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
262 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000263
264 llvm::APSInt IdxInt;
265 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
266 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000267 std::string Name = std::string("'") + AttrName.str() + std::string("'");
268 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
269 << AttrArgNum << ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000270 return false;
271 }
272
273 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000274 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000275 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
276 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
277 return false;
278 }
279 Idx--; // Convert to zero-based.
280 if (HasImplicitThisParam) {
281 if (Idx == 0) {
282 S.Diag(AttrLoc,
283 diag::err_attribute_invalid_implicit_this_argument)
284 << AttrName << IdxExpr->getSourceRange();
285 return false;
286 }
287 --Idx;
288 }
289
290 return true;
291}
292
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000293///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000294/// \brief Check if passed in Decl is a field or potentially shared global var
295/// \return true if the Decl is a field or potentially shared global variable
296///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000297static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000298 if (isa<FieldDecl>(D))
299 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000300 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000301 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000302
303 return false;
304}
305
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000306/// \brief Check if the passed-in expression is of type int or bool.
307static bool isIntOrBool(Expr *Exp) {
308 QualType QT = Exp->getType();
309 return QT->isBooleanType() || QT->isIntegerType();
310}
311
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000312
313// Check to see if the type is a smart pointer of some kind. We assume
314// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000315static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
316 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
317 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000318 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000319 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000320
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000321 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
322 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000323 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000324 return false;
325
326 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000327}
328
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000329/// \brief Check if passed in Decl is a pointer type.
330/// Note that this function may produce an error message.
331/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000332static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
333 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000334 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000335 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000336 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000337 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000338
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000339 if (const RecordType *RT = QT->getAs<RecordType>()) {
340 // If it's an incomplete type, it could be a smart pointer; skip it.
341 // (We don't want to force template instantiation if we can avoid it,
342 // since that would alter the order in which templates are instantiated.)
343 if (RT->isIncompleteType())
344 return true;
345
346 if (threadSafetyCheckIsSmartPointer(S, RT))
347 return true;
348 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000349
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000350 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000351 << Attr.getName()->getName() << QT;
352 } else {
353 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
354 << Attr.getName();
355 }
356 return false;
357}
358
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000359/// \brief Checks that the passed in QualType either is of RecordType or points
360/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000361static const RecordType *getRecordType(QualType QT) {
362 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000363 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000364
365 // Now check if we point to record type.
366 if (const PointerType *PT = QT->getAs<PointerType>())
367 return PT->getPointeeType()->getAs<RecordType>();
368
369 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000370}
371
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000372
Jordy Rosefad5de92012-05-08 03:27:22 +0000373static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
374 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000375 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
376 if (RT->getDecl()->getAttr<LockableAttr>())
377 return true;
378 return false;
379}
380
381
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000382/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000383/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000384static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
385 QualType Ty) {
386 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000387
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000388 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000389 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000390 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000391 << Attr.getName() << Ty.getAsString();
392 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000393 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000394
Michael Hanf1aae3b2012-08-03 17:40:43 +0000395 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000396 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000397 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000398
399 // Allow smart pointers to be used as lockable objects.
400 // FIXME -- Check the type that the smart pointer points to.
401 if (threadSafetyCheckIsSmartPointer(S, RT))
402 return;
403
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000404 // Check if the type is lockable.
405 RecordDecl *RD = RT->getDecl();
406 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000407 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000408
409 // Else check if any base classes are lockable.
410 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
411 CXXBasePaths BPaths(false, false);
412 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
413 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000414 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000415
416 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
417 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000418}
419
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000420/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000421/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000422/// \param Sidx The attribute argument index to start checking with.
423/// \param ParamIdxOk Whether an argument can be indexing into a function
424/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000425static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000426 const AttributeList &Attr,
427 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000428 int Sidx = 0,
429 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000430 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000431 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000432
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000433 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000434 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000435 Args.push_back(ArgExp);
436 continue;
437 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000438
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000439 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000440 if (StrLit->getLength() == 0 ||
441 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000442 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000443 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000444 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000445 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000446 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000447
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000448 // We allow constant strings to be used as a placeholder for expressions
449 // that are not valid C++ syntax, but warn that they are ignored.
450 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
451 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000452 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000453 continue;
454 }
455
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000458 // A pointer to member expression of the form &MyClass::mu is treated
459 // specially -- we need to look at the type of the member.
460 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
461 if (UOp->getOpcode() == UO_AddrOf)
462 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
463 if (DRE->getDecl()->isCXXInstanceMember())
464 ArgTy = DRE->getDecl()->getType();
465
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000466 // First see if we can just cast to record type, or point to record type.
467 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000469 // Now check if we index into a record type function param.
470 if(!RT && ParamIdxOk) {
471 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000472 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
473 if(FD && IL) {
474 unsigned int NumParams = FD->getNumParams();
475 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000476 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
477 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
478 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000479 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
480 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000481 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000483 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000484 }
485 }
486
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000487 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000488
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000489 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000490 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000491}
492
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000493//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000494// Attribute Implementations
495//===----------------------------------------------------------------------===//
496
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000497// FIXME: All this manual attribute parsing code is gross. At the
498// least add some helper functions to check most argument patterns (#
499// and types of args).
500
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000501enum ThreadAttributeDeclKind {
502 ThreadExpectedFieldOrGlobalVar,
503 ThreadExpectedFunctionOrMethod,
504 ThreadExpectedClassOrStruct
505};
506
Michael Hanf1aae3b2012-08-03 17:40:43 +0000507static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000508 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000509 assert(!Attr.isInvalid());
510
511 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000512 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000513
514 // D must be either a member field or global (potentially shared) variable.
515 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000516 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
517 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000518 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000519 }
520
Michael Handc691572012-07-23 18:48:41 +0000521 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000522}
523
Michael Handc691572012-07-23 18:48:41 +0000524static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
525 if (!checkGuardedVarAttrCommon(S, D, Attr))
526 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000527
Michael Han51d8c522013-01-24 16:46:58 +0000528 D->addAttr(::new (S.Context)
529 GuardedVarAttr(Attr.getRange(), S.Context,
530 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000531}
532
Michael Hanf1aae3b2012-08-03 17:40:43 +0000533static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000534 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000535 if (!checkGuardedVarAttrCommon(S, D, Attr))
536 return;
537
538 if (!threadSafetyCheckIsPointer(S, D, Attr))
539 return;
540
Michael Han51d8c522013-01-24 16:46:58 +0000541 D->addAttr(::new (S.Context)
542 PtGuardedVarAttr(Attr.getRange(), S.Context,
543 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000544}
545
Michael Hanf1aae3b2012-08-03 17:40:43 +0000546static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
547 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000548 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000549 assert(!Attr.isInvalid());
550
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000551 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000552 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000553
554 // D must be either a member field or global (potentially shared) variable.
555 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000556 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
557 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000558 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000559 }
560
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000561 SmallVector<Expr*, 1> Args;
562 // check that all arguments are lockable objects
563 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
564 unsigned Size = Args.size();
565 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000566 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000567
Michael Handc691572012-07-23 18:48:41 +0000568 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000569
Michael Handc691572012-07-23 18:48:41 +0000570 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000571}
572
Michael Handc691572012-07-23 18:48:41 +0000573static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
574 Expr *Arg = 0;
575 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
576 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000577
Michael Handc691572012-07-23 18:48:41 +0000578 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
579}
580
Michael Hanf1aae3b2012-08-03 17:40:43 +0000581static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000582 const AttributeList &Attr) {
583 Expr *Arg = 0;
584 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
585 return;
586
587 if (!threadSafetyCheckIsPointer(S, D, Attr))
588 return;
589
590 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
591 S.Context, Arg));
592}
593
Michael Hanf1aae3b2012-08-03 17:40:43 +0000594static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000595 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000596 assert(!Attr.isInvalid());
597
598 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000599 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000600
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000601 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000602 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000603 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
604 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000605 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000606 }
607
Michael Handc691572012-07-23 18:48:41 +0000608 return true;
609}
610
611static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
612 if (!checkLockableAttrCommon(S, D, Attr))
613 return;
614
615 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
616}
617
Michael Hanf1aae3b2012-08-03 17:40:43 +0000618static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000619 const AttributeList &Attr) {
620 if (!checkLockableAttrCommon(S, D, Attr))
621 return;
622
Michael Han51d8c522013-01-24 16:46:58 +0000623 D->addAttr(::new (S.Context)
624 ScopedLockableAttr(Attr.getRange(), S.Context,
625 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000626}
627
Kostya Serebryany85aee962013-02-26 06:58:27 +0000628static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
629 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000630 assert(!Attr.isInvalid());
631
632 if (!checkAttributeNumArgs(S, Attr, 0))
633 return;
634
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000635 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000636 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
637 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000638 return;
639 }
640
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000641 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000642 S.Context));
643}
644
Kostya Serebryany85aee962013-02-26 06:58:27 +0000645static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000646 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000647 assert(!Attr.isInvalid());
648
649 if (!checkAttributeNumArgs(S, Attr, 0))
650 return;
651
652 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000654 << Attr.getName() << ExpectedFunctionOrMethod;
655 return;
656 }
657
Michael Han51d8c522013-01-24 16:46:58 +0000658 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000659 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
660 Attr.getAttributeSpellingListIndex()));
661}
662
663static void handleNoSanitizeMemory(Sema &S, Decl *D,
664 const AttributeList &Attr) {
665 assert(!Attr.isInvalid());
666
667 if (!checkAttributeNumArgs(S, Attr, 0))
668 return;
669
670 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
672 << Attr.getName() << ExpectedFunctionOrMethod;
673 return;
674 }
675
676 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
677 S.Context));
678}
679
680static void handleNoSanitizeThread(Sema &S, Decl *D,
681 const AttributeList &Attr) {
682 assert(!Attr.isInvalid());
683
684 if (!checkAttributeNumArgs(S, Attr, 0))
685 return;
686
687 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
689 << Attr.getName() << ExpectedFunctionOrMethod;
690 return;
691 }
692
693 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
694 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000695}
696
Michael Hanf1aae3b2012-08-03 17:40:43 +0000697static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
698 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000699 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000700 assert(!Attr.isInvalid());
701
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000702 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000703 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000704
705 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000706 ValueDecl *VD = dyn_cast<ValueDecl>(D);
707 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000708 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
709 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000710 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000711 }
712
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000713 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000714 QualType QT = VD->getType();
715 if (!QT->isDependentType()) {
716 const RecordType *RT = getRecordType(QT);
717 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000718 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000719 << Attr.getName();
720 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000721 }
722 }
723
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000724 // Check that all arguments are lockable objects.
725 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000726 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000727 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000728
Michael Handc691572012-07-23 18:48:41 +0000729 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000730}
731
Michael Hanf1aae3b2012-08-03 17:40:43 +0000732static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000733 const AttributeList &Attr) {
734 SmallVector<Expr*, 1> Args;
735 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
736 return;
737
738 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000739 D->addAttr(::new (S.Context)
740 AcquiredAfterAttr(Attr.getRange(), S.Context,
741 StartArg, Args.size(),
742 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000743}
744
Michael Hanf1aae3b2012-08-03 17:40:43 +0000745static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000746 const AttributeList &Attr) {
747 SmallVector<Expr*, 1> Args;
748 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
749 return;
750
751 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000752 D->addAttr(::new (S.Context)
753 AcquiredBeforeAttr(Attr.getRange(), S.Context,
754 StartArg, Args.size(),
755 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000756}
757
Michael Hanf1aae3b2012-08-03 17:40:43 +0000758static bool checkLockFunAttrCommon(Sema &S, Decl *D,
759 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000760 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761 assert(!Attr.isInvalid());
762
763 // zero or more arguments ok
764
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000765 // check that the attribute is applied to a function
766 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000767 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
768 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000769 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000770 }
771
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000772 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000773 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000774
Michael Handc691572012-07-23 18:48:41 +0000775 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000776}
777
Michael Hanf1aae3b2012-08-03 17:40:43 +0000778static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000779 const AttributeList &Attr) {
780 SmallVector<Expr*, 1> Args;
781 if (!checkLockFunAttrCommon(S, D, Attr, Args))
782 return;
783
784 unsigned Size = Args.size();
785 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000786 D->addAttr(::new (S.Context)
787 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000792 const AttributeList &Attr) {
793 SmallVector<Expr*, 1> Args;
794 if (!checkLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000799 D->addAttr(::new (S.Context)
800 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
801 StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000803}
804
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000805static void handleAssertSharedLockAttr(Sema &S, Decl *D,
806 const AttributeList &Attr) {
807 SmallVector<Expr*, 1> Args;
808 if (!checkLockFunAttrCommon(S, D, Attr, Args))
809 return;
810
811 unsigned Size = Args.size();
812 Expr **StartArg = Size == 0 ? 0 : &Args[0];
813 D->addAttr(::new (S.Context)
814 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
815 Attr.getAttributeSpellingListIndex()));
816}
817
818static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
819 const AttributeList &Attr) {
820 SmallVector<Expr*, 1> Args;
821 if (!checkLockFunAttrCommon(S, D, Attr, Args))
822 return;
823
824 unsigned Size = Args.size();
825 Expr **StartArg = Size == 0 ? 0 : &Args[0];
826 D->addAttr(::new (S.Context)
827 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
828 StartArg, Size,
829 Attr.getAttributeSpellingListIndex()));
830}
831
832
Michael Hanf1aae3b2012-08-03 17:40:43 +0000833static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
834 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000835 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000836 assert(!Attr.isInvalid());
837
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000839 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000840
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000841 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000842 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
843 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000844 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000845 }
846
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000847 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000848 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
849 << Attr.getName() << 1 << ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000850 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000851 }
852
853 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000854 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000855
Michael Handc691572012-07-23 18:48:41 +0000856 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000857}
858
Michael Hanf1aae3b2012-08-03 17:40:43 +0000859static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000860 const AttributeList &Attr) {
861 SmallVector<Expr*, 2> Args;
862 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
863 return;
864
865 unsigned Size = Args.size();
866 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000867 D->addAttr(::new (S.Context)
868 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
869 Attr.getArg(0), StartArg, Size,
870 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000871}
872
Michael Hanf1aae3b2012-08-03 17:40:43 +0000873static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000874 const AttributeList &Attr) {
875 SmallVector<Expr*, 2> Args;
876 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
877 return;
878
879 unsigned Size = Args.size();
880 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000881 D->addAttr(::new (S.Context)
882 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
883 Attr.getArg(0), StartArg, Size,
884 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000885}
886
Michael Hanf1aae3b2012-08-03 17:40:43 +0000887static bool checkLocksRequiredCommon(Sema &S, Decl *D,
888 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000889 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000890 assert(!Attr.isInvalid());
891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000893 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000894
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000896 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
897 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000898 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000899 }
900
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000901 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000902 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000903 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000904 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000905
Michael Handc691572012-07-23 18:48:41 +0000906 return true;
907}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000908
Michael Hanf1aae3b2012-08-03 17:40:43 +0000909static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000910 const AttributeList &Attr) {
911 SmallVector<Expr*, 1> Args;
912 if (!checkLocksRequiredCommon(S, D, Attr, Args))
913 return;
914
915 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000916 D->addAttr(::new (S.Context)
917 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
918 StartArg, Args.size(),
919 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000920}
921
Michael Hanf1aae3b2012-08-03 17:40:43 +0000922static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000923 const AttributeList &Attr) {
924 SmallVector<Expr*, 1> Args;
925 if (!checkLocksRequiredCommon(S, D, Attr, Args))
926 return;
927
928 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000929 D->addAttr(::new (S.Context)
930 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
931 StartArg, Args.size(),
932 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000933}
934
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000935static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000936 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000937 assert(!Attr.isInvalid());
938
939 // zero or more arguments ok
940
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000941 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000942 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
943 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000944 return;
945 }
946
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000947 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000948 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000949 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000950 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000951 Expr **StartArg = Size == 0 ? 0 : &Args[0];
952
Michael Han51d8c522013-01-24 16:46:58 +0000953 D->addAttr(::new (S.Context)
954 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
955 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000956}
957
958static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000959 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000960 assert(!Attr.isInvalid());
961
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000962 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000963 return;
964
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000965 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000966 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
967 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968 return;
969 }
970
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000971 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000972 SmallVector<Expr*, 1> Args;
973 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
974 unsigned Size = Args.size();
975 if (Size == 0)
976 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000977
Michael Han51d8c522013-01-24 16:46:58 +0000978 D->addAttr(::new (S.Context)
979 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
980 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000981}
982
983static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000984 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000985 assert(!Attr.isInvalid());
986
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000987 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000988 return;
989
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000990 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000991 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
992 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000993 return;
994 }
995
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000996 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000997 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000998 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000999 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +00001000 if (Size == 0)
1001 return;
1002 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001003
Michael Han51d8c522013-01-24 16:46:58 +00001004 D->addAttr(::new (S.Context)
1005 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1006 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001007}
1008
1009
Chandler Carruth1b03c872011-07-02 00:01:44 +00001010static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1011 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001012 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1013 if (TD == 0) {
1014 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1015 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001016 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001017 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001018 }
Mike Stumpbf916502009-07-24 19:02:52 +00001019
Richard Smitha4fa9002013-01-13 02:11:23 +00001020 // Remember this typedef decl, we will need it later for diagnostics.
1021 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001022}
1023
Chandler Carruth1b03c872011-07-02 00:01:44 +00001024static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001025 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001026 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001028
Chandler Carruth87c44602011-07-01 23:49:12 +00001029 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001030 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001031 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001032 // If the alignment is less than or equal to 8 bits, the packed attribute
1033 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001034 if (!FD->getType()->isDependentType() &&
1035 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001036 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001037 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001038 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001039 else
Michael Han51d8c522013-01-24 16:46:58 +00001040 FD->addAttr(::new (S.Context)
1041 PackedAttr(Attr.getRange(), S.Context,
1042 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001043 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001044 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001045}
1046
Chandler Carruth1b03c872011-07-02 00:01:44 +00001047static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001048 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001049 RD->addAttr(::new (S.Context)
1050 MsStructAttr(Attr.getRange(), S.Context,
1051 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001052 else
1053 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1054}
1055
Chandler Carruth1b03c872011-07-02 00:01:44 +00001056static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001057 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001058 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001059 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001060
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001061 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001062 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001063 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001064 D->addAttr(::new (S.Context)
1065 IBActionAttr(Attr.getRange(), S.Context,
1066 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001067 return;
1068 }
1069
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001070 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001071}
1072
Ted Kremenek2f041d02011-09-29 07:02:25 +00001073static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1074 // The IBOutlet/IBOutletCollection attributes only apply to instance
1075 // variables or properties of Objective-C classes. The outlet must also
1076 // have an object reference type.
1077 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1078 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001079 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001080 << Attr.getName() << VD->getType() << 0;
1081 return false;
1082 }
1083 }
1084 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1085 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001086 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001087 << Attr.getName() << PD->getType() << 1;
1088 return false;
1089 }
1090 }
1091 else {
1092 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1093 return false;
1094 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001095
Ted Kremenek2f041d02011-09-29 07:02:25 +00001096 return true;
1097}
1098
Chandler Carruth1b03c872011-07-02 00:01:44 +00001099static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001100 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001101 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001102 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001103
1104 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001105 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001106
Michael Han51d8c522013-01-24 16:46:58 +00001107 D->addAttr(::new (S.Context)
1108 IBOutletAttr(Attr.getRange(), S.Context,
1109 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001110}
1111
Chandler Carruth1b03c872011-07-02 00:01:44 +00001112static void handleIBOutletCollection(Sema &S, Decl *D,
1113 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001114
1115 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001116 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001117 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1118 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001119 return;
1120 }
1121
Ted Kremenek2f041d02011-09-29 07:02:25 +00001122 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001123 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001124
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001125 IdentifierInfo *II = Attr.getParameterName();
1126 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001127 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001128
John McCallb3d87482010-08-24 05:47:05 +00001129 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001130 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001131 if (!TypeRep) {
1132 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1133 return;
1134 }
John McCallb3d87482010-08-24 05:47:05 +00001135 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001136 // Diagnose use of non-object type in iboutletcollection attribute.
1137 // FIXME. Gnu attribute extension ignores use of builtin types in
1138 // attributes. So, __attribute__((iboutletcollection(char))) will be
1139 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001140 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001141 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1142 return;
1143 }
Michael Han51d8c522013-01-24 16:46:58 +00001144 D->addAttr(::new (S.Context)
1145 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1146 QT, Attr.getParameterLoc(),
1147 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001148}
1149
Chandler Carruthd309c812011-07-01 23:49:16 +00001150static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001151 if (const RecordType *UT = T->getAsUnionType())
1152 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1153 RecordDecl *UD = UT->getDecl();
1154 for (RecordDecl::field_iterator it = UD->field_begin(),
1155 itend = UD->field_end(); it != itend; ++it) {
1156 QualType QT = it->getType();
1157 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1158 T = QT;
1159 return;
1160 }
1161 }
1162 }
1163}
1164
Nuno Lopes587de5b2012-05-24 00:22:00 +00001165static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001166 if (!isFunctionOrMethod(D)) {
1167 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001168 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001169 return;
1170 }
1171
Nuno Lopes587de5b2012-05-24 00:22:00 +00001172 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1173 return;
1174
Nuno Lopes587de5b2012-05-24 00:22:00 +00001175 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001176 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1177 Expr *Ex = Attr.getArg(i);
1178 uint64_t Idx;
1179 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1180 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001181 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001182
1183 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001184 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001185 if (!T->isIntegerType()) {
1186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001187 << "alloc_size" << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001188 return;
1189 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001190 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001191 }
1192
1193 // check if the function returns a pointer
1194 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1195 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001196 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001197 }
1198
Michael Han51d8c522013-01-24 16:46:58 +00001199 D->addAttr(::new (S.Context)
1200 AllocSizeAttr(Attr.getRange(), S.Context,
1201 SizeArgs.data(), SizeArgs.size(),
1202 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001203}
1204
Chandler Carruth1b03c872011-07-02 00:01:44 +00001205static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001206 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1207 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001208 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001210 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001211 return;
1212 }
Mike Stumpbf916502009-07-24 19:02:52 +00001213
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001214 SmallVector<unsigned, 8> NonNullArgs;
1215 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1216 Expr *Ex = Attr.getArg(i);
1217 uint64_t Idx;
1218 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1219 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001220 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001221
1222 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001223 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001224 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001225
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001226 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001227 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001228 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001229 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001230 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001231 }
Mike Stumpbf916502009-07-24 19:02:52 +00001232
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001233 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001234 }
Mike Stumpbf916502009-07-24 19:02:52 +00001235
1236 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1237 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001238 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001239 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1240 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001241 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001242 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001243 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001244 }
Mike Stumpbf916502009-07-24 19:02:52 +00001245
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001246 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001247 if (NonNullArgs.empty()) {
1248 // Warn the trivial case only if attribute is not coming from a
1249 // macro instantiation.
1250 if (Attr.getLoc().isFileID())
1251 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001252 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001253 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001254 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001255
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001256 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001257 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001258 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001259 D->addAttr(::new (S.Context)
1260 NonNullAttr(Attr.getRange(), S.Context, start, size,
1261 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001262}
1263
Chandler Carruth1b03c872011-07-02 00:01:44 +00001264static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001265 // This attribute must be applied to a function declaration.
1266 // The first argument to the attribute must be a string,
1267 // the name of the resource, for example "malloc".
1268 // The following arguments must be argument indexes, the arguments must be
1269 // of integer type for Returns, otherwise of pointer type.
1270 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001271 // after being held. free() should be __attribute((ownership_takes)), whereas
1272 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001273
1274 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001275 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1276 << AL.getName()->getName() << 1 << ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001277 return;
1278 }
1279 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001280 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001281 switch (AL.getKind()) {
1282 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001283 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001284 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001285 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1286 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001287 return;
1288 }
Jordy Rose2a479922010-08-12 08:54:03 +00001289 break;
1290 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001291 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001292 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001293 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1294 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001295 return;
1296 }
Jordy Rose2a479922010-08-12 08:54:03 +00001297 break;
1298 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001299 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001300 if (AL.getNumArgs() > 1) {
1301 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001302 << AL.getName() << AL.getNumArgs() + 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001303 return;
1304 }
Jordy Rose2a479922010-08-12 08:54:03 +00001305 break;
1306 default:
1307 // This should never happen given how we are called.
1308 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001309 }
1310
Chandler Carruth87c44602011-07-01 23:49:12 +00001311 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001312 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1313 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001314 return;
1315 }
1316
Chris Lattner5f9e2722011-07-23 10:55:15 +00001317 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001318
1319 // Normalize the argument, __foo__ becomes foo.
1320 if (Module.startswith("__") && Module.endswith("__"))
1321 Module = Module.substr(2, Module.size() - 4);
1322
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001323
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001324 SmallVector<unsigned, 8> OwnershipArgs;
1325 for (unsigned i = 0; i < AL.getNumArgs(); ++i) {
1326 Expr *Ex = AL.getArg(i);
1327 uint64_t Idx;
1328 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
1329 AL.getLoc(), i + 1, Ex, Idx))
1330 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001331
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001332 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001333 case OwnershipAttr::Takes:
1334 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001335 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001336 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001337 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1338 // FIXME: Should also highlight argument in decl.
1339 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001340 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001342 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001343 continue;
1344 }
1345 break;
1346 }
Sean Huntcf807c42010-08-18 23:23:40 +00001347 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001348 if (AL.getNumArgs() > 1) {
1349 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001350 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001351 llvm::APSInt ArgNum(32);
1352 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1353 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1354 S.Diag(AL.getLoc(), diag::err_ownership_type)
1355 << "ownership_returns" << "integer"
1356 << IdxExpr->getSourceRange();
1357 return;
1358 }
1359 }
1360 break;
1361 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001362 } // switch
1363
1364 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001365 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001366 i = D->specific_attr_begin<OwnershipAttr>(),
1367 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001368 i != e; ++i) {
1369 if ((*i)->getOwnKind() != K) {
1370 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1371 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001372 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001373 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1374 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001375 }
1376 }
1377 }
1378 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001379 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001380 }
1381
1382 unsigned* start = OwnershipArgs.data();
1383 unsigned size = OwnershipArgs.size();
1384 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001385
1386 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001387 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1388 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001389 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001390 }
Sean Huntcf807c42010-08-18 23:23:40 +00001391
Michael Han51d8c522013-01-24 16:46:58 +00001392 D->addAttr(::new (S.Context)
1393 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1394 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001395}
1396
Chandler Carruth1b03c872011-07-02 00:01:44 +00001397static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001398 // Check the attribute arguments.
1399 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1401 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001402 return;
1403 }
1404
Chandler Carruth87c44602011-07-01 23:49:12 +00001405 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001407 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001408 return;
1409 }
1410
Chandler Carruth87c44602011-07-01 23:49:12 +00001411 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001412
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001413 // gcc rejects
1414 // class c {
1415 // static int a __attribute__((weakref ("v2")));
1416 // static int b() __attribute__((weakref ("f3")));
1417 // };
1418 // and ignores the attributes of
1419 // void f(void) {
1420 // static int a __attribute__((weakref ("v2")));
1421 // }
1422 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001423 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001424 if (!Ctx->isFileContext()) {
1425 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001426 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001427 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001428 }
1429
1430 // The GCC manual says
1431 //
1432 // At present, a declaration to which `weakref' is attached can only
1433 // be `static'.
1434 //
1435 // It also says
1436 //
1437 // Without a TARGET,
1438 // given as an argument to `weakref' or to `alias', `weakref' is
1439 // equivalent to `weak'.
1440 //
1441 // gcc 4.4.1 will accept
1442 // int a7 __attribute__((weakref));
1443 // as
1444 // int a7 __attribute__((weak));
1445 // This looks like a bug in gcc. We reject that for now. We should revisit
1446 // it if this behaviour is actually used.
1447
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001448 // GCC rejects
1449 // static ((alias ("y"), weakref)).
1450 // Should we? How to check that weakref is before or after alias?
1451
1452 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001453 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001454 Arg = Arg->IgnoreParenCasts();
1455 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1456
Douglas Gregor5cee1192011-07-27 05:40:30 +00001457 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1459 << Attr.getName() << 1 << ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001460 return;
1461 }
1462 // GCC will accept anything as the argument of weakref. Should we
1463 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001464 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001465 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001466 }
1467
Michael Han51d8c522013-01-24 16:46:58 +00001468 D->addAttr(::new (S.Context)
1469 WeakRefAttr(Attr.getRange(), S.Context,
1470 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001471}
1472
Chandler Carruth1b03c872011-07-02 00:01:44 +00001473static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001474 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001475 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001477
Peter Collingbourne7a730022010-11-23 20:45:58 +00001478 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001479 Arg = Arg->IgnoreParenCasts();
1480 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001481
Douglas Gregor5cee1192011-07-27 05:40:30 +00001482 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1484 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001485 return;
1486 }
Mike Stumpbf916502009-07-24 19:02:52 +00001487
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001488 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001489 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1490 return;
1491 }
1492
Chris Lattner6b6b5372008-06-26 18:38:35 +00001493 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001494
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001495 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001496 Str->getString(),
1497 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001498}
1499
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001500static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1501 // Check the attribute arguments.
1502 if (!checkAttributeNumArgs(S, Attr, 0))
1503 return;
1504
1505 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1506 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1507 << Attr.getName() << ExpectedFunctionOrMethod;
1508 return;
1509 }
1510
Michael Han51d8c522013-01-24 16:46:58 +00001511 D->addAttr(::new (S.Context)
1512 MinSizeAttr(Attr.getRange(), S.Context,
1513 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001514}
1515
Benjamin Krameree409a92012-05-12 21:10:52 +00001516static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1517 // Check the attribute arguments.
1518 if (!checkAttributeNumArgs(S, Attr, 0))
1519 return;
1520
1521 if (!isa<FunctionDecl>(D)) {
1522 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1523 << Attr.getName() << ExpectedFunction;
1524 return;
1525 }
1526
1527 if (D->hasAttr<HotAttr>()) {
1528 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1529 << Attr.getName() << "hot";
1530 return;
1531 }
1532
Michael Han51d8c522013-01-24 16:46:58 +00001533 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1534 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001535}
1536
1537static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1538 // Check the attribute arguments.
1539 if (!checkAttributeNumArgs(S, Attr, 0))
1540 return;
1541
1542 if (!isa<FunctionDecl>(D)) {
1543 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1544 << Attr.getName() << ExpectedFunction;
1545 return;
1546 }
1547
1548 if (D->hasAttr<ColdAttr>()) {
1549 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1550 << Attr.getName() << "cold";
1551 return;
1552 }
1553
Michael Han51d8c522013-01-24 16:46:58 +00001554 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1555 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001556}
1557
Chandler Carruth1b03c872011-07-02 00:01:44 +00001558static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001559 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001560 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001561 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001562
Chandler Carruth87c44602011-07-01 23:49:12 +00001563 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001564 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001565 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001566 return;
1567 }
1568
Michael Han51d8c522013-01-24 16:46:58 +00001569 D->addAttr(::new (S.Context)
1570 NakedAttr(Attr.getRange(), S.Context,
1571 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001572}
1573
Chandler Carruth1b03c872011-07-02 00:01:44 +00001574static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1575 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001576 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001577 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001578 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1579 << Attr.getName() << 0;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001580 return;
1581 }
1582
Chandler Carruth87c44602011-07-01 23:49:12 +00001583 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001585 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001586 return;
1587 }
Mike Stumpbf916502009-07-24 19:02:52 +00001588
Michael Han51d8c522013-01-24 16:46:58 +00001589 D->addAttr(::new (S.Context)
1590 AlwaysInlineAttr(Attr.getRange(), S.Context,
1591 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001592}
1593
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001594static void handleTLSModelAttr(Sema &S, Decl *D,
1595 const AttributeList &Attr) {
1596 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001597 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001598 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001599
1600 Expr *Arg = Attr.getArg(0);
1601 Arg = Arg->IgnoreParenCasts();
1602 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1603
1604 // Check that it is a string.
1605 if (!Str) {
1606 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1607 return;
1608 }
1609
Richard Smith38afbc72013-04-13 02:43:54 +00001610 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001611 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1612 << Attr.getName() << ExpectedTLSVar;
1613 return;
1614 }
1615
1616 // Check that the value.
1617 StringRef Model = Str->getString();
1618 if (Model != "global-dynamic" && Model != "local-dynamic"
1619 && Model != "initial-exec" && Model != "local-exec") {
1620 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1621 return;
1622 }
1623
Michael Han51d8c522013-01-24 16:46:58 +00001624 D->addAttr(::new (S.Context)
1625 TLSModelAttr(Attr.getRange(), S.Context, Model,
1626 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001627}
1628
Chandler Carruth1b03c872011-07-02 00:01:44 +00001629static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001630 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001631 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1633 << Attr.getName() << 0;
Ryan Flynn76168e22009-08-09 20:07:29 +00001634 return;
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chandler Carruth87c44602011-07-01 23:49:12 +00001637 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001638 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001639 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001640 D->addAttr(::new (S.Context)
1641 MallocAttr(Attr.getRange(), S.Context,
1642 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001643 return;
1644 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001645 }
1646
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001647 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001648}
1649
Chandler Carruth1b03c872011-07-02 00:01:44 +00001650static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001651 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001652 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001653 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001654
Michael Han51d8c522013-01-24 16:46:58 +00001655 D->addAttr(::new (S.Context)
1656 MayAliasAttr(Attr.getRange(), S.Context,
1657 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001658}
1659
Chandler Carruth1b03c872011-07-02 00:01:44 +00001660static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001661 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001662 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001663 D->addAttr(::new (S.Context)
1664 NoCommonAttr(Attr.getRange(), S.Context,
1665 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001666 else
1667 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001668 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001669}
1670
Chandler Carruth1b03c872011-07-02 00:01:44 +00001671static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001672 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001673
1674 if (S.LangOpts.CPlusPlus) {
1675 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1676 return;
1677 }
1678
Chandler Carruth87c44602011-07-01 23:49:12 +00001679 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001680 D->addAttr(::new (S.Context)
1681 CommonAttr(Attr.getRange(), S.Context,
1682 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001683 else
1684 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001685 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001686}
1687
Chandler Carruth1b03c872011-07-02 00:01:44 +00001688static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001689 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001690
1691 if (S.CheckNoReturnAttr(attr)) return;
1692
Chandler Carruth87c44602011-07-01 23:49:12 +00001693 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001694 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001695 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001696 return;
1697 }
1698
Michael Han51d8c522013-01-24 16:46:58 +00001699 D->addAttr(::new (S.Context)
1700 NoReturnAttr(attr.getRange(), S.Context,
1701 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001702}
1703
1704bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001705 if (attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001706 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1707 << attr.getName() << 0;
John McCall711c52b2011-01-05 12:14:39 +00001708 attr.setInvalid();
1709 return true;
1710 }
1711
1712 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001713}
1714
Chandler Carruth1b03c872011-07-02 00:01:44 +00001715static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1716 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001717
1718 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1719 // because 'analyzer_noreturn' does not impact the type.
1720
Chandler Carruth1731e202011-07-11 23:30:35 +00001721 if(!checkAttributeNumArgs(S, Attr, 0))
1722 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001723
Chandler Carruth87c44602011-07-01 23:49:12 +00001724 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1725 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001726 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1727 && !VD->getType()->isFunctionPointerType())) {
1728 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001729 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001730 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001731 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001732 return;
1733 }
1734 }
1735
Michael Han51d8c522013-01-24 16:46:58 +00001736 D->addAttr(::new (S.Context)
1737 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1738 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001739}
1740
Richard Smithcd8ab512013-01-17 01:30:42 +00001741static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1742 const AttributeList &Attr) {
1743 // C++11 [dcl.attr.noreturn]p1:
1744 // The attribute may be applied to the declarator-id in a function
1745 // declaration.
1746 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1747 if (!FD) {
1748 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1749 << Attr.getName() << ExpectedFunctionOrMethod;
1750 return;
1751 }
1752
Michael Han51d8c522013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context)
1754 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1755 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001756}
1757
John Thompson35cc9622010-08-09 21:53:52 +00001758// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001759static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001760/*
1761 Returning a Vector Class in Registers
1762
Eric Christopherf48f3672010-12-01 22:13:54 +00001763 According to the PPU ABI specifications, a class with a single member of
1764 vector type is returned in memory when used as the return value of a function.
1765 This results in inefficient code when implementing vector classes. To return
1766 the value in a single vector register, add the vecreturn attribute to the
1767 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001768
1769 Example:
1770
1771 struct Vector
1772 {
1773 __vector float xyzw;
1774 } __attribute__((vecreturn));
1775
1776 Vector Add(Vector lhs, Vector rhs)
1777 {
1778 Vector result;
1779 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1780 return result; // This will be returned in a register
1781 }
1782*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001783 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001784 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001785 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001786 return;
1787 }
1788
Chandler Carruth87c44602011-07-01 23:49:12 +00001789 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001790 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1791 return;
1792 }
1793
Chandler Carruth87c44602011-07-01 23:49:12 +00001794 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001795 int count = 0;
1796
1797 if (!isa<CXXRecordDecl>(record)) {
1798 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1799 return;
1800 }
1801
1802 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1803 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1804 return;
1805 }
1806
Eric Christopherf48f3672010-12-01 22:13:54 +00001807 for (RecordDecl::field_iterator iter = record->field_begin();
1808 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001809 if ((count == 1) || !iter->getType()->isVectorType()) {
1810 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1811 return;
1812 }
1813 count++;
1814 }
1815
Michael Han51d8c522013-01-24 16:46:58 +00001816 D->addAttr(::new (S.Context)
1817 VecReturnAttr(Attr.getRange(), S.Context,
1818 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001819}
1820
Richard Smith3a2b7a12013-01-28 22:42:45 +00001821static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1822 const AttributeList &Attr) {
1823 if (isa<ParmVarDecl>(D)) {
1824 // [[carries_dependency]] can only be applied to a parameter if it is a
1825 // parameter of a function declaration or lambda.
1826 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1827 S.Diag(Attr.getLoc(),
1828 diag::err_carries_dependency_param_not_function_decl);
1829 return;
1830 }
1831 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001832 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001833 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001834 return;
1835 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001836
1837 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1838 Attr.getRange(), S.Context,
1839 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001840}
1841
Chandler Carruth1b03c872011-07-02 00:01:44 +00001842static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001843 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001844 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1846 << Attr.getName() << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001847 return;
1848 }
Mike Stumpbf916502009-07-24 19:02:52 +00001849
Chandler Carruth87c44602011-07-01 23:49:12 +00001850 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001851 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001852 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001853 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001854 return;
1855 }
Mike Stumpbf916502009-07-24 19:02:52 +00001856
Michael Han51d8c522013-01-24 16:46:58 +00001857 D->addAttr(::new (S.Context)
1858 UnusedAttr(Attr.getRange(), S.Context,
1859 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001860}
1861
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001862static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1863 const AttributeList &Attr) {
1864 // check the attribute arguments.
1865 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1867 << Attr.getName() << 0;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001868 return;
1869 }
1870
1871 if (!isa<FunctionDecl>(D)) {
1872 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1873 << Attr.getName() << ExpectedFunction;
1874 return;
1875 }
1876
Michael Han51d8c522013-01-24 16:46:58 +00001877 D->addAttr(::new (S.Context)
1878 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1879 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001880}
1881
Chandler Carruth1b03c872011-07-02 00:01:44 +00001882static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001883 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001884 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001885 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1886 << Attr.getName() << 0;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001887 return;
1888 }
Mike Stumpbf916502009-07-24 19:02:52 +00001889
Chandler Carruth87c44602011-07-01 23:49:12 +00001890 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001891 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001892 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1893 return;
1894 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001895 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001897 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001898 return;
1899 }
Mike Stumpbf916502009-07-24 19:02:52 +00001900
Michael Han51d8c522013-01-24 16:46:58 +00001901 D->addAttr(::new (S.Context)
1902 UsedAttr(Attr.getRange(), S.Context,
1903 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001904}
1905
Chandler Carruth1b03c872011-07-02 00:01:44 +00001906static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001907 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001908 if (Attr.getNumArgs() > 1) {
1909 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001910 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001911 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001912
1913 int priority = 65535; // FIXME: Do not hardcode such constants.
1914 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001915 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001916 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001917 if (E->isTypeDependent() || E->isValueDependent() ||
1918 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001919 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1920 << Attr.getName() << 1 << ArgumentIntegerConstant
1921 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001922 return;
1923 }
1924 priority = Idx.getZExtValue();
1925 }
Mike Stumpbf916502009-07-24 19:02:52 +00001926
Chandler Carruth87c44602011-07-01 23:49:12 +00001927 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001928 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001929 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001930 return;
1931 }
1932
Michael Han51d8c522013-01-24 16:46:58 +00001933 D->addAttr(::new (S.Context)
1934 ConstructorAttr(Attr.getRange(), S.Context, priority,
1935 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001936}
1937
Chandler Carruth1b03c872011-07-02 00:01:44 +00001938static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001939 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001940 if (Attr.getNumArgs() > 1) {
1941 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001942 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001943 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001944
1945 int priority = 65535; // FIXME: Do not hardcode such constants.
1946 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001947 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001948 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001949 if (E->isTypeDependent() || E->isValueDependent() ||
1950 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001951 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1952 << Attr.getName() << 1 << ArgumentIntegerConstant
1953 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001954 return;
1955 }
1956 priority = Idx.getZExtValue();
1957 }
Mike Stumpbf916502009-07-24 19:02:52 +00001958
Chandler Carruth87c44602011-07-01 23:49:12 +00001959 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001960 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001961 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001962 return;
1963 }
1964
Michael Han51d8c522013-01-24 16:46:58 +00001965 D->addAttr(::new (S.Context)
1966 DestructorAttr(Attr.getRange(), S.Context, priority,
1967 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001968}
1969
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001970template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00001971static void handleAttrWithMessage(Sema &S, Decl *D,
1972 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001973 unsigned NumArgs = Attr.getNumArgs();
1974 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001975 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001976 return;
1977 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001978
1979 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001980 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001981 if (NumArgs == 1) {
1982 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001983 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001984 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Aaron Ballman2dbdef22013-07-18 13:13:52 +00001985 << Attr.getName();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001986 return;
1987 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001988 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001989 }
Mike Stumpbf916502009-07-24 19:02:52 +00001990
Michael Han51d8c522013-01-24 16:46:58 +00001991 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1992 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001993}
1994
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001995static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1996 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00001997 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001998 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001999
Michael Han51d8c522013-01-24 16:46:58 +00002000 D->addAttr(::new (S.Context)
2001 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2002 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002003}
2004
Patrick Beardb2f68202012-04-06 18:12:22 +00002005static void handleObjCRootClassAttr(Sema &S, Decl *D,
2006 const AttributeList &Attr) {
2007 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2009 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002010 return;
2011 }
2012
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002013 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002014 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002015
Michael Han51d8c522013-01-24 16:46:58 +00002016 D->addAttr(::new (S.Context)
2017 ObjCRootClassAttr(Attr.getRange(), S.Context,
2018 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002019}
2020
Michael Han51d8c522013-01-24 16:46:58 +00002021static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2022 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002023 if (!isa<ObjCInterfaceDecl>(D)) {
2024 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2025 return;
2026 }
2027
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002028 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002029 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002030
Michael Han51d8c522013-01-24 16:46:58 +00002031 D->addAttr(::new (S.Context)
2032 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2033 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002034}
2035
Jordy Rosefad5de92012-05-08 03:27:22 +00002036static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2037 IdentifierInfo *Platform,
2038 VersionTuple Introduced,
2039 VersionTuple Deprecated,
2040 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002041 StringRef PlatformName
2042 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2043 if (PlatformName.empty())
2044 PlatformName = Platform->getName();
2045
2046 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2047 // of these steps are needed).
2048 if (!Introduced.empty() && !Deprecated.empty() &&
2049 !(Introduced <= Deprecated)) {
2050 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2051 << 1 << PlatformName << Deprecated.getAsString()
2052 << 0 << Introduced.getAsString();
2053 return true;
2054 }
2055
2056 if (!Introduced.empty() && !Obsoleted.empty() &&
2057 !(Introduced <= Obsoleted)) {
2058 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2059 << 2 << PlatformName << Obsoleted.getAsString()
2060 << 0 << Introduced.getAsString();
2061 return true;
2062 }
2063
2064 if (!Deprecated.empty() && !Obsoleted.empty() &&
2065 !(Deprecated <= Obsoleted)) {
2066 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2067 << 2 << PlatformName << Obsoleted.getAsString()
2068 << 1 << Deprecated.getAsString();
2069 return true;
2070 }
2071
2072 return false;
2073}
2074
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002075/// \brief Check whether the two versions match.
2076///
2077/// If either version tuple is empty, then they are assumed to match. If
2078/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2079static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2080 bool BeforeIsOkay) {
2081 if (X.empty() || Y.empty())
2082 return true;
2083
2084 if (X == Y)
2085 return true;
2086
2087 if (BeforeIsOkay && X < Y)
2088 return true;
2089
2090 return false;
2091}
2092
Rafael Espindola51be6e32013-01-08 22:04:34 +00002093AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002094 IdentifierInfo *Platform,
2095 VersionTuple Introduced,
2096 VersionTuple Deprecated,
2097 VersionTuple Obsoleted,
2098 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002099 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002100 bool Override,
2101 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002102 VersionTuple MergedIntroduced = Introduced;
2103 VersionTuple MergedDeprecated = Deprecated;
2104 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002105 bool FoundAny = false;
2106
Rafael Espindola98ae8342012-05-10 02:50:16 +00002107 if (D->hasAttrs()) {
2108 AttrVec &Attrs = D->getAttrs();
2109 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2110 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2111 if (!OldAA) {
2112 ++i;
2113 continue;
2114 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002115
Rafael Espindola98ae8342012-05-10 02:50:16 +00002116 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2117 if (OldPlatform != Platform) {
2118 ++i;
2119 continue;
2120 }
2121
2122 FoundAny = true;
2123 VersionTuple OldIntroduced = OldAA->getIntroduced();
2124 VersionTuple OldDeprecated = OldAA->getDeprecated();
2125 VersionTuple OldObsoleted = OldAA->getObsoleted();
2126 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002127
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002128 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2129 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2130 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2131 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002132 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002133 if (Override) {
2134 int Which = -1;
2135 VersionTuple FirstVersion;
2136 VersionTuple SecondVersion;
2137 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2138 Which = 0;
2139 FirstVersion = OldIntroduced;
2140 SecondVersion = Introduced;
2141 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2142 Which = 1;
2143 FirstVersion = Deprecated;
2144 SecondVersion = OldDeprecated;
2145 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2146 Which = 2;
2147 FirstVersion = Obsoleted;
2148 SecondVersion = OldObsoleted;
2149 }
2150
2151 if (Which == -1) {
2152 Diag(OldAA->getLocation(),
2153 diag::warn_mismatched_availability_override_unavail)
2154 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2155 } else {
2156 Diag(OldAA->getLocation(),
2157 diag::warn_mismatched_availability_override)
2158 << Which
2159 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2160 << FirstVersion.getAsString() << SecondVersion.getAsString();
2161 }
2162 Diag(Range.getBegin(), diag::note_overridden_method);
2163 } else {
2164 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2165 Diag(Range.getBegin(), diag::note_previous_attribute);
2166 }
2167
Rafael Espindola98ae8342012-05-10 02:50:16 +00002168 Attrs.erase(Attrs.begin() + i);
2169 --e;
2170 continue;
2171 }
2172
2173 VersionTuple MergedIntroduced2 = MergedIntroduced;
2174 VersionTuple MergedDeprecated2 = MergedDeprecated;
2175 VersionTuple MergedObsoleted2 = MergedObsoleted;
2176
2177 if (MergedIntroduced2.empty())
2178 MergedIntroduced2 = OldIntroduced;
2179 if (MergedDeprecated2.empty())
2180 MergedDeprecated2 = OldDeprecated;
2181 if (MergedObsoleted2.empty())
2182 MergedObsoleted2 = OldObsoleted;
2183
2184 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2185 MergedIntroduced2, MergedDeprecated2,
2186 MergedObsoleted2)) {
2187 Attrs.erase(Attrs.begin() + i);
2188 --e;
2189 continue;
2190 }
2191
2192 MergedIntroduced = MergedIntroduced2;
2193 MergedDeprecated = MergedDeprecated2;
2194 MergedObsoleted = MergedObsoleted2;
2195 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002196 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002197 }
2198
2199 if (FoundAny &&
2200 MergedIntroduced == Introduced &&
2201 MergedDeprecated == Deprecated &&
2202 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002203 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002204
Ted Kremenekcb344392013-04-06 00:34:27 +00002205 // Only create a new attribute if !Override, but we want to do
2206 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002207 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002208 MergedDeprecated, MergedObsoleted) &&
2209 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002210 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2211 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002212 Obsoleted, IsUnavailable, Message,
2213 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002214 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002215 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002216}
2217
Chandler Carruth1b03c872011-07-02 00:01:44 +00002218static void handleAvailabilityAttr(Sema &S, Decl *D,
2219 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002220 IdentifierInfo *Platform = Attr.getParameterName();
2221 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002222 unsigned Index = Attr.getAttributeSpellingListIndex();
2223
Rafael Espindola3b294362012-05-06 19:56:25 +00002224 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002225 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2226 << Platform;
2227
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002228 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2229 if (!ND) {
2230 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2231 return;
2232 }
2233
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002234 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2235 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2236 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002237 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002238 StringRef Str;
2239 const StringLiteral *SE =
2240 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2241 if (SE)
2242 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002243
Rafael Espindola51be6e32013-01-08 22:04:34 +00002244 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002245 Platform,
2246 Introduced.Version,
2247 Deprecated.Version,
2248 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002249 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002250 /*Override=*/false,
2251 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002252 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002253 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002254}
2255
John McCalld4c3d662013-02-20 01:54:26 +00002256template <class T>
2257static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2258 typename T::VisibilityType value,
2259 unsigned attrSpellingListIndex) {
2260 T *existingAttr = D->getAttr<T>();
2261 if (existingAttr) {
2262 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2263 if (existingValue == value)
2264 return NULL;
2265 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2266 S.Diag(range.getBegin(), diag::note_previous_attribute);
2267 D->dropAttr<T>();
2268 }
2269 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2270}
2271
Rafael Espindola599f1b72012-05-13 03:25:18 +00002272VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002273 VisibilityAttr::VisibilityType Vis,
2274 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002275 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2276 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002277}
2278
John McCalld4c3d662013-02-20 01:54:26 +00002279TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2280 TypeVisibilityAttr::VisibilityType Vis,
2281 unsigned AttrSpellingListIndex) {
2282 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2283 AttrSpellingListIndex);
2284}
2285
2286static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2287 bool isTypeVisibility) {
2288 // Visibility attributes don't mean anything on a typedef.
2289 if (isa<TypedefNameDecl>(D)) {
2290 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2291 << Attr.getName();
2292 return;
2293 }
2294
2295 // 'type_visibility' can only go on a type or namespace.
2296 if (isTypeVisibility &&
2297 !(isa<TagDecl>(D) ||
2298 isa<ObjCInterfaceDecl>(D) ||
2299 isa<NamespaceDecl>(D))) {
2300 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2301 << Attr.getName() << ExpectedTypeOrNamespace;
2302 return;
2303 }
2304
Chris Lattner6b6b5372008-06-26 18:38:35 +00002305 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002306 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002307 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002308
Peter Collingbourne7a730022010-11-23 20:45:58 +00002309 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002310 Arg = Arg->IgnoreParenCasts();
2311 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002312
Douglas Gregor5cee1192011-07-27 05:40:30 +00002313 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2315 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002316 return;
2317 }
Mike Stumpbf916502009-07-24 19:02:52 +00002318
Chris Lattner5f9e2722011-07-23 10:55:15 +00002319 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002320 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002321
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002322 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002323 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002324 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002325 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002326 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002327 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002328 else if (TypeStr == "protected") {
2329 // Complain about attempts to use protected visibility on targets
2330 // (like Darwin) that don't support it.
2331 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2332 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2333 type = VisibilityAttr::Default;
2334 } else {
2335 type = VisibilityAttr::Protected;
2336 }
2337 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002338 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002339 return;
2340 }
Mike Stumpbf916502009-07-24 19:02:52 +00002341
Michael Han51d8c522013-01-24 16:46:58 +00002342 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002343 clang::Attr *newAttr;
2344 if (isTypeVisibility) {
2345 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2346 (TypeVisibilityAttr::VisibilityType) type,
2347 Index);
2348 } else {
2349 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2350 }
2351 if (newAttr)
2352 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002353}
2354
Chandler Carruth1b03c872011-07-02 00:01:44 +00002355static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2356 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002357 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2358 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002359 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002360 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002361 return;
2362 }
2363
Chandler Carruth87c44602011-07-01 23:49:12 +00002364 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2365 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002366 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2367 << Attr.getName() << 1 << ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002368 } else {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2370 << Attr.getName() << 0;
John McCalld5313b02011-03-02 11:33:24 +00002371 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002372 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002373 return;
2374 }
2375
Chris Lattner5f9e2722011-07-23 10:55:15 +00002376 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002377 ObjCMethodFamilyAttr::FamilyKind family;
2378 if (param == "none")
2379 family = ObjCMethodFamilyAttr::OMF_None;
2380 else if (param == "alloc")
2381 family = ObjCMethodFamilyAttr::OMF_alloc;
2382 else if (param == "copy")
2383 family = ObjCMethodFamilyAttr::OMF_copy;
2384 else if (param == "init")
2385 family = ObjCMethodFamilyAttr::OMF_init;
2386 else if (param == "mutableCopy")
2387 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2388 else if (param == "new")
2389 family = ObjCMethodFamilyAttr::OMF_new;
2390 else {
2391 // Just warn and ignore it. This is future-proof against new
2392 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002393 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002394 return;
2395 }
2396
John McCallf85e1932011-06-15 23:02:42 +00002397 if (family == ObjCMethodFamilyAttr::OMF_init &&
2398 !method->getResultType()->isObjCObjectPointerType()) {
2399 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2400 << method->getResultType();
2401 // Ignore the attribute.
2402 return;
2403 }
2404
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002405 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002406 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002407}
2408
Chandler Carruth1b03c872011-07-02 00:01:44 +00002409static void handleObjCExceptionAttr(Sema &S, Decl *D,
2410 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002411 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002412 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002413
Chris Lattner0db29ec2009-02-14 08:09:34 +00002414 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2415 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002416 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2417 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002418 return;
2419 }
Mike Stumpbf916502009-07-24 19:02:52 +00002420
Michael Han51d8c522013-01-24 16:46:58 +00002421 D->addAttr(::new (S.Context)
2422 ObjCExceptionAttr(Attr.getRange(), S.Context,
2423 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002424}
2425
Chandler Carruth1b03c872011-07-02 00:01:44 +00002426static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002427 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002428 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002429 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002430 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002431 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002432 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2433 return;
2434 }
2435 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002436 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2437 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002438 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002439 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2440 return;
2441 }
2442 }
2443 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002444 // It is okay to include this attribute on properties, e.g.:
2445 //
2446 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2447 //
2448 // In this case it follows tradition and suppresses an error in the above
2449 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002450 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002451 }
Michael Han51d8c522013-01-24 16:46:58 +00002452 D->addAttr(::new (S.Context)
2453 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2454 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002455}
2456
Mike Stumpbf916502009-07-24 19:02:52 +00002457static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002458handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002459 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002460 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002461
2462 if (!isa<FunctionDecl>(D)) {
2463 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2464 return;
2465 }
2466
Michael Han51d8c522013-01-24 16:46:58 +00002467 D->addAttr(::new (S.Context)
2468 OverloadableAttr(Attr.getRange(), S.Context,
2469 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002470}
2471
Chandler Carruth1b03c872011-07-02 00:01:44 +00002472static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002473 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2475 << Attr.getName() << 1 << ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002476 return;
2477 }
Mike Stumpbf916502009-07-24 19:02:52 +00002478
Steve Naroff9eae5762008-09-18 16:44:58 +00002479 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2481 << Attr.getName() << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002482 return;
2483 }
Mike Stumpbf916502009-07-24 19:02:52 +00002484
Sean Huntcf807c42010-08-18 23:23:40 +00002485 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002486 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002487 type = BlocksAttr::ByRef;
2488 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002489 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002490 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002491 return;
2492 }
Mike Stumpbf916502009-07-24 19:02:52 +00002493
Michael Han51d8c522013-01-24 16:46:58 +00002494 D->addAttr(::new (S.Context)
2495 BlocksAttr(Attr.getRange(), S.Context, type,
2496 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002497}
2498
Chandler Carruth1b03c872011-07-02 00:01:44 +00002499static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002500 // check the attribute arguments.
2501 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002502 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002503 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002504 }
2505
John McCall3323fad2011-09-09 07:56:05 +00002506 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002507 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002508 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002509 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002510 if (E->isTypeDependent() || E->isValueDependent() ||
2511 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002512 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmand7329282013-07-23 17:35:26 +00002513 << Attr.getName() << 1 << ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002514 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002515 return;
2516 }
Mike Stumpbf916502009-07-24 19:02:52 +00002517
John McCall3323fad2011-09-09 07:56:05 +00002518 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002519 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2520 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002521 return;
2522 }
John McCall3323fad2011-09-09 07:56:05 +00002523
2524 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002525 }
2526
John McCall3323fad2011-09-09 07:56:05 +00002527 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002528 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002529 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002530 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002531 if (E->isTypeDependent() || E->isValueDependent() ||
2532 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmand7329282013-07-23 17:35:26 +00002534 << Attr.getName() << 2 << ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002535 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002536 return;
2537 }
2538 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002539
John McCall3323fad2011-09-09 07:56:05 +00002540 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002541 // FIXME: This error message could be improved, it would be nice
2542 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002543 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2544 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002545 return;
2546 }
2547 }
2548
Chandler Carruth87c44602011-07-01 23:49:12 +00002549 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002550 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002551 if (isa<FunctionNoProtoType>(FT)) {
2552 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2553 return;
2554 }
Mike Stumpbf916502009-07-24 19:02:52 +00002555
Chris Lattner897cd902009-03-17 23:03:47 +00002556 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002557 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002558 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002559 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002560 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002561 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002562 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002563 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002564 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002565 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2566 if (!BD->isVariadic()) {
2567 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2568 return;
2569 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002570 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002571 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002572 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002573 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002574 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002575 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002576 int m = Ty->isFunctionPointerType() ? 0 : 1;
2577 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002578 return;
2579 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002580 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002581 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002582 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002583 return;
2584 }
Anders Carlsson77091822008-10-05 18:05:59 +00002585 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002587 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002588 return;
2589 }
Michael Han51d8c522013-01-24 16:46:58 +00002590 D->addAttr(::new (S.Context)
2591 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2592 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002593}
2594
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002595static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2596 // Check the attribute arguments.
2597 if (!checkAttributeNumArgs(S, Attr, 0))
2598 return;
2599
2600 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2601 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2602 else
2603 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2604}
2605
Chandler Carruth1b03c872011-07-02 00:01:44 +00002606static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002607 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002608 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002609 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002610
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002611 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002613 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002614 return;
2615 }
Mike Stumpbf916502009-07-24 19:02:52 +00002616
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002617 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2618 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2619 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002620 return;
2621 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002622 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2623 if (MD->getResultType()->isVoidType()) {
2624 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2625 << Attr.getName() << 1;
2626 return;
2627 }
2628
Michael Han51d8c522013-01-24 16:46:58 +00002629 D->addAttr(::new (S.Context)
2630 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2631 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002632}
2633
Chandler Carruth1b03c872011-07-02 00:01:44 +00002634static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002635 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002636 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2638 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002639 return;
2640 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002641
Chandler Carruth87c44602011-07-01 23:49:12 +00002642 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002643 if (isa<CXXRecordDecl>(D)) {
2644 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2645 return;
2646 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2648 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002649 return;
2650 }
2651
Chandler Carruth87c44602011-07-01 23:49:12 +00002652 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002653
Michael Han51d8c522013-01-24 16:46:58 +00002654 nd->addAttr(::new (S.Context)
2655 WeakAttr(Attr.getRange(), S.Context,
2656 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002657}
2658
Chandler Carruth1b03c872011-07-02 00:01:44 +00002659static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002660 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002661 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002662 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002663
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002664
2665 // weak_import only applies to variable & function declarations.
2666 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002667 if (!D->canBeWeakImported(isDef)) {
2668 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002669 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2670 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002671 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002672 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002673 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002674 // Nothing to warn about here.
2675 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002676 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002677 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002678
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002679 return;
2680 }
2681
Michael Han51d8c522013-01-24 16:46:58 +00002682 D->addAttr(::new (S.Context)
2683 WeakImportAttr(Attr.getRange(), S.Context,
2684 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002685}
2686
Tanya Lattner0df579e2012-07-09 22:06:01 +00002687// Handles reqd_work_group_size and work_group_size_hint.
2688static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002689 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002690 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2691 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2692
Nate Begeman6f3d8382009-06-26 06:32:41 +00002693 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002694 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002695
2696 unsigned WGSize[3];
2697 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002698 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002699 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002700 if (E->isTypeDependent() || E->isValueDependent() ||
2701 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002702 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002703 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002704 return;
2705 }
2706 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2707 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002708
2709 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2710 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2711 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2712 if (!(A->getXDim() == WGSize[0] &&
2713 A->getYDim() == WGSize[1] &&
2714 A->getZDim() == WGSize[2])) {
2715 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2716 Attr.getName();
2717 }
2718 }
2719
2720 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2721 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2722 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2723 if (!(A->getXDim() == WGSize[0] &&
2724 A->getYDim() == WGSize[1] &&
2725 A->getZDim() == WGSize[2])) {
2726 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2727 Attr.getName();
2728 }
2729 }
2730
2731 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2732 D->addAttr(::new (S.Context)
2733 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002734 WGSize[0], WGSize[1], WGSize[2],
2735 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002736 else
2737 D->addAttr(::new (S.Context)
2738 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002739 WGSize[0], WGSize[1], WGSize[2],
2740 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002741}
2742
Joey Gouly37453b92013-03-08 09:42:32 +00002743static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2744 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2745
2746 // Attribute has 1 argument.
2747 if (!checkAttributeNumArgs(S, Attr, 1))
2748 return;
2749
2750 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2751
2752 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2753 (ParmType->isBooleanType() ||
2754 !ParmType->isIntegralType(S.getASTContext()))) {
2755 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2756 << ParmType;
2757 return;
2758 }
2759
2760 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2761 D->hasAttr<VecTypeHintAttr>()) {
2762 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2763 if (A->getTypeHint() != ParmType) {
2764 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2765 return;
2766 }
2767 }
2768
2769 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2770 ParmType, Attr.getLoc()));
2771}
2772
Joey Gouly96cead52013-03-14 09:54:43 +00002773static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2774 if (!dyn_cast<VarDecl>(D))
2775 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2776 << 9;
2777 StringRef EndianType = Attr.getParameterName()->getName();
2778 if (EndianType != "host" && EndianType != "device")
2779 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2780}
2781
Rafael Espindola599f1b72012-05-13 03:25:18 +00002782SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002783 StringRef Name,
2784 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002785 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2786 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002787 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002788 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2789 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002790 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002791 }
Michael Han51d8c522013-01-24 16:46:58 +00002792 return ::new (Context) SectionAttr(Range, Context, Name,
2793 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002794}
2795
Chandler Carruth1b03c872011-07-02 00:01:44 +00002796static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002797 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002798 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002799 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002800
2801 // Make sure that there is a string literal as the sections's single
2802 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002803 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002804 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002805 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002806 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002807 return;
2808 }
Mike Stump1eb44332009-09-09 15:08:12 +00002809
Chris Lattner797c3c42009-08-10 19:03:04 +00002810 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002811 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002812 if (!Error.empty()) {
2813 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2814 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002815 return;
2816 }
Mike Stump1eb44332009-09-09 15:08:12 +00002817
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002818 // This attribute cannot be applied to local variables.
2819 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2820 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2821 return;
2822 }
Michael Han51d8c522013-01-24 16:46:58 +00002823
2824 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002825 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002826 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002827 if (NewAttr)
2828 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002829}
2830
Chris Lattner6b6b5372008-06-26 18:38:35 +00002831
Chandler Carruth1b03c872011-07-02 00:01:44 +00002832static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002833 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002834 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002835 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2836 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002837 return;
2838 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002839
Chandler Carruth87c44602011-07-01 23:49:12 +00002840 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002841 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002842 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002843 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002844 D->addAttr(::new (S.Context)
2845 NoThrowAttr(Attr.getRange(), S.Context,
2846 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002847 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002848}
2849
Chandler Carruth1b03c872011-07-02 00:01:44 +00002850static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002851 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002852 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2854 << Attr.getName() << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002855 return;
2856 }
Mike Stumpbf916502009-07-24 19:02:52 +00002857
Chandler Carruth87c44602011-07-01 23:49:12 +00002858 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002859 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002860 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002861 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002862 D->addAttr(::new (S.Context)
2863 ConstAttr(Attr.getRange(), S.Context,
2864 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002865 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002866}
2867
Chandler Carruth1b03c872011-07-02 00:01:44 +00002868static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002869 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002870 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002871 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002872
Michael Han51d8c522013-01-24 16:46:58 +00002873 D->addAttr(::new (S.Context)
2874 PureAttr(Attr.getRange(), S.Context,
2875 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002876}
2877
Chandler Carruth1b03c872011-07-02 00:01:44 +00002878static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002879 if (!Attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002880 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2881 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002882 return;
2883 }
Mike Stumpbf916502009-07-24 19:02:52 +00002884
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002885 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2887 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002888 return;
2889 }
Mike Stumpbf916502009-07-24 19:02:52 +00002890
Chandler Carruth87c44602011-07-01 23:49:12 +00002891 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002892
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002893 if (!VD || !VD->hasLocalStorage()) {
2894 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2895 return;
2896 }
Mike Stumpbf916502009-07-24 19:02:52 +00002897
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002898 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002899 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002900 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002901 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2902 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002903 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002904 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002905 Attr.getParameterName();
2906 return;
2907 }
Mike Stumpbf916502009-07-24 19:02:52 +00002908
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002909 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2910 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002911 S.Diag(Attr.getParameterLoc(),
2912 diag::err_attribute_cleanup_arg_not_function)
2913 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002914 return;
2915 }
2916
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002917 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002918 S.Diag(Attr.getParameterLoc(),
2919 diag::err_attribute_cleanup_func_must_take_one_arg)
2920 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002921 return;
2922 }
Mike Stumpbf916502009-07-24 19:02:52 +00002923
Anders Carlsson89941c12009-02-07 23:16:50 +00002924 // We're currently more strict than GCC about what function types we accept.
2925 // If this ever proves to be a problem it should be easy to fix.
2926 QualType Ty = S.Context.getPointerType(VD->getType());
2927 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002928 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2929 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002930 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002931 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2932 Attr.getParameterName() << ParamTy << Ty;
2933 return;
2934 }
Mike Stumpbf916502009-07-24 19:02:52 +00002935
Michael Han51d8c522013-01-24 16:46:58 +00002936 D->addAttr(::new (S.Context)
2937 CleanupAttr(Attr.getRange(), S.Context, FD,
2938 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002939 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00002940 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002941}
2942
Mike Stumpbf916502009-07-24 19:02:52 +00002943/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002944/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002945static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002946 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002947 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002948
Chandler Carruth87c44602011-07-01 23:49:12 +00002949 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002950 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002951 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002952 return;
2953 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002954
Peter Collingbourne7a730022010-11-23 20:45:58 +00002955 Expr *IdxExpr = Attr.getArg(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002956 uint64_t ArgIdx;
2957 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2958 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002959 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002960
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002961 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002962 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002963
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002964 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2965 if (not_nsstring_type &&
2966 !isCFStringType(Ty, S.Context) &&
2967 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002968 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002969 // FIXME: Should highlight the actual expression that has the wrong type.
2970 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002971 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002972 << IdxExpr->getSourceRange();
2973 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002974 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002975 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002976 if (!isNSStringType(Ty, S.Context) &&
2977 !isCFStringType(Ty, S.Context) &&
2978 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002979 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002980 // FIXME: Should highlight the actual expression that has the wrong type.
2981 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002982 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002983 << IdxExpr->getSourceRange();
2984 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002985 }
2986
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002987 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2988 // because that has corrected for the implicit this parameter, and is zero-
2989 // based. The attribute expects what the user wrote explicitly.
2990 llvm::APSInt Val;
2991 IdxExpr->EvaluateAsInt(Val, S.Context);
2992
Michael Han51d8c522013-01-24 16:46:58 +00002993 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002994 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002995 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002996}
2997
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002998enum FormatAttrKind {
2999 CFStringFormat,
3000 NSStringFormat,
3001 StrftimeFormat,
3002 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003003 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003004 InvalidFormat
3005};
3006
3007/// getFormatAttrKind - Map from format attribute names to supported format
3008/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003009static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003010 return llvm::StringSwitch<FormatAttrKind>(Format)
3011 // Check for formats that get handled specially.
3012 .Case("NSString", NSStringFormat)
3013 .Case("CFString", CFStringFormat)
3014 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003015
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003016 // Otherwise, check for supported formats.
3017 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3018 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3019 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003020
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003021 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3022 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003023}
3024
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003025/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003026/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003027static void handleInitPriorityAttr(Sema &S, Decl *D,
3028 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003029 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003030 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3031 return;
3032 }
3033
Chandler Carruth87c44602011-07-01 23:49:12 +00003034 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003035 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3036 Attr.setInvalid();
3037 return;
3038 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003039 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003040 if (S.Context.getAsArrayType(T))
3041 T = S.Context.getBaseElementType(T);
3042 if (!T->getAs<RecordType>()) {
3043 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3044 Attr.setInvalid();
3045 return;
3046 }
3047
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003048 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003049 Attr.setInvalid();
3050 return;
3051 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003052 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003053
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003054 llvm::APSInt priority(32);
3055 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3056 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
3057 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3058 << "init_priority" << priorityExpr->getSourceRange();
3059 Attr.setInvalid();
3060 return;
3061 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003062 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003063 if (prioritynum < 101 || prioritynum > 65535) {
3064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3065 << priorityExpr->getSourceRange();
3066 Attr.setInvalid();
3067 return;
3068 }
Michael Han51d8c522013-01-24 16:46:58 +00003069 D->addAttr(::new (S.Context)
3070 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3071 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003072}
3073
Rafael Espindola599f1b72012-05-13 03:25:18 +00003074FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003075 int FormatIdx, int FirstArg,
3076 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003077 // Check whether we already have an equivalent format attribute.
3078 for (specific_attr_iterator<FormatAttr>
3079 i = D->specific_attr_begin<FormatAttr>(),
3080 e = D->specific_attr_end<FormatAttr>();
3081 i != e ; ++i) {
3082 FormatAttr *f = *i;
3083 if (f->getType() == Format &&
3084 f->getFormatIdx() == FormatIdx &&
3085 f->getFirstArg() == FirstArg) {
3086 // If we don't have a valid location for this attribute, adopt the
3087 // location.
3088 if (f->getLocation().isInvalid())
3089 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003090 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003091 }
3092 }
3093
Michael Han51d8c522013-01-24 16:46:58 +00003094 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3095 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003096}
3097
Mike Stumpbf916502009-07-24 19:02:52 +00003098/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003099/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003100static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003101
Chris Lattner545dd342008-06-28 23:36:30 +00003102 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003103 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3104 << Attr.getName() << 1 << ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003105 return;
3106 }
3107
Chris Lattner545dd342008-06-28 23:36:30 +00003108 if (Attr.getNumArgs() != 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3110 << Attr.getName() << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003111 return;
3112 }
3113
Chandler Carruth87c44602011-07-01 23:49:12 +00003114 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003115 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003116 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003117 return;
3118 }
3119
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003120 // In C++ the implicit 'this' function parameter also counts, and they are
3121 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003122 bool HasImplicitThisParam = isInstanceMethod(D);
3123 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003124 unsigned FirstIdx = 1;
3125
Chris Lattner5f9e2722011-07-23 10:55:15 +00003126 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003127
3128 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003129 if (Format.startswith("__") && Format.endswith("__"))
3130 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003132 // Check for supported formats.
3133 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003134
3135 if (Kind == IgnoredFormat)
3136 return;
3137
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003138 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003139 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003140 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141 return;
3142 }
3143
3144 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003145 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003146 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003147 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3148 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3150 << Attr.getName() << 2 << ArgumentIntegerConstant
3151 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003152 return;
3153 }
3154
3155 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003156 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003157 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003158 return;
3159 }
3160
3161 // FIXME: Do we need to bounds check?
3162 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003163
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003164 if (HasImplicitThisParam) {
3165 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003166 S.Diag(Attr.getLoc(),
3167 diag::err_format_attribute_implicit_this_format_string)
3168 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003169 return;
3170 }
3171 ArgIdx--;
3172 }
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Chris Lattner6b6b5372008-06-26 18:38:35 +00003174 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003175 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003176
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003177 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003178 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003179 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3180 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003181 return;
3182 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003183 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003184 // FIXME: do we need to check if the type is NSString*? What are the
3185 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003186 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003187 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003188 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3189 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003190 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003191 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003192 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003193 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003194 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3196 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197 return;
3198 }
3199
3200 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003201 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003202 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003203 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3204 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
3206 << Attr.getName() << 3 << ArgumentIntegerConstant
3207 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003208 return;
3209 }
3210
3211 // check if the function is variadic if the 3rd argument non-zero
3212 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003213 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003214 ++NumArgs; // +1 for ...
3215 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003216 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003217 return;
3218 }
3219 }
3220
Chris Lattner3c73c412008-11-19 08:23:25 +00003221 // strftime requires FirstArg to be 0 because it doesn't read from any
3222 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003223 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003224 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003225 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3226 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003227 return;
3228 }
3229 // if 0 it disables parameter checking (to use with e.g. va_list)
3230 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003232 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003233 return;
3234 }
3235
Rafael Espindola599f1b72012-05-13 03:25:18 +00003236 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3237 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003238 FirstArg.getZExtValue(),
3239 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003240 if (NewAttr)
3241 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242}
3243
Chandler Carruth1b03c872011-07-02 00:01:44 +00003244static void handleTransparentUnionAttr(Sema &S, Decl *D,
3245 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003246 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003247 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003248 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003249
Chris Lattner6b6b5372008-06-26 18:38:35 +00003250
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003251 // Try to find the underlying union declaration.
3252 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003253 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003254 if (TD && TD->getUnderlyingType()->isUnionType())
3255 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3256 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003257 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003258
3259 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003260 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003261 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003262 return;
3263 }
3264
John McCall5e1cdac2011-10-07 06:10:15 +00003265 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003266 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003267 diag::warn_transparent_union_attribute_not_definition);
3268 return;
3269 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003270
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003271 RecordDecl::field_iterator Field = RD->field_begin(),
3272 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003273 if (Field == FieldEnd) {
3274 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3275 return;
3276 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003277
David Blaikie581deb32012-06-06 20:45:41 +00003278 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003279 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003280 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003281 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003282 diag::warn_transparent_union_attribute_floating)
3283 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003284 return;
3285 }
3286
3287 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3288 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3289 for (; Field != FieldEnd; ++Field) {
3290 QualType FieldType = Field->getType();
3291 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3292 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3293 // Warn if we drop the attribute.
3294 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003295 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003296 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003297 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003298 diag::warn_transparent_union_attribute_field_size_align)
3299 << isSize << Field->getDeclName() << FieldBits;
3300 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003301 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003302 diag::note_transparent_union_first_field_size_align)
3303 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003304 return;
3305 }
3306 }
3307
Michael Han51d8c522013-01-24 16:46:58 +00003308 RD->addAttr(::new (S.Context)
3309 TransparentUnionAttr(Attr.getRange(), S.Context,
3310 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003311}
3312
Chandler Carruth1b03c872011-07-02 00:01:44 +00003313static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003314 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003315 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003316 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003317
Peter Collingbourne7a730022010-11-23 20:45:58 +00003318 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003319 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003320
Chris Lattner6b6b5372008-06-26 18:38:35 +00003321 // Make sure that there is a string literal as the annotation's single
3322 // argument.
3323 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003324 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003325 return;
3326 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003327
3328 // Don't duplicate annotations that are already set.
3329 for (specific_attr_iterator<AnnotateAttr>
3330 i = D->specific_attr_begin<AnnotateAttr>(),
3331 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3332 if ((*i)->getAnnotation() == SE->getString())
3333 return;
3334 }
Michael Han51d8c522013-01-24 16:46:58 +00003335
3336 D->addAttr(::new (S.Context)
3337 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3338 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003339}
3340
Chandler Carruth1b03c872011-07-02 00:01:44 +00003341static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003342 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003343 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3345 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003346 return;
3347 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003348
Richard Smithbe507b62013-02-01 08:12:08 +00003349 if (Attr.getNumArgs() == 0) {
3350 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3351 true, 0, Attr.getAttributeSpellingListIndex()));
3352 return;
3353 }
3354
Richard Smithf6565a92013-02-22 08:32:16 +00003355 Expr *E = Attr.getArg(0);
3356 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3357 S.Diag(Attr.getEllipsisLoc(),
3358 diag::err_pack_expansion_without_parameter_packs);
3359 return;
3360 }
3361
3362 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3363 return;
3364
3365 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3366 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003367}
3368
3369void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003370 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003371 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3372 SourceLocation AttrLoc = AttrRange.getBegin();
3373
Richard Smith4cd81c52013-01-29 09:02:09 +00003374 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003375 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003376 // C++11 [dcl.align]p1:
3377 // An alignment-specifier may be applied to a variable or to a class
3378 // data member, but it shall not be applied to a bit-field, a function
3379 // parameter, the formal parameter of a catch clause, or a variable
3380 // declared with the register storage class specifier. An
3381 // alignment-specifier may also be applied to the declaration of a class
3382 // or enumeration type.
3383 // C11 6.7.5/2:
3384 // An alignment attribute shall not be specified in a declaration of
3385 // a typedef, or a bit-field, or a function, or a parameter, or an
3386 // object declared with the register storage-class specifier.
3387 int DiagKind = -1;
3388 if (isa<ParmVarDecl>(D)) {
3389 DiagKind = 0;
3390 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3391 if (VD->getStorageClass() == SC_Register)
3392 DiagKind = 1;
3393 if (VD->isExceptionVariable())
3394 DiagKind = 2;
3395 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3396 if (FD->isBitField())
3397 DiagKind = 3;
3398 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003399 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3400 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003401 << (TmpAttr.isC11() ? ExpectedVariableOrField
3402 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003403 return;
3404 }
3405 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003406 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003407 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003408 return;
3409 }
3410 }
3411
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003412 if (E->isTypeDependent() || E->isValueDependent()) {
3413 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003414 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3415 AA->setPackExpansion(IsPackExpansion);
3416 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003417 return;
3418 }
Michael Hana31f65b2013-02-01 01:19:17 +00003419
Sean Huntcf807c42010-08-18 23:23:40 +00003420 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003421 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003422 ExprResult ICE
3423 = VerifyIntegerConstantExpression(E, &Alignment,
3424 diag::err_aligned_attribute_argument_not_int,
3425 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003426 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003427 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003428
3429 // C++11 [dcl.align]p2:
3430 // -- if the constant expression evaluates to zero, the alignment
3431 // specifier shall have no effect
3432 // C11 6.7.5p6:
3433 // An alignment specification of zero has no effect.
3434 if (!(TmpAttr.isAlignas() && !Alignment) &&
3435 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003436 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3437 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003438 return;
3439 }
Michael Hana31f65b2013-02-01 01:19:17 +00003440
Richard Smithbe507b62013-02-01 08:12:08 +00003441 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003442 // We've already verified it's a power of 2, now let's make sure it's
3443 // 8192 or less.
3444 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003445 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003446 << E->getSourceRange();
3447 return;
3448 }
3449 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003450
Richard Smithf6565a92013-02-22 08:32:16 +00003451 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3452 ICE.take(), SpellingListIndex);
3453 AA->setPackExpansion(IsPackExpansion);
3454 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003455}
3456
Michael Hana31f65b2013-02-01 01:19:17 +00003457void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003458 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003459 // FIXME: Cache the number on the Attr object if non-dependent?
3460 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003461 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3462 SpellingListIndex);
3463 AA->setPackExpansion(IsPackExpansion);
3464 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003465}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003466
Richard Smithbe507b62013-02-01 08:12:08 +00003467void Sema::CheckAlignasUnderalignment(Decl *D) {
3468 assert(D->hasAttrs() && "no attributes on decl");
3469
3470 QualType Ty;
3471 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3472 Ty = VD->getType();
3473 else
3474 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003475 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003476 return;
3477
3478 // C++11 [dcl.align]p5, C11 6.7.5/4:
3479 // The combined effect of all alignment attributes in a declaration shall
3480 // not specify an alignment that is less strict than the alignment that
3481 // would otherwise be required for the entity being declared.
3482 AlignedAttr *AlignasAttr = 0;
3483 unsigned Align = 0;
3484 for (specific_attr_iterator<AlignedAttr>
3485 I = D->specific_attr_begin<AlignedAttr>(),
3486 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3487 if (I->isAlignmentDependent())
3488 return;
3489 if (I->isAlignas())
3490 AlignasAttr = *I;
3491 Align = std::max(Align, I->getAlignment(Context));
3492 }
3493
3494 if (AlignasAttr && Align) {
3495 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3496 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3497 if (NaturalAlign > RequestedAlign)
3498 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3499 << Ty << (unsigned)NaturalAlign.getQuantity();
3500 }
3501}
3502
Chandler Carruthd309c812011-07-01 23:49:16 +00003503/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003504/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003505///
Mike Stumpbf916502009-07-24 19:02:52 +00003506/// Despite what would be logical, the mode attribute is a decl attribute, not a
3507/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3508/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003509static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003510 // This attribute isn't documented, but glibc uses it. It changes
3511 // the width of an int or unsigned int to the specified size.
3512
3513 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003514 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003515 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003516
Chris Lattnerfbf13472008-06-27 22:18:37 +00003517
3518 IdentifierInfo *Name = Attr.getParameterName();
3519 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003520 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003521 return;
3522 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003523
Chris Lattner5f9e2722011-07-23 10:55:15 +00003524 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003525
3526 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003527 if (Str.startswith("__") && Str.endswith("__"))
3528 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003529
3530 unsigned DestWidth = 0;
3531 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003532 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003533 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003534 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003535 switch (Str[0]) {
3536 case 'Q': DestWidth = 8; break;
3537 case 'H': DestWidth = 16; break;
3538 case 'S': DestWidth = 32; break;
3539 case 'D': DestWidth = 64; break;
3540 case 'X': DestWidth = 96; break;
3541 case 'T': DestWidth = 128; break;
3542 }
3543 if (Str[1] == 'F') {
3544 IntegerMode = false;
3545 } else if (Str[1] == 'C') {
3546 IntegerMode = false;
3547 ComplexMode = true;
3548 } else if (Str[1] != 'I') {
3549 DestWidth = 0;
3550 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003551 break;
3552 case 4:
3553 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3554 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003555 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003556 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003557 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003558 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003559 break;
3560 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003561 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003562 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003563 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003564 case 11:
3565 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003566 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003567 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003568 }
3569
3570 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003571 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003572 OldTy = TD->getUnderlyingType();
3573 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3574 OldTy = VD->getType();
3575 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003576 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003577 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003578 return;
3579 }
Eli Friedman73397492009-03-03 06:41:03 +00003580
John McCall183700f2009-09-21 23:43:11 +00003581 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003582 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3583 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003584 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003585 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3586 } else if (ComplexMode) {
3587 if (!OldTy->isComplexType())
3588 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3589 } else {
3590 if (!OldTy->isFloatingType())
3591 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3592 }
3593
Mike Stump390b4cc2009-05-16 07:39:55 +00003594 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3595 // and friends, at least with glibc.
3596 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3597 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003598 // FIXME: Make sure floating-point mappings are accurate
3599 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003600 QualType NewTy;
3601 switch (DestWidth) {
3602 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003603 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003604 return;
3605 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003606 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003607 return;
3608 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003609 if (!IntegerMode) {
3610 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3611 return;
3612 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003613 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003614 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003615 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003616 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003617 break;
3618 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003619 if (!IntegerMode) {
3620 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3621 return;
3622 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003623 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003624 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003625 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003626 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003627 break;
3628 case 32:
3629 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003630 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003631 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003632 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003633 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003634 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003635 break;
3636 case 64:
3637 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003638 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003639 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003640 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003641 NewTy = S.Context.LongTy;
3642 else
3643 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003644 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003645 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003646 NewTy = S.Context.UnsignedLongTy;
3647 else
3648 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003649 break;
Eli Friedman73397492009-03-03 06:41:03 +00003650 case 96:
3651 NewTy = S.Context.LongDoubleTy;
3652 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003653 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003654 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3655 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003656 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3657 return;
3658 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003659 if (IntegerMode) {
3660 if (OldTy->isSignedIntegerType())
3661 NewTy = S.Context.Int128Ty;
3662 else
3663 NewTy = S.Context.UnsignedInt128Ty;
3664 } else
3665 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003666 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003667 }
3668
Eli Friedman73397492009-03-03 06:41:03 +00003669 if (ComplexMode) {
3670 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003671 }
3672
3673 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003674 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3675 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3676 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003677 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003678
3679 D->addAttr(::new (S.Context)
3680 ModeAttr(Attr.getRange(), S.Context, Name,
3681 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003682}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003683
Chandler Carruth1b03c872011-07-02 00:01:44 +00003684static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003685 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003686 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003687 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003688
Nick Lewycky78d1a102012-07-24 01:40:49 +00003689 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3690 if (!VD->hasGlobalStorage())
3691 S.Diag(Attr.getLoc(),
3692 diag::warn_attribute_requires_functions_or_static_globals)
3693 << Attr.getName();
3694 } else if (!isFunctionOrMethod(D)) {
3695 S.Diag(Attr.getLoc(),
3696 diag::warn_attribute_requires_functions_or_static_globals)
3697 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003698 return;
3699 }
Mike Stumpbf916502009-07-24 19:02:52 +00003700
Michael Han51d8c522013-01-24 16:46:58 +00003701 D->addAttr(::new (S.Context)
3702 NoDebugAttr(Attr.getRange(), S.Context,
3703 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003704}
3705
Chandler Carruth1b03c872011-07-02 00:01:44 +00003706static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003707 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003708 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003709 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003710
Mike Stumpbf916502009-07-24 19:02:52 +00003711
Chandler Carruth87c44602011-07-01 23:49:12 +00003712 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003713 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003714 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003715 return;
3716 }
Mike Stumpbf916502009-07-24 19:02:52 +00003717
Michael Han51d8c522013-01-24 16:46:58 +00003718 D->addAttr(::new (S.Context)
3719 NoInlineAttr(Attr.getRange(), S.Context,
3720 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003721}
3722
Chandler Carruth1b03c872011-07-02 00:01:44 +00003723static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3724 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003725 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003726 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003727 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003728
Chris Lattner7255a2d2010-06-22 00:03:40 +00003729
Chandler Carruth87c44602011-07-01 23:49:12 +00003730 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003732 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003733 return;
3734 }
3735
Michael Han51d8c522013-01-24 16:46:58 +00003736 D->addAttr(::new (S.Context)
3737 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3738 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003739}
3740
Chandler Carruth1b03c872011-07-02 00:01:44 +00003741static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003742 if (S.LangOpts.CUDA) {
3743 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003744 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003745 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3746 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003747 return;
3748 }
3749
Chandler Carruth87c44602011-07-01 23:49:12 +00003750 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003752 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003753 return;
3754 }
3755
Michael Han51d8c522013-01-24 16:46:58 +00003756 D->addAttr(::new (S.Context)
3757 CUDAConstantAttr(Attr.getRange(), S.Context,
3758 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003759 } else {
3760 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3761 }
3762}
3763
Chandler Carruth1b03c872011-07-02 00:01:44 +00003764static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003765 if (S.LangOpts.CUDA) {
3766 // check the attribute arguments.
3767 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3769 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003770 return;
3771 }
3772
Chandler Carruth87c44602011-07-01 23:49:12 +00003773 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003774 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003775 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003776 return;
3777 }
3778
Michael Han51d8c522013-01-24 16:46:58 +00003779 D->addAttr(::new (S.Context)
3780 CUDADeviceAttr(Attr.getRange(), S.Context,
3781 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003782 } else {
3783 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3784 }
3785}
3786
Chandler Carruth1b03c872011-07-02 00:01:44 +00003787static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003788 if (S.LangOpts.CUDA) {
3789 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003790 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003791 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003792
Chandler Carruth87c44602011-07-01 23:49:12 +00003793 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003795 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003796 return;
3797 }
3798
Chandler Carruth87c44602011-07-01 23:49:12 +00003799 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003800 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003801 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003802 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003803 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3804 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003805 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003806 "void");
3807 } else {
3808 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3809 << FD->getType();
3810 }
3811 return;
3812 }
3813
Michael Han51d8c522013-01-24 16:46:58 +00003814 D->addAttr(::new (S.Context)
3815 CUDAGlobalAttr(Attr.getRange(), S.Context,
3816 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003817 } else {
3818 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3819 }
3820}
3821
Chandler Carruth1b03c872011-07-02 00:01:44 +00003822static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003823 if (S.LangOpts.CUDA) {
3824 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003825 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003826 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003827
Peter Collingbourneced76712010-12-01 03:15:31 +00003828
Chandler Carruth87c44602011-07-01 23:49:12 +00003829 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003830 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003831 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003832 return;
3833 }
3834
Michael Han51d8c522013-01-24 16:46:58 +00003835 D->addAttr(::new (S.Context)
3836 CUDAHostAttr(Attr.getRange(), S.Context,
3837 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003838 } else {
3839 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3840 }
3841}
3842
Chandler Carruth1b03c872011-07-02 00:01:44 +00003843static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003844 if (S.LangOpts.CUDA) {
3845 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003846 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003847 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003848
Chandler Carruth87c44602011-07-01 23:49:12 +00003849 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003850 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003851 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003852 return;
3853 }
3854
Michael Han51d8c522013-01-24 16:46:58 +00003855 D->addAttr(::new (S.Context)
3856 CUDASharedAttr(Attr.getRange(), S.Context,
3857 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003858 } else {
3859 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3860 }
3861}
3862
Chandler Carruth1b03c872011-07-02 00:01:44 +00003863static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003864 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003865 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003866 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003867
Chandler Carruth87c44602011-07-01 23:49:12 +00003868 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003869 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003870 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003871 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003872 return;
3873 }
Mike Stumpbf916502009-07-24 19:02:52 +00003874
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003875 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003876 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003877 return;
3878 }
Mike Stumpbf916502009-07-24 19:02:52 +00003879
Michael Han51d8c522013-01-24 16:46:58 +00003880 D->addAttr(::new (S.Context)
3881 GNUInlineAttr(Attr.getRange(), S.Context,
3882 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003883}
3884
Chandler Carruth1b03c872011-07-02 00:01:44 +00003885static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003886 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003887
Aaron Ballmanfff32482012-12-09 17:45:41 +00003888 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003889 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003890 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3891 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003892 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003893 return;
3894
Chandler Carruth87c44602011-07-01 23:49:12 +00003895 if (!isa<ObjCMethodDecl>(D)) {
3896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3897 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003898 return;
3899 }
3900
Chandler Carruth87c44602011-07-01 23:49:12 +00003901 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003902 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003903 D->addAttr(::new (S.Context)
3904 FastCallAttr(Attr.getRange(), S.Context,
3905 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003906 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003907 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003908 D->addAttr(::new (S.Context)
3909 StdCallAttr(Attr.getRange(), S.Context,
3910 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003911 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003912 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003913 D->addAttr(::new (S.Context)
3914 ThisCallAttr(Attr.getRange(), S.Context,
3915 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003916 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003917 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003918 D->addAttr(::new (S.Context)
3919 CDeclAttr(Attr.getRange(), S.Context,
3920 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003921 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003922 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003923 D->addAttr(::new (S.Context)
3924 PascalAttr(Attr.getRange(), S.Context,
3925 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003926 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003927 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003928 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003929 switch (CC) {
3930 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003931 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003932 break;
3933 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003934 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003935 break;
3936 default:
3937 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003938 }
3939
Michael Han51d8c522013-01-24 16:46:58 +00003940 D->addAttr(::new (S.Context)
3941 PcsAttr(Attr.getRange(), S.Context, PCS,
3942 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003943 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003944 }
Derek Schuff263366f2012-10-16 22:30:41 +00003945 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003946 D->addAttr(::new (S.Context)
3947 PnaclCallAttr(Attr.getRange(), S.Context,
3948 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003949 return;
Guy Benyei38980082012-12-25 08:53:55 +00003950 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003951 D->addAttr(::new (S.Context)
3952 IntelOclBiccAttr(Attr.getRange(), S.Context,
3953 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003954 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003955
Abramo Bagnarae215f722010-04-30 13:10:51 +00003956 default:
3957 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003958 }
3959}
3960
Chandler Carruth1b03c872011-07-02 00:01:44 +00003961static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003962 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003963 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003964}
3965
Guy Benyei1db70402013-03-24 13:58:12 +00003966static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
3967 assert(!Attr.isInvalid());
3968
3969 Expr *E = Attr.getArg(0);
3970 llvm::APSInt ArgNum(32);
3971 if (E->isTypeDependent() || E->isValueDependent() ||
3972 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
3973 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3974 << Attr.getName()->getName() << E->getSourceRange();
3975 return;
3976 }
3977
3978 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3979 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3980}
3981
Aaron Ballmanfff32482012-12-09 17:45:41 +00003982bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3983 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003984 if (attr.isInvalid())
3985 return true;
3986
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003987 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3988 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003989 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3990 << attr.getName() << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003991 attr.setInvalid();
3992 return true;
3993 }
3994
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003995 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3996 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003997 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003998 case AttributeList::AT_CDecl: CC = CC_C; break;
3999 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4000 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4001 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4002 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4003 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004004 Expr *Arg = attr.getArg(0);
4005 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004006 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004007 Diag(attr.getLoc(), diag::err_attribute_argument_n_type)
4008 << attr.getName() << 1 << ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004009 attr.setInvalid();
4010 return true;
4011 }
4012
Chris Lattner5f9e2722011-07-23 10:55:15 +00004013 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004014 if (StrRef == "aapcs") {
4015 CC = CC_AAPCS;
4016 break;
4017 } else if (StrRef == "aapcs-vfp") {
4018 CC = CC_AAPCS_VFP;
4019 break;
4020 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004021
4022 attr.setInvalid();
4023 Diag(attr.getLoc(), diag::err_invalid_pcs);
4024 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004025 }
Derek Schuff263366f2012-10-16 22:30:41 +00004026 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004027 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004028 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004029 }
4030
Aaron Ballman82bfa192012-10-02 14:26:08 +00004031 const TargetInfo &TI = Context.getTargetInfo();
4032 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4033 if (A == TargetInfo::CCCR_Warning) {
4034 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004035
4036 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4037 if (FD)
4038 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4039 TargetInfo::CCMT_NonMember;
4040 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004041 }
4042
John McCall711c52b2011-01-05 12:14:39 +00004043 return false;
4044}
4045
Chandler Carruth1b03c872011-07-02 00:01:44 +00004046static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004047 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004048
4049 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004050 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004051 return;
4052
Chandler Carruth87c44602011-07-01 23:49:12 +00004053 if (!isa<ObjCMethodDecl>(D)) {
4054 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4055 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004056 return;
4057 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004058
Michael Han51d8c522013-01-24 16:46:58 +00004059 D->addAttr(::new (S.Context)
4060 RegparmAttr(Attr.getRange(), S.Context, numParams,
4061 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004062}
4063
4064/// Checks a regparm attribute, returning true if it is ill-formed and
4065/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004066bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4067 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004068 return true;
4069
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004070 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004071 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004072 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004073 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004074
Chandler Carruth87c44602011-07-01 23:49:12 +00004075 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004076 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004077 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004078 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004079 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004080 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004081 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004082 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004083 }
4084
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004085 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004086 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004087 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004088 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004089 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004090 }
4091
John McCall711c52b2011-01-05 12:14:39 +00004092 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004093 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004094 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004095 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004096 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004097 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004098 }
4099
John McCall711c52b2011-01-05 12:14:39 +00004100 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004101}
4102
Chandler Carruth1b03c872011-07-02 00:01:44 +00004103static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004104 if (S.LangOpts.CUDA) {
4105 // check the attribute arguments.
4106 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004107 // FIXME: 0 is not okay.
4108 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004109 return;
4110 }
4111
Chandler Carruth87c44602011-07-01 23:49:12 +00004112 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004113 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004114 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004115 return;
4116 }
4117
4118 Expr *MaxThreadsExpr = Attr.getArg(0);
4119 llvm::APSInt MaxThreads(32);
4120 if (MaxThreadsExpr->isTypeDependent() ||
4121 MaxThreadsExpr->isValueDependent() ||
4122 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4124 << Attr.getName() << 1 << ArgumentIntegerConstant
4125 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004126 return;
4127 }
4128
4129 llvm::APSInt MinBlocks(32);
4130 if (Attr.getNumArgs() > 1) {
4131 Expr *MinBlocksExpr = Attr.getArg(1);
4132 if (MinBlocksExpr->isTypeDependent() ||
4133 MinBlocksExpr->isValueDependent() ||
4134 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4136 << Attr.getName() << 2 << ArgumentIntegerConstant
4137 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004138 return;
4139 }
4140 }
4141
Michael Han51d8c522013-01-24 16:46:58 +00004142 D->addAttr(::new (S.Context)
4143 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4144 MaxThreads.getZExtValue(),
4145 MinBlocks.getZExtValue(),
4146 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004147 } else {
4148 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4149 }
4150}
4151
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004152static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4153 const AttributeList &Attr) {
4154 StringRef AttrName = Attr.getName()->getName();
4155 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004156 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4157 << Attr.getName() << /* arg num = */ 1 << ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004158 return;
4159 }
4160
4161 if (Attr.getNumArgs() != 2) {
4162 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004163 << Attr.getName() << /* required args = */ 3;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004164 return;
4165 }
4166
4167 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4168
4169 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4171 << Attr.getName() << ExpectedFunctionOrMethod;
4172 return;
4173 }
4174
4175 uint64_t ArgumentIdx;
4176 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4177 Attr.getLoc(), 2,
4178 Attr.getArg(0), ArgumentIdx))
4179 return;
4180
4181 uint64_t TypeTagIdx;
4182 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4183 Attr.getLoc(), 3,
4184 Attr.getArg(1), TypeTagIdx))
4185 return;
4186
4187 bool IsPointer = (AttrName == "pointer_with_type_tag");
4188 if (IsPointer) {
4189 // Ensure that buffer has a pointer type.
4190 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4191 if (!BufferTy->isPointerType()) {
4192 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004193 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004194 }
4195 }
4196
Michael Han51d8c522013-01-24 16:46:58 +00004197 D->addAttr(::new (S.Context)
4198 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4199 ArgumentIdx, TypeTagIdx, IsPointer,
4200 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004201}
4202
4203static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4204 const AttributeList &Attr) {
4205 IdentifierInfo *PointerKind = Attr.getParameterName();
4206 if (!PointerKind) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4208 << Attr.getName() << 1 << ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004209 return;
4210 }
4211
4212 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4213
Michael Han51d8c522013-01-24 16:46:58 +00004214 D->addAttr(::new (S.Context)
4215 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4216 MatchingCType,
4217 Attr.getLayoutCompatible(),
4218 Attr.getMustBeNull(),
4219 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004220}
4221
Chris Lattner0744e5f2008-06-29 00:23:49 +00004222//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004223// Checker-specific attribute handlers.
4224//===----------------------------------------------------------------------===//
4225
John McCallc7ad3812011-01-25 03:31:58 +00004226static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004227 return type->isDependentType() ||
4228 type->isObjCObjectPointerType() ||
4229 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004230}
4231static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004232 return type->isDependentType() ||
4233 type->isPointerType() ||
4234 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004235}
4236
Chandler Carruth1b03c872011-07-02 00:01:44 +00004237static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004238 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004239 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004240 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004241 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004242 return;
4243 }
4244
4245 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004246 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004247 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4248 cf = false;
4249 } else {
4250 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4251 cf = true;
4252 }
4253
4254 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004255 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004256 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004257 return;
4258 }
4259
4260 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004261 param->addAttr(::new (S.Context)
4262 CFConsumedAttr(Attr.getRange(), S.Context,
4263 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004264 else
Michael Han51d8c522013-01-24 16:46:58 +00004265 param->addAttr(::new (S.Context)
4266 NSConsumedAttr(Attr.getRange(), S.Context,
4267 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004268}
4269
Chandler Carruth1b03c872011-07-02 00:01:44 +00004270static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4271 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004272 if (!isa<ObjCMethodDecl>(D)) {
4273 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004274 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004275 return;
4276 }
4277
Michael Han51d8c522013-01-24 16:46:58 +00004278 D->addAttr(::new (S.Context)
4279 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4280 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004281}
4282
Chandler Carruth1b03c872011-07-02 00:01:44 +00004283static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4284 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004285
John McCallc7ad3812011-01-25 03:31:58 +00004286 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004287
Chandler Carruth87c44602011-07-01 23:49:12 +00004288 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004289 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004290 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004291 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004292 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004293 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4294 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004295 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004296 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004297 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004298 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004299 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004300 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004301 return;
4302 }
Mike Stumpbf916502009-07-24 19:02:52 +00004303
John McCallc7ad3812011-01-25 03:31:58 +00004304 bool typeOK;
4305 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004306 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004307 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004308 case AttributeList::AT_NSReturnsAutoreleased:
4309 case AttributeList::AT_NSReturnsRetained:
4310 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004311 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4312 cf = false;
4313 break;
4314
Sean Hunt8e083e72012-06-19 23:57:03 +00004315 case AttributeList::AT_CFReturnsRetained:
4316 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004317 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4318 cf = true;
4319 break;
4320 }
4321
4322 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004323 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004324 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004325 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004326 }
Mike Stumpbf916502009-07-24 19:02:52 +00004327
Chandler Carruth87c44602011-07-01 23:49:12 +00004328 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004329 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004330 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004331 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004332 D->addAttr(::new (S.Context)
4333 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4334 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004335 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004336 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004337 D->addAttr(::new (S.Context)
4338 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4339 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004340 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004341 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004342 D->addAttr(::new (S.Context)
4343 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4344 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004345 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004346 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004347 D->addAttr(::new (S.Context)
4348 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4349 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004350 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004351 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004352 D->addAttr(::new (S.Context)
4353 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4354 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004355 return;
4356 };
4357}
4358
John McCalldc7c5ad2011-07-22 08:53:00 +00004359static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4360 const AttributeList &attr) {
4361 SourceLocation loc = attr.getLoc();
4362
4363 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4364
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004365 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004366 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004367 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004368 return;
4369 }
4370
4371 // Check that the method returns a normal pointer.
4372 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004373
4374 if (!resultType->isReferenceType() &&
4375 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004376 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4377 << SourceRange(loc)
4378 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4379
4380 // Drop the attribute.
4381 return;
4382 }
4383
Michael Han51d8c522013-01-24 16:46:58 +00004384 method->addAttr(::new (S.Context)
4385 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4386 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004387}
4388
Fariborz Jahanian84101132012-09-07 23:46:23 +00004389static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4390 const AttributeList &attr) {
4391 SourceLocation loc = attr.getLoc();
4392 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4393
4394 if (!method) {
4395 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4396 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4397 return;
4398 }
4399 DeclContext *DC = method->getDeclContext();
4400 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4401 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4402 << attr.getName() << 0;
4403 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4404 return;
4405 }
4406 if (method->getMethodFamily() == OMF_dealloc) {
4407 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4408 << attr.getName() << 1;
4409 return;
4410 }
4411
Michael Han51d8c522013-01-24 16:46:58 +00004412 method->addAttr(::new (S.Context)
4413 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4414 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004415}
4416
John McCall8dfac0b2011-09-30 05:12:12 +00004417/// Handle cf_audited_transfer and cf_unknown_transfer.
4418static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4419 if (!isa<FunctionDecl>(D)) {
4420 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004421 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004422 return;
4423 }
4424
Sean Hunt8e083e72012-06-19 23:57:03 +00004425 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004426
4427 // Check whether there's a conflicting attribute already present.
4428 Attr *Existing;
4429 if (IsAudited) {
4430 Existing = D->getAttr<CFUnknownTransferAttr>();
4431 } else {
4432 Existing = D->getAttr<CFAuditedTransferAttr>();
4433 }
4434 if (Existing) {
4435 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4436 << A.getName()
4437 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4438 << A.getRange() << Existing->getRange();
4439 return;
4440 }
4441
4442 // All clear; add the attribute.
4443 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004444 D->addAttr(::new (S.Context)
4445 CFAuditedTransferAttr(A.getRange(), S.Context,
4446 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004447 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004448 D->addAttr(::new (S.Context)
4449 CFUnknownTransferAttr(A.getRange(), S.Context,
4450 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004451 }
4452}
4453
John McCallfe98da02011-09-29 07:17:38 +00004454static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4455 const AttributeList &Attr) {
4456 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4457 if (!RD || RD->isUnion()) {
4458 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004459 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004460 }
4461
4462 IdentifierInfo *ParmName = Attr.getParameterName();
4463
4464 // In Objective-C, verify that the type names an Objective-C type.
4465 // We don't want to check this outside of ObjC because people sometimes
4466 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004467 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004468 // Check for an existing type with this name.
4469 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4470 Sema::LookupOrdinaryName);
4471 if (S.LookupName(R, Sc)) {
4472 NamedDecl *Target = R.getFoundDecl();
4473 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4474 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4475 S.Diag(Target->getLocStart(), diag::note_declared_at);
4476 }
4477 }
4478 }
4479
Michael Han51d8c522013-01-24 16:46:58 +00004480 D->addAttr(::new (S.Context)
4481 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4482 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004483}
4484
Chandler Carruth1b03c872011-07-02 00:01:44 +00004485static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4486 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004487 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004488
Chandler Carruth87c44602011-07-01 23:49:12 +00004489 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004490 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004491}
4492
Chandler Carruth1b03c872011-07-02 00:01:44 +00004493static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4494 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004495 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004496 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004497 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004498 return;
4499 }
4500
Chandler Carruth87c44602011-07-01 23:49:12 +00004501 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004502 QualType type = vd->getType();
4503
4504 if (!type->isDependentType() &&
4505 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004506 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004507 << type;
4508 return;
4509 }
4510
4511 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4512
4513 // If we have no lifetime yet, check the lifetime we're presumably
4514 // going to infer.
4515 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4516 lifetime = type->getObjCARCImplicitLifetime();
4517
4518 switch (lifetime) {
4519 case Qualifiers::OCL_None:
4520 assert(type->isDependentType() &&
4521 "didn't infer lifetime for non-dependent type?");
4522 break;
4523
4524 case Qualifiers::OCL_Weak: // meaningful
4525 case Qualifiers::OCL_Strong: // meaningful
4526 break;
4527
4528 case Qualifiers::OCL_ExplicitNone:
4529 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004530 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004531 << (lifetime == Qualifiers::OCL_Autoreleasing);
4532 break;
4533 }
4534
Chandler Carruth87c44602011-07-01 23:49:12 +00004535 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004536 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4537 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004538}
4539
Francois Pichet11542142010-12-19 06:50:37 +00004540//===----------------------------------------------------------------------===//
4541// Microsoft specific attribute handlers.
4542//===----------------------------------------------------------------------===//
4543
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004544// Check if MS extensions or some other language extensions are enabled. If
4545// not, issue a diagnostic that the given attribute is unused.
4546static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4547 bool OtherExtension = false) {
4548 if (S.LangOpts.MicrosoftExt || OtherExtension)
4549 return true;
4550 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4551 return false;
4552}
4553
Chandler Carruth1b03c872011-07-02 00:01:44 +00004554static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004555 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4556 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004557
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004558 // check the attribute arguments.
4559 if (!checkAttributeNumArgs(S, Attr, 1))
4560 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004561
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004562 Expr *Arg = Attr.getArg(0);
4563 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4564 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
4566 << Attr.getName() << 1 << ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004567 return;
4568 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004569
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004570 StringRef StrRef = Str->getString();
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004571
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004572 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4573 StrRef.back() == '}';
Francois Pichetd3d3be92010-12-20 01:41:49 +00004574
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004575 // Validate GUID length.
4576 if (IsCurly && StrRef.size() != 38) {
4577 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4578 return;
4579 }
4580 if (!IsCurly && StrRef.size() != 36) {
4581 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4582 return;
4583 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004584
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004585 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4586 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
4587 StringRef::iterator I = StrRef.begin();
4588 if (IsCurly) // Skip the optional '{'
4589 ++I;
4590
4591 for (int i = 0; i < 36; ++i) {
4592 if (i == 8 || i == 13 || i == 18 || i == 23) {
4593 if (*I != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004594 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4595 return;
4596 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004597 } else if (!isHexDigit(*I)) {
4598 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4599 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004600 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004601 I++;
4602 }
Francois Pichet11542142010-12-19 06:50:37 +00004603
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004604 D->addAttr(::new (S.Context)
4605 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4606 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004607}
4608
John McCallc052dbb2012-05-22 21:28:12 +00004609static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004610 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004611 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004612
4613 AttributeList::Kind Kind = Attr.getKind();
4614 if (Kind == AttributeList::AT_SingleInheritance)
4615 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004616 ::new (S.Context)
4617 SingleInheritanceAttr(Attr.getRange(), S.Context,
4618 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004619 else if (Kind == AttributeList::AT_MultipleInheritance)
4620 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004621 ::new (S.Context)
4622 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4623 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004624 else if (Kind == AttributeList::AT_VirtualInheritance)
4625 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004626 ::new (S.Context)
4627 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4628 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004629}
4630
4631static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004632 if (!checkMicrosoftExt(S, Attr))
4633 return;
4634
4635 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004636 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004637 D->addAttr(
4638 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4639 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004640}
4641
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004642static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004643 if (!checkMicrosoftExt(S, Attr))
4644 return;
4645 D->addAttr(::new (S.Context)
4646 ForceInlineAttr(Attr.getRange(), S.Context,
4647 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004648}
4649
Reid Klecknera7225342013-05-20 14:02:37 +00004650static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4651 if (!checkMicrosoftExt(S, Attr))
4652 return;
4653 // Check linkage after possibly merging declaratinos. See
4654 // checkAttributesAfterMerging().
4655 D->addAttr(::new (S.Context)
4656 SelectAnyAttr(Attr.getRange(), S.Context,
4657 Attr.getAttributeSpellingListIndex()));
4658}
4659
Ted Kremenekb71368d2009-05-09 02:44:38 +00004660//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004661// Top Level Sema Entry Points
4662//===----------------------------------------------------------------------===//
4663
Chandler Carruth1b03c872011-07-02 00:01:44 +00004664static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4665 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004666 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004667 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4668 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4669 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004670 default:
4671 break;
4672 }
4673}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004674
Chandler Carruth1b03c872011-07-02 00:01:44 +00004675static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4676 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004677 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004678 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4679 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4680 case AttributeList::AT_IBOutletCollection:
4681 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004683 case AttributeList::AT_ObjCGC:
4684 case AttributeList::AT_VectorSize:
4685 case AttributeList::AT_NeonVectorType:
4686 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004687 case AttributeList::AT_Ptr32:
4688 case AttributeList::AT_Ptr64:
4689 case AttributeList::AT_SPtr:
4690 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004691 // Ignore these, these are type attributes, handled by
4692 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004693 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004694 case AttributeList::AT_CUDADevice:
4695 case AttributeList::AT_CUDAHost:
4696 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004697 // Ignore, this is a non-inheritable attribute, handled
4698 // by ProcessNonInheritableDeclAttr.
4699 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004700 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4701 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4702 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4703 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004704 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004705 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004706 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004707 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004708 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4709 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4710 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004711 handleDependencyAttr(S, scope, D, Attr);
4712 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004713 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4714 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4715 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004716 case AttributeList::AT_CXX11NoReturn:
4717 handleCXX11NoReturnAttr(S, D, Attr);
4718 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004720 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004721 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004722 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4723 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004724 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004725 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004726 case AttributeList::AT_MinSize:
4727 handleMinSizeAttr(S, D, Attr);
4728 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004729 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4730 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4731 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4732 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4733 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004734 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004735 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004736 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4737 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4738 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4739 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4740 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004741 case AttributeList::AT_ownership_returns:
4742 case AttributeList::AT_ownership_takes:
4743 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004744 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4746 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4747 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4748 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4749 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4750 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4751 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004752
Sean Hunt8e083e72012-06-19 23:57:03 +00004753 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004754 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004755 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004756 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004757
Sean Hunt8e083e72012-06-19 23:57:03 +00004758 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004759 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4760
Fariborz Jahanian84101132012-09-07 23:46:23 +00004761 case AttributeList::AT_ObjCRequiresSuper:
4762 handleObjCRequiresSuperAttr(S, D, Attr); break;
4763
Sean Hunt8e083e72012-06-19 23:57:03 +00004764 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004765 handleNSBridgedAttr(S, scope, D, Attr); break;
4766
Sean Hunt8e083e72012-06-19 23:57:03 +00004767 case AttributeList::AT_CFAuditedTransfer:
4768 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004769 handleCFTransferAttr(S, D, Attr); break;
4770
Ted Kremenekb71368d2009-05-09 02:44:38 +00004771 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004772 case AttributeList::AT_CFConsumed:
4773 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4774 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004775 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004776
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_NSReturnsAutoreleased:
4778 case AttributeList::AT_NSReturnsNotRetained:
4779 case AttributeList::AT_CFReturnsNotRetained:
4780 case AttributeList::AT_NSReturnsRetained:
4781 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004782 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004783
Tanya Lattner0df579e2012-07-09 22:06:01 +00004784 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004785 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004786 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004787
Joey Gouly37453b92013-03-08 09:42:32 +00004788 case AttributeList::AT_VecTypeHint:
4789 handleVecTypeHint(S, D, Attr); break;
4790
Joey Gouly96cead52013-03-14 09:54:43 +00004791 case AttributeList::AT_Endian:
4792 handleEndianAttr(S, D, Attr);
4793 break;
4794
Sean Hunt8e083e72012-06-19 23:57:03 +00004795 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004796 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004797
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4799 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4800 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004801 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004802 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004803 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004804 handleArcWeakrefUnavailableAttr (S, D, Attr);
4805 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004806 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004807 handleObjCRootClassAttr(S, D, Attr);
4808 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004809 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004810 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004811 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004812 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4813 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004814 handleReturnsTwiceAttr(S, D, Attr);
4815 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004817 case AttributeList::AT_Visibility:
4818 handleVisibilityAttr(S, D, Attr, false);
4819 break;
4820 case AttributeList::AT_TypeVisibility:
4821 handleVisibilityAttr(S, D, Attr, true);
4822 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004823 case AttributeList::AT_WarnUnused:
4824 handleWarnUnusedAttr(S, D, Attr);
4825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004827 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004828 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4829 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4830 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4831 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004832 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004833 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004834 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004835 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004836 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004837 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004838 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004839 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004840 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4841 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4842 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4843 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4844 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4845 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4846 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4847 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4848 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004849 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004850 // Just ignore
4851 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004852 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004853 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004854 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004855 case AttributeList::AT_StdCall:
4856 case AttributeList::AT_CDecl:
4857 case AttributeList::AT_FastCall:
4858 case AttributeList::AT_ThisCall:
4859 case AttributeList::AT_Pascal:
4860 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004861 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004862 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004863 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004864 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004865 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004866 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004867 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004868 case AttributeList::AT_OpenCLImageAccess:
4869 handleOpenCLImageAccessAttr(S, D, Attr);
4870 break;
John McCallc052dbb2012-05-22 21:28:12 +00004871
4872 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004873 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004875 handleMsStructAttr(S, D, Attr);
4876 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004877 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004878 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004879 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004880 case AttributeList::AT_SingleInheritance:
4881 case AttributeList::AT_MultipleInheritance:
4882 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004883 handleInheritanceAttr(S, D, Attr);
4884 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004885 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004886 handlePortabilityAttr(S, D, Attr);
4887 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004888 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004889 handleForceInlineAttr(S, D, Attr);
4890 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004891 case AttributeList::AT_SelectAny:
4892 handleSelectAnyAttr(S, D, Attr);
4893 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004894
4895 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004896 case AttributeList::AT_AssertExclusiveLock:
4897 handleAssertExclusiveLockAttr(S, D, Attr);
4898 break;
4899 case AttributeList::AT_AssertSharedLock:
4900 handleAssertSharedLockAttr(S, D, Attr);
4901 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004902 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004903 handleGuardedVarAttr(S, D, Attr);
4904 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004905 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004906 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004907 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004908 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004909 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004910 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004911 case AttributeList::AT_NoSanitizeAddress:
4912 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004913 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004914 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004915 handleNoThreadSafetyAnalysis(S, D, Attr);
4916 break;
4917 case AttributeList::AT_NoSanitizeThread:
4918 handleNoSanitizeThread(S, D, Attr);
4919 break;
4920 case AttributeList::AT_NoSanitizeMemory:
4921 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004922 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004923 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004924 handleLockableAttr(S, D, Attr);
4925 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004926 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004927 handleGuardedByAttr(S, D, Attr);
4928 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004929 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004930 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004931 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004932 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004933 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004934 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004935 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004936 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004937 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004938 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004939 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004940 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004941 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004942 handleLockReturnedAttr(S, D, Attr);
4943 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004944 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004945 handleLocksExcludedAttr(S, D, Attr);
4946 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004947 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004948 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004949 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004950 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004951 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004952 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004953 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004954 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004955 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004956 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004957 handleUnlockFunAttr(S, D, Attr);
4958 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004959 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004960 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004961 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004962 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004963 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004964 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004965
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004966 // Type safety attributes.
4967 case AttributeList::AT_ArgumentWithTypeTag:
4968 handleArgumentWithTypeTagAttr(S, D, Attr);
4969 break;
4970 case AttributeList::AT_TypeTagForDatatype:
4971 handleTypeTagForDatatypeAttr(S, D, Attr);
4972 break;
4973
Chris Lattner803d0802008-06-29 00:43:07 +00004974 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004975 // Ask target about the attribute.
4976 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4977 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004978 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4979 diag::warn_unhandled_ms_attribute_ignored :
4980 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004981 break;
4982 }
4983}
4984
Peter Collingbourne60700392011-01-21 02:08:45 +00004985/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4986/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00004987/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004988static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4989 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00004990 bool NonInheritable, bool Inheritable,
4991 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004992 if (Attr.isInvalid())
4993 return;
4994
Richard Smithcd8ab512013-01-17 01:30:42 +00004995 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4996 // instead.
4997 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4998 return;
4999
Peter Collingbourne60700392011-01-21 02:08:45 +00005000 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005001 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005002
5003 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00005004 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00005005}
5006
Chris Lattner803d0802008-06-29 00:43:07 +00005007/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5008/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005009void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005010 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005011 bool NonInheritable, bool Inheritable,
5012 bool IncludeCXX11Attributes) {
5013 for (const AttributeList* l = AttrList; l; l = l->getNext())
5014 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5015 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005016
5017 // GCC accepts
5018 // static int a9 __attribute__((weakref));
5019 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005020 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005021 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005022 cast<NamedDecl>(D)->getNameAsString();
5023 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005024 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005025 }
5026}
5027
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005028// Annotation attributes are the only attributes allowed after an access
5029// specifier.
5030bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5031 const AttributeList *AttrList) {
5032 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005033 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005034 handleAnnotateAttr(*this, ASDecl, *l);
5035 } else {
5036 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5037 return true;
5038 }
5039 }
5040
5041 return false;
5042}
5043
John McCalle82247a2011-10-01 05:17:03 +00005044/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5045/// contains any decl attributes that we should warn about.
5046static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5047 for ( ; A; A = A->getNext()) {
5048 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005049 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005050 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5051
5052 if (A->getKind() == AttributeList::UnknownAttribute) {
5053 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5054 << A->getName() << A->getRange();
5055 } else {
5056 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5057 << A->getName() << A->getRange();
5058 }
5059 }
5060}
5061
5062/// checkUnusedDeclAttributes - Given a declarator which is not being
5063/// used to build a declaration, complain about any decl attributes
5064/// which might be lying around on it.
5065void Sema::checkUnusedDeclAttributes(Declarator &D) {
5066 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5067 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5068 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5069 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5070}
5071
Ryan Flynne25ff832009-07-30 03:15:39 +00005072/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005073/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005074NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5075 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005076 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005077 NamedDecl *NewD = 0;
5078 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005079 FunctionDecl *NewFD;
5080 // FIXME: Missing call to CheckFunctionDeclaration().
5081 // FIXME: Mangling?
5082 // FIXME: Is the qualifier info correct?
5083 // FIXME: Is the DeclContext correct?
5084 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5085 Loc, Loc, DeclarationName(II),
5086 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005087 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005088 FD->hasPrototype(),
5089 false/*isConstexprSpecified*/);
5090 NewD = NewFD;
5091
5092 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005093 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005094
5095 // Fake up parameter variables; they are declared as if this were
5096 // a typedef.
5097 QualType FDTy = FD->getType();
5098 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5099 SmallVector<ParmVarDecl*, 16> Params;
5100 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5101 AE = FT->arg_type_end(); AI != AE; ++AI) {
5102 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5103 Param->setScopeInfo(0, Params.size());
5104 Params.push_back(Param);
5105 }
David Blaikie4278c652011-09-21 18:16:56 +00005106 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005107 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005108 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5109 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005110 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005111 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005112 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005113 if (VD->getQualifier()) {
5114 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005115 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005116 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005117 }
5118 return NewD;
5119}
5120
James Dennett1dfbd922012-06-14 21:40:34 +00005121/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005122/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005123void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005124 if (W.getUsed()) return; // only do this once
5125 W.setUsed(true);
5126 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5127 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005128 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005129 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5130 NDId->getName()));
5131 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005132 WeakTopLevelDecl.push_back(NewD);
5133 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5134 // to insert Decl at TU scope, sorry.
5135 DeclContext *SavedContext = CurContext;
5136 CurContext = Context.getTranslationUnitDecl();
5137 PushOnScopeChains(NewD, S);
5138 CurContext = SavedContext;
5139 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005140 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005141 }
5142}
5143
Rafael Espindola65611bf2013-03-02 21:41:48 +00005144void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5145 // It's valid to "forward-declare" #pragma weak, in which case we
5146 // have to do this.
5147 LoadExternalWeakUndeclaredIdentifiers();
5148 if (!WeakUndeclaredIdentifiers.empty()) {
5149 NamedDecl *ND = NULL;
5150 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5151 if (VD->isExternC())
5152 ND = VD;
5153 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5154 if (FD->isExternC())
5155 ND = FD;
5156 if (ND) {
5157 if (IdentifierInfo *Id = ND->getIdentifier()) {
5158 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5159 = WeakUndeclaredIdentifiers.find(Id);
5160 if (I != WeakUndeclaredIdentifiers.end()) {
5161 WeakInfo W = I->second;
5162 DeclApplyPragmaWeak(S, ND, W);
5163 WeakUndeclaredIdentifiers[Id] = W;
5164 }
5165 }
5166 }
5167 }
5168}
5169
Chris Lattner0744e5f2008-06-29 00:23:49 +00005170/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5171/// it, apply them to D. This is a bit tricky because PD can have attributes
5172/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005173void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5174 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005175 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005176 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005177 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005178
Chris Lattner0744e5f2008-06-29 00:23:49 +00005179 // Walk the declarator structure, applying decl attributes that were in a type
5180 // position to the decl itself. This handles cases like:
5181 // int *__attr__(x)** D;
5182 // when X is a decl attribute.
5183 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5184 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005185 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5186 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005187
Chris Lattner0744e5f2008-06-29 00:23:49 +00005188 // Finally, apply any attributes on the decl itself.
5189 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005190 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005191}
John McCall54abf7d2009-11-04 02:18:39 +00005192
John McCallf85e1932011-06-15 23:02:42 +00005193/// Is the given declaration allowed to use a forbidden type?
5194static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5195 // Private ivars are always okay. Unfortunately, people don't
5196 // always properly make their ivars private, even in system headers.
5197 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005198 // Function declarations in sys headers will be marked unavailable.
5199 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5200 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005201 return false;
5202
5203 // Require it to be declared in a system header.
5204 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5205}
5206
5207/// Handle a delayed forbidden-type diagnostic.
5208static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5209 Decl *decl) {
5210 if (decl && isForbiddenTypeAllowed(S, decl)) {
5211 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5212 "this system declaration uses an unsupported type"));
5213 return;
5214 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005215 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005216 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005217 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005218 // kind of forbidden type messages on unavailable functions.
5219 if (FD->hasAttr<UnavailableAttr>() &&
5220 diag.getForbiddenTypeDiagnostic() ==
5221 diag::err_arc_array_param_no_ownership) {
5222 diag.Triggered = true;
5223 return;
5224 }
5225 }
John McCallf85e1932011-06-15 23:02:42 +00005226
5227 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5228 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5229 diag.Triggered = true;
5230}
5231
John McCall92576642012-05-07 06:16:41 +00005232void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5233 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005234 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005235 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005236
John McCall92576642012-05-07 06:16:41 +00005237 // When delaying diagnostics to run in the context of a parsed
5238 // declaration, we only want to actually emit anything if parsing
5239 // succeeds.
5240 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005241
John McCall92576642012-05-07 06:16:41 +00005242 // We emit all the active diagnostics in this pool or any of its
5243 // parents. In general, we'll get one pool for the decl spec
5244 // and a child pool for each declarator; in a decl group like:
5245 // deprecated_typedef foo, *bar, baz();
5246 // only the declarator pops will be passed decls. This is correct;
5247 // we really do need to consider delayed diagnostics from the decl spec
5248 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005249 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005250 do {
John McCall13489672012-05-07 06:16:58 +00005251 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005252 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5253 // This const_cast is a bit lame. Really, Triggered should be mutable.
5254 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005255 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005256 continue;
5257
John McCalleee1d542011-02-14 07:13:47 +00005258 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005259 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005260 // Don't bother giving deprecation diagnostics if the decl is invalid.
5261 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005262 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005263 break;
5264
5265 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005266 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005267 break;
John McCallf85e1932011-06-15 23:02:42 +00005268
5269 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005270 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005271 break;
John McCall2f514482010-01-27 03:50:35 +00005272 }
5273 }
John McCall92576642012-05-07 06:16:41 +00005274 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005275}
5276
John McCall13489672012-05-07 06:16:58 +00005277/// Given a set of delayed diagnostics, re-emit them as if they had
5278/// been delayed in the current context instead of in the given pool.
5279/// Essentially, this just moves them to the current pool.
5280void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5281 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5282 assert(curPool && "re-emitting in undelayed context not supported");
5283 curPool->steal(pool);
5284}
5285
John McCall54abf7d2009-11-04 02:18:39 +00005286static bool isDeclDeprecated(Decl *D) {
5287 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005288 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005289 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005290 // A category implicitly has the availability of the interface.
5291 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5292 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005293 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5294 return false;
5295}
5296
Eli Friedmanc3b23082012-08-08 21:52:41 +00005297static void
5298DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5299 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005300 const ObjCInterfaceDecl *UnknownObjCClass,
5301 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005302 DeclarationName Name = D->getDeclName();
5303 if (!Message.empty()) {
5304 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5305 S.Diag(D->getLocation(),
5306 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5307 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005308 if (ObjCPropery)
5309 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5310 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005311 } else if (!UnknownObjCClass) {
5312 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5313 S.Diag(D->getLocation(),
5314 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5315 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005316 if (ObjCPropery)
5317 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5318 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005319 } else {
5320 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5321 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5322 }
5323}
5324
John McCall9c3087b2010-08-26 02:13:20 +00005325void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005326 Decl *Ctx) {
5327 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005328 return;
5329
John McCall2f514482010-01-27 03:50:35 +00005330 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005331 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5332 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005333 DD.getUnknownObjCClass(),
5334 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005335}
5336
Chris Lattner5f9e2722011-07-23 10:55:15 +00005337void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005338 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005339 const ObjCInterfaceDecl *UnknownObjCClass,
5340 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005341 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005342 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005343 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5344 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005345 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005346 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005347 return;
5348 }
5349
5350 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005351 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005352 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005353 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005354}