blob: cae855e0065874167a04cf604d00cc3097ca271d [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindolad7a60ad2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000031using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000033
John McCall883cc2c2011-03-02 12:29:23 +000034/// These constants match the enumerated choices of
35/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000036enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000037 ExpectedFunction,
38 ExpectedUnion,
39 ExpectedVariableOrFunction,
40 ExpectedFunctionOrMethod,
41 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000043 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000044 ExpectedFunctionMethodOrParameter,
45 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000046 ExpectedVariable,
47 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000048 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000049 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000050 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000051 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000052 ExpectedTLSVar,
53 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000054 ExpectedVariableFieldOrTag,
Aaron Ballman37a89532013-07-18 14:56:42 +000055 ExpectedTypeOrNamespace,
56 ExpectedObjectiveCInterface
John McCall883cc2c2011-03-02 12:29:23 +000057};
58
Chris Lattnere5c5ee12008-06-29 00:16:31 +000059//===----------------------------------------------------------------------===//
60// Helper functions
61//===----------------------------------------------------------------------===//
62
Chandler Carruth87c44602011-07-01 23:49:12 +000063static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000064 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000065 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000066 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000068 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000069 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000070 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000071 Ty = decl->getUnderlyingType();
72 else
73 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000074
Chris Lattner6b6b5372008-06-26 18:38:35 +000075 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000076 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000077 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000078 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000079
John McCall183700f2009-09-21 23:43:11 +000080 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000081}
82
Daniel Dunbar35682492008-09-26 04:12:28 +000083// FIXME: We should provide an abstraction around a method or function
84// to provide the following bits of information.
85
Nuno Lopesd20254f2009-12-20 23:11:08 +000086/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000087/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000088static bool isFunction(const Decl *D) {
89 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000090}
91
92/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000093/// type (function or function-typed variable) or an Objective-C
94/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000095static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000096 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000097}
98
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000099/// isFunctionOrMethodOrBlock - Return true if the given decl has function
100/// type (function or function-typed variable) or an Objective-C
101/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000102static bool isFunctionOrMethodOrBlock(const Decl *D) {
103 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000104 return true;
105 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000107 QualType Ty = V->getType();
108 return Ty->isBlockPointerType();
109 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000110 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000111}
112
John McCall711c52b2011-01-05 12:14:39 +0000113/// Return true if the given decl has a declarator that should have
114/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000116 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
118 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000119}
120
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000121/// hasFunctionProto - Return true if the given decl has a argument
122/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000123/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000124static bool hasFunctionProto(const Decl *D) {
125 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000126 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000127 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000129 return true;
130 }
131}
132
133/// getFunctionOrMethodNumArgs - Return number of function or method
134/// arguments. It is an error to call this on a K&R function (use
135/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000136static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
137 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000138 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000140 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000146 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000148 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000151}
152
Chandler Carruth87c44602011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodResultType(const Decl *D) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000155 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isFunctionOrMethodVariadic(const Decl *D) {
160 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000161 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000162 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000163 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000164 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000165 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000166 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000167 }
168}
169
Chandler Carruth87c44602011-07-01 23:49:12 +0000170static bool isInstanceMethod(const Decl *D) {
171 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000172 return MethodDecl->isInstance();
173 return false;
174}
175
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000177 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000178 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000179 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000180
John McCall506b57e2010-05-17 21:00:27 +0000181 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
182 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000184
John McCall506b57e2010-05-17 21:00:27 +0000185 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000186
Chris Lattner6b6b5372008-06-26 18:38:35 +0000187 // FIXME: Should we walk the chain of classes?
188 return ClsName == &Ctx.Idents.get("NSString") ||
189 ClsName == &Ctx.Idents.get("NSMutableString");
190}
191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000193 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 if (!PT)
195 return false;
196
Ted Kremenek6217b802009-07-29 21:53:49 +0000197 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000198 if (!RT)
199 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000201 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000202 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 return false;
204
205 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
206}
207
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000208/// \brief Check if the attribute has exactly as many args as Num. May
209/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000210static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
211 unsigned int Num) {
212 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
214 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000215 return false;
216 }
217
218 return true;
219}
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000222/// \brief Check if the attribute has at least as many args as Num. May
223/// output an error.
224static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
225 unsigned int Num) {
226 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000227 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
228 return false;
229 }
230
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000231 return true;
232}
233
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000234/// \brief Check if IdxExpr is a valid argument index for a function or
235/// instance method D. May output an error.
236///
237/// \returns true if IdxExpr is a valid index.
238static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
239 StringRef AttrName,
240 SourceLocation AttrLoc,
241 unsigned AttrArgNum,
242 const Expr *IdxExpr,
243 uint64_t &Idx)
244{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000245 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000246
247 // In C++ the implicit 'this' function parameter also counts.
248 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000249 bool HP = hasFunctionProto(D);
250 bool HasImplicitThisParam = isInstanceMethod(D);
251 bool IV = HP && isFunctionOrMethodVariadic(D);
252 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
253 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000254
255 llvm::APSInt IdxInt;
256 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
257 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000258 std::string Name = std::string("'") + AttrName.str() + std::string("'");
259 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000260 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000261 return false;
262 }
263
264 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000265 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000266 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
267 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
268 return false;
269 }
270 Idx--; // Convert to zero-based.
271 if (HasImplicitThisParam) {
272 if (Idx == 0) {
273 S.Diag(AttrLoc,
274 diag::err_attribute_invalid_implicit_this_argument)
275 << AttrName << IdxExpr->getSourceRange();
276 return false;
277 }
278 --Idx;
279 }
280
281 return true;
282}
283
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000284///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000285/// \brief Check if passed in Decl is a field or potentially shared global var
286/// \return true if the Decl is a field or potentially shared global variable
287///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000288static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000289 if (isa<FieldDecl>(D))
290 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000291 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000292 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000293
294 return false;
295}
296
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000297/// \brief Check if the passed-in expression is of type int or bool.
298static bool isIntOrBool(Expr *Exp) {
299 QualType QT = Exp->getType();
300 return QT->isBooleanType() || QT->isIntegerType();
301}
302
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000303
304// Check to see if the type is a smart pointer of some kind. We assume
305// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000306static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
307 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
308 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000309 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000310 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000311
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000312 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
313 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000314 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000315 return false;
316
317 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000318}
319
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000320/// \brief Check if passed in Decl is a pointer type.
321/// Note that this function may produce an error message.
322/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000323static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
324 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000325 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000326 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000327 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000328 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000329
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000330 if (const RecordType *RT = QT->getAs<RecordType>()) {
331 // If it's an incomplete type, it could be a smart pointer; skip it.
332 // (We don't want to force template instantiation if we can avoid it,
333 // since that would alter the order in which templates are instantiated.)
334 if (RT->isIncompleteType())
335 return true;
336
337 if (threadSafetyCheckIsSmartPointer(S, RT))
338 return true;
339 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000340
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000341 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000342 << Attr.getName()->getName() << QT;
343 } else {
344 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
345 << Attr.getName();
346 }
347 return false;
348}
349
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350/// \brief Checks that the passed in QualType either is of RecordType or points
351/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000352static const RecordType *getRecordType(QualType QT) {
353 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000354 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000355
356 // Now check if we point to record type.
357 if (const PointerType *PT = QT->getAs<PointerType>())
358 return PT->getPointeeType()->getAs<RecordType>();
359
360 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000361}
362
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000363
Jordy Rosefad5de92012-05-08 03:27:22 +0000364static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
365 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000366 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
367 if (RT->getDecl()->getAttr<LockableAttr>())
368 return true;
369 return false;
370}
371
372
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000373/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000374/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000375static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
376 QualType Ty) {
377 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000378
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000379 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000380 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000381 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000382 << Attr.getName() << Ty.getAsString();
383 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000384 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000385
Michael Hanf1aae3b2012-08-03 17:40:43 +0000386 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000387 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000388 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000389
390 // Allow smart pointers to be used as lockable objects.
391 // FIXME -- Check the type that the smart pointer points to.
392 if (threadSafetyCheckIsSmartPointer(S, RT))
393 return;
394
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000395 // Check if the type is lockable.
396 RecordDecl *RD = RT->getDecl();
397 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000398 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000399
400 // Else check if any base classes are lockable.
401 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
402 CXXBasePaths BPaths(false, false);
403 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
404 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000405 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000406
407 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
408 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000409}
410
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000411/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000413/// \param Sidx The attribute argument index to start checking with.
414/// \param ParamIdxOk Whether an argument can be indexing into a function
415/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000416static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000417 const AttributeList &Attr,
418 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000419 int Sidx = 0,
420 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000421 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000422 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000423
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000424 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000425 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000426 Args.push_back(ArgExp);
427 continue;
428 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000429
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000430 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000431 if (StrLit->getLength() == 0 ||
432 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000433 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000434 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000435 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000437 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000438
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000439 // We allow constant strings to be used as a placeholder for expressions
440 // that are not valid C++ syntax, but warn that they are ignored.
441 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
442 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000443 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000444 continue;
445 }
446
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000448
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000449 // A pointer to member expression of the form &MyClass::mu is treated
450 // specially -- we need to look at the type of the member.
451 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
452 if (UOp->getOpcode() == UO_AddrOf)
453 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
454 if (DRE->getDecl()->isCXXInstanceMember())
455 ArgTy = DRE->getDecl()->getType();
456
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000457 // First see if we can just cast to record type, or point to record type.
458 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000460 // Now check if we index into a record type function param.
461 if(!RT && ParamIdxOk) {
462 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000463 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
464 if(FD && IL) {
465 unsigned int NumParams = FD->getNumParams();
466 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
468 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
469 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
471 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000472 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000473 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000474 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000475 }
476 }
477
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000478 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000479
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000480 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000481 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482}
483
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000484//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000485// Attribute Implementations
486//===----------------------------------------------------------------------===//
487
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000488// FIXME: All this manual attribute parsing code is gross. At the
489// least add some helper functions to check most argument patterns (#
490// and types of args).
491
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000492enum ThreadAttributeDeclKind {
493 ThreadExpectedFieldOrGlobalVar,
494 ThreadExpectedFunctionOrMethod,
495 ThreadExpectedClassOrStruct
496};
497
Michael Hanf1aae3b2012-08-03 17:40:43 +0000498static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000499 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500 assert(!Attr.isInvalid());
501
502 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000503 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000504
505 // D must be either a member field or global (potentially shared) variable.
506 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000507 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
508 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000509 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000510 }
511
Michael Handc691572012-07-23 18:48:41 +0000512 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000513}
514
Michael Handc691572012-07-23 18:48:41 +0000515static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
516 if (!checkGuardedVarAttrCommon(S, D, Attr))
517 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000518
Michael Han51d8c522013-01-24 16:46:58 +0000519 D->addAttr(::new (S.Context)
520 GuardedVarAttr(Attr.getRange(), S.Context,
521 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000522}
523
Michael Hanf1aae3b2012-08-03 17:40:43 +0000524static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000525 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000526 if (!checkGuardedVarAttrCommon(S, D, Attr))
527 return;
528
529 if (!threadSafetyCheckIsPointer(S, D, Attr))
530 return;
531
Michael Han51d8c522013-01-24 16:46:58 +0000532 D->addAttr(::new (S.Context)
533 PtGuardedVarAttr(Attr.getRange(), S.Context,
534 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000535}
536
Michael Hanf1aae3b2012-08-03 17:40:43 +0000537static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
538 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000539 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540 assert(!Attr.isInvalid());
541
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000542 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000543 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000544
545 // D must be either a member field or global (potentially shared) variable.
546 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000547 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
548 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000549 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000550 }
551
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000552 SmallVector<Expr*, 1> Args;
553 // check that all arguments are lockable objects
554 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
555 unsigned Size = Args.size();
556 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000557 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000558
Michael Handc691572012-07-23 18:48:41 +0000559 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000560
Michael Handc691572012-07-23 18:48:41 +0000561 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000562}
563
Michael Handc691572012-07-23 18:48:41 +0000564static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
565 Expr *Arg = 0;
566 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
567 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000568
Michael Handc691572012-07-23 18:48:41 +0000569 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
570}
571
Michael Hanf1aae3b2012-08-03 17:40:43 +0000572static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000573 const AttributeList &Attr) {
574 Expr *Arg = 0;
575 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
576 return;
577
578 if (!threadSafetyCheckIsPointer(S, D, Attr))
579 return;
580
581 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
582 S.Context, Arg));
583}
584
Michael Hanf1aae3b2012-08-03 17:40:43 +0000585static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000586 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000587 assert(!Attr.isInvalid());
588
589 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000591
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000592 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000593 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000594 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
595 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000596 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000597 }
598
Michael Handc691572012-07-23 18:48:41 +0000599 return true;
600}
601
602static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
603 if (!checkLockableAttrCommon(S, D, Attr))
604 return;
605
606 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
607}
608
Michael Hanf1aae3b2012-08-03 17:40:43 +0000609static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
611 if (!checkLockableAttrCommon(S, D, Attr))
612 return;
613
Michael Han51d8c522013-01-24 16:46:58 +0000614 D->addAttr(::new (S.Context)
615 ScopedLockableAttr(Attr.getRange(), S.Context,
616 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000617}
618
Kostya Serebryany85aee962013-02-26 06:58:27 +0000619static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
620 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000621 assert(!Attr.isInvalid());
622
623 if (!checkAttributeNumArgs(S, Attr, 0))
624 return;
625
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000626 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000627 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
628 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000629 return;
630 }
631
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000632 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000633 S.Context));
634}
635
Kostya Serebryany85aee962013-02-26 06:58:27 +0000636static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000637 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000638 assert(!Attr.isInvalid());
639
640 if (!checkAttributeNumArgs(S, Attr, 0))
641 return;
642
643 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000645 << Attr.getName() << ExpectedFunctionOrMethod;
646 return;
647 }
648
Michael Han51d8c522013-01-24 16:46:58 +0000649 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000650 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
651 Attr.getAttributeSpellingListIndex()));
652}
653
654static void handleNoSanitizeMemory(Sema &S, Decl *D,
655 const AttributeList &Attr) {
656 assert(!Attr.isInvalid());
657
658 if (!checkAttributeNumArgs(S, Attr, 0))
659 return;
660
661 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
663 << Attr.getName() << ExpectedFunctionOrMethod;
664 return;
665 }
666
667 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
668 S.Context));
669}
670
671static void handleNoSanitizeThread(Sema &S, Decl *D,
672 const AttributeList &Attr) {
673 assert(!Attr.isInvalid());
674
675 if (!checkAttributeNumArgs(S, Attr, 0))
676 return;
677
678 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
680 << Attr.getName() << ExpectedFunctionOrMethod;
681 return;
682 }
683
684 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
685 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000686}
687
Michael Hanf1aae3b2012-08-03 17:40:43 +0000688static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
689 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000690 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000691 assert(!Attr.isInvalid());
692
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000693 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000694 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000695
696 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000697 ValueDecl *VD = dyn_cast<ValueDecl>(D);
698 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000699 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
700 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000701 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000702 }
703
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000704 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 QualType QT = VD->getType();
706 if (!QT->isDependentType()) {
707 const RecordType *RT = getRecordType(QT);
708 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000709 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000710 << Attr.getName();
711 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000712 }
713 }
714
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000715 // Check that all arguments are lockable objects.
716 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000717 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000718 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000719
Michael Handc691572012-07-23 18:48:41 +0000720 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000721}
722
Michael Hanf1aae3b2012-08-03 17:40:43 +0000723static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000724 const AttributeList &Attr) {
725 SmallVector<Expr*, 1> Args;
726 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
727 return;
728
729 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000730 D->addAttr(::new (S.Context)
731 AcquiredAfterAttr(Attr.getRange(), S.Context,
732 StartArg, Args.size(),
733 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000734}
735
Michael Hanf1aae3b2012-08-03 17:40:43 +0000736static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000737 const AttributeList &Attr) {
738 SmallVector<Expr*, 1> Args;
739 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
740 return;
741
742 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000743 D->addAttr(::new (S.Context)
744 AcquiredBeforeAttr(Attr.getRange(), S.Context,
745 StartArg, Args.size(),
746 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000747}
748
Michael Hanf1aae3b2012-08-03 17:40:43 +0000749static bool checkLockFunAttrCommon(Sema &S, Decl *D,
750 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000751 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000752 assert(!Attr.isInvalid());
753
754 // zero or more arguments ok
755
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000756 // check that the attribute is applied to a function
757 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000758 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
759 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000760 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761 }
762
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000763 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000764 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000765
Michael Handc691572012-07-23 18:48:41 +0000766 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000767}
768
Michael Hanf1aae3b2012-08-03 17:40:43 +0000769static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000770 const AttributeList &Attr) {
771 SmallVector<Expr*, 1> Args;
772 if (!checkLockFunAttrCommon(S, D, Attr, Args))
773 return;
774
775 unsigned Size = Args.size();
776 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000777 D->addAttr(::new (S.Context)
778 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
779 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000780}
781
Michael Hanf1aae3b2012-08-03 17:40:43 +0000782static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000783 const AttributeList &Attr) {
784 SmallVector<Expr*, 1> Args;
785 if (!checkLockFunAttrCommon(S, D, Attr, Args))
786 return;
787
788 unsigned Size = Args.size();
789 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000790 D->addAttr(::new (S.Context)
791 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
792 StartArg, Size,
793 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000794}
795
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000796static void handleAssertSharedLockAttr(Sema &S, Decl *D,
797 const AttributeList &Attr) {
798 SmallVector<Expr*, 1> Args;
799 if (!checkLockFunAttrCommon(S, D, Attr, Args))
800 return;
801
802 unsigned Size = Args.size();
803 Expr **StartArg = Size == 0 ? 0 : &Args[0];
804 D->addAttr(::new (S.Context)
805 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
806 Attr.getAttributeSpellingListIndex()));
807}
808
809static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
810 const AttributeList &Attr) {
811 SmallVector<Expr*, 1> Args;
812 if (!checkLockFunAttrCommon(S, D, Attr, Args))
813 return;
814
815 unsigned Size = Args.size();
816 Expr **StartArg = Size == 0 ? 0 : &Args[0];
817 D->addAttr(::new (S.Context)
818 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
819 StartArg, Size,
820 Attr.getAttributeSpellingListIndex()));
821}
822
823
Michael Hanf1aae3b2012-08-03 17:40:43 +0000824static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
825 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000826 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000827 assert(!Attr.isInvalid());
828
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000829 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000830 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000831
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000832 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000833 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
834 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000835 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000836 }
837
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000839 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000840 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000841 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842 }
843
844 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000845 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000846
Michael Handc691572012-07-23 18:48:41 +0000847 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000848}
849
Michael Hanf1aae3b2012-08-03 17:40:43 +0000850static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000851 const AttributeList &Attr) {
852 SmallVector<Expr*, 2> Args;
853 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
854 return;
855
856 unsigned Size = Args.size();
857 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000858 D->addAttr(::new (S.Context)
859 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
860 Attr.getArg(0), StartArg, Size,
861 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000862}
863
Michael Hanf1aae3b2012-08-03 17:40:43 +0000864static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000865 const AttributeList &Attr) {
866 SmallVector<Expr*, 2> Args;
867 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
868 return;
869
870 unsigned Size = Args.size();
871 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000872 D->addAttr(::new (S.Context)
873 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
874 Attr.getArg(0), StartArg, Size,
875 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000876}
877
Michael Hanf1aae3b2012-08-03 17:40:43 +0000878static bool checkLocksRequiredCommon(Sema &S, Decl *D,
879 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000880 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000881 assert(!Attr.isInvalid());
882
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000883 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000884 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000885
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000887 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
888 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000889 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000890 }
891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000893 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000894 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000895 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000896
Michael Handc691572012-07-23 18:48:41 +0000897 return true;
898}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000899
Michael Hanf1aae3b2012-08-03 17:40:43 +0000900static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000901 const AttributeList &Attr) {
902 SmallVector<Expr*, 1> Args;
903 if (!checkLocksRequiredCommon(S, D, Attr, Args))
904 return;
905
906 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000907 D->addAttr(::new (S.Context)
908 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
909 StartArg, Args.size(),
910 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000911}
912
Michael Hanf1aae3b2012-08-03 17:40:43 +0000913static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000914 const AttributeList &Attr) {
915 SmallVector<Expr*, 1> Args;
916 if (!checkLocksRequiredCommon(S, D, Attr, Args))
917 return;
918
919 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000920 D->addAttr(::new (S.Context)
921 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
922 StartArg, Args.size(),
923 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000924}
925
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000927 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000928 assert(!Attr.isInvalid());
929
930 // zero or more arguments ok
931
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000932 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000933 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
934 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000935 return;
936 }
937
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000938 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000939 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000940 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000941 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000942 Expr **StartArg = Size == 0 ? 0 : &Args[0];
943
Michael Han51d8c522013-01-24 16:46:58 +0000944 D->addAttr(::new (S.Context)
945 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
946 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000947}
948
949static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000950 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000951 assert(!Attr.isInvalid());
952
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000953 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000954 return;
955
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000956 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000957 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
958 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000959 return;
960 }
961
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000962 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000963 SmallVector<Expr*, 1> Args;
964 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
965 unsigned Size = Args.size();
966 if (Size == 0)
967 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000968
Michael Han51d8c522013-01-24 16:46:58 +0000969 D->addAttr(::new (S.Context)
970 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
971 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000972}
973
974static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000975 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000976 assert(!Attr.isInvalid());
977
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000978 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000979 return;
980
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000981 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000982 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
983 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000984 return;
985 }
986
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000987 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000988 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000989 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000990 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000991 if (Size == 0)
992 return;
993 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000994
Michael Han51d8c522013-01-24 16:46:58 +0000995 D->addAttr(::new (S.Context)
996 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
997 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000998}
999
1000
Chandler Carruth1b03c872011-07-02 00:01:44 +00001001static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1002 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001003 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1004 if (TD == 0) {
1005 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1006 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001007 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001008 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001009 }
Mike Stumpbf916502009-07-24 19:02:52 +00001010
Richard Smitha4fa9002013-01-13 02:11:23 +00001011 // Remember this typedef decl, we will need it later for diagnostics.
1012 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001013}
1014
Chandler Carruth1b03c872011-07-02 00:01:44 +00001015static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001016 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001017 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001018 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001019
Chandler Carruth87c44602011-07-01 23:49:12 +00001020 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001021 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001022 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001023 // If the alignment is less than or equal to 8 bits, the packed attribute
1024 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001025 if (!FD->getType()->isDependentType() &&
1026 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001027 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001028 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001029 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001030 else
Michael Han51d8c522013-01-24 16:46:58 +00001031 FD->addAttr(::new (S.Context)
1032 PackedAttr(Attr.getRange(), S.Context,
1033 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001035 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001036}
1037
Chandler Carruth1b03c872011-07-02 00:01:44 +00001038static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001039 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001040 RD->addAttr(::new (S.Context)
1041 MsStructAttr(Attr.getRange(), S.Context,
1042 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001043 else
1044 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1045}
1046
Chandler Carruth1b03c872011-07-02 00:01:44 +00001047static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001048 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001049 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001050 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001051
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001052 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001053 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001054 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001055 D->addAttr(::new (S.Context)
1056 IBActionAttr(Attr.getRange(), S.Context,
1057 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001058 return;
1059 }
1060
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001061 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001062}
1063
Ted Kremenek2f041d02011-09-29 07:02:25 +00001064static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1065 // The IBOutlet/IBOutletCollection attributes only apply to instance
1066 // variables or properties of Objective-C classes. The outlet must also
1067 // have an object reference type.
1068 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1069 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001070 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001071 << Attr.getName() << VD->getType() << 0;
1072 return false;
1073 }
1074 }
1075 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1076 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001077 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001078 << Attr.getName() << PD->getType() << 1;
1079 return false;
1080 }
1081 }
1082 else {
1083 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1084 return false;
1085 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001086
Ted Kremenek2f041d02011-09-29 07:02:25 +00001087 return true;
1088}
1089
Chandler Carruth1b03c872011-07-02 00:01:44 +00001090static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001091 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001092 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001093 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001094
1095 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001096 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001097
Michael Han51d8c522013-01-24 16:46:58 +00001098 D->addAttr(::new (S.Context)
1099 IBOutletAttr(Attr.getRange(), S.Context,
1100 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001101}
1102
Chandler Carruth1b03c872011-07-02 00:01:44 +00001103static void handleIBOutletCollection(Sema &S, Decl *D,
1104 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001105
1106 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001107 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001108 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1109 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001110 return;
1111 }
1112
Ted Kremenek2f041d02011-09-29 07:02:25 +00001113 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001114 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001115
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001116 IdentifierInfo *II = Attr.getParameterName();
1117 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001118 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001119
John McCallb3d87482010-08-24 05:47:05 +00001120 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001121 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001122 if (!TypeRep) {
1123 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1124 return;
1125 }
John McCallb3d87482010-08-24 05:47:05 +00001126 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001127 // Diagnose use of non-object type in iboutletcollection attribute.
1128 // FIXME. Gnu attribute extension ignores use of builtin types in
1129 // attributes. So, __attribute__((iboutletcollection(char))) will be
1130 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001131 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001132 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1133 return;
1134 }
Michael Han51d8c522013-01-24 16:46:58 +00001135 D->addAttr(::new (S.Context)
1136 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1137 QT, Attr.getParameterLoc(),
1138 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001139}
1140
Chandler Carruthd309c812011-07-01 23:49:16 +00001141static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001142 if (const RecordType *UT = T->getAsUnionType())
1143 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1144 RecordDecl *UD = UT->getDecl();
1145 for (RecordDecl::field_iterator it = UD->field_begin(),
1146 itend = UD->field_end(); it != itend; ++it) {
1147 QualType QT = it->getType();
1148 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1149 T = QT;
1150 return;
1151 }
1152 }
1153 }
1154}
1155
Nuno Lopes587de5b2012-05-24 00:22:00 +00001156static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001157 if (!isFunctionOrMethod(D)) {
1158 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001159 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001160 return;
1161 }
1162
Nuno Lopes587de5b2012-05-24 00:22:00 +00001163 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1164 return;
1165
Nuno Lopes587de5b2012-05-24 00:22:00 +00001166 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001167 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1168 Expr *Ex = Attr.getArg(i);
1169 uint64_t Idx;
1170 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1171 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001172 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001173
1174 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001175 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001176 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1178 << Attr.getName() << AANT_ArgumentIntegerConstant
1179 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001180 return;
1181 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001182 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001183 }
1184
1185 // check if the function returns a pointer
1186 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1187 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001188 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001189 }
1190
Michael Han51d8c522013-01-24 16:46:58 +00001191 D->addAttr(::new (S.Context)
1192 AllocSizeAttr(Attr.getRange(), S.Context,
1193 SizeArgs.data(), SizeArgs.size(),
1194 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001195}
1196
Chandler Carruth1b03c872011-07-02 00:01:44 +00001197static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001198 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1199 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001200 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001202 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001203 return;
1204 }
Mike Stumpbf916502009-07-24 19:02:52 +00001205
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001206 SmallVector<unsigned, 8> NonNullArgs;
1207 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1208 Expr *Ex = Attr.getArg(i);
1209 uint64_t Idx;
1210 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1211 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001212 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001213
1214 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001215 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001216 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001217
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001218 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001219 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001220 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001221 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001222 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001223 }
Mike Stumpbf916502009-07-24 19:02:52 +00001224
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001225 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001226 }
Mike Stumpbf916502009-07-24 19:02:52 +00001227
1228 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1229 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001230 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001231 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1232 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001233 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001234 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001235 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001236 }
Mike Stumpbf916502009-07-24 19:02:52 +00001237
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001238 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001239 if (NonNullArgs.empty()) {
1240 // Warn the trivial case only if attribute is not coming from a
1241 // macro instantiation.
1242 if (Attr.getLoc().isFileID())
1243 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001244 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001245 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001246 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001247
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001248 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001249 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001250 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001251 D->addAttr(::new (S.Context)
1252 NonNullAttr(Attr.getRange(), S.Context, start, size,
1253 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001254}
1255
Chandler Carruth1b03c872011-07-02 00:01:44 +00001256static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001257 // This attribute must be applied to a function declaration.
1258 // The first argument to the attribute must be a string,
1259 // the name of the resource, for example "malloc".
1260 // The following arguments must be argument indexes, the arguments must be
1261 // of integer type for Returns, otherwise of pointer type.
1262 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001263 // after being held. free() should be __attribute((ownership_takes)), whereas
1264 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001265
1266 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001267 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001268 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001269 return;
1270 }
1271 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001272 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001273 switch (AL.getKind()) {
1274 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001275 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001276 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001277 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1278 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001279 return;
1280 }
Jordy Rose2a479922010-08-12 08:54:03 +00001281 break;
1282 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001283 K = OwnershipAttr::Holds;
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_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001291 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001292 if (AL.getNumArgs() > 1) {
1293 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001294 << AL.getName() << AL.getNumArgs() + 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001295 return;
1296 }
Jordy Rose2a479922010-08-12 08:54:03 +00001297 break;
1298 default:
1299 // This should never happen given how we are called.
1300 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001301 }
1302
Chandler Carruth87c44602011-07-01 23:49:12 +00001303 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001304 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1305 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001306 return;
1307 }
1308
Chris Lattner5f9e2722011-07-23 10:55:15 +00001309 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001310
1311 // Normalize the argument, __foo__ becomes foo.
1312 if (Module.startswith("__") && Module.endswith("__"))
1313 Module = Module.substr(2, Module.size() - 4);
1314
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001315
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001316 SmallVector<unsigned, 8> OwnershipArgs;
1317 for (unsigned i = 0; i < AL.getNumArgs(); ++i) {
1318 Expr *Ex = AL.getArg(i);
1319 uint64_t Idx;
1320 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
1321 AL.getLoc(), i + 1, Ex, Idx))
1322 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001323
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001324 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001325 case OwnershipAttr::Takes:
1326 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001327 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001328 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001329 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1330 // FIXME: Should also highlight argument in decl.
1331 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001332 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001333 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001334 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001335 continue;
1336 }
1337 break;
1338 }
Sean Huntcf807c42010-08-18 23:23:40 +00001339 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001340 if (AL.getNumArgs() > 1) {
1341 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001342 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001343 llvm::APSInt ArgNum(32);
1344 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1345 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1346 S.Diag(AL.getLoc(), diag::err_ownership_type)
1347 << "ownership_returns" << "integer"
1348 << IdxExpr->getSourceRange();
1349 return;
1350 }
1351 }
1352 break;
1353 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001354 } // switch
1355
1356 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001357 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001358 i = D->specific_attr_begin<OwnershipAttr>(),
1359 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001360 i != e; ++i) {
1361 if ((*i)->getOwnKind() != K) {
1362 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1363 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001364 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001365 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1366 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001367 }
1368 }
1369 }
1370 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001371 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001372 }
1373
1374 unsigned* start = OwnershipArgs.data();
1375 unsigned size = OwnershipArgs.size();
1376 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001377
1378 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001379 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1380 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001381 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001382 }
Sean Huntcf807c42010-08-18 23:23:40 +00001383
Michael Han51d8c522013-01-24 16:46:58 +00001384 D->addAttr(::new (S.Context)
1385 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1386 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001387}
1388
Chandler Carruth1b03c872011-07-02 00:01:44 +00001389static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001390 // Check the attribute arguments.
1391 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1393 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001394 return;
1395 }
1396
Chandler Carruth87c44602011-07-01 23:49:12 +00001397 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001399 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001400 return;
1401 }
1402
Chandler Carruth87c44602011-07-01 23:49:12 +00001403 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001404
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001405 // gcc rejects
1406 // class c {
1407 // static int a __attribute__((weakref ("v2")));
1408 // static int b() __attribute__((weakref ("f3")));
1409 // };
1410 // and ignores the attributes of
1411 // void f(void) {
1412 // static int a __attribute__((weakref ("v2")));
1413 // }
1414 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001415 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001416 if (!Ctx->isFileContext()) {
1417 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001418 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001419 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001420 }
1421
1422 // The GCC manual says
1423 //
1424 // At present, a declaration to which `weakref' is attached can only
1425 // be `static'.
1426 //
1427 // It also says
1428 //
1429 // Without a TARGET,
1430 // given as an argument to `weakref' or to `alias', `weakref' is
1431 // equivalent to `weak'.
1432 //
1433 // gcc 4.4.1 will accept
1434 // int a7 __attribute__((weakref));
1435 // as
1436 // int a7 __attribute__((weak));
1437 // This looks like a bug in gcc. We reject that for now. We should revisit
1438 // it if this behaviour is actually used.
1439
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001440 // GCC rejects
1441 // static ((alias ("y"), weakref)).
1442 // Should we? How to check that weakref is before or after alias?
1443
1444 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001445 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001446 Arg = Arg->IgnoreParenCasts();
1447 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1448
Douglas Gregor5cee1192011-07-27 05:40:30 +00001449 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001451 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001452 return;
1453 }
1454 // GCC will accept anything as the argument of weakref. Should we
1455 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001456 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001457 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001458 }
1459
Michael Han51d8c522013-01-24 16:46:58 +00001460 D->addAttr(::new (S.Context)
1461 WeakRefAttr(Attr.getRange(), S.Context,
1462 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001463}
1464
Chandler Carruth1b03c872011-07-02 00:01:44 +00001465static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001466 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001467 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001468 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001469
Peter Collingbourne7a730022010-11-23 20:45:58 +00001470 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001471 Arg = Arg->IgnoreParenCasts();
1472 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001473
Douglas Gregor5cee1192011-07-27 05:40:30 +00001474 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001475 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1476 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001477 return;
1478 }
Mike Stumpbf916502009-07-24 19:02:52 +00001479
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001480 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001481 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1482 return;
1483 }
1484
Chris Lattner6b6b5372008-06-26 18:38:35 +00001485 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001486
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001487 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001488 Str->getString(),
1489 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001490}
1491
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001492static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1493 // Check the attribute arguments.
1494 if (!checkAttributeNumArgs(S, Attr, 0))
1495 return;
1496
1497 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1498 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1499 << Attr.getName() << ExpectedFunctionOrMethod;
1500 return;
1501 }
1502
Michael Han51d8c522013-01-24 16:46:58 +00001503 D->addAttr(::new (S.Context)
1504 MinSizeAttr(Attr.getRange(), S.Context,
1505 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001506}
1507
Benjamin Krameree409a92012-05-12 21:10:52 +00001508static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1509 // Check the attribute arguments.
1510 if (!checkAttributeNumArgs(S, Attr, 0))
1511 return;
1512
1513 if (!isa<FunctionDecl>(D)) {
1514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1515 << Attr.getName() << ExpectedFunction;
1516 return;
1517 }
1518
1519 if (D->hasAttr<HotAttr>()) {
1520 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1521 << Attr.getName() << "hot";
1522 return;
1523 }
1524
Michael Han51d8c522013-01-24 16:46:58 +00001525 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1526 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001527}
1528
1529static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1530 // Check the attribute arguments.
1531 if (!checkAttributeNumArgs(S, Attr, 0))
1532 return;
1533
1534 if (!isa<FunctionDecl>(D)) {
1535 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1536 << Attr.getName() << ExpectedFunction;
1537 return;
1538 }
1539
1540 if (D->hasAttr<ColdAttr>()) {
1541 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1542 << Attr.getName() << "cold";
1543 return;
1544 }
1545
Michael Han51d8c522013-01-24 16:46:58 +00001546 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1547 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001548}
1549
Chandler Carruth1b03c872011-07-02 00:01:44 +00001550static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001551 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001552 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001553 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001554
Chandler Carruth87c44602011-07-01 23:49:12 +00001555 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001557 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001558 return;
1559 }
1560
Michael Han51d8c522013-01-24 16:46:58 +00001561 D->addAttr(::new (S.Context)
1562 NakedAttr(Attr.getRange(), S.Context,
1563 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001564}
1565
Chandler Carruth1b03c872011-07-02 00:01:44 +00001566static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1567 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001568 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001569 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001570 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1571 << Attr.getName() << 0;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001572 return;
1573 }
1574
Chandler Carruth87c44602011-07-01 23:49:12 +00001575 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001576 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001577 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001578 return;
1579 }
Mike Stumpbf916502009-07-24 19:02:52 +00001580
Michael Han51d8c522013-01-24 16:46:58 +00001581 D->addAttr(::new (S.Context)
1582 AlwaysInlineAttr(Attr.getRange(), S.Context,
1583 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001584}
1585
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001586static void handleTLSModelAttr(Sema &S, Decl *D,
1587 const AttributeList &Attr) {
1588 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001589 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001590 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001591
1592 Expr *Arg = Attr.getArg(0);
1593 Arg = Arg->IgnoreParenCasts();
1594 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1595
1596 // Check that it is a string.
1597 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001598 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1599 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001600 return;
1601 }
1602
Richard Smith38afbc72013-04-13 02:43:54 +00001603 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1605 << Attr.getName() << ExpectedTLSVar;
1606 return;
1607 }
1608
1609 // Check that the value.
1610 StringRef Model = Str->getString();
1611 if (Model != "global-dynamic" && Model != "local-dynamic"
1612 && Model != "initial-exec" && Model != "local-exec") {
1613 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1614 return;
1615 }
1616
Michael Han51d8c522013-01-24 16:46:58 +00001617 D->addAttr(::new (S.Context)
1618 TLSModelAttr(Attr.getRange(), S.Context, Model,
1619 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001620}
1621
Chandler Carruth1b03c872011-07-02 00:01:44 +00001622static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001623 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001624 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001625 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1626 << Attr.getName() << 0;
Ryan Flynn76168e22009-08-09 20:07:29 +00001627 return;
1628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Chandler Carruth87c44602011-07-01 23:49:12 +00001630 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001631 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001632 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001633 D->addAttr(::new (S.Context)
1634 MallocAttr(Attr.getRange(), S.Context,
1635 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001636 return;
1637 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001638 }
1639
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001640 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001641}
1642
Chandler Carruth1b03c872011-07-02 00:01:44 +00001643static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001644 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001645 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001646 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001647
Michael Han51d8c522013-01-24 16:46:58 +00001648 D->addAttr(::new (S.Context)
1649 MayAliasAttr(Attr.getRange(), S.Context,
1650 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001651}
1652
Chandler Carruth1b03c872011-07-02 00:01:44 +00001653static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001654 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001655 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001656 D->addAttr(::new (S.Context)
1657 NoCommonAttr(Attr.getRange(), S.Context,
1658 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001659 else
1660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001661 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001662}
1663
Chandler Carruth1b03c872011-07-02 00:01:44 +00001664static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001665 assert(!Attr.isInvalid());
Eli Friedman3e1aca22013-06-20 22:55:04 +00001666
1667 if (S.LangOpts.CPlusPlus) {
1668 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1669 return;
1670 }
1671
Chandler Carruth87c44602011-07-01 23:49:12 +00001672 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 CommonAttr(Attr.getRange(), S.Context,
1675 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001676 else
1677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001678 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001679}
1680
Chandler Carruth1b03c872011-07-02 00:01:44 +00001681static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001682 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001683
1684 if (S.CheckNoReturnAttr(attr)) return;
1685
Chandler Carruth87c44602011-07-01 23:49:12 +00001686 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001687 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001688 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001689 return;
1690 }
1691
Michael Han51d8c522013-01-24 16:46:58 +00001692 D->addAttr(::new (S.Context)
1693 NoReturnAttr(attr.getRange(), S.Context,
1694 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001695}
1696
1697bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001698 if (attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001699 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1700 << attr.getName() << 0;
John McCall711c52b2011-01-05 12:14:39 +00001701 attr.setInvalid();
1702 return true;
1703 }
1704
1705 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001706}
1707
Chandler Carruth1b03c872011-07-02 00:01:44 +00001708static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1709 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001710
1711 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1712 // because 'analyzer_noreturn' does not impact the type.
1713
Chandler Carruth1731e202011-07-11 23:30:35 +00001714 if(!checkAttributeNumArgs(S, Attr, 0))
1715 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001716
Chandler Carruth87c44602011-07-01 23:49:12 +00001717 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1718 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001719 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1720 && !VD->getType()->isFunctionPointerType())) {
1721 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001722 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001723 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001724 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001725 return;
1726 }
1727 }
1728
Michael Han51d8c522013-01-24 16:46:58 +00001729 D->addAttr(::new (S.Context)
1730 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1731 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001732}
1733
Richard Smithcd8ab512013-01-17 01:30:42 +00001734static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1735 const AttributeList &Attr) {
1736 // C++11 [dcl.attr.noreturn]p1:
1737 // The attribute may be applied to the declarator-id in a function
1738 // declaration.
1739 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1740 if (!FD) {
1741 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1742 << Attr.getName() << ExpectedFunctionOrMethod;
1743 return;
1744 }
1745
Michael Han51d8c522013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context)
1747 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1748 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001749}
1750
John Thompson35cc9622010-08-09 21:53:52 +00001751// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001752static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001753/*
1754 Returning a Vector Class in Registers
1755
Eric Christopherf48f3672010-12-01 22:13:54 +00001756 According to the PPU ABI specifications, a class with a single member of
1757 vector type is returned in memory when used as the return value of a function.
1758 This results in inefficient code when implementing vector classes. To return
1759 the value in a single vector register, add the vecreturn attribute to the
1760 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001761
1762 Example:
1763
1764 struct Vector
1765 {
1766 __vector float xyzw;
1767 } __attribute__((vecreturn));
1768
1769 Vector Add(Vector lhs, Vector rhs)
1770 {
1771 Vector result;
1772 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1773 return result; // This will be returned in a register
1774 }
1775*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001776 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001778 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001779 return;
1780 }
1781
Chandler Carruth87c44602011-07-01 23:49:12 +00001782 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001783 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1784 return;
1785 }
1786
Chandler Carruth87c44602011-07-01 23:49:12 +00001787 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001788 int count = 0;
1789
1790 if (!isa<CXXRecordDecl>(record)) {
1791 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1792 return;
1793 }
1794
1795 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1796 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1797 return;
1798 }
1799
Eric Christopherf48f3672010-12-01 22:13:54 +00001800 for (RecordDecl::field_iterator iter = record->field_begin();
1801 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001802 if ((count == 1) || !iter->getType()->isVectorType()) {
1803 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1804 return;
1805 }
1806 count++;
1807 }
1808
Michael Han51d8c522013-01-24 16:46:58 +00001809 D->addAttr(::new (S.Context)
1810 VecReturnAttr(Attr.getRange(), S.Context,
1811 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001812}
1813
Richard Smith3a2b7a12013-01-28 22:42:45 +00001814static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1815 const AttributeList &Attr) {
1816 if (isa<ParmVarDecl>(D)) {
1817 // [[carries_dependency]] can only be applied to a parameter if it is a
1818 // parameter of a function declaration or lambda.
1819 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1820 S.Diag(Attr.getLoc(),
1821 diag::err_carries_dependency_param_not_function_decl);
1822 return;
1823 }
1824 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001825 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001826 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001827 return;
1828 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001829
1830 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1831 Attr.getRange(), S.Context,
1832 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001833}
1834
Chandler Carruth1b03c872011-07-02 00:01:44 +00001835static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001836 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001837 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001838 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1839 << Attr.getName() << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001840 return;
1841 }
Mike Stumpbf916502009-07-24 19:02:52 +00001842
Chandler Carruth87c44602011-07-01 23:49:12 +00001843 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001844 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001845 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001846 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001847 return;
1848 }
Mike Stumpbf916502009-07-24 19:02:52 +00001849
Michael Han51d8c522013-01-24 16:46:58 +00001850 D->addAttr(::new (S.Context)
1851 UnusedAttr(Attr.getRange(), S.Context,
1852 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001853}
1854
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001855static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1856 const AttributeList &Attr) {
1857 // check the attribute arguments.
1858 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1860 << Attr.getName() << 0;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001861 return;
1862 }
1863
1864 if (!isa<FunctionDecl>(D)) {
1865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1866 << Attr.getName() << ExpectedFunction;
1867 return;
1868 }
1869
Michael Han51d8c522013-01-24 16:46:58 +00001870 D->addAttr(::new (S.Context)
1871 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1872 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001873}
1874
Chandler Carruth1b03c872011-07-02 00:01:44 +00001875static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001876 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001877 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001878 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1879 << Attr.getName() << 0;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001880 return;
1881 }
Mike Stumpbf916502009-07-24 19:02:52 +00001882
Chandler Carruth87c44602011-07-01 23:49:12 +00001883 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001884 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1886 return;
1887 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001888 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001889 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001890 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001891 return;
1892 }
Mike Stumpbf916502009-07-24 19:02:52 +00001893
Michael Han51d8c522013-01-24 16:46:58 +00001894 D->addAttr(::new (S.Context)
1895 UsedAttr(Attr.getRange(), S.Context,
1896 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001897}
1898
Chandler Carruth1b03c872011-07-02 00:01:44 +00001899static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001900 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001901 if (Attr.getNumArgs() > 1) {
1902 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001903 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001904 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001905
1906 int priority = 65535; // FIXME: Do not hardcode such constants.
1907 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001908 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001909 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001910 if (E->isTypeDependent() || E->isValueDependent() ||
1911 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001912 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001913 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001914 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001915 return;
1916 }
1917 priority = Idx.getZExtValue();
1918 }
Mike Stumpbf916502009-07-24 19:02:52 +00001919
Chandler Carruth87c44602011-07-01 23:49:12 +00001920 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001921 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001922 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001923 return;
1924 }
1925
Michael Han51d8c522013-01-24 16:46:58 +00001926 D->addAttr(::new (S.Context)
1927 ConstructorAttr(Attr.getRange(), S.Context, priority,
1928 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001929}
1930
Chandler Carruth1b03c872011-07-02 00:01:44 +00001931static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001932 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001933 if (Attr.getNumArgs() > 1) {
1934 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001935 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001936 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001937
1938 int priority = 65535; // FIXME: Do not hardcode such constants.
1939 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001940 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001941 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001942 if (E->isTypeDependent() || E->isValueDependent() ||
1943 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001944 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001945 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001946 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001947 return;
1948 }
1949 priority = Idx.getZExtValue();
1950 }
Mike Stumpbf916502009-07-24 19:02:52 +00001951
Chandler Carruth87c44602011-07-01 23:49:12 +00001952 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001953 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001954 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001955 return;
1956 }
1957
Michael Han51d8c522013-01-24 16:46:58 +00001958 D->addAttr(::new (S.Context)
1959 DestructorAttr(Attr.getRange(), S.Context, priority,
1960 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001961}
1962
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001963template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00001964static void handleAttrWithMessage(Sema &S, Decl *D,
1965 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001966 unsigned NumArgs = Attr.getNumArgs();
1967 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001968 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001969 return;
1970 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001971
1972 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001973 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001974 if (NumArgs == 1) {
1975 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001976 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001977 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type)
1978 << Attr.getName() << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001979 return;
1980 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001981 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001982 }
Mike Stumpbf916502009-07-24 19:02:52 +00001983
Michael Han51d8c522013-01-24 16:46:58 +00001984 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1985 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001986}
1987
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001988static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1989 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00001990 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001991 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001992
Michael Han51d8c522013-01-24 16:46:58 +00001993 D->addAttr(::new (S.Context)
1994 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
1995 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001996}
1997
Patrick Beardb2f68202012-04-06 18:12:22 +00001998static void handleObjCRootClassAttr(Sema &S, Decl *D,
1999 const AttributeList &Attr) {
2000 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002001 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2002 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002003 return;
2004 }
2005
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002006 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002007 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002008
Michael Han51d8c522013-01-24 16:46:58 +00002009 D->addAttr(::new (S.Context)
2010 ObjCRootClassAttr(Attr.getRange(), S.Context,
2011 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002012}
2013
Michael Han51d8c522013-01-24 16:46:58 +00002014static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2015 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002016 if (!isa<ObjCInterfaceDecl>(D)) {
2017 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2018 return;
2019 }
2020
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002021 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002022 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002023
Michael Han51d8c522013-01-24 16:46:58 +00002024 D->addAttr(::new (S.Context)
2025 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2026 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002027}
2028
Jordy Rosefad5de92012-05-08 03:27:22 +00002029static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2030 IdentifierInfo *Platform,
2031 VersionTuple Introduced,
2032 VersionTuple Deprecated,
2033 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002034 StringRef PlatformName
2035 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2036 if (PlatformName.empty())
2037 PlatformName = Platform->getName();
2038
2039 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2040 // of these steps are needed).
2041 if (!Introduced.empty() && !Deprecated.empty() &&
2042 !(Introduced <= Deprecated)) {
2043 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2044 << 1 << PlatformName << Deprecated.getAsString()
2045 << 0 << Introduced.getAsString();
2046 return true;
2047 }
2048
2049 if (!Introduced.empty() && !Obsoleted.empty() &&
2050 !(Introduced <= Obsoleted)) {
2051 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2052 << 2 << PlatformName << Obsoleted.getAsString()
2053 << 0 << Introduced.getAsString();
2054 return true;
2055 }
2056
2057 if (!Deprecated.empty() && !Obsoleted.empty() &&
2058 !(Deprecated <= Obsoleted)) {
2059 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2060 << 2 << PlatformName << Obsoleted.getAsString()
2061 << 1 << Deprecated.getAsString();
2062 return true;
2063 }
2064
2065 return false;
2066}
2067
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002068/// \brief Check whether the two versions match.
2069///
2070/// If either version tuple is empty, then they are assumed to match. If
2071/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2072static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2073 bool BeforeIsOkay) {
2074 if (X.empty() || Y.empty())
2075 return true;
2076
2077 if (X == Y)
2078 return true;
2079
2080 if (BeforeIsOkay && X < Y)
2081 return true;
2082
2083 return false;
2084}
2085
Rafael Espindola51be6e32013-01-08 22:04:34 +00002086AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002087 IdentifierInfo *Platform,
2088 VersionTuple Introduced,
2089 VersionTuple Deprecated,
2090 VersionTuple Obsoleted,
2091 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002092 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002093 bool Override,
2094 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002095 VersionTuple MergedIntroduced = Introduced;
2096 VersionTuple MergedDeprecated = Deprecated;
2097 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002098 bool FoundAny = false;
2099
Rafael Espindola98ae8342012-05-10 02:50:16 +00002100 if (D->hasAttrs()) {
2101 AttrVec &Attrs = D->getAttrs();
2102 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2103 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2104 if (!OldAA) {
2105 ++i;
2106 continue;
2107 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002108
Rafael Espindola98ae8342012-05-10 02:50:16 +00002109 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2110 if (OldPlatform != Platform) {
2111 ++i;
2112 continue;
2113 }
2114
2115 FoundAny = true;
2116 VersionTuple OldIntroduced = OldAA->getIntroduced();
2117 VersionTuple OldDeprecated = OldAA->getDeprecated();
2118 VersionTuple OldObsoleted = OldAA->getObsoleted();
2119 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002120
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002121 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2122 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2123 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2124 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002125 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002126 if (Override) {
2127 int Which = -1;
2128 VersionTuple FirstVersion;
2129 VersionTuple SecondVersion;
2130 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2131 Which = 0;
2132 FirstVersion = OldIntroduced;
2133 SecondVersion = Introduced;
2134 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2135 Which = 1;
2136 FirstVersion = Deprecated;
2137 SecondVersion = OldDeprecated;
2138 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2139 Which = 2;
2140 FirstVersion = Obsoleted;
2141 SecondVersion = OldObsoleted;
2142 }
2143
2144 if (Which == -1) {
2145 Diag(OldAA->getLocation(),
2146 diag::warn_mismatched_availability_override_unavail)
2147 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2148 } else {
2149 Diag(OldAA->getLocation(),
2150 diag::warn_mismatched_availability_override)
2151 << Which
2152 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2153 << FirstVersion.getAsString() << SecondVersion.getAsString();
2154 }
2155 Diag(Range.getBegin(), diag::note_overridden_method);
2156 } else {
2157 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2158 Diag(Range.getBegin(), diag::note_previous_attribute);
2159 }
2160
Rafael Espindola98ae8342012-05-10 02:50:16 +00002161 Attrs.erase(Attrs.begin() + i);
2162 --e;
2163 continue;
2164 }
2165
2166 VersionTuple MergedIntroduced2 = MergedIntroduced;
2167 VersionTuple MergedDeprecated2 = MergedDeprecated;
2168 VersionTuple MergedObsoleted2 = MergedObsoleted;
2169
2170 if (MergedIntroduced2.empty())
2171 MergedIntroduced2 = OldIntroduced;
2172 if (MergedDeprecated2.empty())
2173 MergedDeprecated2 = OldDeprecated;
2174 if (MergedObsoleted2.empty())
2175 MergedObsoleted2 = OldObsoleted;
2176
2177 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2178 MergedIntroduced2, MergedDeprecated2,
2179 MergedObsoleted2)) {
2180 Attrs.erase(Attrs.begin() + i);
2181 --e;
2182 continue;
2183 }
2184
2185 MergedIntroduced = MergedIntroduced2;
2186 MergedDeprecated = MergedDeprecated2;
2187 MergedObsoleted = MergedObsoleted2;
2188 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002189 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002190 }
2191
2192 if (FoundAny &&
2193 MergedIntroduced == Introduced &&
2194 MergedDeprecated == Deprecated &&
2195 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002196 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002197
Ted Kremenekcb344392013-04-06 00:34:27 +00002198 // Only create a new attribute if !Override, but we want to do
2199 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002200 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002201 MergedDeprecated, MergedObsoleted) &&
2202 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002203 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2204 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002205 Obsoleted, IsUnavailable, Message,
2206 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002207 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002208 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002209}
2210
Chandler Carruth1b03c872011-07-02 00:01:44 +00002211static void handleAvailabilityAttr(Sema &S, Decl *D,
2212 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002213 IdentifierInfo *Platform = Attr.getParameterName();
2214 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002215 unsigned Index = Attr.getAttributeSpellingListIndex();
2216
Rafael Espindola3b294362012-05-06 19:56:25 +00002217 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002218 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2219 << Platform;
2220
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002221 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2222 if (!ND) {
2223 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2224 return;
2225 }
2226
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002227 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2228 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2229 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002230 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002231 StringRef Str;
2232 const StringLiteral *SE =
2233 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2234 if (SE)
2235 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002236
Rafael Espindola51be6e32013-01-08 22:04:34 +00002237 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002238 Platform,
2239 Introduced.Version,
2240 Deprecated.Version,
2241 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002242 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002243 /*Override=*/false,
2244 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002245 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002246 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002247}
2248
John McCalld4c3d662013-02-20 01:54:26 +00002249template <class T>
2250static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2251 typename T::VisibilityType value,
2252 unsigned attrSpellingListIndex) {
2253 T *existingAttr = D->getAttr<T>();
2254 if (existingAttr) {
2255 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2256 if (existingValue == value)
2257 return NULL;
2258 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2259 S.Diag(range.getBegin(), diag::note_previous_attribute);
2260 D->dropAttr<T>();
2261 }
2262 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2263}
2264
Rafael Espindola599f1b72012-05-13 03:25:18 +00002265VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002266 VisibilityAttr::VisibilityType Vis,
2267 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002268 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2269 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002270}
2271
John McCalld4c3d662013-02-20 01:54:26 +00002272TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2273 TypeVisibilityAttr::VisibilityType Vis,
2274 unsigned AttrSpellingListIndex) {
2275 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2276 AttrSpellingListIndex);
2277}
2278
2279static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2280 bool isTypeVisibility) {
2281 // Visibility attributes don't mean anything on a typedef.
2282 if (isa<TypedefNameDecl>(D)) {
2283 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2284 << Attr.getName();
2285 return;
2286 }
2287
2288 // 'type_visibility' can only go on a type or namespace.
2289 if (isTypeVisibility &&
2290 !(isa<TagDecl>(D) ||
2291 isa<ObjCInterfaceDecl>(D) ||
2292 isa<NamespaceDecl>(D))) {
2293 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2294 << Attr.getName() << ExpectedTypeOrNamespace;
2295 return;
2296 }
2297
Chris Lattner6b6b5372008-06-26 18:38:35 +00002298 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002299 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002300 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002301
Peter Collingbourne7a730022010-11-23 20:45:58 +00002302 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002303 Arg = Arg->IgnoreParenCasts();
2304 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002305
Douglas Gregor5cee1192011-07-27 05:40:30 +00002306 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002307 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2308 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002309 return;
2310 }
Mike Stumpbf916502009-07-24 19:02:52 +00002311
Chris Lattner5f9e2722011-07-23 10:55:15 +00002312 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002313 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002314
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002315 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002316 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002317 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002318 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002319 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002320 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002321 else if (TypeStr == "protected") {
2322 // Complain about attempts to use protected visibility on targets
2323 // (like Darwin) that don't support it.
2324 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2325 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2326 type = VisibilityAttr::Default;
2327 } else {
2328 type = VisibilityAttr::Protected;
2329 }
2330 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002331 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002332 return;
2333 }
Mike Stumpbf916502009-07-24 19:02:52 +00002334
Michael Han51d8c522013-01-24 16:46:58 +00002335 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002336 clang::Attr *newAttr;
2337 if (isTypeVisibility) {
2338 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2339 (TypeVisibilityAttr::VisibilityType) type,
2340 Index);
2341 } else {
2342 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2343 }
2344 if (newAttr)
2345 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002346}
2347
Chandler Carruth1b03c872011-07-02 00:01:44 +00002348static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2349 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002350 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2351 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002352 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002353 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002354 return;
2355 }
2356
Chandler Carruth87c44602011-07-01 23:49:12 +00002357 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2358 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002359 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002360 << Attr.getName() << 1 << AANT_ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002361 } else {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2363 << Attr.getName() << 0;
John McCalld5313b02011-03-02 11:33:24 +00002364 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002365 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002366 return;
2367 }
2368
Chris Lattner5f9e2722011-07-23 10:55:15 +00002369 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002370 ObjCMethodFamilyAttr::FamilyKind family;
2371 if (param == "none")
2372 family = ObjCMethodFamilyAttr::OMF_None;
2373 else if (param == "alloc")
2374 family = ObjCMethodFamilyAttr::OMF_alloc;
2375 else if (param == "copy")
2376 family = ObjCMethodFamilyAttr::OMF_copy;
2377 else if (param == "init")
2378 family = ObjCMethodFamilyAttr::OMF_init;
2379 else if (param == "mutableCopy")
2380 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2381 else if (param == "new")
2382 family = ObjCMethodFamilyAttr::OMF_new;
2383 else {
2384 // Just warn and ignore it. This is future-proof against new
2385 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002386 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002387 return;
2388 }
2389
John McCallf85e1932011-06-15 23:02:42 +00002390 if (family == ObjCMethodFamilyAttr::OMF_init &&
2391 !method->getResultType()->isObjCObjectPointerType()) {
2392 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2393 << method->getResultType();
2394 // Ignore the attribute.
2395 return;
2396 }
2397
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002398 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002399 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002400}
2401
Chandler Carruth1b03c872011-07-02 00:01:44 +00002402static void handleObjCExceptionAttr(Sema &S, Decl *D,
2403 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002404 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002405 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002406
Chris Lattner0db29ec2009-02-14 08:09:34 +00002407 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2408 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002409 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2410 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002411 return;
2412 }
Mike Stumpbf916502009-07-24 19:02:52 +00002413
Michael Han51d8c522013-01-24 16:46:58 +00002414 D->addAttr(::new (S.Context)
2415 ObjCExceptionAttr(Attr.getRange(), S.Context,
2416 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002417}
2418
Chandler Carruth1b03c872011-07-02 00:01:44 +00002419static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002420 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002421 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002422 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002423 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002424 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002425 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2426 return;
2427 }
2428 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002429 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2430 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002431 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002432 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2433 return;
2434 }
2435 }
2436 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002437 // It is okay to include this attribute on properties, e.g.:
2438 //
2439 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2440 //
2441 // In this case it follows tradition and suppresses an error in the above
2442 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002443 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002444 }
Michael Han51d8c522013-01-24 16:46:58 +00002445 D->addAttr(::new (S.Context)
2446 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2447 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002448}
2449
Mike Stumpbf916502009-07-24 19:02:52 +00002450static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002451handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002452 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002453 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002454
2455 if (!isa<FunctionDecl>(D)) {
2456 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2457 return;
2458 }
2459
Michael Han51d8c522013-01-24 16:46:58 +00002460 D->addAttr(::new (S.Context)
2461 OverloadableAttr(Attr.getRange(), S.Context,
2462 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002463}
2464
Chandler Carruth1b03c872011-07-02 00:01:44 +00002465static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002466 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002468 << Attr.getName() << 1 << AANT_ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002469 return;
2470 }
Mike Stumpbf916502009-07-24 19:02:52 +00002471
Steve Naroff9eae5762008-09-18 16:44:58 +00002472 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2474 << Attr.getName() << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002475 return;
2476 }
Mike Stumpbf916502009-07-24 19:02:52 +00002477
Sean Huntcf807c42010-08-18 23:23:40 +00002478 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002479 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002480 type = BlocksAttr::ByRef;
2481 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002482 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002483 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002484 return;
2485 }
Mike Stumpbf916502009-07-24 19:02:52 +00002486
Michael Han51d8c522013-01-24 16:46:58 +00002487 D->addAttr(::new (S.Context)
2488 BlocksAttr(Attr.getRange(), S.Context, type,
2489 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002490}
2491
Chandler Carruth1b03c872011-07-02 00:01:44 +00002492static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002493 // check the attribute arguments.
2494 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002495 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002496 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002497 }
2498
John McCall3323fad2011-09-09 07:56:05 +00002499 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002500 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002501 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002502 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002503 if (E->isTypeDependent() || E->isValueDependent() ||
2504 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002506 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002507 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002508 return;
2509 }
Mike Stumpbf916502009-07-24 19:02:52 +00002510
John McCall3323fad2011-09-09 07:56:05 +00002511 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002512 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2513 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002514 return;
2515 }
John McCall3323fad2011-09-09 07:56:05 +00002516
2517 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002518 }
2519
John McCall3323fad2011-09-09 07:56:05 +00002520 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002521 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002522 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002523 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002524 if (E->isTypeDependent() || E->isValueDependent() ||
2525 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002527 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002528 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002529 return;
2530 }
2531 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002532
John McCall3323fad2011-09-09 07:56:05 +00002533 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002534 // FIXME: This error message could be improved, it would be nice
2535 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002536 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2537 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002538 return;
2539 }
2540 }
2541
Chandler Carruth87c44602011-07-01 23:49:12 +00002542 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002543 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002544 if (isa<FunctionNoProtoType>(FT)) {
2545 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2546 return;
2547 }
Mike Stumpbf916502009-07-24 19:02:52 +00002548
Chris Lattner897cd902009-03-17 23:03:47 +00002549 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002550 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002551 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002552 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002553 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002554 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002555 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002556 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002557 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002558 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2559 if (!BD->isVariadic()) {
2560 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2561 return;
2562 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002563 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002564 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002565 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002566 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002567 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002568 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002569 int m = Ty->isFunctionPointerType() ? 0 : 1;
2570 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002571 return;
2572 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002573 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002575 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002576 return;
2577 }
Anders Carlsson77091822008-10-05 18:05:59 +00002578 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002579 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002580 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002581 return;
2582 }
Michael Han51d8c522013-01-24 16:46:58 +00002583 D->addAttr(::new (S.Context)
2584 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2585 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002586}
2587
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002588static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2589 // Check the attribute arguments.
2590 if (!checkAttributeNumArgs(S, Attr, 0))
2591 return;
2592
2593 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2594 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2595 else
2596 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2597}
2598
Chandler Carruth1b03c872011-07-02 00:01:44 +00002599static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002600 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002601 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002602 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002603
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002604 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002605 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002606 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002607 return;
2608 }
Mike Stumpbf916502009-07-24 19:02:52 +00002609
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002610 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2611 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2612 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002613 return;
2614 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002615 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2616 if (MD->getResultType()->isVoidType()) {
2617 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2618 << Attr.getName() << 1;
2619 return;
2620 }
2621
Michael Han51d8c522013-01-24 16:46:58 +00002622 D->addAttr(::new (S.Context)
2623 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2624 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002625}
2626
Chandler Carruth1b03c872011-07-02 00:01:44 +00002627static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002628 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002629 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002630 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2631 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002632 return;
2633 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002634
Chandler Carruth87c44602011-07-01 23:49:12 +00002635 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002636 if (isa<CXXRecordDecl>(D)) {
2637 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2638 return;
2639 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2641 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002642 return;
2643 }
2644
Chandler Carruth87c44602011-07-01 23:49:12 +00002645 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002646
Michael Han51d8c522013-01-24 16:46:58 +00002647 nd->addAttr(::new (S.Context)
2648 WeakAttr(Attr.getRange(), S.Context,
2649 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002650}
2651
Chandler Carruth1b03c872011-07-02 00:01:44 +00002652static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002653 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002654 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002655 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002656
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002657
2658 // weak_import only applies to variable & function declarations.
2659 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002660 if (!D->canBeWeakImported(isDef)) {
2661 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2663 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002664 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002665 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002666 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002667 // Nothing to warn about here.
2668 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002670 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002671
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002672 return;
2673 }
2674
Michael Han51d8c522013-01-24 16:46:58 +00002675 D->addAttr(::new (S.Context)
2676 WeakImportAttr(Attr.getRange(), S.Context,
2677 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002678}
2679
Tanya Lattner0df579e2012-07-09 22:06:01 +00002680// Handles reqd_work_group_size and work_group_size_hint.
2681static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002682 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002683 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2684 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2685
Nate Begeman6f3d8382009-06-26 06:32:41 +00002686 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002687 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002688
2689 unsigned WGSize[3];
2690 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002691 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002692 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002693 if (E->isTypeDependent() || E->isValueDependent() ||
2694 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2696 << Attr.getName() << AANT_ArgumentIntegerConstant
2697 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002698 return;
2699 }
2700 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2701 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002702
2703 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2704 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2705 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2706 if (!(A->getXDim() == WGSize[0] &&
2707 A->getYDim() == WGSize[1] &&
2708 A->getZDim() == WGSize[2])) {
2709 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2710 Attr.getName();
2711 }
2712 }
2713
2714 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2715 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2716 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2717 if (!(A->getXDim() == WGSize[0] &&
2718 A->getYDim() == WGSize[1] &&
2719 A->getZDim() == WGSize[2])) {
2720 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2721 Attr.getName();
2722 }
2723 }
2724
2725 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2726 D->addAttr(::new (S.Context)
2727 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002728 WGSize[0], WGSize[1], WGSize[2],
2729 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002730 else
2731 D->addAttr(::new (S.Context)
2732 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002733 WGSize[0], WGSize[1], WGSize[2],
2734 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002735}
2736
Joey Gouly37453b92013-03-08 09:42:32 +00002737static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2738 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2739
2740 // Attribute has 1 argument.
2741 if (!checkAttributeNumArgs(S, Attr, 1))
2742 return;
2743
2744 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2745
2746 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2747 (ParmType->isBooleanType() ||
2748 !ParmType->isIntegralType(S.getASTContext()))) {
2749 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2750 << ParmType;
2751 return;
2752 }
2753
2754 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2755 D->hasAttr<VecTypeHintAttr>()) {
2756 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2757 if (A->getTypeHint() != ParmType) {
2758 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2759 return;
2760 }
2761 }
2762
2763 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2764 ParmType, Attr.getLoc()));
2765}
2766
Joey Gouly96cead52013-03-14 09:54:43 +00002767static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2768 if (!dyn_cast<VarDecl>(D))
2769 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2770 << 9;
2771 StringRef EndianType = Attr.getParameterName()->getName();
2772 if (EndianType != "host" && EndianType != "device")
2773 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2774}
2775
Rafael Espindola599f1b72012-05-13 03:25:18 +00002776SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002777 StringRef Name,
2778 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002779 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2780 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002781 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002782 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2783 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002784 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002785 }
Michael Han51d8c522013-01-24 16:46:58 +00002786 return ::new (Context) SectionAttr(Range, Context, Name,
2787 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002788}
2789
Chandler Carruth1b03c872011-07-02 00:01:44 +00002790static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002791 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002792 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002793 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002794
2795 // Make sure that there is a string literal as the sections's single
2796 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002797 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002798 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002799 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002800 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2801 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002802 return;
2803 }
Mike Stump1eb44332009-09-09 15:08:12 +00002804
Chris Lattner797c3c42009-08-10 19:03:04 +00002805 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002806 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002807 if (!Error.empty()) {
2808 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2809 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002810 return;
2811 }
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002813 // This attribute cannot be applied to local variables.
2814 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2815 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2816 return;
2817 }
Michael Han51d8c522013-01-24 16:46:58 +00002818
2819 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002820 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002821 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002822 if (NewAttr)
2823 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002824}
2825
Chris Lattner6b6b5372008-06-26 18:38:35 +00002826
Chandler Carruth1b03c872011-07-02 00:01:44 +00002827static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002828 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002829 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2831 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002832 return;
2833 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002834
Chandler Carruth87c44602011-07-01 23:49:12 +00002835 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002836 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002837 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002838 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002839 D->addAttr(::new (S.Context)
2840 NoThrowAttr(Attr.getRange(), S.Context,
2841 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002842 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002843}
2844
Chandler Carruth1b03c872011-07-02 00:01:44 +00002845static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002846 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002847 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002848 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2849 << Attr.getName() << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002850 return;
2851 }
Mike Stumpbf916502009-07-24 19:02:52 +00002852
Chandler Carruth87c44602011-07-01 23:49:12 +00002853 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002854 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002855 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002856 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002857 D->addAttr(::new (S.Context)
2858 ConstAttr(Attr.getRange(), S.Context,
2859 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002860 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002861}
2862
Chandler Carruth1b03c872011-07-02 00:01:44 +00002863static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002864 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002865 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002866 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002867
Michael Han51d8c522013-01-24 16:46:58 +00002868 D->addAttr(::new (S.Context)
2869 PureAttr(Attr.getRange(), S.Context,
2870 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002871}
2872
Chandler Carruth1b03c872011-07-02 00:01:44 +00002873static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002874 if (!Attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2876 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002877 return;
2878 }
Mike Stumpbf916502009-07-24 19:02:52 +00002879
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002880 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002881 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2882 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002883 return;
2884 }
Mike Stumpbf916502009-07-24 19:02:52 +00002885
Chandler Carruth87c44602011-07-01 23:49:12 +00002886 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002887
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002888 if (!VD || !VD->hasLocalStorage()) {
2889 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2890 return;
2891 }
Mike Stumpbf916502009-07-24 19:02:52 +00002892
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002893 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002894 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002895 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002896 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2897 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002898 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002899 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002900 Attr.getParameterName();
2901 return;
2902 }
Mike Stumpbf916502009-07-24 19:02:52 +00002903
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002904 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2905 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002906 S.Diag(Attr.getParameterLoc(),
2907 diag::err_attribute_cleanup_arg_not_function)
2908 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002909 return;
2910 }
2911
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002912 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002913 S.Diag(Attr.getParameterLoc(),
2914 diag::err_attribute_cleanup_func_must_take_one_arg)
2915 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002916 return;
2917 }
Mike Stumpbf916502009-07-24 19:02:52 +00002918
Anders Carlsson89941c12009-02-07 23:16:50 +00002919 // We're currently more strict than GCC about what function types we accept.
2920 // If this ever proves to be a problem it should be easy to fix.
2921 QualType Ty = S.Context.getPointerType(VD->getType());
2922 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002923 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2924 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002925 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002926 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2927 Attr.getParameterName() << ParamTy << Ty;
2928 return;
2929 }
Mike Stumpbf916502009-07-24 19:02:52 +00002930
Michael Han51d8c522013-01-24 16:46:58 +00002931 D->addAttr(::new (S.Context)
2932 CleanupAttr(Attr.getRange(), S.Context, FD,
2933 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002934 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00002935 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002936}
2937
Mike Stumpbf916502009-07-24 19:02:52 +00002938/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002939/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002940static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002941 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002942 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002943
Chandler Carruth87c44602011-07-01 23:49:12 +00002944 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002946 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002947 return;
2948 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002949
Peter Collingbourne7a730022010-11-23 20:45:58 +00002950 Expr *IdxExpr = Attr.getArg(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002951 uint64_t ArgIdx;
2952 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2953 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002954 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002955
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002956 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002957 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002958
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002959 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2960 if (not_nsstring_type &&
2961 !isCFStringType(Ty, S.Context) &&
2962 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002963 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002964 // FIXME: Should highlight the actual expression that has the wrong type.
2965 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002966 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002967 << IdxExpr->getSourceRange();
2968 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002969 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002970 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002971 if (!isNSStringType(Ty, S.Context) &&
2972 !isCFStringType(Ty, S.Context) &&
2973 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002974 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002975 // FIXME: Should highlight the actual expression that has the wrong type.
2976 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002977 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002978 << IdxExpr->getSourceRange();
2979 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002980 }
2981
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002982 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2983 // because that has corrected for the implicit this parameter, and is zero-
2984 // based. The attribute expects what the user wrote explicitly.
2985 llvm::APSInt Val;
2986 IdxExpr->EvaluateAsInt(Val, S.Context);
2987
Michael Han51d8c522013-01-24 16:46:58 +00002988 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002989 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002990 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002991}
2992
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002993enum FormatAttrKind {
2994 CFStringFormat,
2995 NSStringFormat,
2996 StrftimeFormat,
2997 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002998 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002999 InvalidFormat
3000};
3001
3002/// getFormatAttrKind - Map from format attribute names to supported format
3003/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003004static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003005 return llvm::StringSwitch<FormatAttrKind>(Format)
3006 // Check for formats that get handled specially.
3007 .Case("NSString", NSStringFormat)
3008 .Case("CFString", CFStringFormat)
3009 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003010
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003011 // Otherwise, check for supported formats.
3012 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3013 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3014 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003015
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003016 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3017 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003018}
3019
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003020/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003021/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003022static void handleInitPriorityAttr(Sema &S, Decl *D,
3023 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003024 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003025 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3026 return;
3027 }
3028
Chandler Carruth87c44602011-07-01 23:49:12 +00003029 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003030 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3031 Attr.setInvalid();
3032 return;
3033 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003034 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003035 if (S.Context.getAsArrayType(T))
3036 T = S.Context.getBaseElementType(T);
3037 if (!T->getAs<RecordType>()) {
3038 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3039 Attr.setInvalid();
3040 return;
3041 }
3042
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003043 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003044 Attr.setInvalid();
3045 return;
3046 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003047 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003048
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003049 llvm::APSInt priority(32);
3050 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3051 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3053 << Attr.getName() << AANT_ArgumentIntegerConstant
3054 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003055 Attr.setInvalid();
3056 return;
3057 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003058 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003059 if (prioritynum < 101 || prioritynum > 65535) {
3060 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3061 << priorityExpr->getSourceRange();
3062 Attr.setInvalid();
3063 return;
3064 }
Michael Han51d8c522013-01-24 16:46:58 +00003065 D->addAttr(::new (S.Context)
3066 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3067 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003068}
3069
Rafael Espindola599f1b72012-05-13 03:25:18 +00003070FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003071 int FormatIdx, int FirstArg,
3072 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003073 // Check whether we already have an equivalent format attribute.
3074 for (specific_attr_iterator<FormatAttr>
3075 i = D->specific_attr_begin<FormatAttr>(),
3076 e = D->specific_attr_end<FormatAttr>();
3077 i != e ; ++i) {
3078 FormatAttr *f = *i;
3079 if (f->getType() == Format &&
3080 f->getFormatIdx() == FormatIdx &&
3081 f->getFirstArg() == FirstArg) {
3082 // If we don't have a valid location for this attribute, adopt the
3083 // location.
3084 if (f->getLocation().isInvalid())
3085 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003086 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003087 }
3088 }
3089
Michael Han51d8c522013-01-24 16:46:58 +00003090 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3091 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003092}
3093
Mike Stumpbf916502009-07-24 19:02:52 +00003094/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003095/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003096static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003097
Chris Lattner545dd342008-06-28 23:36:30 +00003098 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003100 << Attr.getName() << 1 << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003101 return;
3102 }
3103
Chris Lattner545dd342008-06-28 23:36:30 +00003104 if (Attr.getNumArgs() != 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3106 << Attr.getName() << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003107 return;
3108 }
3109
Chandler Carruth87c44602011-07-01 23:49:12 +00003110 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003111 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003112 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003113 return;
3114 }
3115
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003116 // In C++ the implicit 'this' function parameter also counts, and they are
3117 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003118 bool HasImplicitThisParam = isInstanceMethod(D);
3119 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003120 unsigned FirstIdx = 1;
3121
Chris Lattner5f9e2722011-07-23 10:55:15 +00003122 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003123
3124 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003125 if (Format.startswith("__") && Format.endswith("__"))
3126 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003127
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003128 // Check for supported formats.
3129 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003130
3131 if (Kind == IgnoredFormat)
3132 return;
3133
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003134 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003135 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003136 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003137 return;
3138 }
3139
3140 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003141 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003142 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003143 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3144 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003145 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003146 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003147 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003148 return;
3149 }
3150
3151 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003153 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003154 return;
3155 }
3156
3157 // FIXME: Do we need to bounds check?
3158 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003159
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003160 if (HasImplicitThisParam) {
3161 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003162 S.Diag(Attr.getLoc(),
3163 diag::err_format_attribute_implicit_this_format_string)
3164 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003165 return;
3166 }
3167 ArgIdx--;
3168 }
Mike Stump1eb44332009-09-09 15:08:12 +00003169
Chris Lattner6b6b5372008-06-26 18:38:35 +00003170 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003171 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003172
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003173 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003174 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003175 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3176 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003177 return;
3178 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003179 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003180 // FIXME: do we need to check if the type is NSString*? What are the
3181 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003182 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003183 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003184 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3185 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003186 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003187 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003188 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003189 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003190 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003191 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3192 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003193 return;
3194 }
3195
3196 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003197 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003198 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003199 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3200 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003202 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003203 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003204 return;
3205 }
3206
3207 // check if the function is variadic if the 3rd argument non-zero
3208 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003209 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003210 ++NumArgs; // +1 for ...
3211 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003212 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003213 return;
3214 }
3215 }
3216
Chris Lattner3c73c412008-11-19 08:23:25 +00003217 // strftime requires FirstArg to be 0 because it doesn't read from any
3218 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003219 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003220 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003221 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3222 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003223 return;
3224 }
3225 // if 0 it disables parameter checking (to use with e.g. va_list)
3226 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003228 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003229 return;
3230 }
3231
Rafael Espindola599f1b72012-05-13 03:25:18 +00003232 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3233 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003234 FirstArg.getZExtValue(),
3235 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003236 if (NewAttr)
3237 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003238}
3239
Chandler Carruth1b03c872011-07-02 00:01:44 +00003240static void handleTransparentUnionAttr(Sema &S, Decl *D,
3241 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003243 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003244 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003245
Chris Lattner6b6b5372008-06-26 18:38:35 +00003246
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003247 // Try to find the underlying union declaration.
3248 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003249 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003250 if (TD && TD->getUnderlyingType()->isUnionType())
3251 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3252 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003253 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003254
3255 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003256 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003257 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003258 return;
3259 }
3260
John McCall5e1cdac2011-10-07 06:10:15 +00003261 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003262 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003263 diag::warn_transparent_union_attribute_not_definition);
3264 return;
3265 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003266
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003267 RecordDecl::field_iterator Field = RD->field_begin(),
3268 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003269 if (Field == FieldEnd) {
3270 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3271 return;
3272 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003273
David Blaikie581deb32012-06-06 20:45:41 +00003274 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003275 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003276 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003277 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003278 diag::warn_transparent_union_attribute_floating)
3279 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003280 return;
3281 }
3282
3283 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3284 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3285 for (; Field != FieldEnd; ++Field) {
3286 QualType FieldType = Field->getType();
3287 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3288 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3289 // Warn if we drop the attribute.
3290 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003291 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003292 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003293 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003294 diag::warn_transparent_union_attribute_field_size_align)
3295 << isSize << Field->getDeclName() << FieldBits;
3296 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003297 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003298 diag::note_transparent_union_first_field_size_align)
3299 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003300 return;
3301 }
3302 }
3303
Michael Han51d8c522013-01-24 16:46:58 +00003304 RD->addAttr(::new (S.Context)
3305 TransparentUnionAttr(Attr.getRange(), S.Context,
3306 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307}
3308
Chandler Carruth1b03c872011-07-02 00:01:44 +00003309static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003310 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003311 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003312 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003313
Peter Collingbourne7a730022010-11-23 20:45:58 +00003314 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003315 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003316
Chris Lattner6b6b5372008-06-26 18:38:35 +00003317 // Make sure that there is a string literal as the annotation's single
3318 // argument.
3319 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003320 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3321 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003322 return;
3323 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003324
3325 // Don't duplicate annotations that are already set.
3326 for (specific_attr_iterator<AnnotateAttr>
3327 i = D->specific_attr_begin<AnnotateAttr>(),
3328 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3329 if ((*i)->getAnnotation() == SE->getString())
3330 return;
3331 }
Michael Han51d8c522013-01-24 16:46:58 +00003332
3333 D->addAttr(::new (S.Context)
3334 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3335 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003336}
3337
Chandler Carruth1b03c872011-07-02 00:01:44 +00003338static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003339 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003340 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003341 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3342 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003343 return;
3344 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003345
Richard Smithbe507b62013-02-01 08:12:08 +00003346 if (Attr.getNumArgs() == 0) {
3347 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3348 true, 0, Attr.getAttributeSpellingListIndex()));
3349 return;
3350 }
3351
Richard Smithf6565a92013-02-22 08:32:16 +00003352 Expr *E = Attr.getArg(0);
3353 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3354 S.Diag(Attr.getEllipsisLoc(),
3355 diag::err_pack_expansion_without_parameter_packs);
3356 return;
3357 }
3358
3359 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3360 return;
3361
3362 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3363 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003364}
3365
3366void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003367 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003368 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3369 SourceLocation AttrLoc = AttrRange.getBegin();
3370
Richard Smith4cd81c52013-01-29 09:02:09 +00003371 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003372 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003373 // C++11 [dcl.align]p1:
3374 // An alignment-specifier may be applied to a variable or to a class
3375 // data member, but it shall not be applied to a bit-field, a function
3376 // parameter, the formal parameter of a catch clause, or a variable
3377 // declared with the register storage class specifier. An
3378 // alignment-specifier may also be applied to the declaration of a class
3379 // or enumeration type.
3380 // C11 6.7.5/2:
3381 // An alignment attribute shall not be specified in a declaration of
3382 // a typedef, or a bit-field, or a function, or a parameter, or an
3383 // object declared with the register storage-class specifier.
3384 int DiagKind = -1;
3385 if (isa<ParmVarDecl>(D)) {
3386 DiagKind = 0;
3387 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3388 if (VD->getStorageClass() == SC_Register)
3389 DiagKind = 1;
3390 if (VD->isExceptionVariable())
3391 DiagKind = 2;
3392 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3393 if (FD->isBitField())
3394 DiagKind = 3;
3395 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003396 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3397 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003398 << (TmpAttr.isC11() ? ExpectedVariableOrField
3399 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003400 return;
3401 }
3402 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003403 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003404 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003405 return;
3406 }
3407 }
3408
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003409 if (E->isTypeDependent() || E->isValueDependent()) {
3410 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003411 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3412 AA->setPackExpansion(IsPackExpansion);
3413 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003414 return;
3415 }
Michael Hana31f65b2013-02-01 01:19:17 +00003416
Sean Huntcf807c42010-08-18 23:23:40 +00003417 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003418 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003419 ExprResult ICE
3420 = VerifyIntegerConstantExpression(E, &Alignment,
3421 diag::err_aligned_attribute_argument_not_int,
3422 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003423 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003424 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003425
3426 // C++11 [dcl.align]p2:
3427 // -- if the constant expression evaluates to zero, the alignment
3428 // specifier shall have no effect
3429 // C11 6.7.5p6:
3430 // An alignment specification of zero has no effect.
3431 if (!(TmpAttr.isAlignas() && !Alignment) &&
3432 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003433 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3434 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003435 return;
3436 }
Michael Hana31f65b2013-02-01 01:19:17 +00003437
Richard Smithbe507b62013-02-01 08:12:08 +00003438 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003439 // We've already verified it's a power of 2, now let's make sure it's
3440 // 8192 or less.
3441 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003442 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003443 << E->getSourceRange();
3444 return;
3445 }
3446 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003447
Richard Smithf6565a92013-02-22 08:32:16 +00003448 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3449 ICE.take(), SpellingListIndex);
3450 AA->setPackExpansion(IsPackExpansion);
3451 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003452}
3453
Michael Hana31f65b2013-02-01 01:19:17 +00003454void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003455 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003456 // FIXME: Cache the number on the Attr object if non-dependent?
3457 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003458 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3459 SpellingListIndex);
3460 AA->setPackExpansion(IsPackExpansion);
3461 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003462}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003463
Richard Smithbe507b62013-02-01 08:12:08 +00003464void Sema::CheckAlignasUnderalignment(Decl *D) {
3465 assert(D->hasAttrs() && "no attributes on decl");
3466
3467 QualType Ty;
3468 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3469 Ty = VD->getType();
3470 else
3471 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003472 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003473 return;
3474
3475 // C++11 [dcl.align]p5, C11 6.7.5/4:
3476 // The combined effect of all alignment attributes in a declaration shall
3477 // not specify an alignment that is less strict than the alignment that
3478 // would otherwise be required for the entity being declared.
3479 AlignedAttr *AlignasAttr = 0;
3480 unsigned Align = 0;
3481 for (specific_attr_iterator<AlignedAttr>
3482 I = D->specific_attr_begin<AlignedAttr>(),
3483 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3484 if (I->isAlignmentDependent())
3485 return;
3486 if (I->isAlignas())
3487 AlignasAttr = *I;
3488 Align = std::max(Align, I->getAlignment(Context));
3489 }
3490
3491 if (AlignasAttr && Align) {
3492 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3493 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3494 if (NaturalAlign > RequestedAlign)
3495 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3496 << Ty << (unsigned)NaturalAlign.getQuantity();
3497 }
3498}
3499
Chandler Carruthd309c812011-07-01 23:49:16 +00003500/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003501/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003502///
Mike Stumpbf916502009-07-24 19:02:52 +00003503/// Despite what would be logical, the mode attribute is a decl attribute, not a
3504/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3505/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003506static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003507 // This attribute isn't documented, but glibc uses it. It changes
3508 // the width of an int or unsigned int to the specified size.
3509
3510 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003511 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003512 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003513
Chris Lattnerfbf13472008-06-27 22:18:37 +00003514
3515 IdentifierInfo *Name = Attr.getParameterName();
3516 if (!Name) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003517 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3518 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003519 return;
3520 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003521
Chris Lattner5f9e2722011-07-23 10:55:15 +00003522 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003523
3524 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003525 if (Str.startswith("__") && Str.endswith("__"))
3526 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003527
3528 unsigned DestWidth = 0;
3529 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003530 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003531 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003532 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003533 switch (Str[0]) {
3534 case 'Q': DestWidth = 8; break;
3535 case 'H': DestWidth = 16; break;
3536 case 'S': DestWidth = 32; break;
3537 case 'D': DestWidth = 64; break;
3538 case 'X': DestWidth = 96; break;
3539 case 'T': DestWidth = 128; break;
3540 }
3541 if (Str[1] == 'F') {
3542 IntegerMode = false;
3543 } else if (Str[1] == 'C') {
3544 IntegerMode = false;
3545 ComplexMode = true;
3546 } else if (Str[1] != 'I') {
3547 DestWidth = 0;
3548 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003549 break;
3550 case 4:
3551 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3552 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003553 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003554 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003555 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003556 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003557 break;
3558 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003559 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003560 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003561 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003562 case 11:
3563 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003564 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003565 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003566 }
3567
3568 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003569 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003570 OldTy = TD->getUnderlyingType();
3571 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3572 OldTy = VD->getType();
3573 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003574 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003575 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003576 return;
3577 }
Eli Friedman73397492009-03-03 06:41:03 +00003578
John McCall183700f2009-09-21 23:43:11 +00003579 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003580 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3581 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003582 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003583 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3584 } else if (ComplexMode) {
3585 if (!OldTy->isComplexType())
3586 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3587 } else {
3588 if (!OldTy->isFloatingType())
3589 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3590 }
3591
Mike Stump390b4cc2009-05-16 07:39:55 +00003592 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3593 // and friends, at least with glibc.
3594 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3595 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003596 // FIXME: Make sure floating-point mappings are accurate
3597 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003598 QualType NewTy;
3599 switch (DestWidth) {
3600 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003601 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003602 return;
3603 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003604 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003605 return;
3606 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003607 if (!IntegerMode) {
3608 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3609 return;
3610 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003611 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003612 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003613 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003614 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003615 break;
3616 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003617 if (!IntegerMode) {
3618 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3619 return;
3620 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003621 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003622 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003623 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003624 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003625 break;
3626 case 32:
3627 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003628 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003629 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003630 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003631 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003632 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003633 break;
3634 case 64:
3635 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003636 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003637 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003638 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003639 NewTy = S.Context.LongTy;
3640 else
3641 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003642 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003643 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003644 NewTy = S.Context.UnsignedLongTy;
3645 else
3646 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003647 break;
Eli Friedman73397492009-03-03 06:41:03 +00003648 case 96:
3649 NewTy = S.Context.LongDoubleTy;
3650 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003651 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003652 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3653 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003654 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3655 return;
3656 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003657 if (IntegerMode) {
3658 if (OldTy->isSignedIntegerType())
3659 NewTy = S.Context.Int128Ty;
3660 else
3661 NewTy = S.Context.UnsignedInt128Ty;
3662 } else
3663 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003664 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003665 }
3666
Eli Friedman73397492009-03-03 06:41:03 +00003667 if (ComplexMode) {
3668 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003669 }
3670
3671 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003672 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3673 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3674 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003675 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003676
3677 D->addAttr(::new (S.Context)
3678 ModeAttr(Attr.getRange(), S.Context, Name,
3679 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003680}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003681
Chandler Carruth1b03c872011-07-02 00:01:44 +00003682static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003683 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003684 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003685 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003686
Nick Lewycky78d1a102012-07-24 01:40:49 +00003687 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3688 if (!VD->hasGlobalStorage())
3689 S.Diag(Attr.getLoc(),
3690 diag::warn_attribute_requires_functions_or_static_globals)
3691 << Attr.getName();
3692 } else if (!isFunctionOrMethod(D)) {
3693 S.Diag(Attr.getLoc(),
3694 diag::warn_attribute_requires_functions_or_static_globals)
3695 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003696 return;
3697 }
Mike Stumpbf916502009-07-24 19:02:52 +00003698
Michael Han51d8c522013-01-24 16:46:58 +00003699 D->addAttr(::new (S.Context)
3700 NoDebugAttr(Attr.getRange(), S.Context,
3701 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003702}
3703
Chandler Carruth1b03c872011-07-02 00:01:44 +00003704static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003705 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003706 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003707 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003708
Mike Stumpbf916502009-07-24 19:02:52 +00003709
Chandler Carruth87c44602011-07-01 23:49:12 +00003710 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003711 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003712 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003713 return;
3714 }
Mike Stumpbf916502009-07-24 19:02:52 +00003715
Michael Han51d8c522013-01-24 16:46:58 +00003716 D->addAttr(::new (S.Context)
3717 NoInlineAttr(Attr.getRange(), S.Context,
3718 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003719}
3720
Chandler Carruth1b03c872011-07-02 00:01:44 +00003721static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3722 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003723 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003724 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003725 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003726
Chris Lattner7255a2d2010-06-22 00:03:40 +00003727
Chandler Carruth87c44602011-07-01 23:49:12 +00003728 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003730 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003731 return;
3732 }
3733
Michael Han51d8c522013-01-24 16:46:58 +00003734 D->addAttr(::new (S.Context)
3735 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3736 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003737}
3738
Chandler Carruth1b03c872011-07-02 00:01:44 +00003739static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003740 if (S.LangOpts.CUDA) {
3741 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003742 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003743 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3744 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003745 return;
3746 }
3747
Chandler Carruth87c44602011-07-01 23:49:12 +00003748 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003750 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003751 return;
3752 }
3753
Michael Han51d8c522013-01-24 16:46:58 +00003754 D->addAttr(::new (S.Context)
3755 CUDAConstantAttr(Attr.getRange(), S.Context,
3756 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003757 } else {
3758 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3759 }
3760}
3761
Chandler Carruth1b03c872011-07-02 00:01:44 +00003762static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003763 if (S.LangOpts.CUDA) {
3764 // check the attribute arguments.
3765 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3767 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003768 return;
3769 }
3770
Chandler Carruth87c44602011-07-01 23:49:12 +00003771 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003772 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003773 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003774 return;
3775 }
3776
Michael Han51d8c522013-01-24 16:46:58 +00003777 D->addAttr(::new (S.Context)
3778 CUDADeviceAttr(Attr.getRange(), S.Context,
3779 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003780 } else {
3781 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3782 }
3783}
3784
Chandler Carruth1b03c872011-07-02 00:01:44 +00003785static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003786 if (S.LangOpts.CUDA) {
3787 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003788 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003789 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003790
Chandler Carruth87c44602011-07-01 23:49:12 +00003791 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003792 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003793 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003794 return;
3795 }
3796
Chandler Carruth87c44602011-07-01 23:49:12 +00003797 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003798 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003799 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003800 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003801 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3802 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003803 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003804 "void");
3805 } else {
3806 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3807 << FD->getType();
3808 }
3809 return;
3810 }
3811
Michael Han51d8c522013-01-24 16:46:58 +00003812 D->addAttr(::new (S.Context)
3813 CUDAGlobalAttr(Attr.getRange(), S.Context,
3814 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003815 } else {
3816 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3817 }
3818}
3819
Chandler Carruth1b03c872011-07-02 00:01:44 +00003820static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003821 if (S.LangOpts.CUDA) {
3822 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003823 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003824 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003825
Peter Collingbourneced76712010-12-01 03:15:31 +00003826
Chandler Carruth87c44602011-07-01 23:49:12 +00003827 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003829 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003830 return;
3831 }
3832
Michael Han51d8c522013-01-24 16:46:58 +00003833 D->addAttr(::new (S.Context)
3834 CUDAHostAttr(Attr.getRange(), S.Context,
3835 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003836 } else {
3837 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3838 }
3839}
3840
Chandler Carruth1b03c872011-07-02 00:01:44 +00003841static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003842 if (S.LangOpts.CUDA) {
3843 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003844 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003845 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003846
Chandler Carruth87c44602011-07-01 23:49:12 +00003847 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003848 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003849 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003850 return;
3851 }
3852
Michael Han51d8c522013-01-24 16:46:58 +00003853 D->addAttr(::new (S.Context)
3854 CUDASharedAttr(Attr.getRange(), S.Context,
3855 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003856 } else {
3857 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3858 }
3859}
3860
Chandler Carruth1b03c872011-07-02 00:01:44 +00003861static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003862 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003863 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003864 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003865
Chandler Carruth87c44602011-07-01 23:49:12 +00003866 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003867 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003868 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003869 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003870 return;
3871 }
Mike Stumpbf916502009-07-24 19:02:52 +00003872
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003873 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003874 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003875 return;
3876 }
Mike Stumpbf916502009-07-24 19:02:52 +00003877
Michael Han51d8c522013-01-24 16:46:58 +00003878 D->addAttr(::new (S.Context)
3879 GNUInlineAttr(Attr.getRange(), S.Context,
3880 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003881}
3882
Chandler Carruth1b03c872011-07-02 00:01:44 +00003883static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003884 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003885
Aaron Ballmanfff32482012-12-09 17:45:41 +00003886 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003887 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003888 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3889 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003890 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003891 return;
3892
Chandler Carruth87c44602011-07-01 23:49:12 +00003893 if (!isa<ObjCMethodDecl>(D)) {
3894 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3895 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003896 return;
3897 }
3898
Chandler Carruth87c44602011-07-01 23:49:12 +00003899 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003900 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003901 D->addAttr(::new (S.Context)
3902 FastCallAttr(Attr.getRange(), S.Context,
3903 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003904 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003905 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003906 D->addAttr(::new (S.Context)
3907 StdCallAttr(Attr.getRange(), S.Context,
3908 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003909 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003910 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003911 D->addAttr(::new (S.Context)
3912 ThisCallAttr(Attr.getRange(), S.Context,
3913 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003914 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003915 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003916 D->addAttr(::new (S.Context)
3917 CDeclAttr(Attr.getRange(), S.Context,
3918 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003919 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003920 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003921 D->addAttr(::new (S.Context)
3922 PascalAttr(Attr.getRange(), S.Context,
3923 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003924 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003925 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003926 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003927 switch (CC) {
3928 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003929 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003930 break;
3931 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003932 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003933 break;
3934 default:
3935 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003936 }
3937
Michael Han51d8c522013-01-24 16:46:58 +00003938 D->addAttr(::new (S.Context)
3939 PcsAttr(Attr.getRange(), S.Context, PCS,
3940 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003941 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003942 }
Derek Schuff263366f2012-10-16 22:30:41 +00003943 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003944 D->addAttr(::new (S.Context)
3945 PnaclCallAttr(Attr.getRange(), S.Context,
3946 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003947 return;
Guy Benyei38980082012-12-25 08:53:55 +00003948 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003949 D->addAttr(::new (S.Context)
3950 IntelOclBiccAttr(Attr.getRange(), S.Context,
3951 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003952 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003953
Abramo Bagnarae215f722010-04-30 13:10:51 +00003954 default:
3955 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003956 }
3957}
3958
Chandler Carruth1b03c872011-07-02 00:01:44 +00003959static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003960 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003961 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003962}
3963
Guy Benyei1db70402013-03-24 13:58:12 +00003964static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
3965 assert(!Attr.isInvalid());
3966
3967 Expr *E = Attr.getArg(0);
3968 llvm::APSInt ArgNum(32);
3969 if (E->isTypeDependent() || E->isValueDependent() ||
3970 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003971 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3972 << Attr.getName() << AANT_ArgumentIntegerConstant
3973 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003974 return;
3975 }
3976
3977 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3978 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3979}
3980
Aaron Ballmanfff32482012-12-09 17:45:41 +00003981bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3982 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003983 if (attr.isInvalid())
3984 return true;
3985
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003986 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3987 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003988 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3989 << attr.getName() << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003990 attr.setInvalid();
3991 return true;
3992 }
3993
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003994 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3995 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003996 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003997 case AttributeList::AT_CDecl: CC = CC_C; break;
3998 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3999 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4000 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4001 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
4002 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004003 Expr *Arg = attr.getArg(0);
4004 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004005 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004006 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4007 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004008 attr.setInvalid();
4009 return true;
4010 }
4011
Chris Lattner5f9e2722011-07-23 10:55:15 +00004012 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004013 if (StrRef == "aapcs") {
4014 CC = CC_AAPCS;
4015 break;
4016 } else if (StrRef == "aapcs-vfp") {
4017 CC = CC_AAPCS_VFP;
4018 break;
4019 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004020
4021 attr.setInvalid();
4022 Diag(attr.getLoc(), diag::err_invalid_pcs);
4023 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004024 }
Derek Schuff263366f2012-10-16 22:30:41 +00004025 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004026 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004027 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004028 }
4029
Aaron Ballman82bfa192012-10-02 14:26:08 +00004030 const TargetInfo &TI = Context.getTargetInfo();
4031 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4032 if (A == TargetInfo::CCCR_Warning) {
4033 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004034
4035 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4036 if (FD)
4037 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4038 TargetInfo::CCMT_NonMember;
4039 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004040 }
4041
John McCall711c52b2011-01-05 12:14:39 +00004042 return false;
4043}
4044
Chandler Carruth1b03c872011-07-02 00:01:44 +00004045static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004046 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004047
4048 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004049 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004050 return;
4051
Chandler Carruth87c44602011-07-01 23:49:12 +00004052 if (!isa<ObjCMethodDecl>(D)) {
4053 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4054 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004055 return;
4056 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004057
Michael Han51d8c522013-01-24 16:46:58 +00004058 D->addAttr(::new (S.Context)
4059 RegparmAttr(Attr.getRange(), S.Context, numParams,
4060 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004061}
4062
4063/// Checks a regparm attribute, returning true if it is ill-formed and
4064/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004065bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4066 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004067 return true;
4068
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004069 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004070 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004071 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004072 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004073
Chandler Carruth87c44602011-07-01 23:49:12 +00004074 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004075 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004076 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004077 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004078 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4079 << Attr.getName() << AANT_ArgumentIntegerConstant
4080 << 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)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004124 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004125 << 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)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004136 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004137 << 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)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004157 << Attr.getName() << /* arg num = */ 1 << AANT_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)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004208 << Attr.getName() << 1 << AANT_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 Ballman3cd6feb2013-07-30 01:31:03 +00004565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4566 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004567 return;
4568 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004569
David Majnemerbb0ed292013-08-09 08:56:20 +00004570 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4571 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004572 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004573 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4574 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004575
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004576 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004577 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004578 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4579 return;
4580 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004581
David Majnemerbb0ed292013-08-09 08:56:20 +00004582 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004583 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004584 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004585 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4586 return;
4587 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004588 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004589 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4590 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004591 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004592 }
Francois Pichet11542142010-12-19 06:50:37 +00004593
David Majnemerbb0ed292013-08-09 08:56:20 +00004594 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4595 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004596}
4597
John McCallc052dbb2012-05-22 21:28:12 +00004598static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004599 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004600 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004601
4602 AttributeList::Kind Kind = Attr.getKind();
4603 if (Kind == AttributeList::AT_SingleInheritance)
4604 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004605 ::new (S.Context)
4606 SingleInheritanceAttr(Attr.getRange(), S.Context,
4607 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004608 else if (Kind == AttributeList::AT_MultipleInheritance)
4609 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004610 ::new (S.Context)
4611 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4612 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004613 else if (Kind == AttributeList::AT_VirtualInheritance)
4614 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004615 ::new (S.Context)
4616 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4617 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004618}
4619
4620static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004621 if (!checkMicrosoftExt(S, Attr))
4622 return;
4623
4624 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004625 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004626 D->addAttr(
4627 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4628 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004629}
4630
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004631static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004632 if (!checkMicrosoftExt(S, Attr))
4633 return;
4634 D->addAttr(::new (S.Context)
4635 ForceInlineAttr(Attr.getRange(), S.Context,
4636 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004637}
4638
Reid Klecknera7225342013-05-20 14:02:37 +00004639static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4640 if (!checkMicrosoftExt(S, Attr))
4641 return;
4642 // Check linkage after possibly merging declaratinos. See
4643 // checkAttributesAfterMerging().
4644 D->addAttr(::new (S.Context)
4645 SelectAnyAttr(Attr.getRange(), S.Context,
4646 Attr.getAttributeSpellingListIndex()));
4647}
4648
Ted Kremenekb71368d2009-05-09 02:44:38 +00004649//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004650// Top Level Sema Entry Points
4651//===----------------------------------------------------------------------===//
4652
Chandler Carruth1b03c872011-07-02 00:01:44 +00004653static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4654 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004655 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004656 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4657 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4658 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004659 default:
4660 break;
4661 }
4662}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004663
Chandler Carruth1b03c872011-07-02 00:01:44 +00004664static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4665 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004666 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004667 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4668 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4669 case AttributeList::AT_IBOutletCollection:
4670 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004671 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004672 case AttributeList::AT_ObjCGC:
4673 case AttributeList::AT_VectorSize:
4674 case AttributeList::AT_NeonVectorType:
4675 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004676 case AttributeList::AT_Ptr32:
4677 case AttributeList::AT_Ptr64:
4678 case AttributeList::AT_SPtr:
4679 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004680 // Ignore these, these are type attributes, handled by
4681 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004682 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004683 case AttributeList::AT_CUDADevice:
4684 case AttributeList::AT_CUDAHost:
4685 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004686 // Ignore, this is a non-inheritable attribute, handled
4687 // by ProcessNonInheritableDeclAttr.
4688 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004689 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4690 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4691 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4692 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004693 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004694 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004695 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004696 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004697 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4698 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4699 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004700 handleDependencyAttr(S, scope, D, Attr);
4701 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004702 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4703 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4704 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004705 case AttributeList::AT_CXX11NoReturn:
4706 handleCXX11NoReturnAttr(S, D, Attr);
4707 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004708 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004709 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004710 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004711 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4712 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004713 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004714 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004715 case AttributeList::AT_MinSize:
4716 handleMinSizeAttr(S, D, Attr);
4717 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004718 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4719 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4720 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4721 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4722 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004723 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004724 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004725 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4726 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4727 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4728 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4729 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004730 case AttributeList::AT_ownership_returns:
4731 case AttributeList::AT_ownership_takes:
4732 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004733 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004734 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4735 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4736 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4737 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4738 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4739 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4740 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004741
Sean Hunt8e083e72012-06-19 23:57:03 +00004742 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004743 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004744 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004745 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004746
Sean Hunt8e083e72012-06-19 23:57:03 +00004747 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004748 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4749
Fariborz Jahanian84101132012-09-07 23:46:23 +00004750 case AttributeList::AT_ObjCRequiresSuper:
4751 handleObjCRequiresSuperAttr(S, D, Attr); break;
4752
Sean Hunt8e083e72012-06-19 23:57:03 +00004753 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004754 handleNSBridgedAttr(S, scope, D, Attr); break;
4755
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_CFAuditedTransfer:
4757 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004758 handleCFTransferAttr(S, D, Attr); break;
4759
Ted Kremenekb71368d2009-05-09 02:44:38 +00004760 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004761 case AttributeList::AT_CFConsumed:
4762 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4763 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004764 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004765
Sean Hunt8e083e72012-06-19 23:57:03 +00004766 case AttributeList::AT_NSReturnsAutoreleased:
4767 case AttributeList::AT_NSReturnsNotRetained:
4768 case AttributeList::AT_CFReturnsNotRetained:
4769 case AttributeList::AT_NSReturnsRetained:
4770 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004771 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004772
Tanya Lattner0df579e2012-07-09 22:06:01 +00004773 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004775 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004776
Joey Gouly37453b92013-03-08 09:42:32 +00004777 case AttributeList::AT_VecTypeHint:
4778 handleVecTypeHint(S, D, Attr); break;
4779
Joey Gouly96cead52013-03-14 09:54:43 +00004780 case AttributeList::AT_Endian:
4781 handleEndianAttr(S, D, Attr);
4782 break;
4783
Sean Hunt8e083e72012-06-19 23:57:03 +00004784 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004785 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004786
Sean Hunt8e083e72012-06-19 23:57:03 +00004787 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4788 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4789 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004790 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004791 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004792 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004793 handleArcWeakrefUnavailableAttr (S, D, Attr);
4794 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004795 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004796 handleObjCRootClassAttr(S, D, Attr);
4797 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004799 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004800 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004801 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4802 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004803 handleReturnsTwiceAttr(S, D, Attr);
4804 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004805 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004806 case AttributeList::AT_Visibility:
4807 handleVisibilityAttr(S, D, Attr, false);
4808 break;
4809 case AttributeList::AT_TypeVisibility:
4810 handleVisibilityAttr(S, D, Attr, true);
4811 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004812 case AttributeList::AT_WarnUnused:
4813 handleWarnUnusedAttr(S, D, Attr);
4814 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004815 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004816 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4818 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4819 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4820 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004821 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004822 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004823 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004824 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004827 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004828 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4830 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4831 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4832 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4833 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4834 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4835 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4836 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4837 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004838 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004839 // Just ignore
4840 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004841 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004842 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004843 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_StdCall:
4845 case AttributeList::AT_CDecl:
4846 case AttributeList::AT_FastCall:
4847 case AttributeList::AT_ThisCall:
4848 case AttributeList::AT_Pascal:
4849 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004850 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004851 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004852 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004853 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004854 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004855 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004856 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004857 case AttributeList::AT_OpenCLImageAccess:
4858 handleOpenCLImageAccessAttr(S, D, Attr);
4859 break;
John McCallc052dbb2012-05-22 21:28:12 +00004860
4861 // Microsoft attributes:
John McCall76da55d2013-04-16 07:28:30 +00004862 case AttributeList::AT_MsProperty: break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004863 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004864 handleMsStructAttr(S, D, Attr);
4865 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004866 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004867 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004868 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004869 case AttributeList::AT_SingleInheritance:
4870 case AttributeList::AT_MultipleInheritance:
4871 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004872 handleInheritanceAttr(S, D, Attr);
4873 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004875 handlePortabilityAttr(S, D, Attr);
4876 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004877 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004878 handleForceInlineAttr(S, D, Attr);
4879 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004880 case AttributeList::AT_SelectAny:
4881 handleSelectAnyAttr(S, D, Attr);
4882 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004883
4884 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004885 case AttributeList::AT_AssertExclusiveLock:
4886 handleAssertExclusiveLockAttr(S, D, Attr);
4887 break;
4888 case AttributeList::AT_AssertSharedLock:
4889 handleAssertSharedLockAttr(S, D, Attr);
4890 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004891 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004892 handleGuardedVarAttr(S, D, Attr);
4893 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004894 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004895 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004896 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004897 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004898 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004899 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004900 case AttributeList::AT_NoSanitizeAddress:
4901 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004902 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004903 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004904 handleNoThreadSafetyAnalysis(S, D, Attr);
4905 break;
4906 case AttributeList::AT_NoSanitizeThread:
4907 handleNoSanitizeThread(S, D, Attr);
4908 break;
4909 case AttributeList::AT_NoSanitizeMemory:
4910 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004911 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004912 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004913 handleLockableAttr(S, D, Attr);
4914 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004915 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004916 handleGuardedByAttr(S, D, Attr);
4917 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004918 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004919 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004920 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004921 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004922 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004923 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004924 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004925 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004926 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004927 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004928 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004929 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004930 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004931 handleLockReturnedAttr(S, D, Attr);
4932 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004933 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004934 handleLocksExcludedAttr(S, D, Attr);
4935 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004936 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004937 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004938 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004939 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004940 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004941 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004942 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004943 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004944 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004945 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004946 handleUnlockFunAttr(S, D, Attr);
4947 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004948 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004949 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004950 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004951 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004952 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004953 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004954
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004955 // Type safety attributes.
4956 case AttributeList::AT_ArgumentWithTypeTag:
4957 handleArgumentWithTypeTagAttr(S, D, Attr);
4958 break;
4959 case AttributeList::AT_TypeTagForDatatype:
4960 handleTypeTagForDatatypeAttr(S, D, Attr);
4961 break;
4962
Chris Lattner803d0802008-06-29 00:43:07 +00004963 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004964 // Ask target about the attribute.
4965 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4966 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004967 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4968 diag::warn_unhandled_ms_attribute_ignored :
4969 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004970 break;
4971 }
4972}
4973
Peter Collingbourne60700392011-01-21 02:08:45 +00004974/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4975/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smithcd8ab512013-01-17 01:30:42 +00004976/// silently ignore it if a GNU attribute.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004977static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4978 const AttributeList &Attr,
Richard Smithcd8ab512013-01-17 01:30:42 +00004979 bool NonInheritable, bool Inheritable,
4980 bool IncludeCXX11Attributes) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004981 if (Attr.isInvalid())
4982 return;
4983
Richard Smithcd8ab512013-01-17 01:30:42 +00004984 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4985 // instead.
4986 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4987 return;
4988
Peter Collingbourne60700392011-01-21 02:08:45 +00004989 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004990 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004991
4992 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004993 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004994}
4995
Chris Lattner803d0802008-06-29 00:43:07 +00004996/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4997/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004998void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004999 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005000 bool NonInheritable, bool Inheritable,
5001 bool IncludeCXX11Attributes) {
5002 for (const AttributeList* l = AttrList; l; l = l->getNext())
5003 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
5004 IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005005
5006 // GCC accepts
5007 // static int a9 __attribute__((weakref));
5008 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00005009 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005010 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005011 cast<NamedDecl>(D)->getNameAsString();
5012 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005013 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005014 }
5015}
5016
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005017// Annotation attributes are the only attributes allowed after an access
5018// specifier.
5019bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5020 const AttributeList *AttrList) {
5021 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005022 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005023 handleAnnotateAttr(*this, ASDecl, *l);
5024 } else {
5025 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5026 return true;
5027 }
5028 }
5029
5030 return false;
5031}
5032
John McCalle82247a2011-10-01 05:17:03 +00005033/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5034/// contains any decl attributes that we should warn about.
5035static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5036 for ( ; A; A = A->getNext()) {
5037 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005038 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005039 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5040
5041 if (A->getKind() == AttributeList::UnknownAttribute) {
5042 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5043 << A->getName() << A->getRange();
5044 } else {
5045 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5046 << A->getName() << A->getRange();
5047 }
5048 }
5049}
5050
5051/// checkUnusedDeclAttributes - Given a declarator which is not being
5052/// used to build a declaration, complain about any decl attributes
5053/// which might be lying around on it.
5054void Sema::checkUnusedDeclAttributes(Declarator &D) {
5055 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5056 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5057 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5058 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5059}
5060
Ryan Flynne25ff832009-07-30 03:15:39 +00005061/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005062/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005063NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5064 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005065 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005066 NamedDecl *NewD = 0;
5067 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005068 FunctionDecl *NewFD;
5069 // FIXME: Missing call to CheckFunctionDeclaration().
5070 // FIXME: Mangling?
5071 // FIXME: Is the qualifier info correct?
5072 // FIXME: Is the DeclContext correct?
5073 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5074 Loc, Loc, DeclarationName(II),
5075 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005076 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005077 FD->hasPrototype(),
5078 false/*isConstexprSpecified*/);
5079 NewD = NewFD;
5080
5081 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005082 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005083
5084 // Fake up parameter variables; they are declared as if this were
5085 // a typedef.
5086 QualType FDTy = FD->getType();
5087 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5088 SmallVector<ParmVarDecl*, 16> Params;
5089 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5090 AE = FT->arg_type_end(); AI != AE; ++AI) {
5091 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5092 Param->setScopeInfo(0, Params.size());
5093 Params.push_back(Param);
5094 }
David Blaikie4278c652011-09-21 18:16:56 +00005095 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005096 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005097 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5098 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005099 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005100 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005101 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005102 if (VD->getQualifier()) {
5103 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005104 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005105 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005106 }
5107 return NewD;
5108}
5109
James Dennett1dfbd922012-06-14 21:40:34 +00005110/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005111/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005112void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005113 if (W.getUsed()) return; // only do this once
5114 W.setUsed(true);
5115 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5116 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005117 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005118 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5119 NDId->getName()));
5120 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005121 WeakTopLevelDecl.push_back(NewD);
5122 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5123 // to insert Decl at TU scope, sorry.
5124 DeclContext *SavedContext = CurContext;
5125 CurContext = Context.getTranslationUnitDecl();
5126 PushOnScopeChains(NewD, S);
5127 CurContext = SavedContext;
5128 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005129 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005130 }
5131}
5132
Rafael Espindola65611bf2013-03-02 21:41:48 +00005133void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5134 // It's valid to "forward-declare" #pragma weak, in which case we
5135 // have to do this.
5136 LoadExternalWeakUndeclaredIdentifiers();
5137 if (!WeakUndeclaredIdentifiers.empty()) {
5138 NamedDecl *ND = NULL;
5139 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5140 if (VD->isExternC())
5141 ND = VD;
5142 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5143 if (FD->isExternC())
5144 ND = FD;
5145 if (ND) {
5146 if (IdentifierInfo *Id = ND->getIdentifier()) {
5147 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5148 = WeakUndeclaredIdentifiers.find(Id);
5149 if (I != WeakUndeclaredIdentifiers.end()) {
5150 WeakInfo W = I->second;
5151 DeclApplyPragmaWeak(S, ND, W);
5152 WeakUndeclaredIdentifiers[Id] = W;
5153 }
5154 }
5155 }
5156 }
5157}
5158
Chris Lattner0744e5f2008-06-29 00:23:49 +00005159/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5160/// it, apply them to D. This is a bit tricky because PD can have attributes
5161/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00005162void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5163 bool NonInheritable, bool Inheritable) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005164 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005165 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00005166 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00005167
Chris Lattner0744e5f2008-06-29 00:23:49 +00005168 // Walk the declarator structure, applying decl attributes that were in a type
5169 // position to the decl itself. This handles cases like:
5170 // int *__attr__(x)** D;
5171 // when X is a decl attribute.
5172 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5173 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithcd8ab512013-01-17 01:30:42 +00005174 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5175 /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005176
Chris Lattner0744e5f2008-06-29 00:23:49 +00005177 // Finally, apply any attributes on the decl itself.
5178 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00005179 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005180}
John McCall54abf7d2009-11-04 02:18:39 +00005181
John McCallf85e1932011-06-15 23:02:42 +00005182/// Is the given declaration allowed to use a forbidden type?
5183static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5184 // Private ivars are always okay. Unfortunately, people don't
5185 // always properly make their ivars private, even in system headers.
5186 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005187 // Function declarations in sys headers will be marked unavailable.
5188 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5189 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005190 return false;
5191
5192 // Require it to be declared in a system header.
5193 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5194}
5195
5196/// Handle a delayed forbidden-type diagnostic.
5197static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5198 Decl *decl) {
5199 if (decl && isForbiddenTypeAllowed(S, decl)) {
5200 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5201 "this system declaration uses an unsupported type"));
5202 return;
5203 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005204 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005205 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005206 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005207 // kind of forbidden type messages on unavailable functions.
5208 if (FD->hasAttr<UnavailableAttr>() &&
5209 diag.getForbiddenTypeDiagnostic() ==
5210 diag::err_arc_array_param_no_ownership) {
5211 diag.Triggered = true;
5212 return;
5213 }
5214 }
John McCallf85e1932011-06-15 23:02:42 +00005215
5216 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5217 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5218 diag.Triggered = true;
5219}
5220
John McCall92576642012-05-07 06:16:41 +00005221void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5222 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005223 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005224 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005225
John McCall92576642012-05-07 06:16:41 +00005226 // When delaying diagnostics to run in the context of a parsed
5227 // declaration, we only want to actually emit anything if parsing
5228 // succeeds.
5229 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005230
John McCall92576642012-05-07 06:16:41 +00005231 // We emit all the active diagnostics in this pool or any of its
5232 // parents. In general, we'll get one pool for the decl spec
5233 // and a child pool for each declarator; in a decl group like:
5234 // deprecated_typedef foo, *bar, baz();
5235 // only the declarator pops will be passed decls. This is correct;
5236 // we really do need to consider delayed diagnostics from the decl spec
5237 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005238 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005239 do {
John McCall13489672012-05-07 06:16:58 +00005240 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005241 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5242 // This const_cast is a bit lame. Really, Triggered should be mutable.
5243 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005244 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005245 continue;
5246
John McCalleee1d542011-02-14 07:13:47 +00005247 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005248 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005249 // Don't bother giving deprecation diagnostics if the decl is invalid.
5250 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005251 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005252 break;
5253
5254 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005255 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005256 break;
John McCallf85e1932011-06-15 23:02:42 +00005257
5258 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005259 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005260 break;
John McCall2f514482010-01-27 03:50:35 +00005261 }
5262 }
John McCall92576642012-05-07 06:16:41 +00005263 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005264}
5265
John McCall13489672012-05-07 06:16:58 +00005266/// Given a set of delayed diagnostics, re-emit them as if they had
5267/// been delayed in the current context instead of in the given pool.
5268/// Essentially, this just moves them to the current pool.
5269void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5270 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5271 assert(curPool && "re-emitting in undelayed context not supported");
5272 curPool->steal(pool);
5273}
5274
John McCall54abf7d2009-11-04 02:18:39 +00005275static bool isDeclDeprecated(Decl *D) {
5276 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005277 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005278 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005279 // A category implicitly has the availability of the interface.
5280 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5281 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005282 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5283 return false;
5284}
5285
Eli Friedmanc3b23082012-08-08 21:52:41 +00005286static void
5287DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5288 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005289 const ObjCInterfaceDecl *UnknownObjCClass,
5290 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005291 DeclarationName Name = D->getDeclName();
5292 if (!Message.empty()) {
5293 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5294 S.Diag(D->getLocation(),
5295 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5296 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005297 if (ObjCPropery)
5298 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5299 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005300 } else if (!UnknownObjCClass) {
5301 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5302 S.Diag(D->getLocation(),
5303 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5304 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005305 if (ObjCPropery)
5306 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5307 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005308 } else {
5309 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5310 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5311 }
5312}
5313
John McCall9c3087b2010-08-26 02:13:20 +00005314void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005315 Decl *Ctx) {
5316 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005317 return;
5318
John McCall2f514482010-01-27 03:50:35 +00005319 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005320 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5321 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005322 DD.getUnknownObjCClass(),
5323 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005324}
5325
Chris Lattner5f9e2722011-07-23 10:55:15 +00005326void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005327 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005328 const ObjCInterfaceDecl *UnknownObjCClass,
5329 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005330 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005331 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005332 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5333 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005334 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005335 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005336 return;
5337 }
5338
5339 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005340 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005341 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005342 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005343}