blob: 9d2b269d30696893838afa2ad7f8728e38e23c88 [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"
Benjamin Kramer1a343e22013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000032using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000034
John McCall883cc2c2011-03-02 12:29:23 +000035/// These constants match the enumerated choices of
36/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballman37a89532013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
57 ExpectedObjectiveCInterface
John McCall883cc2c2011-03-02 12:29:23 +000058};
59
Chris Lattnere5c5ee12008-06-29 00:16:31 +000060//===----------------------------------------------------------------------===//
61// Helper functions
62//===----------------------------------------------------------------------===//
63
Chandler Carruth87c44602011-07-01 23:49:12 +000064static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000065 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000067 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000068 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000069 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000070 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000071 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000072 Ty = decl->getUnderlyingType();
73 else
74 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000075
Chris Lattner6b6b5372008-06-26 18:38:35 +000076 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000077 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000078 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000079 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000080
John McCall183700f2009-09-21 23:43:11 +000081 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000082}
83
Daniel Dunbar35682492008-09-26 04:12:28 +000084// FIXME: We should provide an abstraction around a method or function
85// to provide the following bits of information.
86
Nuno Lopesd20254f2009-12-20 23:11:08 +000087/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000088/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000089static bool isFunction(const Decl *D) {
90 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000091}
92
93/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000094/// type (function or function-typed variable) or an Objective-C
95/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000096static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000097 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000098}
99
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000100/// isFunctionOrMethodOrBlock - Return true if the given decl has function
101/// type (function or function-typed variable) or an Objective-C
102/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000103static bool isFunctionOrMethodOrBlock(const Decl *D) {
104 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000105 return true;
106 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000107 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000108 QualType Ty = V->getType();
109 return Ty->isBlockPointerType();
110 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000111 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000112}
113
John McCall711c52b2011-01-05 12:14:39 +0000114/// Return true if the given decl has a declarator that should have
115/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000116static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000117 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000118 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
119 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000120}
121
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000122/// hasFunctionProto - Return true if the given decl has a argument
123/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000124/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000125static bool hasFunctionProto(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000128 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000129 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000130 return true;
131 }
132}
133
134/// getFunctionOrMethodNumArgs - Return number of function or method
135/// arguments. It is an error to call this on a K&R function (use
136/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000137static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000139 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000140 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000141 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000142 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000143}
144
Chandler Carruth87c44602011-07-01 23:49:12 +0000145static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
146 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000147 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000148 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000149 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000150
Chandler Carruth87c44602011-07-01 23:49:12 +0000151 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000152}
153
Chandler Carruth87c44602011-07-01 23:49:12 +0000154static QualType getFunctionOrMethodResultType(const Decl *D) {
155 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000156 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000158}
159
Chandler Carruth87c44602011-07-01 23:49:12 +0000160static bool isFunctionOrMethodVariadic(const Decl *D) {
161 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000162 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000163 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000164 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000165 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000166 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000167 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000168 }
169}
170
Chandler Carruth87c44602011-07-01 23:49:12 +0000171static bool isInstanceMethod(const Decl *D) {
172 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000173 return MethodDecl->isInstance();
174 return false;
175}
176
Chris Lattner6b6b5372008-06-26 18:38:35 +0000177static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000178 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000179 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000180 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000181
John McCall506b57e2010-05-17 21:00:27 +0000182 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
183 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000184 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000185
John McCall506b57e2010-05-17 21:00:27 +0000186 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000187
Chris Lattner6b6b5372008-06-26 18:38:35 +0000188 // FIXME: Should we walk the chain of classes?
189 return ClsName == &Ctx.Idents.get("NSString") ||
190 ClsName == &Ctx.Idents.get("NSMutableString");
191}
192
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000193static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000194 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000195 if (!PT)
196 return false;
197
Ted Kremenek6217b802009-07-29 21:53:49 +0000198 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000199 if (!RT)
200 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000201
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000202 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000203 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000204 return false;
205
206 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
207}
208
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000209/// \brief Check if the attribute has exactly as many args as Num. May
210/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000211static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
212 unsigned int Num) {
213 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
215 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000216 return false;
217 }
218
219 return true;
220}
221
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000222
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000223/// \brief Check if the attribute has at least as many args as Num. May
224/// output an error.
225static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
226 unsigned int Num) {
227 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000228 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
229 return false;
230 }
231
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000232 return true;
233}
234
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000235/// \brief Check if IdxExpr is a valid argument index for a function or
236/// instance method D. May output an error.
237///
238/// \returns true if IdxExpr is a valid index.
239static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
240 StringRef AttrName,
241 SourceLocation AttrLoc,
242 unsigned AttrArgNum,
243 const Expr *IdxExpr,
244 uint64_t &Idx)
245{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000246 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000247
248 // In C++ the implicit 'this' function parameter also counts.
249 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000250 bool HP = hasFunctionProto(D);
251 bool HasImplicitThisParam = isInstanceMethod(D);
252 bool IV = HP && isFunctionOrMethodVariadic(D);
253 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
254 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000255
256 llvm::APSInt IdxInt;
257 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
258 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000259 std::string Name = std::string("'") + AttrName.str() + std::string("'");
260 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000261 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000262 return false;
263 }
264
265 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000266 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000267 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
268 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
269 return false;
270 }
271 Idx--; // Convert to zero-based.
272 if (HasImplicitThisParam) {
273 if (Idx == 0) {
274 S.Diag(AttrLoc,
275 diag::err_attribute_invalid_implicit_this_argument)
276 << AttrName << IdxExpr->getSourceRange();
277 return false;
278 }
279 --Idx;
280 }
281
282 return true;
283}
284
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000285/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
286/// If not emit an error and return false. If the argument is an identifier it
287/// will emit an error with a fixit hint and treat it as if it was a string
288/// literal.
289static bool checkStringLiteralArgument(Sema &S, StringRef &Str,
290 const AttributeList &Attr,
291 unsigned ArgNum,
292 SourceLocation *ArgLocation = 0) {
293 // Look for identifiers. If we have one emit a hint to fix it to a literal.
294 if (Attr.isArgIdent(ArgNum)) {
295 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
296 S.Diag(Loc->Loc, diag::err_attribute_argument_type)
297 << Attr.getName() << AANT_ArgumentString
298 << FixItHint::CreateInsertion(Loc->Loc, "\"")
299 << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Loc->Loc), "\"");
300 Str = Loc->Ident->getName();
301 if (ArgLocation)
302 *ArgLocation = Loc->Loc;
303 return true;
304 }
305
306 // Now check for an actual string literal.
307 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
308 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
309 if (ArgLocation)
310 *ArgLocation = ArgExpr->getLocStart();
311
312 if (!Literal || !Literal->isAscii()) {
313 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
314 << Attr.getName() << AANT_ArgumentString;
315 return false;
316 }
317
318 Str = Literal->getString();
319 return true;
320}
321
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000322///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000323/// \brief Check if passed in Decl is a field or potentially shared global var
324/// \return true if the Decl is a field or potentially shared global variable
325///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000326static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000327 if (isa<FieldDecl>(D))
328 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000329 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000330 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000331
332 return false;
333}
334
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000335/// \brief Check if the passed-in expression is of type int or bool.
336static bool isIntOrBool(Expr *Exp) {
337 QualType QT = Exp->getType();
338 return QT->isBooleanType() || QT->isIntegerType();
339}
340
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000341
342// Check to see if the type is a smart pointer of some kind. We assume
343// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000344static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
345 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
346 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000347 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000348 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000349
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000350 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
351 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000352 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000353 return false;
354
355 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000356}
357
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000358/// \brief Check if passed in Decl is a pointer type.
359/// Note that this function may produce an error message.
360/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000361static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
362 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000363 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000364 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000365 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000366 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000367
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000368 if (const RecordType *RT = QT->getAs<RecordType>()) {
369 // If it's an incomplete type, it could be a smart pointer; skip it.
370 // (We don't want to force template instantiation if we can avoid it,
371 // since that would alter the order in which templates are instantiated.)
372 if (RT->isIncompleteType())
373 return true;
374
375 if (threadSafetyCheckIsSmartPointer(S, RT))
376 return true;
377 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000378
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000379 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000380 << Attr.getName()->getName() << QT;
381 } else {
382 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
383 << Attr.getName();
384 }
385 return false;
386}
387
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000388/// \brief Checks that the passed in QualType either is of RecordType or points
389/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000390static const RecordType *getRecordType(QualType QT) {
391 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000392 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000393
394 // Now check if we point to record type.
395 if (const PointerType *PT = QT->getAs<PointerType>())
396 return PT->getPointeeType()->getAs<RecordType>();
397
398 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000399}
400
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000401
Jordy Rosefad5de92012-05-08 03:27:22 +0000402static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
403 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000404 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
405 if (RT->getDecl()->getAttr<LockableAttr>())
406 return true;
407 return false;
408}
409
410
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000411/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000413static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
414 QualType Ty) {
415 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000416
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000417 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000418 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000419 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000420 << Attr.getName() << Ty.getAsString();
421 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000422 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000423
Michael Hanf1aae3b2012-08-03 17:40:43 +0000424 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000425 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000426 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000427
428 // Allow smart pointers to be used as lockable objects.
429 // FIXME -- Check the type that the smart pointer points to.
430 if (threadSafetyCheckIsSmartPointer(S, RT))
431 return;
432
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000433 // Check if the type is lockable.
434 RecordDecl *RD = RT->getDecl();
435 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000436 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000437
438 // Else check if any base classes are lockable.
439 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
440 CXXBasePaths BPaths(false, false);
441 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
442 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000443 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000444
445 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
446 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447}
448
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000449/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000450/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000451/// \param Sidx The attribute argument index to start checking with.
452/// \param ParamIdxOk Whether an argument can be indexing into a function
453/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000454static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000455 const AttributeList &Attr,
456 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 int Sidx = 0,
458 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000459 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000460 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000462 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000463 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000464 Args.push_back(ArgExp);
465 continue;
466 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000467
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000468 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000469 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000470 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000471 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000472 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000473 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000474 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000475 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000476
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000477 // We allow constant strings to be used as a placeholder for expressions
478 // that are not valid C++ syntax, but warn that they are ignored.
479 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
480 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000481 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000482 continue;
483 }
484
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000485 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000486
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000487 // A pointer to member expression of the form &MyClass::mu is treated
488 // specially -- we need to look at the type of the member.
489 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
490 if (UOp->getOpcode() == UO_AddrOf)
491 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
492 if (DRE->getDecl()->isCXXInstanceMember())
493 ArgTy = DRE->getDecl()->getType();
494
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000495 // First see if we can just cast to record type, or point to record type.
496 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000497
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000498 // Now check if we index into a record type function param.
499 if(!RT && ParamIdxOk) {
500 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000501 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
502 if(FD && IL) {
503 unsigned int NumParams = FD->getNumParams();
504 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000505 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
506 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
507 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
509 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000510 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000511 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000512 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000513 }
514 }
515
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000516 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000517
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000518 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000519 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000520}
521
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000522//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000523// Attribute Implementations
524//===----------------------------------------------------------------------===//
525
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000526// FIXME: All this manual attribute parsing code is gross. At the
527// least add some helper functions to check most argument patterns (#
528// and types of args).
529
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000530enum ThreadAttributeDeclKind {
531 ThreadExpectedFieldOrGlobalVar,
532 ThreadExpectedFunctionOrMethod,
533 ThreadExpectedClassOrStruct
534};
535
Michael Hanf1aae3b2012-08-03 17:40:43 +0000536static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000537 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000538 // D must be either a member field or global (potentially shared) variable.
539 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000540 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
541 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000542 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000543 }
544
Michael Handc691572012-07-23 18:48:41 +0000545 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000546}
547
Michael Handc691572012-07-23 18:48:41 +0000548static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
549 if (!checkGuardedVarAttrCommon(S, D, Attr))
550 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000551
Michael Han51d8c522013-01-24 16:46:58 +0000552 D->addAttr(::new (S.Context)
553 GuardedVarAttr(Attr.getRange(), S.Context,
554 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000555}
556
Michael Hanf1aae3b2012-08-03 17:40:43 +0000557static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000558 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000559 if (!checkGuardedVarAttrCommon(S, D, Attr))
560 return;
561
562 if (!threadSafetyCheckIsPointer(S, D, Attr))
563 return;
564
Michael Han51d8c522013-01-24 16:46:58 +0000565 D->addAttr(::new (S.Context)
566 PtGuardedVarAttr(Attr.getRange(), S.Context,
567 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000568}
569
Michael Hanf1aae3b2012-08-03 17:40:43 +0000570static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
571 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000572 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000573 // D must be either a member field or global (potentially shared) variable.
574 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000575 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
576 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000577 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000578 }
579
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000580 SmallVector<Expr*, 1> Args;
581 // check that all arguments are lockable objects
582 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
583 unsigned Size = Args.size();
584 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000585 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000586
Michael Handc691572012-07-23 18:48:41 +0000587 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000588
Michael Handc691572012-07-23 18:48:41 +0000589 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000590}
591
Michael Handc691572012-07-23 18:48:41 +0000592static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
593 Expr *Arg = 0;
594 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
595 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000596
Michael Handc691572012-07-23 18:48:41 +0000597 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
598}
599
Michael Hanf1aae3b2012-08-03 17:40:43 +0000600static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000601 const AttributeList &Attr) {
602 Expr *Arg = 0;
603 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
604 return;
605
606 if (!threadSafetyCheckIsPointer(S, D, Attr))
607 return;
608
609 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
610 S.Context, Arg));
611}
612
Michael Hanf1aae3b2012-08-03 17:40:43 +0000613static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000614 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000615 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000616 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000617 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
618 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000619 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000620 }
621
Michael Handc691572012-07-23 18:48:41 +0000622 return true;
623}
624
625static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
626 if (!checkLockableAttrCommon(S, D, Attr))
627 return;
628
629 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
630}
631
Michael Hanf1aae3b2012-08-03 17:40:43 +0000632static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000633 const AttributeList &Attr) {
634 if (!checkLockableAttrCommon(S, D, Attr))
635 return;
636
Michael Han51d8c522013-01-24 16:46:58 +0000637 D->addAttr(::new (S.Context)
638 ScopedLockableAttr(Attr.getRange(), S.Context,
639 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000640}
641
Kostya Serebryany85aee962013-02-26 06:58:27 +0000642static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
643 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000644 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000645 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
646 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000647 return;
648 }
649
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000650 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000651 S.Context));
652}
653
Kostya Serebryany85aee962013-02-26 06:58:27 +0000654static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000655 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000656 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000658 << Attr.getName() << ExpectedFunctionOrMethod;
659 return;
660 }
661
Michael Han51d8c522013-01-24 16:46:58 +0000662 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000663 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
664 Attr.getAttributeSpellingListIndex()));
665}
666
667static void handleNoSanitizeMemory(Sema &S, Decl *D,
668 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000669 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
671 << Attr.getName() << ExpectedFunctionOrMethod;
672 return;
673 }
674
675 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
676 S.Context));
677}
678
679static void handleNoSanitizeThread(Sema &S, Decl *D,
680 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000681 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
683 << Attr.getName() << ExpectedFunctionOrMethod;
684 return;
685 }
686
687 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
688 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000689}
690
Michael Hanf1aae3b2012-08-03 17:40:43 +0000691static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000693 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000694 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000695 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000696
697 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 ValueDecl *VD = dyn_cast<ValueDecl>(D);
699 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000700 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
701 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000703 }
704
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000705 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000706 QualType QT = VD->getType();
707 if (!QT->isDependentType()) {
708 const RecordType *RT = getRecordType(QT);
709 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000710 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000711 << Attr.getName();
712 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000713 }
714 }
715
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000716 // Check that all arguments are lockable objects.
717 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000718 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000719 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720
Michael Handc691572012-07-23 18:48:41 +0000721 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000722}
723
Michael Hanf1aae3b2012-08-03 17:40:43 +0000724static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
728 return;
729
730 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000731 D->addAttr(::new (S.Context)
732 AcquiredAfterAttr(Attr.getRange(), S.Context,
733 StartArg, Args.size(),
734 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000735}
736
Michael Hanf1aae3b2012-08-03 17:40:43 +0000737static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000738 const AttributeList &Attr) {
739 SmallVector<Expr*, 1> Args;
740 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
741 return;
742
743 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000744 D->addAttr(::new (S.Context)
745 AcquiredBeforeAttr(Attr.getRange(), S.Context,
746 StartArg, Args.size(),
747 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000748}
749
Michael Hanf1aae3b2012-08-03 17:40:43 +0000750static bool checkLockFunAttrCommon(Sema &S, Decl *D,
751 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000752 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000753 // zero or more arguments ok
754
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000755 // check that the attribute is applied to a function
756 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000757 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
758 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000759 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000760 }
761
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000762 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000763 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000764
Michael Handc691572012-07-23 18:48:41 +0000765 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000766}
767
Michael Hanf1aae3b2012-08-03 17:40:43 +0000768static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000769 const AttributeList &Attr) {
770 SmallVector<Expr*, 1> Args;
771 if (!checkLockFunAttrCommon(S, D, Attr, Args))
772 return;
773
774 unsigned Size = Args.size();
775 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000776 D->addAttr(::new (S.Context)
777 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
778 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000779}
780
Michael Hanf1aae3b2012-08-03 17:40:43 +0000781static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000782 const AttributeList &Attr) {
783 SmallVector<Expr*, 1> Args;
784 if (!checkLockFunAttrCommon(S, D, Attr, Args))
785 return;
786
787 unsigned Size = Args.size();
788 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000789 D->addAttr(::new (S.Context)
790 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
791 StartArg, Size,
792 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000793}
794
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000795static void handleAssertSharedLockAttr(Sema &S, Decl *D,
796 const AttributeList &Attr) {
797 SmallVector<Expr*, 1> Args;
798 if (!checkLockFunAttrCommon(S, D, Attr, Args))
799 return;
800
801 unsigned Size = Args.size();
802 Expr **StartArg = Size == 0 ? 0 : &Args[0];
803 D->addAttr(::new (S.Context)
804 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
805 Attr.getAttributeSpellingListIndex()));
806}
807
808static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
809 const AttributeList &Attr) {
810 SmallVector<Expr*, 1> Args;
811 if (!checkLockFunAttrCommon(S, D, Attr, Args))
812 return;
813
814 unsigned Size = Args.size();
815 Expr **StartArg = Size == 0 ? 0 : &Args[0];
816 D->addAttr(::new (S.Context)
817 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
818 StartArg, Size,
819 Attr.getAttributeSpellingListIndex()));
820}
821
822
Michael Hanf1aae3b2012-08-03 17:40:43 +0000823static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
824 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000825 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000826 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000827 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000828
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000829 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000830 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
831 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000832 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000833 }
834
Aaron Ballman624421f2013-08-31 01:11:41 +0000835 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000836 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000837 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000838 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839 }
840
841 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000842 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000843
Michael Handc691572012-07-23 18:48:41 +0000844 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000845}
846
Michael Hanf1aae3b2012-08-03 17:40:43 +0000847static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000848 const AttributeList &Attr) {
849 SmallVector<Expr*, 2> Args;
850 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
851 return;
852
Michael Han51d8c522013-01-24 16:46:58 +0000853 D->addAttr(::new (S.Context)
854 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000855 Attr.getArgAsExpr(0),
856 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000857 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000858}
859
Michael Hanf1aae3b2012-08-03 17:40:43 +0000860static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000861 const AttributeList &Attr) {
862 SmallVector<Expr*, 2> Args;
863 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
864 return;
865
Michael Han51d8c522013-01-24 16:46:58 +0000866 D->addAttr(::new (S.Context)
867 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000868 Attr.getArgAsExpr(0),
869 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000870 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000871}
872
Michael Hanf1aae3b2012-08-03 17:40:43 +0000873static bool checkLocksRequiredCommon(Sema &S, Decl *D,
874 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000875 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000876 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000877 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000878
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000879 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000880 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
881 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000882 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000883 }
884
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000885 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000886 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000887 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000888 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000889
Michael Handc691572012-07-23 18:48:41 +0000890 return true;
891}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000892
Michael Hanf1aae3b2012-08-03 17:40:43 +0000893static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000894 const AttributeList &Attr) {
895 SmallVector<Expr*, 1> Args;
896 if (!checkLocksRequiredCommon(S, D, Attr, Args))
897 return;
898
899 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000900 D->addAttr(::new (S.Context)
901 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
902 StartArg, Args.size(),
903 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000904}
905
Michael Hanf1aae3b2012-08-03 17:40:43 +0000906static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000907 const AttributeList &Attr) {
908 SmallVector<Expr*, 1> Args;
909 if (!checkLocksRequiredCommon(S, D, Attr, Args))
910 return;
911
912 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000913 D->addAttr(::new (S.Context)
914 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
915 StartArg, Args.size(),
916 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000917}
918
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000919static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000920 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000921 // zero or more arguments ok
922
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000923 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000924 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
925 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926 return;
927 }
928
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000929 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000930 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000931 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000932 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000933 Expr **StartArg = Size == 0 ? 0 : &Args[0];
934
Michael Han51d8c522013-01-24 16:46:58 +0000935 D->addAttr(::new (S.Context)
936 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
937 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000938}
939
940static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000941 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000942 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000943 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
944 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000945 return;
946 }
947
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000948 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000949 SmallVector<Expr*, 1> Args;
950 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
951 unsigned Size = Args.size();
952 if (Size == 0)
953 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000954
Michael Han51d8c522013-01-24 16:46:58 +0000955 D->addAttr(::new (S.Context)
956 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
957 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000958}
959
960static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000961 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000962 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000963 return;
964
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000965 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000966 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
967 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968 return;
969 }
970
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000971 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000972 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000973 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000974 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000975 if (Size == 0)
976 return;
977 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000978
Michael Han51d8c522013-01-24 16:46:58 +0000979 D->addAttr(::new (S.Context)
980 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
981 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000982}
983
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000984static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000985 ConsumableAttr::ConsumedState DefaultState;
986
987 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000988 IdentifierLoc *IL = Attr.getArgAsIdent(0);
989 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
990 DefaultState)) {
991 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
992 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +0000993 return;
994 }
David Blaikiea33ab602013-09-06 01:28:43 +0000995 } else {
996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
997 << Attr.getName() << AANT_ArgumentIdentifier;
998 return;
999 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001000
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001001 if (!isa<CXXRecordDecl>(D)) {
1002 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1003 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001004 return;
1005 }
1006
1007 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001008 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001009 Attr.getAttributeSpellingListIndex()));
1010}
1011
1012static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1013 const AttributeList &Attr) {
1014 ASTContext &CurrContext = S.getASTContext();
1015 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1016
1017 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1018 if (!RD->hasAttr<ConsumableAttr>()) {
1019 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1020 RD->getNameAsString();
1021
1022 return false;
1023 }
1024 }
1025
1026 return true;
1027}
1028
1029static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001030 if (!isa<CXXMethodDecl>(D)) {
1031 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1032 Attr.getName() << ExpectedMethod;
1033 return;
1034 }
1035
1036 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1037 return;
1038
1039 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001040 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001041 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001042}
1043
1044static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1045 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001046 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001047 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1048 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001049 return;
1050 }
1051
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001052 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1053 return;
1054
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001055 D->addAttr(::new (S.Context)
1056 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001057 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001058}
1059
1060static void handleTestsConsumedAttr(Sema &S, Decl *D,
1061 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001062 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001063 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1064 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001065 return;
1066 }
1067
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001068 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1069 return;
1070
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001071 D->addAttr(::new (S.Context)
1072 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001073 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001074}
1075
1076static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1077 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001078 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001079 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1080 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001081 return;
1082 }
1083
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001084 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1085 return;
1086
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001087 D->addAttr(::new (S.Context)
1088 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001089 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001090}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001091
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001092static void handleReturnTypestateAttr(Sema &S, Decl *D,
1093 const AttributeList &Attr) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001094 ReturnTypestateAttr::ConsumedState ReturnState;
1095
1096 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001097 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1098 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1099 ReturnState)) {
1100 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1101 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001102 return;
1103 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001104 } else {
1105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1106 Attr.getName() << AANT_ArgumentIdentifier;
1107 return;
1108 }
1109
1110 if (!isa<FunctionDecl>(D)) {
1111 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1112 Attr.getName() << ExpectedFunction;
1113 return;
1114 }
1115
1116 // FIXME: This check is currently being done in the analysis. It can be
1117 // enabled here only after the parser propagates attributes at
1118 // template specialization definition, not declaration.
1119 //QualType ReturnType;
1120 //
1121 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1122 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1123 //
1124 //} else {
1125 //
1126 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1127 //}
1128 //
1129 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1130 //
1131 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1132 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1133 // ReturnType.getAsString();
1134 // return;
1135 //}
1136
1137 D->addAttr(::new (S.Context)
1138 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1139 Attr.getAttributeSpellingListIndex()));
1140}
1141
Chandler Carruth1b03c872011-07-02 00:01:44 +00001142static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1143 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001144 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1145 if (TD == 0) {
1146 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1147 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001148 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001149 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001150 }
Mike Stumpbf916502009-07-24 19:02:52 +00001151
Richard Smitha4fa9002013-01-13 02:11:23 +00001152 // Remember this typedef decl, we will need it later for diagnostics.
1153 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001154}
1155
Chandler Carruth1b03c872011-07-02 00:01:44 +00001156static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001157 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001158 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001159 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001160 // If the alignment is less than or equal to 8 bits, the packed attribute
1161 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001162 if (!FD->getType()->isDependentType() &&
1163 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001164 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001165 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001166 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001167 else
Michael Han51d8c522013-01-24 16:46:58 +00001168 FD->addAttr(::new (S.Context)
1169 PackedAttr(Attr.getRange(), S.Context,
1170 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001171 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001172 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001173}
1174
Chandler Carruth1b03c872011-07-02 00:01:44 +00001175static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001176 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001177 RD->addAttr(::new (S.Context)
1178 MsStructAttr(Attr.getRange(), S.Context,
1179 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001180 else
1181 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1182}
1183
Chandler Carruth1b03c872011-07-02 00:01:44 +00001184static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001185 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001186 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001187 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001188 D->addAttr(::new (S.Context)
1189 IBActionAttr(Attr.getRange(), S.Context,
1190 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001191 return;
1192 }
1193
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001194 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001195}
1196
Ted Kremenek2f041d02011-09-29 07:02:25 +00001197static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1198 // The IBOutlet/IBOutletCollection attributes only apply to instance
1199 // variables or properties of Objective-C classes. The outlet must also
1200 // have an object reference type.
1201 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1202 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001203 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001204 << Attr.getName() << VD->getType() << 0;
1205 return false;
1206 }
1207 }
1208 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1209 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001210 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001211 << Attr.getName() << PD->getType() << 1;
1212 return false;
1213 }
1214 }
1215 else {
1216 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1217 return false;
1218 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001219
Ted Kremenek2f041d02011-09-29 07:02:25 +00001220 return true;
1221}
1222
Chandler Carruth1b03c872011-07-02 00:01:44 +00001223static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001224 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001225 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001226
Michael Han51d8c522013-01-24 16:46:58 +00001227 D->addAttr(::new (S.Context)
1228 IBOutletAttr(Attr.getRange(), S.Context,
1229 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001230}
1231
Chandler Carruth1b03c872011-07-02 00:01:44 +00001232static void handleIBOutletCollection(Sema &S, Decl *D,
1233 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001234
1235 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001236 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001237 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1238 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001239 return;
1240 }
1241
Ted Kremenek2f041d02011-09-29 07:02:25 +00001242 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001243 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001244
Aaron Ballman624421f2013-08-31 01:11:41 +00001245 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1246 IdentifierInfo *II;
1247 SourceLocation ILS;
1248 if (IL) {
1249 II = IL->Ident;
1250 ILS = IL->Loc;
1251 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001252 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001253 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001254
John McCallb3d87482010-08-24 05:47:05 +00001255 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001256 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001257 if (!TypeRep) {
1258 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1259 return;
1260 }
John McCallb3d87482010-08-24 05:47:05 +00001261 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001262 // Diagnose use of non-object type in iboutletcollection attribute.
1263 // FIXME. Gnu attribute extension ignores use of builtin types in
1264 // attributes. So, __attribute__((iboutletcollection(char))) will be
1265 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001266 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001267 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1268 return;
1269 }
Michael Han51d8c522013-01-24 16:46:58 +00001270 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001271 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001272 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001273}
1274
Chandler Carruthd309c812011-07-01 23:49:16 +00001275static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001276 if (const RecordType *UT = T->getAsUnionType())
1277 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1278 RecordDecl *UD = UT->getDecl();
1279 for (RecordDecl::field_iterator it = UD->field_begin(),
1280 itend = UD->field_end(); it != itend; ++it) {
1281 QualType QT = it->getType();
1282 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1283 T = QT;
1284 return;
1285 }
1286 }
1287 }
1288}
1289
Nuno Lopes587de5b2012-05-24 00:22:00 +00001290static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001291 if (!isFunctionOrMethod(D)) {
1292 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001293 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001294 return;
1295 }
1296
Nuno Lopes587de5b2012-05-24 00:22:00 +00001297 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1298 return;
1299
Nuno Lopes587de5b2012-05-24 00:22:00 +00001300 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001301 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001302 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001303 uint64_t Idx;
1304 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1305 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001306 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001307
1308 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001309 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001310 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1312 << Attr.getName() << AANT_ArgumentIntegerConstant
1313 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001314 return;
1315 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001316 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001317 }
1318
1319 // check if the function returns a pointer
1320 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1321 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001322 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001323 }
1324
Michael Han51d8c522013-01-24 16:46:58 +00001325 D->addAttr(::new (S.Context)
1326 AllocSizeAttr(Attr.getRange(), S.Context,
1327 SizeArgs.data(), SizeArgs.size(),
1328 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001329}
1330
Chandler Carruth1b03c872011-07-02 00:01:44 +00001331static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001332 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1333 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001334 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001335 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001336 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001337 return;
1338 }
Mike Stumpbf916502009-07-24 19:02:52 +00001339
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001340 SmallVector<unsigned, 8> NonNullArgs;
1341 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001342 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001343 uint64_t Idx;
1344 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1345 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001346 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001347
1348 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001349 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001350 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001351
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001352 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001353 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001354 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001355 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001356 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001357 }
Mike Stumpbf916502009-07-24 19:02:52 +00001358
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001359 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001360 }
Mike Stumpbf916502009-07-24 19:02:52 +00001361
1362 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1363 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001364 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001365 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1366 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001367 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001368 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001369 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001370 }
Mike Stumpbf916502009-07-24 19:02:52 +00001371
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001372 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001373 if (NonNullArgs.empty()) {
1374 // Warn the trivial case only if attribute is not coming from a
1375 // macro instantiation.
1376 if (Attr.getLoc().isFileID())
1377 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001378 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001379 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001380 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001381
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001382 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001383 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001384 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001385 D->addAttr(::new (S.Context)
1386 NonNullAttr(Attr.getRange(), S.Context, start, size,
1387 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001388}
1389
Chandler Carruth1b03c872011-07-02 00:01:44 +00001390static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001391 // This attribute must be applied to a function declaration.
1392 // The first argument to the attribute must be a string,
1393 // the name of the resource, for example "malloc".
1394 // The following arguments must be argument indexes, the arguments must be
1395 // of integer type for Returns, otherwise of pointer type.
1396 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001397 // after being held. free() should be __attribute((ownership_takes)), whereas
1398 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001399
Aaron Ballman624421f2013-08-31 01:11:41 +00001400 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001401 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001402 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 return;
1404 }
1405 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001406 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001407 switch (AL.getKind()) {
1408 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001409 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001410 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001411 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1412 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001413 return;
1414 }
Jordy Rose2a479922010-08-12 08:54:03 +00001415 break;
1416 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001417 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001418 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001419 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1420 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001421 return;
1422 }
Jordy Rose2a479922010-08-12 08:54:03 +00001423 break;
1424 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001425 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001426 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001427 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001428 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001429 return;
1430 }
Jordy Rose2a479922010-08-12 08:54:03 +00001431 break;
1432 default:
1433 // This should never happen given how we are called.
1434 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001435 }
1436
Chandler Carruth87c44602011-07-01 23:49:12 +00001437 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001438 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1439 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001440 return;
1441 }
1442
Aaron Ballman624421f2013-08-31 01:11:41 +00001443 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001444
1445 // Normalize the argument, __foo__ becomes foo.
1446 if (Module.startswith("__") && Module.endswith("__"))
1447 Module = Module.substr(2, Module.size() - 4);
1448
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001449
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001450 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001451 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1452 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001453 uint64_t Idx;
1454 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001455 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001456 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001457
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001458 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001459 case OwnershipAttr::Takes:
1460 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001461 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001462 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001463 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1464 // FIXME: Should also highlight argument in decl.
1465 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001466 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001467 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001468 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001469 continue;
1470 }
1471 break;
1472 }
Sean Huntcf807c42010-08-18 23:23:40 +00001473 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001474 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001475 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001476 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001477 llvm::APSInt ArgNum(32);
1478 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1479 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1480 S.Diag(AL.getLoc(), diag::err_ownership_type)
1481 << "ownership_returns" << "integer"
1482 << IdxExpr->getSourceRange();
1483 return;
1484 }
1485 }
1486 break;
1487 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001488 } // switch
1489
1490 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001491 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001492 i = D->specific_attr_begin<OwnershipAttr>(),
1493 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001494 i != e; ++i) {
1495 if ((*i)->getOwnKind() != K) {
1496 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1497 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001498 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001499 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1500 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001501 }
1502 }
1503 }
1504 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001505 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001506 }
1507
1508 unsigned* start = OwnershipArgs.data();
1509 unsigned size = OwnershipArgs.size();
1510 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001511
1512 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001513 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1514 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001515 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001516 }
Sean Huntcf807c42010-08-18 23:23:40 +00001517
Michael Han51d8c522013-01-24 16:46:58 +00001518 D->addAttr(::new (S.Context)
1519 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1520 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001521}
1522
Chandler Carruth1b03c872011-07-02 00:01:44 +00001523static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001524 // Check the attribute arguments.
1525 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1527 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001528 return;
1529 }
1530
Chandler Carruth87c44602011-07-01 23:49:12 +00001531 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001533 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001534 return;
1535 }
1536
Chandler Carruth87c44602011-07-01 23:49:12 +00001537 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001538
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001539 // gcc rejects
1540 // class c {
1541 // static int a __attribute__((weakref ("v2")));
1542 // static int b() __attribute__((weakref ("f3")));
1543 // };
1544 // and ignores the attributes of
1545 // void f(void) {
1546 // static int a __attribute__((weakref ("v2")));
1547 // }
1548 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001549 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001550 if (!Ctx->isFileContext()) {
1551 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001552 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001553 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001554 }
1555
1556 // The GCC manual says
1557 //
1558 // At present, a declaration to which `weakref' is attached can only
1559 // be `static'.
1560 //
1561 // It also says
1562 //
1563 // Without a TARGET,
1564 // given as an argument to `weakref' or to `alias', `weakref' is
1565 // equivalent to `weak'.
1566 //
1567 // gcc 4.4.1 will accept
1568 // int a7 __attribute__((weakref));
1569 // as
1570 // int a7 __attribute__((weak));
1571 // This looks like a bug in gcc. We reject that for now. We should revisit
1572 // it if this behaviour is actually used.
1573
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001574 // GCC rejects
1575 // static ((alias ("y"), weakref)).
1576 // Should we? How to check that weakref is before or after alias?
1577
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001578 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1579 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1580 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001581 StringRef Str;
1582 if (Attr.getNumArgs() && checkStringLiteralArgument(S, Str, Attr, 0))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001583 // GCC will accept anything as the argument of weakref. Should we
1584 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001585 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1586 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001587
Michael Han51d8c522013-01-24 16:46:58 +00001588 D->addAttr(::new (S.Context)
1589 WeakRefAttr(Attr.getRange(), S.Context,
1590 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001591}
1592
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001593static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1594 StringRef Str;
1595 if (!checkStringLiteralArgument(S, Str, Attr, 0))
1596 return;
1597
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001598 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001599 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1600 return;
1601 }
1602
Chris Lattner6b6b5372008-06-26 18:38:35 +00001603 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001604
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001605 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001606 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001607}
1608
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001609static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001610 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1612 << Attr.getName() << ExpectedFunctionOrMethod;
1613 return;
1614 }
1615
Michael Han51d8c522013-01-24 16:46:58 +00001616 D->addAttr(::new (S.Context)
1617 MinSizeAttr(Attr.getRange(), S.Context,
1618 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001619}
1620
Benjamin Krameree409a92012-05-12 21:10:52 +00001621static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001622 if (!isa<FunctionDecl>(D)) {
1623 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1624 << Attr.getName() << ExpectedFunction;
1625 return;
1626 }
1627
1628 if (D->hasAttr<HotAttr>()) {
1629 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1630 << Attr.getName() << "hot";
1631 return;
1632 }
1633
Michael Han51d8c522013-01-24 16:46:58 +00001634 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1635 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001636}
1637
1638static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001639 if (!isa<FunctionDecl>(D)) {
1640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1641 << Attr.getName() << ExpectedFunction;
1642 return;
1643 }
1644
1645 if (D->hasAttr<ColdAttr>()) {
1646 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1647 << Attr.getName() << "cold";
1648 return;
1649 }
1650
Michael Han51d8c522013-01-24 16:46:58 +00001651 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1652 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001653}
1654
Chandler Carruth1b03c872011-07-02 00:01:44 +00001655static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001656 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001658 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001659 return;
1660 }
1661
Michael Han51d8c522013-01-24 16:46:58 +00001662 D->addAttr(::new (S.Context)
1663 NakedAttr(Attr.getRange(), S.Context,
1664 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001665}
1666
Chandler Carruth1b03c872011-07-02 00:01:44 +00001667static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1668 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001669 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001671 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001672 return;
1673 }
Mike Stumpbf916502009-07-24 19:02:52 +00001674
Michael Han51d8c522013-01-24 16:46:58 +00001675 D->addAttr(::new (S.Context)
1676 AlwaysInlineAttr(Attr.getRange(), S.Context,
1677 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001678}
1679
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001680static void handleTLSModelAttr(Sema &S, Decl *D,
1681 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001682 StringRef Model;
1683 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001684 // Check that it is a string.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001685 if (!checkStringLiteralArgument(S, Model, Attr, 0, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001686 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001687
Richard Smith38afbc72013-04-13 02:43:54 +00001688 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1690 << Attr.getName() << ExpectedTLSVar;
1691 return;
1692 }
1693
1694 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001695 if (Model != "global-dynamic" && Model != "local-dynamic"
1696 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001697 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001698 return;
1699 }
1700
Michael Han51d8c522013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 TLSModelAttr(Attr.getRange(), S.Context, Model,
1703 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001704}
1705
Chandler Carruth1b03c872011-07-02 00:01:44 +00001706static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001707 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001708 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001709 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001710 D->addAttr(::new (S.Context)
1711 MallocAttr(Attr.getRange(), S.Context,
1712 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001713 return;
1714 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001715 }
1716
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001717 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001718}
1719
Chandler Carruth1b03c872011-07-02 00:01:44 +00001720static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001721 D->addAttr(::new (S.Context)
1722 MayAliasAttr(Attr.getRange(), S.Context,
1723 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001724}
1725
Chandler Carruth1b03c872011-07-02 00:01:44 +00001726static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001727 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
1729 NoCommonAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001731 else
1732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001733 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001737 if (S.LangOpts.CPlusPlus) {
1738 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1739 return;
1740 }
1741
Chandler Carruth87c44602011-07-01 23:49:12 +00001742 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001743 D->addAttr(::new (S.Context)
1744 CommonAttr(Attr.getRange(), S.Context,
1745 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001746 else
1747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001748 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001749}
1750
Chandler Carruth1b03c872011-07-02 00:01:44 +00001751static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001753
1754 if (S.CheckNoReturnAttr(attr)) return;
1755
Chandler Carruth87c44602011-07-01 23:49:12 +00001756 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001757 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001758 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001759 return;
1760 }
1761
Michael Han51d8c522013-01-24 16:46:58 +00001762 D->addAttr(::new (S.Context)
1763 NoReturnAttr(attr.getRange(), S.Context,
1764 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001765}
1766
1767bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001768 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001769 attr.setInvalid();
1770 return true;
1771 }
1772
1773 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001774}
1775
Chandler Carruth1b03c872011-07-02 00:01:44 +00001776static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1777 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001778
1779 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1780 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001781 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1782 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001783 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1784 && !VD->getType()->isFunctionPointerType())) {
1785 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001786 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001787 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001788 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001789 return;
1790 }
1791 }
1792
Michael Han51d8c522013-01-24 16:46:58 +00001793 D->addAttr(::new (S.Context)
1794 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1795 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001796}
1797
Richard Smithcd8ab512013-01-17 01:30:42 +00001798static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1799 const AttributeList &Attr) {
1800 // C++11 [dcl.attr.noreturn]p1:
1801 // The attribute may be applied to the declarator-id in a function
1802 // declaration.
1803 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1804 if (!FD) {
1805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1806 << Attr.getName() << ExpectedFunctionOrMethod;
1807 return;
1808 }
1809
Michael Han51d8c522013-01-24 16:46:58 +00001810 D->addAttr(::new (S.Context)
1811 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1812 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001813}
1814
John Thompson35cc9622010-08-09 21:53:52 +00001815// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001816static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001817/*
1818 Returning a Vector Class in Registers
1819
Eric Christopherf48f3672010-12-01 22:13:54 +00001820 According to the PPU ABI specifications, a class with a single member of
1821 vector type is returned in memory when used as the return value of a function.
1822 This results in inefficient code when implementing vector classes. To return
1823 the value in a single vector register, add the vecreturn attribute to the
1824 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001825
1826 Example:
1827
1828 struct Vector
1829 {
1830 __vector float xyzw;
1831 } __attribute__((vecreturn));
1832
1833 Vector Add(Vector lhs, Vector rhs)
1834 {
1835 Vector result;
1836 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1837 return result; // This will be returned in a register
1838 }
1839*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001840 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001841 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001842 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001843 return;
1844 }
1845
Chandler Carruth87c44602011-07-01 23:49:12 +00001846 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001847 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1848 return;
1849 }
1850
Chandler Carruth87c44602011-07-01 23:49:12 +00001851 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001852 int count = 0;
1853
1854 if (!isa<CXXRecordDecl>(record)) {
1855 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1856 return;
1857 }
1858
1859 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1860 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1861 return;
1862 }
1863
Eric Christopherf48f3672010-12-01 22:13:54 +00001864 for (RecordDecl::field_iterator iter = record->field_begin();
1865 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001866 if ((count == 1) || !iter->getType()->isVectorType()) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1868 return;
1869 }
1870 count++;
1871 }
1872
Michael Han51d8c522013-01-24 16:46:58 +00001873 D->addAttr(::new (S.Context)
1874 VecReturnAttr(Attr.getRange(), S.Context,
1875 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001876}
1877
Richard Smith3a2b7a12013-01-28 22:42:45 +00001878static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1879 const AttributeList &Attr) {
1880 if (isa<ParmVarDecl>(D)) {
1881 // [[carries_dependency]] can only be applied to a parameter if it is a
1882 // parameter of a function declaration or lambda.
1883 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1884 S.Diag(Attr.getLoc(),
1885 diag::err_carries_dependency_param_not_function_decl);
1886 return;
1887 }
1888 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001890 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001891 return;
1892 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001893
1894 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1895 Attr.getRange(), S.Context,
1896 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001897}
1898
Chandler Carruth1b03c872011-07-02 00:01:44 +00001899static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001900 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001901 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001903 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001904 return;
1905 }
Mike Stumpbf916502009-07-24 19:02:52 +00001906
Michael Han51d8c522013-01-24 16:46:58 +00001907 D->addAttr(::new (S.Context)
1908 UnusedAttr(Attr.getRange(), S.Context,
1909 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001910}
1911
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001912static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1913 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001914 if (!isa<FunctionDecl>(D)) {
1915 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1916 << Attr.getName() << ExpectedFunction;
1917 return;
1918 }
1919
Michael Han51d8c522013-01-24 16:46:58 +00001920 D->addAttr(::new (S.Context)
1921 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1922 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001923}
1924
Chandler Carruth1b03c872011-07-02 00:01:44 +00001925static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001926 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001927 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001928 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1929 return;
1930 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001931 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001932 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001933 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001934 return;
1935 }
Mike Stumpbf916502009-07-24 19:02:52 +00001936
Michael Han51d8c522013-01-24 16:46:58 +00001937 D->addAttr(::new (S.Context)
1938 UsedAttr(Attr.getRange(), S.Context,
1939 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001940}
1941
Chandler Carruth1b03c872011-07-02 00:01:44 +00001942static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001943 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001944 if (Attr.getNumArgs() > 1) {
1945 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001946 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001947 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001948
1949 int priority = 65535; // FIXME: Do not hardcode such constants.
1950 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001951 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001952 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001953 if (E->isTypeDependent() || E->isValueDependent() ||
1954 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001955 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001956 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001957 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001958 return;
1959 }
1960 priority = Idx.getZExtValue();
1961 }
Mike Stumpbf916502009-07-24 19:02:52 +00001962
Chandler Carruth87c44602011-07-01 23:49:12 +00001963 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001965 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001966 return;
1967 }
1968
Michael Han51d8c522013-01-24 16:46:58 +00001969 D->addAttr(::new (S.Context)
1970 ConstructorAttr(Attr.getRange(), S.Context, priority,
1971 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001972}
1973
Chandler Carruth1b03c872011-07-02 00:01:44 +00001974static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001975 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001976 if (Attr.getNumArgs() > 1) {
1977 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001978 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001979 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001980
1981 int priority = 65535; // FIXME: Do not hardcode such constants.
1982 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001983 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001984 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001985 if (E->isTypeDependent() || E->isValueDependent() ||
1986 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001988 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001989 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001990 return;
1991 }
1992 priority = Idx.getZExtValue();
1993 }
Mike Stumpbf916502009-07-24 19:02:52 +00001994
Chandler Carruth87c44602011-07-01 23:49:12 +00001995 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001997 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001998 return;
1999 }
2000
Michael Han51d8c522013-01-24 16:46:58 +00002001 D->addAttr(::new (S.Context)
2002 DestructorAttr(Attr.getRange(), S.Context, priority,
2003 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002004}
2005
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002006template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002007static void handleAttrWithMessage(Sema &S, Decl *D,
2008 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002009 unsigned NumArgs = Attr.getNumArgs();
2010 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002011 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002012 return;
2013 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002014
2015 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002016 StringRef Str;
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002017 if (NumArgs == 1 && !checkStringLiteralArgument(S, Str, Attr, 0))
2018 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002019
Michael Han51d8c522013-01-24 16:46:58 +00002020 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2021 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002022}
2023
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002024static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2025 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002026 D->addAttr(::new (S.Context)
2027 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2028 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002029}
2030
Patrick Beardb2f68202012-04-06 18:12:22 +00002031static void handleObjCRootClassAttr(Sema &S, Decl *D,
2032 const AttributeList &Attr) {
2033 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002034 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2035 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002036 return;
2037 }
2038
Michael Han51d8c522013-01-24 16:46:58 +00002039 D->addAttr(::new (S.Context)
2040 ObjCRootClassAttr(Attr.getRange(), S.Context,
2041 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002042}
2043
Michael Han51d8c522013-01-24 16:46:58 +00002044static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2045 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002046 if (!isa<ObjCInterfaceDecl>(D)) {
2047 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2048 return;
2049 }
2050
Michael Han51d8c522013-01-24 16:46:58 +00002051 D->addAttr(::new (S.Context)
2052 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2053 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002054}
2055
Jordy Rosefad5de92012-05-08 03:27:22 +00002056static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2057 IdentifierInfo *Platform,
2058 VersionTuple Introduced,
2059 VersionTuple Deprecated,
2060 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002061 StringRef PlatformName
2062 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2063 if (PlatformName.empty())
2064 PlatformName = Platform->getName();
2065
2066 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2067 // of these steps are needed).
2068 if (!Introduced.empty() && !Deprecated.empty() &&
2069 !(Introduced <= Deprecated)) {
2070 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2071 << 1 << PlatformName << Deprecated.getAsString()
2072 << 0 << Introduced.getAsString();
2073 return true;
2074 }
2075
2076 if (!Introduced.empty() && !Obsoleted.empty() &&
2077 !(Introduced <= Obsoleted)) {
2078 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2079 << 2 << PlatformName << Obsoleted.getAsString()
2080 << 0 << Introduced.getAsString();
2081 return true;
2082 }
2083
2084 if (!Deprecated.empty() && !Obsoleted.empty() &&
2085 !(Deprecated <= Obsoleted)) {
2086 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2087 << 2 << PlatformName << Obsoleted.getAsString()
2088 << 1 << Deprecated.getAsString();
2089 return true;
2090 }
2091
2092 return false;
2093}
2094
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002095/// \brief Check whether the two versions match.
2096///
2097/// If either version tuple is empty, then they are assumed to match. If
2098/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2099static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2100 bool BeforeIsOkay) {
2101 if (X.empty() || Y.empty())
2102 return true;
2103
2104 if (X == Y)
2105 return true;
2106
2107 if (BeforeIsOkay && X < Y)
2108 return true;
2109
2110 return false;
2111}
2112
Rafael Espindola51be6e32013-01-08 22:04:34 +00002113AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002114 IdentifierInfo *Platform,
2115 VersionTuple Introduced,
2116 VersionTuple Deprecated,
2117 VersionTuple Obsoleted,
2118 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002119 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002120 bool Override,
2121 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002122 VersionTuple MergedIntroduced = Introduced;
2123 VersionTuple MergedDeprecated = Deprecated;
2124 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002125 bool FoundAny = false;
2126
Rafael Espindola98ae8342012-05-10 02:50:16 +00002127 if (D->hasAttrs()) {
2128 AttrVec &Attrs = D->getAttrs();
2129 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2130 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2131 if (!OldAA) {
2132 ++i;
2133 continue;
2134 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002135
Rafael Espindola98ae8342012-05-10 02:50:16 +00002136 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2137 if (OldPlatform != Platform) {
2138 ++i;
2139 continue;
2140 }
2141
2142 FoundAny = true;
2143 VersionTuple OldIntroduced = OldAA->getIntroduced();
2144 VersionTuple OldDeprecated = OldAA->getDeprecated();
2145 VersionTuple OldObsoleted = OldAA->getObsoleted();
2146 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002147
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002148 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2149 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2150 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2151 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002152 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002153 if (Override) {
2154 int Which = -1;
2155 VersionTuple FirstVersion;
2156 VersionTuple SecondVersion;
2157 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2158 Which = 0;
2159 FirstVersion = OldIntroduced;
2160 SecondVersion = Introduced;
2161 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2162 Which = 1;
2163 FirstVersion = Deprecated;
2164 SecondVersion = OldDeprecated;
2165 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2166 Which = 2;
2167 FirstVersion = Obsoleted;
2168 SecondVersion = OldObsoleted;
2169 }
2170
2171 if (Which == -1) {
2172 Diag(OldAA->getLocation(),
2173 diag::warn_mismatched_availability_override_unavail)
2174 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2175 } else {
2176 Diag(OldAA->getLocation(),
2177 diag::warn_mismatched_availability_override)
2178 << Which
2179 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2180 << FirstVersion.getAsString() << SecondVersion.getAsString();
2181 }
2182 Diag(Range.getBegin(), diag::note_overridden_method);
2183 } else {
2184 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2185 Diag(Range.getBegin(), diag::note_previous_attribute);
2186 }
2187
Rafael Espindola98ae8342012-05-10 02:50:16 +00002188 Attrs.erase(Attrs.begin() + i);
2189 --e;
2190 continue;
2191 }
2192
2193 VersionTuple MergedIntroduced2 = MergedIntroduced;
2194 VersionTuple MergedDeprecated2 = MergedDeprecated;
2195 VersionTuple MergedObsoleted2 = MergedObsoleted;
2196
2197 if (MergedIntroduced2.empty())
2198 MergedIntroduced2 = OldIntroduced;
2199 if (MergedDeprecated2.empty())
2200 MergedDeprecated2 = OldDeprecated;
2201 if (MergedObsoleted2.empty())
2202 MergedObsoleted2 = OldObsoleted;
2203
2204 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2205 MergedIntroduced2, MergedDeprecated2,
2206 MergedObsoleted2)) {
2207 Attrs.erase(Attrs.begin() + i);
2208 --e;
2209 continue;
2210 }
2211
2212 MergedIntroduced = MergedIntroduced2;
2213 MergedDeprecated = MergedDeprecated2;
2214 MergedObsoleted = MergedObsoleted2;
2215 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002216 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002217 }
2218
2219 if (FoundAny &&
2220 MergedIntroduced == Introduced &&
2221 MergedDeprecated == Deprecated &&
2222 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002223 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002224
Ted Kremenekcb344392013-04-06 00:34:27 +00002225 // Only create a new attribute if !Override, but we want to do
2226 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002227 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002228 MergedDeprecated, MergedObsoleted) &&
2229 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002230 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2231 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002232 Obsoleted, IsUnavailable, Message,
2233 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002234 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002235 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002236}
2237
Chandler Carruth1b03c872011-07-02 00:01:44 +00002238static void handleAvailabilityAttr(Sema &S, Decl *D,
2239 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002240 if (!checkAttributeNumArgs(S, Attr, 1))
2241 return;
2242 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002243 unsigned Index = Attr.getAttributeSpellingListIndex();
2244
Aaron Ballman624421f2013-08-31 01:11:41 +00002245 IdentifierInfo *II = Platform->Ident;
2246 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2247 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2248 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002249
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002250 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2251 if (!ND) {
2252 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2253 return;
2254 }
2255
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002256 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2257 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2258 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002259 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002260 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002261 if (const StringLiteral *SE =
2262 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002263 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002264
Aaron Ballman624421f2013-08-31 01:11:41 +00002265 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002266 Introduced.Version,
2267 Deprecated.Version,
2268 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002269 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002270 /*Override=*/false,
2271 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002272 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002273 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002274}
2275
John McCalld4c3d662013-02-20 01:54:26 +00002276template <class T>
2277static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2278 typename T::VisibilityType value,
2279 unsigned attrSpellingListIndex) {
2280 T *existingAttr = D->getAttr<T>();
2281 if (existingAttr) {
2282 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2283 if (existingValue == value)
2284 return NULL;
2285 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2286 S.Diag(range.getBegin(), diag::note_previous_attribute);
2287 D->dropAttr<T>();
2288 }
2289 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2290}
2291
Rafael Espindola599f1b72012-05-13 03:25:18 +00002292VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002293 VisibilityAttr::VisibilityType Vis,
2294 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002295 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2296 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002297}
2298
John McCalld4c3d662013-02-20 01:54:26 +00002299TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2300 TypeVisibilityAttr::VisibilityType Vis,
2301 unsigned AttrSpellingListIndex) {
2302 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2303 AttrSpellingListIndex);
2304}
2305
2306static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2307 bool isTypeVisibility) {
2308 // Visibility attributes don't mean anything on a typedef.
2309 if (isa<TypedefNameDecl>(D)) {
2310 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2311 << Attr.getName();
2312 return;
2313 }
2314
2315 // 'type_visibility' can only go on a type or namespace.
2316 if (isTypeVisibility &&
2317 !(isa<TagDecl>(D) ||
2318 isa<ObjCInterfaceDecl>(D) ||
2319 isa<NamespaceDecl>(D))) {
2320 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2321 << Attr.getName() << ExpectedTypeOrNamespace;
2322 return;
2323 }
2324
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002325 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002326 StringRef TypeStr;
2327 SourceLocation LiteralLoc;
2328 if (!checkStringLiteralArgument(S, TypeStr, Attr, 0, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002329 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002330
Sean Huntcf807c42010-08-18 23:23:40 +00002331 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002332 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002333 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002334 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002335 return;
2336 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002337
2338 // Complain about attempts to use protected visibility on targets
2339 // (like Darwin) that don't support it.
2340 if (type == VisibilityAttr::Protected &&
2341 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2342 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2343 type = VisibilityAttr::Default;
2344 }
Mike Stumpbf916502009-07-24 19:02:52 +00002345
Michael Han51d8c522013-01-24 16:46:58 +00002346 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002347 clang::Attr *newAttr;
2348 if (isTypeVisibility) {
2349 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2350 (TypeVisibilityAttr::VisibilityType) type,
2351 Index);
2352 } else {
2353 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2354 }
2355 if (newAttr)
2356 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002357}
2358
Chandler Carruth1b03c872011-07-02 00:01:44 +00002359static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2360 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002361 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2362 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002363 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002364 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002365 return;
2366 }
2367
Aaron Ballman624421f2013-08-31 01:11:41 +00002368 if (!Attr.isArgIdent(0)) {
2369 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2370 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002371 return;
2372 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002373
Aaron Ballmand0686072013-09-11 19:47:58 +00002374 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2375 ObjCMethodFamilyAttr::FamilyKind F;
2376 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2377 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2378 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002379 return;
2380 }
2381
Aaron Ballmand0686072013-09-11 19:47:58 +00002382 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002383 !method->getResultType()->isObjCObjectPointerType()) {
2384 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2385 << method->getResultType();
2386 // Ignore the attribute.
2387 return;
2388 }
2389
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002390 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002391 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002392}
2393
Chandler Carruth1b03c872011-07-02 00:01:44 +00002394static void handleObjCExceptionAttr(Sema &S, Decl *D,
2395 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002396 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2397 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2399 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002400 return;
2401 }
Mike Stumpbf916502009-07-24 19:02:52 +00002402
Michael Han51d8c522013-01-24 16:46:58 +00002403 D->addAttr(::new (S.Context)
2404 ObjCExceptionAttr(Attr.getRange(), S.Context,
2405 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002406}
2407
Chandler Carruth1b03c872011-07-02 00:01:44 +00002408static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002409 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002410 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002411 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002412 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2413 return;
2414 }
2415 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002416 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2417 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002418 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002419 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2420 return;
2421 }
2422 }
2423 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002424 // It is okay to include this attribute on properties, e.g.:
2425 //
2426 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2427 //
2428 // In this case it follows tradition and suppresses an error in the above
2429 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002430 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002431 }
Michael Han51d8c522013-01-24 16:46:58 +00002432 D->addAttr(::new (S.Context)
2433 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2434 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002435}
2436
Mike Stumpbf916502009-07-24 19:02:52 +00002437static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002438handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002439 if (!isa<FunctionDecl>(D)) {
2440 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2441 return;
2442 }
2443
Michael Han51d8c522013-01-24 16:46:58 +00002444 D->addAttr(::new (S.Context)
2445 OverloadableAttr(Attr.getRange(), S.Context,
2446 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002447}
2448
Chandler Carruth1b03c872011-07-02 00:01:44 +00002449static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002450 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002451 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002452 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002453 return;
2454 }
Mike Stumpbf916502009-07-24 19:02:52 +00002455
Aaron Ballman624421f2013-08-31 01:11:41 +00002456 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002457 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002458 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2459 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2460 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002461 return;
2462 }
Mike Stumpbf916502009-07-24 19:02:52 +00002463
Michael Han51d8c522013-01-24 16:46:58 +00002464 D->addAttr(::new (S.Context)
2465 BlocksAttr(Attr.getRange(), S.Context, type,
2466 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002467}
2468
Chandler Carruth1b03c872011-07-02 00:01:44 +00002469static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002470 // check the attribute arguments.
2471 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002472 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002473 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002474 }
2475
John McCall3323fad2011-09-09 07:56:05 +00002476 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002477 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002478 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002479 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002480 if (E->isTypeDependent() || E->isValueDependent() ||
2481 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002483 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002484 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002485 return;
2486 }
Mike Stumpbf916502009-07-24 19:02:52 +00002487
John McCall3323fad2011-09-09 07:56:05 +00002488 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002489 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2490 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002491 return;
2492 }
John McCall3323fad2011-09-09 07:56:05 +00002493
2494 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002495 }
2496
John McCall3323fad2011-09-09 07:56:05 +00002497 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002498 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002499 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002500 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002501 if (E->isTypeDependent() || E->isValueDependent() ||
2502 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002503 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002504 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002505 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002506 return;
2507 }
2508 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002509
John McCall3323fad2011-09-09 07:56:05 +00002510 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002511 // FIXME: This error message could be improved, it would be nice
2512 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002513 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2514 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002515 return;
2516 }
2517 }
2518
Chandler Carruth87c44602011-07-01 23:49:12 +00002519 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002520 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002521 if (isa<FunctionNoProtoType>(FT)) {
2522 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2523 return;
2524 }
Mike Stumpbf916502009-07-24 19:02:52 +00002525
Chris Lattner897cd902009-03-17 23:03:47 +00002526 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002527 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002528 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002529 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002530 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002531 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002532 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002533 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002534 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002535 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2536 if (!BD->isVariadic()) {
2537 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2538 return;
2539 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002540 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002541 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002542 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002543 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002544 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002545 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002546 int m = Ty->isFunctionPointerType() ? 0 : 1;
2547 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002548 return;
2549 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002550 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002552 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002553 return;
2554 }
Anders Carlsson77091822008-10-05 18:05:59 +00002555 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002557 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002558 return;
2559 }
Michael Han51d8c522013-01-24 16:46:58 +00002560 D->addAttr(::new (S.Context)
2561 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2562 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002563}
2564
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002565static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002566 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2567 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2568 else
2569 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2570}
2571
Chandler Carruth1b03c872011-07-02 00:01:44 +00002572static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002573 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002575 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002576 return;
2577 }
Mike Stumpbf916502009-07-24 19:02:52 +00002578
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002579 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2580 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2581 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002582 return;
2583 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002584 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2585 if (MD->getResultType()->isVoidType()) {
2586 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2587 << Attr.getName() << 1;
2588 return;
2589 }
2590
Michael Han51d8c522013-01-24 16:46:58 +00002591 D->addAttr(::new (S.Context)
2592 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2593 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002594}
2595
Chandler Carruth1b03c872011-07-02 00:01:44 +00002596static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002597 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002598 if (isa<CXXRecordDecl>(D)) {
2599 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2600 return;
2601 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002602 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2603 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002604 return;
2605 }
2606
Chandler Carruth87c44602011-07-01 23:49:12 +00002607 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002608
Michael Han51d8c522013-01-24 16:46:58 +00002609 nd->addAttr(::new (S.Context)
2610 WeakAttr(Attr.getRange(), S.Context,
2611 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002612}
2613
Chandler Carruth1b03c872011-07-02 00:01:44 +00002614static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002615 // weak_import only applies to variable & function declarations.
2616 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002617 if (!D->canBeWeakImported(isDef)) {
2618 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002619 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2620 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002621 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002622 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002623 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002624 // Nothing to warn about here.
2625 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002626 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002627 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002628
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002629 return;
2630 }
2631
Michael Han51d8c522013-01-24 16:46:58 +00002632 D->addAttr(::new (S.Context)
2633 WeakImportAttr(Attr.getRange(), S.Context,
2634 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002635}
2636
Tanya Lattner0df579e2012-07-09 22:06:01 +00002637// Handles reqd_work_group_size and work_group_size_hint.
2638static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002639 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002640 unsigned WGSize[3];
2641 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002642 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002643 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002644 if (E->isTypeDependent() || E->isValueDependent() ||
2645 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002646 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2647 << Attr.getName() << AANT_ArgumentIntegerConstant
2648 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002649 return;
2650 }
2651 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2652 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002653
2654 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2655 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2656 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2657 if (!(A->getXDim() == WGSize[0] &&
2658 A->getYDim() == WGSize[1] &&
2659 A->getZDim() == WGSize[2])) {
2660 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2661 Attr.getName();
2662 }
2663 }
2664
2665 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2666 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2667 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2668 if (!(A->getXDim() == WGSize[0] &&
2669 A->getYDim() == WGSize[1] &&
2670 A->getZDim() == WGSize[2])) {
2671 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2672 Attr.getName();
2673 }
2674 }
2675
2676 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2677 D->addAttr(::new (S.Context)
2678 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002679 WGSize[0], WGSize[1], WGSize[2],
2680 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002681 else
2682 D->addAttr(::new (S.Context)
2683 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002684 WGSize[0], WGSize[1], WGSize[2],
2685 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002686}
2687
Joey Gouly37453b92013-03-08 09:42:32 +00002688static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2689 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2690
Aaron Ballman624421f2013-08-31 01:11:41 +00002691 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002692 return;
2693
Aaron Ballman624421f2013-08-31 01:11:41 +00002694 if (!Attr.hasParsedType()) {
2695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2696 << Attr.getName() << 1;
2697 return;
2698 }
2699
Joey Gouly37453b92013-03-08 09:42:32 +00002700 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2701
2702 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2703 (ParmType->isBooleanType() ||
2704 !ParmType->isIntegralType(S.getASTContext()))) {
2705 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2706 << ParmType;
2707 return;
2708 }
2709
2710 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2711 D->hasAttr<VecTypeHintAttr>()) {
2712 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2713 if (A->getTypeHint() != ParmType) {
2714 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2715 return;
2716 }
2717 }
2718
2719 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2720 ParmType, Attr.getLoc()));
2721}
2722
Rafael Espindola599f1b72012-05-13 03:25:18 +00002723SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002724 StringRef Name,
2725 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002726 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2727 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002728 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002729 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2730 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002731 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002732 }
Michael Han51d8c522013-01-24 16:46:58 +00002733 return ::new (Context) SectionAttr(Range, Context, Name,
2734 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002735}
2736
Chandler Carruth1b03c872011-07-02 00:01:44 +00002737static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002738 // Make sure that there is a string literal as the sections's single
2739 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002740 StringRef Str;
2741 SourceLocation LiteralLoc;
2742 if (!checkStringLiteralArgument(S, Str, Attr, 0, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002743 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Chris Lattner797c3c42009-08-10 19:03:04 +00002745 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002746 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002747 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002748 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002749 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002750 return;
2751 }
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002753 // This attribute cannot be applied to local variables.
2754 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002755 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002756 return;
2757 }
Michael Han51d8c522013-01-24 16:46:58 +00002758
2759 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002760 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002761 if (NewAttr)
2762 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002763}
2764
Chris Lattner6b6b5372008-06-26 18:38:35 +00002765
Chandler Carruth1b03c872011-07-02 00:01:44 +00002766static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002767 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002768 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002769 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002770 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002771 D->addAttr(::new (S.Context)
2772 NoThrowAttr(Attr.getRange(), S.Context,
2773 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002774 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002775}
2776
Chandler Carruth1b03c872011-07-02 00:01:44 +00002777static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002778 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002779 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002780 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002781 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002782 D->addAttr(::new (S.Context)
2783 ConstAttr(Attr.getRange(), S.Context,
2784 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002785 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002786}
2787
Chandler Carruth1b03c872011-07-02 00:01:44 +00002788static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002789 D->addAttr(::new (S.Context)
2790 PureAttr(Attr.getRange(), S.Context,
2791 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002792}
2793
Chandler Carruth1b03c872011-07-02 00:01:44 +00002794static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002795 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002796 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002797 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002798 return;
2799 }
Mike Stumpbf916502009-07-24 19:02:52 +00002800
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002801 Expr *E = Attr.getArgAsExpr(0);
2802 SourceLocation Loc = E->getExprLoc();
2803 FunctionDecl *FD = 0;
2804 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002805
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002806 // gcc only allows for simple identifiers. Since we support more than gcc, we
2807 // will warn the user.
2808 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2809 if (DRE->hasQualifier())
2810 S.Diag(Loc, diag::warn_cleanup_ext);
2811 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2812 NI = DRE->getNameInfo();
2813 if (!FD) {
2814 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2815 << NI.getName();
2816 return;
2817 }
2818 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2819 if (ULE->hasExplicitTemplateArgs())
2820 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002821
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002822 // This will diagnose the case where the function cannot be found.
2823 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2824 NI = ULE->getNameInfo();
2825 } else {
2826 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002827 return;
2828 }
2829
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002830 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002831 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2832 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002833 return;
2834 }
Mike Stumpbf916502009-07-24 19:02:52 +00002835
Anders Carlsson89941c12009-02-07 23:16:50 +00002836 // We're currently more strict than GCC about what function types we accept.
2837 // If this ever proves to be a problem it should be easy to fix.
2838 QualType Ty = S.Context.getPointerType(VD->getType());
2839 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002840 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2841 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002842 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2843 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002844 return;
2845 }
Mike Stumpbf916502009-07-24 19:02:52 +00002846
Michael Han51d8c522013-01-24 16:46:58 +00002847 D->addAttr(::new (S.Context)
2848 CleanupAttr(Attr.getRange(), S.Context, FD,
2849 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002850}
2851
Mike Stumpbf916502009-07-24 19:02:52 +00002852/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002853/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002854static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002855 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002857 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002858 return;
2859 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002860
Aaron Ballman624421f2013-08-31 01:11:41 +00002861 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002862 uint64_t ArgIdx;
2863 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2864 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002865 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002866
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002867 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002868 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002869
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002870 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2871 if (not_nsstring_type &&
2872 !isCFStringType(Ty, S.Context) &&
2873 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002874 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002875 // FIXME: Should highlight the actual expression that has the wrong type.
2876 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002877 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002878 << IdxExpr->getSourceRange();
2879 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002880 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002881 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002882 if (!isNSStringType(Ty, S.Context) &&
2883 !isCFStringType(Ty, S.Context) &&
2884 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002885 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002886 // FIXME: Should highlight the actual expression that has the wrong type.
2887 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002888 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002889 << IdxExpr->getSourceRange();
2890 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002891 }
2892
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002893 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2894 // because that has corrected for the implicit this parameter, and is zero-
2895 // based. The attribute expects what the user wrote explicitly.
2896 llvm::APSInt Val;
2897 IdxExpr->EvaluateAsInt(Val, S.Context);
2898
Michael Han51d8c522013-01-24 16:46:58 +00002899 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002900 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002901 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002902}
2903
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002904enum FormatAttrKind {
2905 CFStringFormat,
2906 NSStringFormat,
2907 StrftimeFormat,
2908 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002909 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002910 InvalidFormat
2911};
2912
2913/// getFormatAttrKind - Map from format attribute names to supported format
2914/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002915static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002916 return llvm::StringSwitch<FormatAttrKind>(Format)
2917 // Check for formats that get handled specially.
2918 .Case("NSString", NSStringFormat)
2919 .Case("CFString", CFStringFormat)
2920 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002921
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002922 // Otherwise, check for supported formats.
2923 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2924 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2925 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002926
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002927 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2928 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002929}
2930
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002931/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002932/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002933static void handleInitPriorityAttr(Sema &S, Decl *D,
2934 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002935 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002936 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2937 return;
2938 }
2939
Chandler Carruth87c44602011-07-01 23:49:12 +00002940 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002941 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2942 Attr.setInvalid();
2943 return;
2944 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002945 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002946 if (S.Context.getAsArrayType(T))
2947 T = S.Context.getBaseElementType(T);
2948 if (!T->getAs<RecordType>()) {
2949 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2950 Attr.setInvalid();
2951 return;
2952 }
2953
Aaron Ballman624421f2013-08-31 01:11:41 +00002954 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002955
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002956 llvm::APSInt priority(32);
2957 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2958 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002959 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2960 << Attr.getName() << AANT_ArgumentIntegerConstant
2961 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002962 Attr.setInvalid();
2963 return;
2964 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002965 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002966 if (prioritynum < 101 || prioritynum > 65535) {
2967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2968 << priorityExpr->getSourceRange();
2969 Attr.setInvalid();
2970 return;
2971 }
Michael Han51d8c522013-01-24 16:46:58 +00002972 D->addAttr(::new (S.Context)
2973 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2974 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002975}
2976
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00002977FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2978 IdentifierInfo *Format, int FormatIdx,
2979 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00002980 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002981 // Check whether we already have an equivalent format attribute.
2982 for (specific_attr_iterator<FormatAttr>
2983 i = D->specific_attr_begin<FormatAttr>(),
2984 e = D->specific_attr_end<FormatAttr>();
2985 i != e ; ++i) {
2986 FormatAttr *f = *i;
2987 if (f->getType() == Format &&
2988 f->getFormatIdx() == FormatIdx &&
2989 f->getFirstArg() == FirstArg) {
2990 // If we don't have a valid location for this attribute, adopt the
2991 // location.
2992 if (f->getLocation().isInvalid())
2993 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002994 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002995 }
2996 }
2997
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00002998 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2999 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003000}
3001
Mike Stumpbf916502009-07-24 19:02:52 +00003002/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003003/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003004static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003005 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003007 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003008 return;
3009 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003010
Chandler Carruth87c44602011-07-01 23:49:12 +00003011 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003012 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003013 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003014 return;
3015 }
3016
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003017 // In C++ the implicit 'this' function parameter also counts, and they are
3018 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003019 bool HasImplicitThisParam = isInstanceMethod(D);
3020 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003021 unsigned FirstIdx = 1;
3022
Aaron Ballman624421f2013-08-31 01:11:41 +00003023 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3024 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003025
3026 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003027 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003028 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003029 // If we've modified the string name, we need a new identifier for it.
3030 II = &S.Context.Idents.get(Format);
3031 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003032
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003033 // Check for supported formats.
3034 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003035
3036 if (Kind == IgnoredFormat)
3037 return;
3038
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003039 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003040 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003041 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003042 return;
3043 }
3044
3045 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003046 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003047 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003048 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3049 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003051 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003052 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003053 return;
3054 }
3055
3056 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003057 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003058 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003059 return;
3060 }
3061
3062 // FIXME: Do we need to bounds check?
3063 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003064
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003065 if (HasImplicitThisParam) {
3066 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003067 S.Diag(Attr.getLoc(),
3068 diag::err_format_attribute_implicit_this_format_string)
3069 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003070 return;
3071 }
3072 ArgIdx--;
3073 }
Mike Stump1eb44332009-09-09 15:08:12 +00003074
Chris Lattner6b6b5372008-06-26 18:38:35 +00003075 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003076 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003077
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003078 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003079 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003080 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3081 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003082 return;
3083 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003084 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003085 // FIXME: do we need to check if the type is NSString*? What are the
3086 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003087 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003088 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003089 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3090 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003091 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003092 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003093 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003094 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003095 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003096 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3097 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003098 return;
3099 }
3100
3101 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003102 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003103 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003104 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3105 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003107 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003108 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003109 return;
3110 }
3111
3112 // check if the function is variadic if the 3rd argument non-zero
3113 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003114 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003115 ++NumArgs; // +1 for ...
3116 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003117 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003118 return;
3119 }
3120 }
3121
Chris Lattner3c73c412008-11-19 08:23:25 +00003122 // strftime requires FirstArg to be 0 because it doesn't read from any
3123 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003124 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003125 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003126 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3127 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003128 return;
3129 }
3130 // if 0 it disables parameter checking (to use with e.g. va_list)
3131 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003132 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003133 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 return;
3135 }
3136
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003137 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003138 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003139 FirstArg.getZExtValue(),
3140 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003141 if (NewAttr)
3142 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003143}
3144
Chandler Carruth1b03c872011-07-02 00:01:44 +00003145static void handleTransparentUnionAttr(Sema &S, Decl *D,
3146 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003147 // Try to find the underlying union declaration.
3148 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003149 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003150 if (TD && TD->getUnderlyingType()->isUnionType())
3151 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3152 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003153 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003154
3155 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003156 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003157 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003158 return;
3159 }
3160
John McCall5e1cdac2011-10-07 06:10:15 +00003161 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003162 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003163 diag::warn_transparent_union_attribute_not_definition);
3164 return;
3165 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003166
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003167 RecordDecl::field_iterator Field = RD->field_begin(),
3168 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003169 if (Field == FieldEnd) {
3170 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3171 return;
3172 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003173
David Blaikie581deb32012-06-06 20:45:41 +00003174 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003175 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003176 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003177 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003178 diag::warn_transparent_union_attribute_floating)
3179 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003180 return;
3181 }
3182
3183 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3184 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3185 for (; Field != FieldEnd; ++Field) {
3186 QualType FieldType = Field->getType();
3187 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3188 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3189 // Warn if we drop the attribute.
3190 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003191 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003192 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003193 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003194 diag::warn_transparent_union_attribute_field_size_align)
3195 << isSize << Field->getDeclName() << FieldBits;
3196 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003197 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003198 diag::note_transparent_union_first_field_size_align)
3199 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003200 return;
3201 }
3202 }
3203
Michael Han51d8c522013-01-24 16:46:58 +00003204 RD->addAttr(::new (S.Context)
3205 TransparentUnionAttr(Attr.getRange(), S.Context,
3206 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003207}
3208
Chandler Carruth1b03c872011-07-02 00:01:44 +00003209static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003210 // Make sure that there is a string literal as the annotation's single
3211 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003212 StringRef Str;
3213 if (!checkStringLiteralArgument(S, Str, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003214 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003215
3216 // Don't duplicate annotations that are already set.
3217 for (specific_attr_iterator<AnnotateAttr>
3218 i = D->specific_attr_begin<AnnotateAttr>(),
3219 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003220 if ((*i)->getAnnotation() == Str)
3221 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003222 }
Michael Han51d8c522013-01-24 16:46:58 +00003223
3224 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003225 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003226 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003227}
3228
Chandler Carruth1b03c872011-07-02 00:01:44 +00003229static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003230 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003231 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3233 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003234 return;
3235 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003236
Richard Smithbe507b62013-02-01 08:12:08 +00003237 if (Attr.getNumArgs() == 0) {
3238 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3239 true, 0, Attr.getAttributeSpellingListIndex()));
3240 return;
3241 }
3242
Aaron Ballman624421f2013-08-31 01:11:41 +00003243 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003244 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3245 S.Diag(Attr.getEllipsisLoc(),
3246 diag::err_pack_expansion_without_parameter_packs);
3247 return;
3248 }
3249
3250 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3251 return;
3252
3253 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3254 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003255}
3256
3257void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003258 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003259 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3260 SourceLocation AttrLoc = AttrRange.getBegin();
3261
Richard Smith4cd81c52013-01-29 09:02:09 +00003262 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003263 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003264 // C++11 [dcl.align]p1:
3265 // An alignment-specifier may be applied to a variable or to a class
3266 // data member, but it shall not be applied to a bit-field, a function
3267 // parameter, the formal parameter of a catch clause, or a variable
3268 // declared with the register storage class specifier. An
3269 // alignment-specifier may also be applied to the declaration of a class
3270 // or enumeration type.
3271 // C11 6.7.5/2:
3272 // An alignment attribute shall not be specified in a declaration of
3273 // a typedef, or a bit-field, or a function, or a parameter, or an
3274 // object declared with the register storage-class specifier.
3275 int DiagKind = -1;
3276 if (isa<ParmVarDecl>(D)) {
3277 DiagKind = 0;
3278 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3279 if (VD->getStorageClass() == SC_Register)
3280 DiagKind = 1;
3281 if (VD->isExceptionVariable())
3282 DiagKind = 2;
3283 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3284 if (FD->isBitField())
3285 DiagKind = 3;
3286 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003287 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3288 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003289 << (TmpAttr.isC11() ? ExpectedVariableOrField
3290 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003291 return;
3292 }
3293 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003294 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003295 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003296 return;
3297 }
3298 }
3299
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003300 if (E->isTypeDependent() || E->isValueDependent()) {
3301 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003302 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3303 AA->setPackExpansion(IsPackExpansion);
3304 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003305 return;
3306 }
Michael Hana31f65b2013-02-01 01:19:17 +00003307
Sean Huntcf807c42010-08-18 23:23:40 +00003308 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003309 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003310 ExprResult ICE
3311 = VerifyIntegerConstantExpression(E, &Alignment,
3312 diag::err_aligned_attribute_argument_not_int,
3313 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003314 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003315 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003316
3317 // C++11 [dcl.align]p2:
3318 // -- if the constant expression evaluates to zero, the alignment
3319 // specifier shall have no effect
3320 // C11 6.7.5p6:
3321 // An alignment specification of zero has no effect.
3322 if (!(TmpAttr.isAlignas() && !Alignment) &&
3323 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003324 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3325 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003326 return;
3327 }
Michael Hana31f65b2013-02-01 01:19:17 +00003328
Richard Smithbe507b62013-02-01 08:12:08 +00003329 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003330 // We've already verified it's a power of 2, now let's make sure it's
3331 // 8192 or less.
3332 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003333 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003334 << E->getSourceRange();
3335 return;
3336 }
3337 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003338
Richard Smithf6565a92013-02-22 08:32:16 +00003339 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3340 ICE.take(), SpellingListIndex);
3341 AA->setPackExpansion(IsPackExpansion);
3342 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003343}
3344
Michael Hana31f65b2013-02-01 01:19:17 +00003345void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003346 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003347 // FIXME: Cache the number on the Attr object if non-dependent?
3348 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003349 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3350 SpellingListIndex);
3351 AA->setPackExpansion(IsPackExpansion);
3352 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003353}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003354
Richard Smithbe507b62013-02-01 08:12:08 +00003355void Sema::CheckAlignasUnderalignment(Decl *D) {
3356 assert(D->hasAttrs() && "no attributes on decl");
3357
3358 QualType Ty;
3359 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3360 Ty = VD->getType();
3361 else
3362 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003363 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003364 return;
3365
3366 // C++11 [dcl.align]p5, C11 6.7.5/4:
3367 // The combined effect of all alignment attributes in a declaration shall
3368 // not specify an alignment that is less strict than the alignment that
3369 // would otherwise be required for the entity being declared.
3370 AlignedAttr *AlignasAttr = 0;
3371 unsigned Align = 0;
3372 for (specific_attr_iterator<AlignedAttr>
3373 I = D->specific_attr_begin<AlignedAttr>(),
3374 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3375 if (I->isAlignmentDependent())
3376 return;
3377 if (I->isAlignas())
3378 AlignasAttr = *I;
3379 Align = std::max(Align, I->getAlignment(Context));
3380 }
3381
3382 if (AlignasAttr && Align) {
3383 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3384 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3385 if (NaturalAlign > RequestedAlign)
3386 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3387 << Ty << (unsigned)NaturalAlign.getQuantity();
3388 }
3389}
3390
Chandler Carruthd309c812011-07-01 23:49:16 +00003391/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003392/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003393///
Mike Stumpbf916502009-07-24 19:02:52 +00003394/// Despite what would be logical, the mode attribute is a decl attribute, not a
3395/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3396/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003397static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003398 // This attribute isn't documented, but glibc uses it. It changes
3399 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003400 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003401 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3402 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003403 return;
3404 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003405
Aaron Ballman624421f2013-08-31 01:11:41 +00003406 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3407 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003408
3409 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003410 if (Str.startswith("__") && Str.endswith("__"))
3411 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003412
3413 unsigned DestWidth = 0;
3414 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003415 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003416 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003417 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003418 switch (Str[0]) {
3419 case 'Q': DestWidth = 8; break;
3420 case 'H': DestWidth = 16; break;
3421 case 'S': DestWidth = 32; break;
3422 case 'D': DestWidth = 64; break;
3423 case 'X': DestWidth = 96; break;
3424 case 'T': DestWidth = 128; break;
3425 }
3426 if (Str[1] == 'F') {
3427 IntegerMode = false;
3428 } else if (Str[1] == 'C') {
3429 IntegerMode = false;
3430 ComplexMode = true;
3431 } else if (Str[1] != 'I') {
3432 DestWidth = 0;
3433 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003434 break;
3435 case 4:
3436 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3437 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003438 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003439 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003440 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003441 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003442 break;
3443 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003444 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003445 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003446 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003447 case 11:
3448 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003449 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003450 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003451 }
3452
3453 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003454 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003455 OldTy = TD->getUnderlyingType();
3456 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3457 OldTy = VD->getType();
3458 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003459 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003460 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003461 return;
3462 }
Eli Friedman73397492009-03-03 06:41:03 +00003463
John McCall183700f2009-09-21 23:43:11 +00003464 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003465 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3466 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003467 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003468 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3469 } else if (ComplexMode) {
3470 if (!OldTy->isComplexType())
3471 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3472 } else {
3473 if (!OldTy->isFloatingType())
3474 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3475 }
3476
Mike Stump390b4cc2009-05-16 07:39:55 +00003477 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3478 // and friends, at least with glibc.
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003479 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3480 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003481 // FIXME: Make sure floating-point mappings are accurate
3482 // FIXME: Support XF and TF types
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003483 QualType NewTy;
3484 switch (DestWidth) {
3485 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003486 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003487 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003488 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003489 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003490 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003491 case 8:
3492 if (!IntegerMode) {
3493 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3494 return;
3495 }
3496 if (OldTy->isSignedIntegerType())
3497 NewTy = S.Context.SignedCharTy;
3498 else
3499 NewTy = S.Context.UnsignedCharTy;
3500 break;
3501 case 16:
3502 if (!IntegerMode) {
3503 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3504 return;
3505 }
3506 if (OldTy->isSignedIntegerType())
3507 NewTy = S.Context.ShortTy;
3508 else
3509 NewTy = S.Context.UnsignedShortTy;
3510 break;
3511 case 32:
3512 if (!IntegerMode)
3513 NewTy = S.Context.FloatTy;
3514 else if (OldTy->isSignedIntegerType())
3515 NewTy = S.Context.IntTy;
3516 else
3517 NewTy = S.Context.UnsignedIntTy;
3518 break;
3519 case 64:
3520 if (!IntegerMode)
3521 NewTy = S.Context.DoubleTy;
3522 else if (OldTy->isSignedIntegerType())
3523 if (S.Context.getTargetInfo().getLongWidth() == 64)
3524 NewTy = S.Context.LongTy;
3525 else
3526 NewTy = S.Context.LongLongTy;
3527 else
3528 if (S.Context.getTargetInfo().getLongWidth() == 64)
3529 NewTy = S.Context.UnsignedLongTy;
3530 else
3531 NewTy = S.Context.UnsignedLongLongTy;
3532 break;
3533 case 96:
3534 NewTy = S.Context.LongDoubleTy;
3535 break;
3536 case 128:
3537 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3538 &llvm::APFloat::PPCDoubleDouble) {
3539 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3540 return;
3541 }
3542 if (IntegerMode) {
3543 if (OldTy->isSignedIntegerType())
3544 NewTy = S.Context.Int128Ty;
3545 else
3546 NewTy = S.Context.UnsignedInt128Ty;
3547 } else
3548 NewTy = S.Context.LongDoubleTy;
3549 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003550 }
3551
Eli Friedman73397492009-03-03 06:41:03 +00003552 if (ComplexMode) {
3553 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003554 }
3555
3556 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003557 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3558 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3559 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003560 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003561
3562 D->addAttr(::new (S.Context)
3563 ModeAttr(Attr.getRange(), S.Context, Name,
3564 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003565}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003566
Chandler Carruth1b03c872011-07-02 00:01:44 +00003567static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003568 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3569 if (!VD->hasGlobalStorage())
3570 S.Diag(Attr.getLoc(),
3571 diag::warn_attribute_requires_functions_or_static_globals)
3572 << Attr.getName();
3573 } else if (!isFunctionOrMethod(D)) {
3574 S.Diag(Attr.getLoc(),
3575 diag::warn_attribute_requires_functions_or_static_globals)
3576 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003577 return;
3578 }
Mike Stumpbf916502009-07-24 19:02:52 +00003579
Michael Han51d8c522013-01-24 16:46:58 +00003580 D->addAttr(::new (S.Context)
3581 NoDebugAttr(Attr.getRange(), S.Context,
3582 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003583}
3584
Chandler Carruth1b03c872011-07-02 00:01:44 +00003585static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003586 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003588 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003589 return;
3590 }
Mike Stumpbf916502009-07-24 19:02:52 +00003591
Michael Han51d8c522013-01-24 16:46:58 +00003592 D->addAttr(::new (S.Context)
3593 NoInlineAttr(Attr.getRange(), S.Context,
3594 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003595}
3596
Chandler Carruth1b03c872011-07-02 00:01:44 +00003597static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3598 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003599 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003600 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003601 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003602 return;
3603 }
3604
Michael Han51d8c522013-01-24 16:46:58 +00003605 D->addAttr(::new (S.Context)
3606 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3607 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003608}
3609
Chandler Carruth1b03c872011-07-02 00:01:44 +00003610static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003611 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003612 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003614 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003615 return;
3616 }
3617
Michael Han51d8c522013-01-24 16:46:58 +00003618 D->addAttr(::new (S.Context)
3619 CUDAConstantAttr(Attr.getRange(), S.Context,
3620 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003621 } else {
3622 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3623 }
3624}
3625
Chandler Carruth1b03c872011-07-02 00:01:44 +00003626static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003627 if (S.LangOpts.CUDA) {
3628 // check the attribute arguments.
3629 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003630 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3631 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003632 return;
3633 }
3634
Chandler Carruth87c44602011-07-01 23:49:12 +00003635 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003636 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003637 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003638 return;
3639 }
3640
Michael Han51d8c522013-01-24 16:46:58 +00003641 D->addAttr(::new (S.Context)
3642 CUDADeviceAttr(Attr.getRange(), S.Context,
3643 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003644 } else {
3645 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3646 }
3647}
3648
Chandler Carruth1b03c872011-07-02 00:01:44 +00003649static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003650 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003651 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003653 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003654 return;
3655 }
3656
Chandler Carruth87c44602011-07-01 23:49:12 +00003657 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003658 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003659 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003660 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003661 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3662 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003663 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003664 "void");
3665 } else {
3666 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3667 << FD->getType();
3668 }
3669 return;
3670 }
3671
Michael Han51d8c522013-01-24 16:46:58 +00003672 D->addAttr(::new (S.Context)
3673 CUDAGlobalAttr(Attr.getRange(), S.Context,
3674 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003675 } else {
3676 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3677 }
3678}
3679
Chandler Carruth1b03c872011-07-02 00:01:44 +00003680static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003681 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003682 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003683 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003684 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003685 return;
3686 }
3687
Michael Han51d8c522013-01-24 16:46:58 +00003688 D->addAttr(::new (S.Context)
3689 CUDAHostAttr(Attr.getRange(), S.Context,
3690 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003691 } else {
3692 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3693 }
3694}
3695
Chandler Carruth1b03c872011-07-02 00:01:44 +00003696static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003697 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003698 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003700 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003701 return;
3702 }
3703
Michael Han51d8c522013-01-24 16:46:58 +00003704 D->addAttr(::new (S.Context)
3705 CUDASharedAttr(Attr.getRange(), S.Context,
3706 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003707 } else {
3708 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3709 }
3710}
3711
Chandler Carruth1b03c872011-07-02 00:01:44 +00003712static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003713 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003714 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003716 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003717 return;
3718 }
Mike Stumpbf916502009-07-24 19:02:52 +00003719
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003720 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003721 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003722 return;
3723 }
Mike Stumpbf916502009-07-24 19:02:52 +00003724
Michael Han51d8c522013-01-24 16:46:58 +00003725 D->addAttr(::new (S.Context)
3726 GNUInlineAttr(Attr.getRange(), S.Context,
3727 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003728}
3729
Chandler Carruth1b03c872011-07-02 00:01:44 +00003730static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003731 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003732
Aaron Ballmanfff32482012-12-09 17:45:41 +00003733 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003734 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003735 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3736 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003737 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003738 return;
3739
Chandler Carruth87c44602011-07-01 23:49:12 +00003740 if (!isa<ObjCMethodDecl>(D)) {
3741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3742 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003743 return;
3744 }
3745
Chandler Carruth87c44602011-07-01 23:49:12 +00003746 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003747 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003748 D->addAttr(::new (S.Context)
3749 FastCallAttr(Attr.getRange(), S.Context,
3750 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003751 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003752 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003753 D->addAttr(::new (S.Context)
3754 StdCallAttr(Attr.getRange(), S.Context,
3755 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003756 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003757 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003758 D->addAttr(::new (S.Context)
3759 ThisCallAttr(Attr.getRange(), S.Context,
3760 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003761 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003762 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003763 D->addAttr(::new (S.Context)
3764 CDeclAttr(Attr.getRange(), S.Context,
3765 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003766 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003767 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003768 D->addAttr(::new (S.Context)
3769 PascalAttr(Attr.getRange(), S.Context,
3770 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003771 return;
Charles Davise8519c32013-08-30 04:39:01 +00003772 case AttributeList::AT_MSABI:
3773 D->addAttr(::new (S.Context)
3774 MSABIAttr(Attr.getRange(), S.Context,
3775 Attr.getAttributeSpellingListIndex()));
3776 return;
3777 case AttributeList::AT_SysVABI:
3778 D->addAttr(::new (S.Context)
3779 SysVABIAttr(Attr.getRange(), S.Context,
3780 Attr.getAttributeSpellingListIndex()));
3781 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003782 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003783 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003784 switch (CC) {
3785 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003786 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003787 break;
3788 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003789 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003790 break;
3791 default:
3792 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003793 }
3794
Michael Han51d8c522013-01-24 16:46:58 +00003795 D->addAttr(::new (S.Context)
3796 PcsAttr(Attr.getRange(), S.Context, PCS,
3797 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003798 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003799 }
Derek Schuff263366f2012-10-16 22:30:41 +00003800 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003801 D->addAttr(::new (S.Context)
3802 PnaclCallAttr(Attr.getRange(), S.Context,
3803 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003804 return;
Guy Benyei38980082012-12-25 08:53:55 +00003805 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003806 D->addAttr(::new (S.Context)
3807 IntelOclBiccAttr(Attr.getRange(), S.Context,
3808 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003809 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003810
Abramo Bagnarae215f722010-04-30 13:10:51 +00003811 default:
3812 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003813 }
3814}
3815
Chandler Carruth1b03c872011-07-02 00:01:44 +00003816static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003817 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003818}
3819
Guy Benyei1db70402013-03-24 13:58:12 +00003820static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003821 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003822 llvm::APSInt ArgNum(32);
3823 if (E->isTypeDependent() || E->isValueDependent() ||
3824 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003825 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3826 << Attr.getName() << AANT_ArgumentIntegerConstant
3827 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003828 return;
3829 }
3830
3831 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3832 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3833}
3834
Aaron Ballmanfff32482012-12-09 17:45:41 +00003835bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3836 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003837 if (attr.isInvalid())
3838 return true;
3839
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003840 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003841 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003842 attr.setInvalid();
3843 return true;
3844 }
3845
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003846 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3847 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003848 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003849 case AttributeList::AT_CDecl: CC = CC_C; break;
3850 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3851 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3852 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3853 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003854 case AttributeList::AT_MSABI:
3855 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3856 CC_X86_64Win64;
3857 break;
3858 case AttributeList::AT_SysVABI:
3859 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3860 CC_C;
3861 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003862 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003863 StringRef StrRef;
3864 if (!checkStringLiteralArgument(*this, StrRef, attr, 0)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003865 attr.setInvalid();
3866 return true;
3867 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003868 if (StrRef == "aapcs") {
3869 CC = CC_AAPCS;
3870 break;
3871 } else if (StrRef == "aapcs-vfp") {
3872 CC = CC_AAPCS_VFP;
3873 break;
3874 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003875
3876 attr.setInvalid();
3877 Diag(attr.getLoc(), diag::err_invalid_pcs);
3878 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003879 }
Derek Schuff263366f2012-10-16 22:30:41 +00003880 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003881 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003882 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003883 }
3884
Aaron Ballman82bfa192012-10-02 14:26:08 +00003885 const TargetInfo &TI = Context.getTargetInfo();
3886 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3887 if (A == TargetInfo::CCCR_Warning) {
3888 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003889
3890 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3891 if (FD)
3892 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3893 TargetInfo::CCMT_NonMember;
3894 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003895 }
3896
John McCall711c52b2011-01-05 12:14:39 +00003897 return false;
3898}
3899
Chandler Carruth1b03c872011-07-02 00:01:44 +00003900static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003901 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003902
3903 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003904 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003905 return;
3906
Chandler Carruth87c44602011-07-01 23:49:12 +00003907 if (!isa<ObjCMethodDecl>(D)) {
3908 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3909 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003910 return;
3911 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003912
Michael Han51d8c522013-01-24 16:46:58 +00003913 D->addAttr(::new (S.Context)
3914 RegparmAttr(Attr.getRange(), S.Context, numParams,
3915 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003916}
3917
3918/// Checks a regparm attribute, returning true if it is ill-formed and
3919/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003920bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3921 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003922 return true;
3923
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003924 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003925 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003926 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003927 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003928
Aaron Ballman624421f2013-08-31 01:11:41 +00003929 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003930 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003931 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003932 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003933 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3934 << Attr.getName() << AANT_ArgumentIntegerConstant
3935 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003936 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003937 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003938 }
3939
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003940 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003941 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003942 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003943 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003944 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003945 }
3946
John McCall711c52b2011-01-05 12:14:39 +00003947 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003948 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003949 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003950 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003951 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003952 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003953 }
3954
John McCall711c52b2011-01-05 12:14:39 +00003955 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003956}
3957
Chandler Carruth1b03c872011-07-02 00:01:44 +00003958static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003959 if (S.LangOpts.CUDA) {
3960 // check the attribute arguments.
3961 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003962 // FIXME: 0 is not okay.
3963 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003964 return;
3965 }
3966
Chandler Carruth87c44602011-07-01 23:49:12 +00003967 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003968 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003969 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003970 return;
3971 }
3972
Aaron Ballman624421f2013-08-31 01:11:41 +00003973 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003974 llvm::APSInt MaxThreads(32);
3975 if (MaxThreadsExpr->isTypeDependent() ||
3976 MaxThreadsExpr->isValueDependent() ||
3977 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003978 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003979 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003980 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003981 return;
3982 }
3983
3984 llvm::APSInt MinBlocks(32);
3985 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003986 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003987 if (MinBlocksExpr->isTypeDependent() ||
3988 MinBlocksExpr->isValueDependent() ||
3989 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003990 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003991 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003992 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003993 return;
3994 }
3995 }
3996
Michael Han51d8c522013-01-24 16:46:58 +00003997 D->addAttr(::new (S.Context)
3998 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3999 MaxThreads.getZExtValue(),
4000 MinBlocks.getZExtValue(),
4001 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004002 } else {
4003 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4004 }
4005}
4006
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004007static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4008 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004009 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004011 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004012 return;
4013 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004014
4015 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004016 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004017
Aaron Ballman624421f2013-08-31 01:11:41 +00004018 StringRef AttrName = Attr.getName()->getName();
4019 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004020
4021 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4022 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4023 << Attr.getName() << ExpectedFunctionOrMethod;
4024 return;
4025 }
4026
4027 uint64_t ArgumentIdx;
4028 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4029 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004030 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004031 return;
4032
4033 uint64_t TypeTagIdx;
4034 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4035 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004036 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004037 return;
4038
4039 bool IsPointer = (AttrName == "pointer_with_type_tag");
4040 if (IsPointer) {
4041 // Ensure that buffer has a pointer type.
4042 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4043 if (!BufferTy->isPointerType()) {
4044 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004045 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004046 }
4047 }
4048
Michael Han51d8c522013-01-24 16:46:58 +00004049 D->addAttr(::new (S.Context)
4050 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4051 ArgumentIdx, TypeTagIdx, IsPointer,
4052 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004053}
4054
4055static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4056 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004057 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004058 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004059 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004060 return;
4061 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004062
4063 if (!checkAttributeNumArgs(S, Attr, 1))
4064 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004065
Aaron Ballman624421f2013-08-31 01:11:41 +00004066 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004067 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4068
Michael Han51d8c522013-01-24 16:46:58 +00004069 D->addAttr(::new (S.Context)
4070 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4071 MatchingCType,
4072 Attr.getLayoutCompatible(),
4073 Attr.getMustBeNull(),
4074 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004075}
4076
Chris Lattner0744e5f2008-06-29 00:23:49 +00004077//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004078// Checker-specific attribute handlers.
4079//===----------------------------------------------------------------------===//
4080
John McCallc7ad3812011-01-25 03:31:58 +00004081static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004082 return type->isDependentType() ||
4083 type->isObjCObjectPointerType() ||
4084 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004085}
4086static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004087 return type->isDependentType() ||
4088 type->isPointerType() ||
4089 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004090}
4091
Chandler Carruth1b03c872011-07-02 00:01:44 +00004092static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004093 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004094 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004095 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004096 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004097 return;
4098 }
4099
4100 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004101 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004102 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4103 cf = false;
4104 } else {
4105 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4106 cf = true;
4107 }
4108
4109 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004110 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004111 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004112 return;
4113 }
4114
4115 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004116 param->addAttr(::new (S.Context)
4117 CFConsumedAttr(Attr.getRange(), S.Context,
4118 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004119 else
Michael Han51d8c522013-01-24 16:46:58 +00004120 param->addAttr(::new (S.Context)
4121 NSConsumedAttr(Attr.getRange(), S.Context,
4122 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004123}
4124
Chandler Carruth1b03c872011-07-02 00:01:44 +00004125static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4126 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004127 if (!isa<ObjCMethodDecl>(D)) {
4128 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004129 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004130 return;
4131 }
4132
Michael Han51d8c522013-01-24 16:46:58 +00004133 D->addAttr(::new (S.Context)
4134 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4135 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004136}
4137
Chandler Carruth1b03c872011-07-02 00:01:44 +00004138static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4139 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004140
John McCallc7ad3812011-01-25 03:31:58 +00004141 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004142
Chandler Carruth87c44602011-07-01 23:49:12 +00004143 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004144 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004145 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004146 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004147 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004148 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4149 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004150 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004151 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004152 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004153 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004154 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004155 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004156 return;
4157 }
Mike Stumpbf916502009-07-24 19:02:52 +00004158
John McCallc7ad3812011-01-25 03:31:58 +00004159 bool typeOK;
4160 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004161 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004162 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004163 case AttributeList::AT_NSReturnsAutoreleased:
4164 case AttributeList::AT_NSReturnsRetained:
4165 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004166 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4167 cf = false;
4168 break;
4169
Sean Hunt8e083e72012-06-19 23:57:03 +00004170 case AttributeList::AT_CFReturnsRetained:
4171 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004172 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4173 cf = true;
4174 break;
4175 }
4176
4177 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004178 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004179 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004180 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004181 }
Mike Stumpbf916502009-07-24 19:02:52 +00004182
Chandler Carruth87c44602011-07-01 23:49:12 +00004183 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004184 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004185 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004186 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004187 D->addAttr(::new (S.Context)
4188 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4189 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004190 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004191 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004192 D->addAttr(::new (S.Context)
4193 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4194 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004195 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004196 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004197 D->addAttr(::new (S.Context)
4198 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4199 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004200 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004201 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004202 D->addAttr(::new (S.Context)
4203 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4204 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004205 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004206 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004207 D->addAttr(::new (S.Context)
4208 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4209 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004210 return;
4211 };
4212}
4213
John McCalldc7c5ad2011-07-22 08:53:00 +00004214static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4215 const AttributeList &attr) {
4216 SourceLocation loc = attr.getLoc();
4217
4218 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4219
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004220 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004221 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004222 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004223 return;
4224 }
4225
4226 // Check that the method returns a normal pointer.
4227 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004228
4229 if (!resultType->isReferenceType() &&
4230 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004231 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4232 << SourceRange(loc)
4233 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4234
4235 // Drop the attribute.
4236 return;
4237 }
4238
Michael Han51d8c522013-01-24 16:46:58 +00004239 method->addAttr(::new (S.Context)
4240 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4241 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004242}
4243
Fariborz Jahanian84101132012-09-07 23:46:23 +00004244static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4245 const AttributeList &attr) {
4246 SourceLocation loc = attr.getLoc();
4247 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4248
4249 if (!method) {
4250 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4251 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4252 return;
4253 }
4254 DeclContext *DC = method->getDeclContext();
4255 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4256 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4257 << attr.getName() << 0;
4258 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4259 return;
4260 }
4261 if (method->getMethodFamily() == OMF_dealloc) {
4262 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4263 << attr.getName() << 1;
4264 return;
4265 }
4266
Michael Han51d8c522013-01-24 16:46:58 +00004267 method->addAttr(::new (S.Context)
4268 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4269 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004270}
4271
John McCall8dfac0b2011-09-30 05:12:12 +00004272/// Handle cf_audited_transfer and cf_unknown_transfer.
4273static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4274 if (!isa<FunctionDecl>(D)) {
4275 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004276 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004277 return;
4278 }
4279
Sean Hunt8e083e72012-06-19 23:57:03 +00004280 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004281
4282 // Check whether there's a conflicting attribute already present.
4283 Attr *Existing;
4284 if (IsAudited) {
4285 Existing = D->getAttr<CFUnknownTransferAttr>();
4286 } else {
4287 Existing = D->getAttr<CFAuditedTransferAttr>();
4288 }
4289 if (Existing) {
4290 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4291 << A.getName()
4292 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4293 << A.getRange() << Existing->getRange();
4294 return;
4295 }
4296
4297 // All clear; add the attribute.
4298 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004299 D->addAttr(::new (S.Context)
4300 CFAuditedTransferAttr(A.getRange(), S.Context,
4301 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004302 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004303 D->addAttr(::new (S.Context)
4304 CFUnknownTransferAttr(A.getRange(), S.Context,
4305 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004306 }
4307}
4308
John McCallfe98da02011-09-29 07:17:38 +00004309static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4310 const AttributeList &Attr) {
4311 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4312 if (!RD || RD->isUnion()) {
4313 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004314 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004315 }
4316
Aaron Ballman624421f2013-08-31 01:11:41 +00004317 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004318
4319 // In Objective-C, verify that the type names an Objective-C type.
4320 // We don't want to check this outside of ObjC because people sometimes
4321 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004322 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004323 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004324 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004325 Sema::LookupOrdinaryName);
4326 if (S.LookupName(R, Sc)) {
4327 NamedDecl *Target = R.getFoundDecl();
4328 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4329 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4330 S.Diag(Target->getLocStart(), diag::note_declared_at);
4331 }
4332 }
4333 }
4334
Michael Han51d8c522013-01-24 16:46:58 +00004335 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004336 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004337 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004338}
4339
Chandler Carruth1b03c872011-07-02 00:01:44 +00004340static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4341 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004342 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004343
Chandler Carruth87c44602011-07-01 23:49:12 +00004344 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004345 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004346}
4347
Chandler Carruth1b03c872011-07-02 00:01:44 +00004348static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4349 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004350 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004351 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004352 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004353 return;
4354 }
4355
Chandler Carruth87c44602011-07-01 23:49:12 +00004356 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004357 QualType type = vd->getType();
4358
4359 if (!type->isDependentType() &&
4360 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004361 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004362 << type;
4363 return;
4364 }
4365
4366 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4367
4368 // If we have no lifetime yet, check the lifetime we're presumably
4369 // going to infer.
4370 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4371 lifetime = type->getObjCARCImplicitLifetime();
4372
4373 switch (lifetime) {
4374 case Qualifiers::OCL_None:
4375 assert(type->isDependentType() &&
4376 "didn't infer lifetime for non-dependent type?");
4377 break;
4378
4379 case Qualifiers::OCL_Weak: // meaningful
4380 case Qualifiers::OCL_Strong: // meaningful
4381 break;
4382
4383 case Qualifiers::OCL_ExplicitNone:
4384 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004385 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004386 << (lifetime == Qualifiers::OCL_Autoreleasing);
4387 break;
4388 }
4389
Chandler Carruth87c44602011-07-01 23:49:12 +00004390 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004391 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4392 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004393}
4394
Francois Pichet11542142010-12-19 06:50:37 +00004395//===----------------------------------------------------------------------===//
4396// Microsoft specific attribute handlers.
4397//===----------------------------------------------------------------------===//
4398
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004399// Check if MS extensions or some other language extensions are enabled. If
4400// not, issue a diagnostic that the given attribute is unused.
4401static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4402 bool OtherExtension = false) {
4403 if (S.LangOpts.MicrosoftExt || OtherExtension)
4404 return true;
4405 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4406 return false;
4407}
4408
Chandler Carruth1b03c872011-07-02 00:01:44 +00004409static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004410 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4411 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004412
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004413 StringRef StrRef;
4414 SourceLocation LiteralLoc;
4415 if (!checkStringLiteralArgument(S, StrRef, Attr, 0, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004416 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004417
David Majnemerbb0ed292013-08-09 08:56:20 +00004418 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4419 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004420 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4421 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004422
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004423 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004424 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004425 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004426 return;
4427 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004428
David Majnemerbb0ed292013-08-09 08:56:20 +00004429 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004430 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004431 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004432 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004433 return;
4434 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004435 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004436 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004437 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004438 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004439 }
Francois Pichet11542142010-12-19 06:50:37 +00004440
David Majnemerbb0ed292013-08-09 08:56:20 +00004441 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4442 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004443}
4444
John McCallc052dbb2012-05-22 21:28:12 +00004445static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004446 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004447 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004448
4449 AttributeList::Kind Kind = Attr.getKind();
4450 if (Kind == AttributeList::AT_SingleInheritance)
4451 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004452 ::new (S.Context)
4453 SingleInheritanceAttr(Attr.getRange(), S.Context,
4454 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004455 else if (Kind == AttributeList::AT_MultipleInheritance)
4456 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004457 ::new (S.Context)
4458 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4459 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004460 else if (Kind == AttributeList::AT_VirtualInheritance)
4461 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004462 ::new (S.Context)
4463 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4464 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004465}
4466
4467static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004468 if (!checkMicrosoftExt(S, Attr))
4469 return;
4470
4471 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004472 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004473 D->addAttr(
4474 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4475 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004476}
4477
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004478static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004479 if (!checkMicrosoftExt(S, Attr))
4480 return;
4481 D->addAttr(::new (S.Context)
4482 ForceInlineAttr(Attr.getRange(), S.Context,
4483 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004484}
4485
Reid Klecknera7225342013-05-20 14:02:37 +00004486static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4487 if (!checkMicrosoftExt(S, Attr))
4488 return;
4489 // Check linkage after possibly merging declaratinos. See
4490 // checkAttributesAfterMerging().
4491 D->addAttr(::new (S.Context)
4492 SelectAnyAttr(Attr.getRange(), S.Context,
4493 Attr.getAttributeSpellingListIndex()));
4494}
4495
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004496/// Handles semantic checking for features that are common to all attributes,
4497/// such as checking whether a parameter was properly specified, or the correct
4498/// number of arguments were passed, etc.
4499static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4500 const AttributeList &Attr) {
4501 // Several attributes carry different semantics than the parsing requires, so
4502 // those are opted out of the common handling.
4503 //
4504 // We also bail on unknown and ignored attributes because those are handled
4505 // as part of the target-specific handling logic.
4506 if (Attr.hasCustomParsing() ||
4507 Attr.getKind() == AttributeList::UnknownAttribute ||
4508 Attr.getKind() == AttributeList::IgnoredAttribute)
4509 return false;
4510
4511 // If there are no optional arguments, then checking for the argument count
4512 // is trivial.
4513 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4514 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4515 return true;
4516 return false;
4517}
4518
Ted Kremenekb71368d2009-05-09 02:44:38 +00004519//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004520// Top Level Sema Entry Points
4521//===----------------------------------------------------------------------===//
4522
Richard Smith4a97b8e2013-08-29 00:47:48 +00004523/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4524/// the attribute applies to decls. If the attribute is a type attribute, just
4525/// silently ignore it if a GNU attribute.
4526static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4527 const AttributeList &Attr,
4528 bool IncludeCXX11Attributes) {
4529 if (Attr.isInvalid())
4530 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004531
Richard Smith4a97b8e2013-08-29 00:47:48 +00004532 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4533 // instead.
4534 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4535 return;
4536
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004537 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4538 return;
4539
Chris Lattner803d0802008-06-29 00:43:07 +00004540 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004541 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4542 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4543 case AttributeList::AT_IBOutletCollection:
4544 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004545 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004546 case AttributeList::AT_ObjCGC:
4547 case AttributeList::AT_VectorSize:
4548 case AttributeList::AT_NeonVectorType:
4549 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004550 case AttributeList::AT_Ptr32:
4551 case AttributeList::AT_Ptr64:
4552 case AttributeList::AT_SPtr:
4553 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004554 // Ignore these, these are type attributes, handled by
4555 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004556 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004557 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4558 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4559 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4560 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004561 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004562 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004563 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004564 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004565 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4566 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4567 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004568 handleDependencyAttr(S, scope, D, Attr);
4569 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004570 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4571 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4572 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004573 case AttributeList::AT_CXX11NoReturn:
4574 handleCXX11NoReturnAttr(S, D, Attr);
4575 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004576 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004577 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004578 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004579 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4580 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004581 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004582 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004583 case AttributeList::AT_MinSize:
4584 handleMinSizeAttr(S, D, Attr);
4585 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004586 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4587 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4588 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004589 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4590 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004591 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4592 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004593 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004594 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004595 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4596 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004597 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004598 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4599 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004600 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004601 case AttributeList::AT_ownership_returns:
4602 case AttributeList::AT_ownership_takes:
4603 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004604 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004605 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4606 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4607 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4608 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4609 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4610 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4611 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004612
Sean Hunt8e083e72012-06-19 23:57:03 +00004613 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004614 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004615 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004616 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004617
Sean Hunt8e083e72012-06-19 23:57:03 +00004618 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004619 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4620
Fariborz Jahanian84101132012-09-07 23:46:23 +00004621 case AttributeList::AT_ObjCRequiresSuper:
4622 handleObjCRequiresSuperAttr(S, D, Attr); break;
4623
Sean Hunt8e083e72012-06-19 23:57:03 +00004624 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004625 handleNSBridgedAttr(S, scope, D, Attr); break;
4626
Sean Hunt8e083e72012-06-19 23:57:03 +00004627 case AttributeList::AT_CFAuditedTransfer:
4628 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004629 handleCFTransferAttr(S, D, Attr); break;
4630
Ted Kremenekb71368d2009-05-09 02:44:38 +00004631 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004632 case AttributeList::AT_CFConsumed:
4633 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4634 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004635 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004636
Sean Hunt8e083e72012-06-19 23:57:03 +00004637 case AttributeList::AT_NSReturnsAutoreleased:
4638 case AttributeList::AT_NSReturnsNotRetained:
4639 case AttributeList::AT_CFReturnsNotRetained:
4640 case AttributeList::AT_NSReturnsRetained:
4641 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004642 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004643
Tanya Lattner0df579e2012-07-09 22:06:01 +00004644 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004645 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004646 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004647
Joey Gouly37453b92013-03-08 09:42:32 +00004648 case AttributeList::AT_VecTypeHint:
4649 handleVecTypeHint(S, D, Attr); break;
4650
Sean Hunt8e083e72012-06-19 23:57:03 +00004651 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004652 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004653
Sean Hunt8e083e72012-06-19 23:57:03 +00004654 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4655 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4656 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004657 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004658 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004659 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004660 handleArcWeakrefUnavailableAttr (S, D, Attr);
4661 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004662 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004663 handleObjCRootClassAttr(S, D, Attr);
4664 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004665 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004666 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004667 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004668 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4669 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004670 handleReturnsTwiceAttr(S, D, Attr);
4671 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004672 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004673 case AttributeList::AT_Visibility:
4674 handleVisibilityAttr(S, D, Attr, false);
4675 break;
4676 case AttributeList::AT_TypeVisibility:
4677 handleVisibilityAttr(S, D, Attr, true);
4678 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004679 case AttributeList::AT_WarnUnused:
4680 handleWarnUnusedAttr(S, D, Attr);
4681 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004683 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004684 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4685 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4686 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4687 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004688 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004689 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004690 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004691 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004692 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004693 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004694 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004695 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004696 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4697 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4698 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4699 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4700 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4701 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4702 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4703 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4704 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004705 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004706 // Just ignore
4707 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004708 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004709 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004710 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004711 case AttributeList::AT_StdCall:
4712 case AttributeList::AT_CDecl:
4713 case AttributeList::AT_FastCall:
4714 case AttributeList::AT_ThisCall:
4715 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004716 case AttributeList::AT_MSABI:
4717 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004718 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004719 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004720 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004721 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004722 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004723 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004724 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004725 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004726 case AttributeList::AT_OpenCLImageAccess:
4727 handleOpenCLImageAccessAttr(S, D, Attr);
4728 break;
John McCallc052dbb2012-05-22 21:28:12 +00004729
4730 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004731 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004732 handleMsStructAttr(S, D, Attr);
4733 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004734 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004735 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004736 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004737 case AttributeList::AT_SingleInheritance:
4738 case AttributeList::AT_MultipleInheritance:
4739 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004740 handleInheritanceAttr(S, D, Attr);
4741 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004742 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004743 handlePortabilityAttr(S, D, Attr);
4744 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004746 handleForceInlineAttr(S, D, Attr);
4747 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004748 case AttributeList::AT_SelectAny:
4749 handleSelectAnyAttr(S, D, Attr);
4750 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004751
4752 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004753 case AttributeList::AT_AssertExclusiveLock:
4754 handleAssertExclusiveLockAttr(S, D, Attr);
4755 break;
4756 case AttributeList::AT_AssertSharedLock:
4757 handleAssertSharedLockAttr(S, D, Attr);
4758 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004759 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004760 handleGuardedVarAttr(S, D, Attr);
4761 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004762 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004763 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004764 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004765 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004766 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004767 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004768 case AttributeList::AT_NoSanitizeAddress:
4769 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004770 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004771 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004772 handleNoThreadSafetyAnalysis(S, D, Attr);
4773 break;
4774 case AttributeList::AT_NoSanitizeThread:
4775 handleNoSanitizeThread(S, D, Attr);
4776 break;
4777 case AttributeList::AT_NoSanitizeMemory:
4778 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004779 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004780 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004781 handleLockableAttr(S, D, Attr);
4782 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004784 handleGuardedByAttr(S, D, Attr);
4785 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004786 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004787 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004788 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004790 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004791 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004792 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004793 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004794 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004795 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004796 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004797 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004799 handleLockReturnedAttr(S, D, Attr);
4800 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004801 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004802 handleLocksExcludedAttr(S, D, Attr);
4803 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004804 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004805 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004806 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004808 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004809 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004810 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004811 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004812 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004813 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004814 handleUnlockFunAttr(S, D, Attr);
4815 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004817 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004818 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004819 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004820 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004821 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004822
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004823 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004824 case AttributeList::AT_Consumable:
4825 handleConsumableAttr(S, D, Attr);
4826 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004827 case AttributeList::AT_Consumes:
4828 handleConsumesAttr(S, D, Attr);
4829 break;
4830 case AttributeList::AT_CallableWhenUnconsumed:
4831 handleCallableWhenUnconsumedAttr(S, D, Attr);
4832 break;
4833 case AttributeList::AT_TestsConsumed:
4834 handleTestsConsumedAttr(S, D, Attr);
4835 break;
4836 case AttributeList::AT_TestsUnconsumed:
4837 handleTestsUnconsumedAttr(S, D, Attr);
4838 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004839 case AttributeList::AT_ReturnTypestate:
4840 handleReturnTypestateAttr(S, D, Attr);
4841 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004842
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004843 // Type safety attributes.
4844 case AttributeList::AT_ArgumentWithTypeTag:
4845 handleArgumentWithTypeTagAttr(S, D, Attr);
4846 break;
4847 case AttributeList::AT_TypeTagForDatatype:
4848 handleTypeTagForDatatypeAttr(S, D, Attr);
4849 break;
4850
Chris Lattner803d0802008-06-29 00:43:07 +00004851 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004852 // Ask target about the attribute.
4853 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4854 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004855 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4856 diag::warn_unhandled_ms_attribute_ignored :
4857 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004858 break;
4859 }
4860}
4861
4862/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4863/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004864void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004865 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004866 bool IncludeCXX11Attributes) {
4867 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004868 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004869
4870 // GCC accepts
4871 // static int a9 __attribute__((weakref));
4872 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004873 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004874 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004875 cast<NamedDecl>(D)->getNameAsString();
4876 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004877 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004878 }
4879}
4880
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004881// Annotation attributes are the only attributes allowed after an access
4882// specifier.
4883bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4884 const AttributeList *AttrList) {
4885 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004886 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004887 handleAnnotateAttr(*this, ASDecl, *l);
4888 } else {
4889 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4890 return true;
4891 }
4892 }
4893
4894 return false;
4895}
4896
John McCalle82247a2011-10-01 05:17:03 +00004897/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4898/// contains any decl attributes that we should warn about.
4899static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4900 for ( ; A; A = A->getNext()) {
4901 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004902 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004903 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4904
4905 if (A->getKind() == AttributeList::UnknownAttribute) {
4906 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4907 << A->getName() << A->getRange();
4908 } else {
4909 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4910 << A->getName() << A->getRange();
4911 }
4912 }
4913}
4914
4915/// checkUnusedDeclAttributes - Given a declarator which is not being
4916/// used to build a declaration, complain about any decl attributes
4917/// which might be lying around on it.
4918void Sema::checkUnusedDeclAttributes(Declarator &D) {
4919 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4920 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4921 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4922 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4923}
4924
Ryan Flynne25ff832009-07-30 03:15:39 +00004925/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004926/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004927NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4928 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004929 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004930 NamedDecl *NewD = 0;
4931 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004932 FunctionDecl *NewFD;
4933 // FIXME: Missing call to CheckFunctionDeclaration().
4934 // FIXME: Mangling?
4935 // FIXME: Is the qualifier info correct?
4936 // FIXME: Is the DeclContext correct?
4937 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4938 Loc, Loc, DeclarationName(II),
4939 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004940 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004941 FD->hasPrototype(),
4942 false/*isConstexprSpecified*/);
4943 NewD = NewFD;
4944
4945 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004946 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004947
4948 // Fake up parameter variables; they are declared as if this were
4949 // a typedef.
4950 QualType FDTy = FD->getType();
4951 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4952 SmallVector<ParmVarDecl*, 16> Params;
4953 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4954 AE = FT->arg_type_end(); AI != AE; ++AI) {
4955 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4956 Param->setScopeInfo(0, Params.size());
4957 Params.push_back(Param);
4958 }
David Blaikie4278c652011-09-21 18:16:56 +00004959 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004960 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004961 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4962 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004963 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004964 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004965 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00004966 if (VD->getQualifier()) {
4967 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004968 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004969 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004970 }
4971 return NewD;
4972}
4973
James Dennett1dfbd922012-06-14 21:40:34 +00004974/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004975/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004976void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004977 if (W.getUsed()) return; // only do this once
4978 W.setUsed(true);
4979 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4980 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004981 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004982 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4983 NDId->getName()));
4984 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004985 WeakTopLevelDecl.push_back(NewD);
4986 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4987 // to insert Decl at TU scope, sorry.
4988 DeclContext *SavedContext = CurContext;
4989 CurContext = Context.getTranslationUnitDecl();
4990 PushOnScopeChains(NewD, S);
4991 CurContext = SavedContext;
4992 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004993 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004994 }
4995}
4996
Rafael Espindola65611bf2013-03-02 21:41:48 +00004997void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4998 // It's valid to "forward-declare" #pragma weak, in which case we
4999 // have to do this.
5000 LoadExternalWeakUndeclaredIdentifiers();
5001 if (!WeakUndeclaredIdentifiers.empty()) {
5002 NamedDecl *ND = NULL;
5003 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5004 if (VD->isExternC())
5005 ND = VD;
5006 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5007 if (FD->isExternC())
5008 ND = FD;
5009 if (ND) {
5010 if (IdentifierInfo *Id = ND->getIdentifier()) {
5011 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5012 = WeakUndeclaredIdentifiers.find(Id);
5013 if (I != WeakUndeclaredIdentifiers.end()) {
5014 WeakInfo W = I->second;
5015 DeclApplyPragmaWeak(S, ND, W);
5016 WeakUndeclaredIdentifiers[Id] = W;
5017 }
5018 }
5019 }
5020 }
5021}
5022
Chris Lattner0744e5f2008-06-29 00:23:49 +00005023/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5024/// it, apply them to D. This is a bit tricky because PD can have attributes
5025/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005026void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005027 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005028 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005029 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005030
Chris Lattner0744e5f2008-06-29 00:23:49 +00005031 // Walk the declarator structure, applying decl attributes that were in a type
5032 // position to the decl itself. This handles cases like:
5033 // int *__attr__(x)** D;
5034 // when X is a decl attribute.
5035 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5036 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005037 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005038
Chris Lattner0744e5f2008-06-29 00:23:49 +00005039 // Finally, apply any attributes on the decl itself.
5040 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005041 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005042}
John McCall54abf7d2009-11-04 02:18:39 +00005043
John McCallf85e1932011-06-15 23:02:42 +00005044/// Is the given declaration allowed to use a forbidden type?
5045static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5046 // Private ivars are always okay. Unfortunately, people don't
5047 // always properly make their ivars private, even in system headers.
5048 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005049 // Function declarations in sys headers will be marked unavailable.
5050 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5051 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005052 return false;
5053
5054 // Require it to be declared in a system header.
5055 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5056}
5057
5058/// Handle a delayed forbidden-type diagnostic.
5059static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5060 Decl *decl) {
5061 if (decl && isForbiddenTypeAllowed(S, decl)) {
5062 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5063 "this system declaration uses an unsupported type"));
5064 return;
5065 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005066 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005067 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005068 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005069 // kind of forbidden type messages on unavailable functions.
5070 if (FD->hasAttr<UnavailableAttr>() &&
5071 diag.getForbiddenTypeDiagnostic() ==
5072 diag::err_arc_array_param_no_ownership) {
5073 diag.Triggered = true;
5074 return;
5075 }
5076 }
John McCallf85e1932011-06-15 23:02:42 +00005077
5078 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5079 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5080 diag.Triggered = true;
5081}
5082
John McCall92576642012-05-07 06:16:41 +00005083void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5084 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005085 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005086 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005087
John McCall92576642012-05-07 06:16:41 +00005088 // When delaying diagnostics to run in the context of a parsed
5089 // declaration, we only want to actually emit anything if parsing
5090 // succeeds.
5091 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005092
John McCall92576642012-05-07 06:16:41 +00005093 // We emit all the active diagnostics in this pool or any of its
5094 // parents. In general, we'll get one pool for the decl spec
5095 // and a child pool for each declarator; in a decl group like:
5096 // deprecated_typedef foo, *bar, baz();
5097 // only the declarator pops will be passed decls. This is correct;
5098 // we really do need to consider delayed diagnostics from the decl spec
5099 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005100 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005101 do {
John McCall13489672012-05-07 06:16:58 +00005102 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005103 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5104 // This const_cast is a bit lame. Really, Triggered should be mutable.
5105 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005106 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005107 continue;
5108
John McCalleee1d542011-02-14 07:13:47 +00005109 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005110 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005111 // Don't bother giving deprecation diagnostics if the decl is invalid.
5112 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005113 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005114 break;
5115
5116 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005117 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005118 break;
John McCallf85e1932011-06-15 23:02:42 +00005119
5120 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005121 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005122 break;
John McCall2f514482010-01-27 03:50:35 +00005123 }
5124 }
John McCall92576642012-05-07 06:16:41 +00005125 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005126}
5127
John McCall13489672012-05-07 06:16:58 +00005128/// Given a set of delayed diagnostics, re-emit them as if they had
5129/// been delayed in the current context instead of in the given pool.
5130/// Essentially, this just moves them to the current pool.
5131void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5132 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5133 assert(curPool && "re-emitting in undelayed context not supported");
5134 curPool->steal(pool);
5135}
5136
John McCall54abf7d2009-11-04 02:18:39 +00005137static bool isDeclDeprecated(Decl *D) {
5138 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005139 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005140 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005141 // A category implicitly has the availability of the interface.
5142 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5143 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005144 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5145 return false;
5146}
5147
Eli Friedmanc3b23082012-08-08 21:52:41 +00005148static void
5149DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5150 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005151 const ObjCInterfaceDecl *UnknownObjCClass,
5152 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005153 DeclarationName Name = D->getDeclName();
5154 if (!Message.empty()) {
5155 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5156 S.Diag(D->getLocation(),
5157 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5158 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005159 if (ObjCPropery)
5160 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5161 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005162 } else if (!UnknownObjCClass) {
5163 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5164 S.Diag(D->getLocation(),
5165 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5166 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005167 if (ObjCPropery)
5168 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5169 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005170 } else {
5171 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5172 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5173 }
5174}
5175
John McCall9c3087b2010-08-26 02:13:20 +00005176void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005177 Decl *Ctx) {
5178 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005179 return;
5180
John McCall2f514482010-01-27 03:50:35 +00005181 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005182 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5183 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005184 DD.getUnknownObjCClass(),
5185 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005186}
5187
Chris Lattner5f9e2722011-07-23 10:55:15 +00005188void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005189 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005190 const ObjCInterfaceDecl *UnknownObjCClass,
5191 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005192 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005193 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005194 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5195 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005196 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005197 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005198 return;
5199 }
5200
5201 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005202 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005203 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005204 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005205}