blob: 30f2e67e2588e30285353cf1284f4b4975fd0e7c [file] [log] [blame]
Chris Lattner2c6fcf52008-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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000021#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000025#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000034namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000035 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036 C,
37 Cpp,
38 ObjC
39 };
40}
41
Chris Lattner58418ff2008-06-29 00:16:31 +000042//===----------------------------------------------------------------------===//
43// Helper functions
44//===----------------------------------------------------------------------===//
45
Chandler Carruthff4c4f02011-07-01 23:49:12 +000046static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000047 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000048 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000050 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000051 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000052 Ty = decl->getUnderlyingType();
53 else
54 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000055
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000057 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000058 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000059 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000060
John McCall9dd450b2009-09-21 23:43:11 +000061 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000062}
63
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000064// FIXME: We should provide an abstraction around a method or function
65// to provide the following bits of information.
66
Nuno Lopes518e3702009-12-20 23:11:08 +000067/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000068/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069static bool isFunction(const Decl *D) {
70 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000071}
72
73/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000074/// type (function or function-typed variable) or an Objective-C
75/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000077 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000078}
79
Fariborz Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethodOrBlock - Return true if the given decl has function
81/// type (function or function-typed variable) or an Objective-C
82/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000083static bool isFunctionOrMethodOrBlock(const Decl *D) {
84 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000085 return true;
86 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088 QualType Ty = V->getType();
89 return Ty->isBlockPointerType();
90 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000092}
93
John McCall3882ace2011-01-05 12:14:39 +000094/// Return true if the given decl has a declarator that should have
95/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000097 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000098 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
99 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000100}
101
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000102/// hasFunctionProto - Return true if the given decl has a argument
103/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000105static bool hasFunctionProto(const Decl *D) {
106 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000107 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000108 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000109 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110 return true;
111 }
112}
113
114/// getFunctionOrMethodNumArgs - Return number of function or method
115/// arguments. It is an error to call this on a K&R function (use
116/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
118 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000119 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000120 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000121 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123}
124
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000130
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000131 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static QualType getFunctionOrMethodResultType(const Decl *D) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000136 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000138}
139
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140static bool isFunctionOrMethodVariadic(const Decl *D) {
141 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000142 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000143 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000145 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000146 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000148 }
149}
150
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000151static bool isInstanceMethod(const Decl *D) {
152 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000153 return MethodDecl->isInstance();
154 return false;
155}
156
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000157static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000158 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000159 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000160 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000161
John McCall96fa4842010-05-17 21:00:27 +0000162 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
163 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000164 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000165
John McCall96fa4842010-05-17 21:00:27 +0000166 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 // FIXME: Should we walk the chain of classes?
169 return ClsName == &Ctx.Idents.get("NSString") ||
170 ClsName == &Ctx.Idents.get("NSMutableString");
171}
172
Daniel Dunbar980c6692008-09-26 03:32:58 +0000173static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000174 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000175 if (!PT)
176 return false;
177
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000178 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000179 if (!RT)
180 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000181
Daniel Dunbar980c6692008-09-26 03:32:58 +0000182 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000183 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000184 return false;
185
186 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
187}
188
Richard Smithb87c4652013-10-31 21:23:20 +0000189static unsigned getNumAttributeArgs(const AttributeList &Attr) {
190 // FIXME: Include the type in the argument list.
191 return Attr.getNumArgs() + Attr.hasParsedType();
192}
193
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000194/// \brief Check if the attribute has exactly as many args as Num. May
195/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000196static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000197 unsigned Num) {
198 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
200 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000201 return false;
202 }
203
204 return true;
205}
206
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000207/// \brief Check if the attribute has at least as many args as Num. May
208/// output an error.
209static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000210 unsigned Num) {
211 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000212 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
213 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214 return false;
215 }
216
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217 return true;
218}
219
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000220/// \brief If Expr is a valid integer constant, get the value of the integer
221/// expression and return success or failure. May output an error.
222static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
223 const Expr *Expr, uint32_t &Val,
224 unsigned Idx = UINT_MAX) {
225 llvm::APSInt I(32);
226 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
227 !Expr->isIntegerConstantExpr(I, S.Context)) {
228 if (Idx != UINT_MAX)
229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
230 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
231 << Expr->getSourceRange();
232 else
233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
234 << Attr.getName() << AANT_ArgumentIntegerConstant
235 << Expr->getSourceRange();
236 return false;
237 }
238 Val = (uint32_t)I.getZExtValue();
239 return true;
240}
241
Aaron Ballmanfb763042013-12-02 18:05:46 +0000242/// \brief Diagnose mutually exclusive attributes when present on a given
243/// declaration. Returns true if diagnosed.
244template <typename AttrTy>
245static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000246 const AttributeList &Attr) {
247 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000248 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000249 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000250 return true;
251 }
252 return false;
253}
254
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000255/// \brief Check if IdxExpr is a valid argument index for a function or
256/// instance method D. May output an error.
257///
258/// \returns true if IdxExpr is a valid index.
259static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000260 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000261 unsigned AttrArgNum,
262 const Expr *IdxExpr,
263 uint64_t &Idx)
264{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000265 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266
267 // In C++ the implicit 'this' function parameter also counts.
268 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000269 bool HP = hasFunctionProto(D);
270 bool HasImplicitThisParam = isInstanceMethod(D);
271 bool IV = HP && isFunctionOrMethodVariadic(D);
272 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
273 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000274
275 llvm::APSInt IdxInt;
276 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
277 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
279 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
280 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000281 return false;
282 }
283
284 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000285 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
287 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000288 return false;
289 }
290 Idx--; // Convert to zero-based.
291 if (HasImplicitThisParam) {
292 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000293 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000295 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000296 return false;
297 }
298 --Idx;
299 }
300
301 return true;
302}
303
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000304/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
305/// If not emit an error and return false. If the argument is an identifier it
306/// will emit an error with a fixit hint and treat it as if it was a string
307/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000308bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
309 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000310 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000311 // Look for identifiers. If we have one emit a hint to fix it to a literal.
312 if (Attr.isArgIdent(ArgNum)) {
313 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000314 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000315 << Attr.getName() << AANT_ArgumentString
316 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000317 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000318 Str = Loc->Ident->getName();
319 if (ArgLocation)
320 *ArgLocation = Loc->Loc;
321 return true;
322 }
323
324 // Now check for an actual string literal.
325 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
326 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
327 if (ArgLocation)
328 *ArgLocation = ArgExpr->getLocStart();
329
330 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000331 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000332 << Attr.getName() << AANT_ArgumentString;
333 return false;
334 }
335
336 Str = Literal->getString();
337 return true;
338}
339
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000340/// \brief Applies the given attribute to the Decl without performing any
341/// additional semantic checking.
342template <typename AttrType>
343static void handleSimpleAttribute(Sema &S, Decl *D,
344 const AttributeList &Attr) {
345 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
346 Attr.getAttributeSpellingListIndex()));
347}
348
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000349/// \brief Check if the passed-in expression is of type int or bool.
350static bool isIntOrBool(Expr *Exp) {
351 QualType QT = Exp->getType();
352 return QT->isBooleanType() || QT->isIntegerType();
353}
354
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000355
356// Check to see if the type is a smart pointer of some kind. We assume
357// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000358static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
359 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
360 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000361 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000363
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000364 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
365 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000366 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000367 return false;
368
369 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000370}
371
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000372/// \brief Check if passed in Decl is a pointer type.
373/// Note that this function may produce an error message.
374/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
376 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000377 const ValueDecl *vd = cast<ValueDecl>(D);
378 QualType QT = vd->getType();
379 if (QT->isAnyPointerType())
380 return true;
381
382 if (const RecordType *RT = QT->getAs<RecordType>()) {
383 // If it's an incomplete type, it could be a smart pointer; skip it.
384 // (We don't want to force template instantiation if we can avoid it,
385 // since that would alter the order in which templates are instantiated.)
386 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000387 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000388
Aaron Ballman553e6812013-12-26 14:54:11 +0000389 if (threadSafetyCheckIsSmartPointer(S, RT))
390 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000391 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000392
393 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000394 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000395 return false;
396}
397
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000398/// \brief Checks that the passed in QualType either is of RecordType or points
399/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000400static const RecordType *getRecordType(QualType QT) {
401 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000402 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000403
404 // Now check if we point to record type.
405 if (const PointerType *PT = QT->getAs<PointerType>())
406 return PT->getPointeeType()->getAs<RecordType>();
407
408 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000409}
410
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000411
Jordy Rose740b0c22012-05-08 03:27:22 +0000412static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
413 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000414 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000415 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000416}
417
418
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000420/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000421static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
422 QualType Ty) {
423 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000424
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000425 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000426 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000427 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000428 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000429 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000430 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000431
Michael Hana9171bc2012-08-03 17:40:43 +0000432 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000433 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435
436 // Allow smart pointers to be used as lockable objects.
437 // FIXME -- Check the type that the smart pointer points to.
438 if (threadSafetyCheckIsSmartPointer(S, RT))
439 return;
440
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000441 // Check if the type is lockable.
442 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000443 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000444 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000445
446 // Else check if any base classes are lockable.
447 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
448 CXXBasePaths BPaths(false, false);
449 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
450 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000451 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452
453 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000454 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000455}
456
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000458/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459/// \param Sidx The attribute argument index to start checking with.
460/// \param ParamIdxOk Whether an argument can be indexing into a function
461/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000462static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 const AttributeList &Attr,
464 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 int Sidx = 0,
466 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000468 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000470 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000471 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000472 Args.push_back(ArgExp);
473 continue;
474 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000476 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000477 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000478 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000479 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000480 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000481 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000482 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000483 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000484
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000485 // We allow constant strings to be used as a placeholder for expressions
486 // that are not valid C++ syntax, but warn that they are ignored.
487 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
488 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000489 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000490 continue;
491 }
492
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000493 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000494
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000495 // A pointer to member expression of the form &MyClass::mu is treated
496 // specially -- we need to look at the type of the member.
497 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
498 if (UOp->getOpcode() == UO_AddrOf)
499 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
500 if (DRE->getDecl()->isCXXInstanceMember())
501 ArgTy = DRE->getDecl()->getType();
502
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000503 // First see if we can just cast to record type, or point to record type.
504 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000506 // Now check if we index into a record type function param.
507 if(!RT && ParamIdxOk) {
508 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
510 if(FD && IL) {
511 unsigned int NumParams = FD->getNumParams();
512 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000513 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
514 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
515 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
517 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000518 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000519 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000520 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 }
522 }
523
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000524 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000526 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528}
529
Chris Lattner58418ff2008-06-29 00:16:31 +0000530//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000531// Attribute Implementations
532//===----------------------------------------------------------------------===//
533
Daniel Dunbar032db472008-07-31 22:40:48 +0000534// FIXME: All this manual attribute parsing code is gross. At the
535// least add some helper functions to check most argument patterns (#
536// and types of args).
537
Michael Hana9171bc2012-08-03 17:40:43 +0000538static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000539 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000540 if (!threadSafetyCheckIsPointer(S, D, Attr))
541 return;
542
Michael Han99315932013-01-24 16:46:58 +0000543 D->addAttr(::new (S.Context)
544 PtGuardedVarAttr(Attr.getRange(), S.Context,
545 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000546}
547
Michael Hana9171bc2012-08-03 17:40:43 +0000548static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
549 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000550 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000551 SmallVector<Expr*, 1> Args;
552 // check that all arguments are lockable objects
553 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
554 unsigned Size = Args.size();
555 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000556 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000557
Michael Han3be3b442012-07-23 18:48:41 +0000558 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000559
Michael Han3be3b442012-07-23 18:48:41 +0000560 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000561}
562
Michael Han3be3b442012-07-23 18:48:41 +0000563static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
564 Expr *Arg = 0;
565 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
566 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000567
Michael Han3be3b442012-07-23 18:48:41 +0000568 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
569}
570
Michael Hana9171bc2012-08-03 17:40:43 +0000571static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000572 const AttributeList &Attr) {
573 Expr *Arg = 0;
574 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
575 return;
576
577 if (!threadSafetyCheckIsPointer(S, D, Attr))
578 return;
579
580 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
581 S.Context, Arg));
582}
583
Michael Hana9171bc2012-08-03 17:40:43 +0000584static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
585 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000586 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000587 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000588 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000589
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000590 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000591 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 if (!QT->isDependentType()) {
593 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000594 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000595 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000596 << Attr.getName();
597 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000598 }
599 }
600
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000601 // Check that all arguments are lockable objects.
602 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000603 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000604 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000605
Michael Han3be3b442012-07-23 18:48:41 +0000606 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000607}
608
Michael Hana9171bc2012-08-03 17:40:43 +0000609static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
611 SmallVector<Expr*, 1> Args;
612 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
613 return;
614
615 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000616 D->addAttr(::new (S.Context)
617 AcquiredAfterAttr(Attr.getRange(), S.Context,
618 StartArg, Args.size(),
619 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000620}
621
Michael Hana9171bc2012-08-03 17:40:43 +0000622static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000623 const AttributeList &Attr) {
624 SmallVector<Expr*, 1> Args;
625 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
626 return;
627
628 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000629 D->addAttr(::new (S.Context)
630 AcquiredBeforeAttr(Attr.getRange(), S.Context,
631 StartArg, Args.size(),
632 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000633}
634
Michael Hana9171bc2012-08-03 17:40:43 +0000635static bool checkLockFunAttrCommon(Sema &S, Decl *D,
636 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000637 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000638 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000639 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000640 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000641
Michael Han3be3b442012-07-23 18:48:41 +0000642 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643}
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000646 const AttributeList &Attr) {
647 SmallVector<Expr*, 1> Args;
648 if (!checkLockFunAttrCommon(S, D, Attr, Args))
649 return;
650
651 unsigned Size = Args.size();
652 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000653 D->addAttr(::new (S.Context)
654 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
655 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000656}
657
Michael Hana9171bc2012-08-03 17:40:43 +0000658static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000659 const AttributeList &Attr) {
660 SmallVector<Expr*, 1> Args;
661 if (!checkLockFunAttrCommon(S, D, Attr, Args))
662 return;
663
664 unsigned Size = Args.size();
665 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000666 D->addAttr(::new (S.Context)
667 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
668 StartArg, Size,
669 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000670}
671
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000672static void handleAssertSharedLockAttr(Sema &S, Decl *D,
673 const AttributeList &Attr) {
674 SmallVector<Expr*, 1> Args;
675 if (!checkLockFunAttrCommon(S, D, Attr, Args))
676 return;
677
678 unsigned Size = Args.size();
679 Expr **StartArg = Size == 0 ? 0 : &Args[0];
680 D->addAttr(::new (S.Context)
681 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
682 Attr.getAttributeSpellingListIndex()));
683}
684
685static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
686 const AttributeList &Attr) {
687 SmallVector<Expr*, 1> Args;
688 if (!checkLockFunAttrCommon(S, D, Attr, Args))
689 return;
690
691 unsigned Size = Args.size();
692 Expr **StartArg = Size == 0 ? 0 : &Args[0];
693 D->addAttr(::new (S.Context)
694 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
695 StartArg, Size,
696 Attr.getAttributeSpellingListIndex()));
697}
698
699
Michael Hana9171bc2012-08-03 17:40:43 +0000700static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
701 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000702 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000703 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000705
Aaron Ballman00e99962013-08-31 01:11:41 +0000706 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000708 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000709 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000710 }
711
712 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000713 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000714
Michael Han3be3b442012-07-23 18:48:41 +0000715 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000716}
717
Michael Hana9171bc2012-08-03 17:40:43 +0000718static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000719 const AttributeList &Attr) {
720 SmallVector<Expr*, 2> Args;
721 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
722 return;
723
Michael Han99315932013-01-24 16:46:58 +0000724 D->addAttr(::new (S.Context)
725 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000726 Attr.getArgAsExpr(0),
727 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000728 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000729}
730
Michael Hana9171bc2012-08-03 17:40:43 +0000731static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 2> Args;
734 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
735 return;
736
Michael Han99315932013-01-24 16:46:58 +0000737 D->addAttr(::new (S.Context)
738 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000739 Attr.getArgAsExpr(0),
740 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000741 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000742}
743
Michael Hana9171bc2012-08-03 17:40:43 +0000744static bool checkLocksRequiredCommon(Sema &S, Decl *D,
745 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000746 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000747 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000750 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000751 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000752 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000753 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000754
Michael Han3be3b442012-07-23 18:48:41 +0000755 return true;
756}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000757
Michael Hana9171bc2012-08-03 17:40:43 +0000758static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000759 const AttributeList &Attr) {
760 SmallVector<Expr*, 1> Args;
761 if (!checkLocksRequiredCommon(S, D, Attr, Args))
762 return;
763
764 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000765 D->addAttr(::new (S.Context)
766 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
767 StartArg, Args.size(),
768 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000769}
770
Michael Hana9171bc2012-08-03 17:40:43 +0000771static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000772 const AttributeList &Attr) {
773 SmallVector<Expr*, 1> Args;
774 if (!checkLocksRequiredCommon(S, D, Attr, Args))
775 return;
776
777 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000778 D->addAttr(::new (S.Context)
779 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
780 StartArg, Args.size(),
781 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000782}
783
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000785 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000788 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000789 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000790 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000791 Expr **StartArg = Size == 0 ? 0 : &Args[0];
792
Michael Han99315932013-01-24 16:46:58 +0000793 D->addAttr(::new (S.Context)
794 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
795 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000796}
797
798static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000799 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000800 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000801 SmallVector<Expr*, 1> Args;
802 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
803 unsigned Size = Args.size();
804 if (Size == 0)
805 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000806
Michael Han99315932013-01-24 16:46:58 +0000807 D->addAttr(::new (S.Context)
808 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
809 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000810}
811
812static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000813 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000814 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000815 return;
816
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000817 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000818 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000819 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000820 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000821 if (Size == 0)
822 return;
823 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000824
Michael Han99315932013-01-24 16:46:58 +0000825 D->addAttr(::new (S.Context)
826 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
827 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000828}
829
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000830static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
831 Expr *Cond = Attr.getArgAsExpr(0);
832 if (!Cond->isTypeDependent()) {
833 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
834 if (Converted.isInvalid())
835 return;
836 Cond = Converted.take();
837 }
838
839 StringRef Msg;
840 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
841 return;
842
843 SmallVector<PartialDiagnosticAt, 8> Diags;
844 if (!Cond->isValueDependent() &&
845 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
846 Diags)) {
847 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
848 for (int I = 0, N = Diags.size(); I != N; ++I)
849 S.Diag(Diags[I].first, Diags[I].second);
850 return;
851 }
852
853 D->addAttr(::new (S.Context)
854 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
855 Attr.getAttributeSpellingListIndex()));
856}
857
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000858static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000859 ConsumableAttr::ConsumedState DefaultState;
860
861 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000862 IdentifierLoc *IL = Attr.getArgAsIdent(0);
863 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
864 DefaultState)) {
865 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
866 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000867 return;
868 }
David Blaikie16f76d22013-09-06 01:28:43 +0000869 } else {
870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
871 << Attr.getName() << AANT_ArgumentIdentifier;
872 return;
873 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000874
875 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000876 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000877 Attr.getAttributeSpellingListIndex()));
878}
879
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000880
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000881static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
882 const AttributeList &Attr) {
883 ASTContext &CurrContext = S.getASTContext();
884 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
885
886 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
887 if (!RD->hasAttr<ConsumableAttr>()) {
888 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
889 RD->getNameAsString();
890
891 return false;
892 }
893 }
894
895 return true;
896}
897
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000898
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000899static void handleCallableWhenAttr(Sema &S, Decl *D,
900 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000901 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
902 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000903
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000904 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
905 return;
906
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000907 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
908 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
909 CallableWhenAttr::ConsumedState CallableState;
910
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000911 StringRef StateString;
912 SourceLocation Loc;
913 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
914 return;
915
916 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000917 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000918 S.Diag(Loc, diag::warn_attribute_type_not_supported)
919 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000920 return;
921 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000922
923 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000924 }
925
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000926 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000927 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
928 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000929}
930
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000931
DeLesley Hutchins69391772013-10-17 23:23:53 +0000932static void handleParamTypestateAttr(Sema &S, Decl *D,
933 const AttributeList &Attr) {
934 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000935
DeLesley Hutchins69391772013-10-17 23:23:53 +0000936 ParamTypestateAttr::ConsumedState ParamState;
937
938 if (Attr.isArgIdent(0)) {
939 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
940 StringRef StateString = Ident->Ident->getName();
941
942 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
943 ParamState)) {
944 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
945 << Attr.getName() << StateString;
946 return;
947 }
948 } else {
949 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
950 Attr.getName() << AANT_ArgumentIdentifier;
951 return;
952 }
953
954 // FIXME: This check is currently being done in the analysis. It can be
955 // enabled here only after the parser propagates attributes at
956 // template specialization definition, not declaration.
957 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
958 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
959 //
960 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
961 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
962 // ReturnType.getAsString();
963 // return;
964 //}
965
966 D->addAttr(::new (S.Context)
967 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
968 Attr.getAttributeSpellingListIndex()));
969}
970
971
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000972static void handleReturnTypestateAttr(Sema &S, Decl *D,
973 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000974 if (!checkAttributeNumArgs(S, Attr, 1)) return;
975
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000976 ReturnTypestateAttr::ConsumedState ReturnState;
977
978 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000979 IdentifierLoc *IL = Attr.getArgAsIdent(0);
980 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
981 ReturnState)) {
982 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
983 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000984 return;
985 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000986 } else {
987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
988 Attr.getName() << AANT_ArgumentIdentifier;
989 return;
990 }
991
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000992 // FIXME: This check is currently being done in the analysis. It can be
993 // enabled here only after the parser propagates attributes at
994 // template specialization definition, not declaration.
995 //QualType ReturnType;
996 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000997 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
998 // ReturnType = Param->getType();
999 //
1000 //} else if (const CXXConstructorDecl *Constructor =
1001 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001002 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1003 //
1004 //} else {
1005 //
1006 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1007 //}
1008 //
1009 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1010 //
1011 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1012 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1013 // ReturnType.getAsString();
1014 // return;
1015 //}
1016
1017 D->addAttr(::new (S.Context)
1018 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1019 Attr.getAttributeSpellingListIndex()));
1020}
1021
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001022
1023static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001024 if (!checkAttributeNumArgs(S, Attr, 1))
1025 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001026
1027 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1028 return;
1029
1030 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001031 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001032 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1033 StringRef Param = Ident->Ident->getName();
1034 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1035 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1036 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001037 return;
1038 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001039 } else {
1040 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1041 Attr.getName() << AANT_ArgumentIdentifier;
1042 return;
1043 }
1044
1045 D->addAttr(::new (S.Context)
1046 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1047 Attr.getAttributeSpellingListIndex()));
1048}
1049
Chris Wailes9385f9f2013-10-29 20:28:41 +00001050static void handleTestTypestateAttr(Sema &S, Decl *D,
1051 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001052 if (!checkAttributeNumArgs(S, Attr, 1))
1053 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001054
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001055 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1056 return;
1057
Chris Wailes9385f9f2013-10-29 20:28:41 +00001058 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001059 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001060 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1061 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001062 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001063 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1064 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001065 return;
1066 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001067 } else {
1068 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1069 Attr.getName() << AANT_ArgumentIdentifier;
1070 return;
1071 }
1072
1073 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001074 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001075 Attr.getAttributeSpellingListIndex()));
1076}
1077
Chandler Carruthedc2c642011-07-02 00:01:44 +00001078static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1079 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001080 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001081 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001082}
1083
Chandler Carruthedc2c642011-07-02 00:01:44 +00001084static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001085 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001086 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001087 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001088 // If the alignment is less than or equal to 8 bits, the packed attribute
1089 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001090 if (!FD->getType()->isDependentType() &&
1091 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001092 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001093 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001094 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001095 else
Michael Han99315932013-01-24 16:46:58 +00001096 FD->addAttr(::new (S.Context)
1097 PackedAttr(Attr.getRange(), S.Context,
1098 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001099 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001100 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001101}
1102
Ted Kremenek7fd17232011-09-29 07:02:25 +00001103static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1104 // The IBOutlet/IBOutletCollection attributes only apply to instance
1105 // variables or properties of Objective-C classes. The outlet must also
1106 // have an object reference type.
1107 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1108 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001109 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001110 << Attr.getName() << VD->getType() << 0;
1111 return false;
1112 }
1113 }
1114 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1115 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001116 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001117 << Attr.getName() << PD->getType() << 1;
1118 return false;
1119 }
1120 }
1121 else {
1122 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1123 return false;
1124 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001125
Ted Kremenek7fd17232011-09-29 07:02:25 +00001126 return true;
1127}
1128
Chandler Carruthedc2c642011-07-02 00:01:44 +00001129static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001130 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001131 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001132
Michael Han99315932013-01-24 16:46:58 +00001133 D->addAttr(::new (S.Context)
1134 IBOutletAttr(Attr.getRange(), S.Context,
1135 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001136}
1137
Chandler Carruthedc2c642011-07-02 00:01:44 +00001138static void handleIBOutletCollection(Sema &S, Decl *D,
1139 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001140
1141 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001142 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001143 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1144 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001145 return;
1146 }
1147
Ted Kremenek7fd17232011-09-29 07:02:25 +00001148 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001149 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001150
Richard Smithb1f9a282013-10-31 01:56:18 +00001151 ParsedType PT;
1152
1153 if (Attr.hasParsedType())
1154 PT = Attr.getTypeArg();
1155 else {
1156 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1157 S.getScopeForContext(D->getDeclContext()->getParent()));
1158 if (!PT) {
1159 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1160 return;
1161 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001162 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001163
Richard Smithb87c4652013-10-31 21:23:20 +00001164 TypeSourceInfo *QTLoc = 0;
1165 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1166 if (!QTLoc)
1167 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001168
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001169 // Diagnose use of non-object type in iboutletcollection attribute.
1170 // FIXME. Gnu attribute extension ignores use of builtin types in
1171 // attributes. So, __attribute__((iboutletcollection(char))) will be
1172 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001173 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001174 S.Diag(Attr.getLoc(),
1175 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1176 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001177 return;
1178 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001179
Michael Han99315932013-01-24 16:46:58 +00001180 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001181 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001182 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001183}
1184
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001185static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001186 if (const RecordType *UT = T->getAsUnionType())
1187 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1188 RecordDecl *UD = UT->getDecl();
1189 for (RecordDecl::field_iterator it = UD->field_begin(),
1190 itend = UD->field_end(); it != itend; ++it) {
1191 QualType QT = it->getType();
1192 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1193 T = QT;
1194 return;
1195 }
1196 }
1197 }
1198}
1199
Chandler Carruthedc2c642011-07-02 00:01:44 +00001200static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001201 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1202 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001203 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001204 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001205 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001206 return;
1207 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001208
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001209 SmallVector<unsigned, 8> NonNullArgs;
1210 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001211 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001212 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001213 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001214 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001215
1216 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001217 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001218 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001219
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001220 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001221 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001222 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1223 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001224 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001225 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001226
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001227 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001229
1230 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1231 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001232 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001233 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1234 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001235 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001236 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001237 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001238 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001239
Ted Kremenek22813f42010-10-21 18:49:36 +00001240 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001241 if (NonNullArgs.empty()) {
1242 // Warn the trivial case only if attribute is not coming from a
1243 // macro instantiation.
1244 if (Attr.getLoc().isFileID())
1245 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001246 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001247 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001248 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001249
Nick Lewyckye1121512013-01-24 01:12:16 +00001250 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001251 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001252 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001253 D->addAttr(::new (S.Context)
1254 NonNullAttr(Attr.getRange(), S.Context, start, size,
1255 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001256}
1257
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001258static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1259 switch (K) {
1260 case OwnershipAttr::Holds: return "'ownership_holds'";
1261 case OwnershipAttr::Takes: return "'ownership_takes'";
1262 case OwnershipAttr::Returns: return "'ownership_returns'";
1263 }
1264 llvm_unreachable("unknown ownership");
1265}
1266
Chandler Carruthedc2c642011-07-02 00:01:44 +00001267static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001268 // This attribute must be applied to a function declaration. The first
1269 // argument to the attribute must be an identifier, the name of the resource,
1270 // for example: malloc. The following arguments must be argument indexes, the
1271 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001272 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001273 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001274 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001275
Aaron Ballman00e99962013-08-31 01:11:41 +00001276 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001277 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001278 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001279 return;
1280 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001281
Richard Smith852e9ce2013-11-27 01:46:48 +00001282 // Figure out our Kind.
1283 OwnershipAttr::OwnershipKind K =
1284 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1285 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001286
Richard Smith852e9ce2013-11-27 01:46:48 +00001287 // Check arguments.
1288 switch (K) {
1289 case OwnershipAttr::Takes:
1290 case OwnershipAttr::Holds:
1291 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001292 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1293 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001294 return;
1295 }
1296 break;
1297 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001298 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001299 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1300 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001301 return;
1302 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001303 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001304 }
1305
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001306 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001307 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1308 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001309 return;
1310 }
1311
Richard Smith852e9ce2013-11-27 01:46:48 +00001312 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001313
1314 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001315 StringRef ModuleName = Module->getName();
1316 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1317 ModuleName.size() > 4) {
1318 ModuleName = ModuleName.drop_front(2).drop_back(2);
1319 Module = &S.PP.getIdentifierTable().get(ModuleName);
1320 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001322 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001323 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1324 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001325 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001326 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001327 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001328
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001329 // Is the function argument a pointer type?
1330 QualType T = getFunctionOrMethodArgType(D, Idx);
1331 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001332 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001333 case OwnershipAttr::Takes:
1334 case OwnershipAttr::Holds:
1335 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1336 Err = 0;
1337 break;
1338 case OwnershipAttr::Returns:
1339 if (!T->isIntegerType())
1340 Err = 1;
1341 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001342 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001343 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001344 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001345 << Ex->getSourceRange();
1346 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001347 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001348
1349 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001350 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001351 i = D->specific_attr_begin<OwnershipAttr>(),
1352 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001353 // FIXME: A returns attribute should conflict with any returns attribute
1354 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001355 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1356 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1357 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1358 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1359 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001360 }
1361 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001362 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363 }
1364
1365 unsigned* start = OwnershipArgs.data();
1366 unsigned size = OwnershipArgs.size();
1367 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001368
Michael Han99315932013-01-24 16:46:58 +00001369 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001370 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001371 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001372}
1373
Chandler Carruthedc2c642011-07-02 00:01:44 +00001374static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001375 // Check the attribute arguments.
1376 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1378 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001379 return;
1380 }
1381
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001382 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001383
Rafael Espindolac18086a2010-02-23 22:00:30 +00001384 // gcc rejects
1385 // class c {
1386 // static int a __attribute__((weakref ("v2")));
1387 // static int b() __attribute__((weakref ("f3")));
1388 // };
1389 // and ignores the attributes of
1390 // void f(void) {
1391 // static int a __attribute__((weakref ("v2")));
1392 // }
1393 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001394 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001395 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001396 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1397 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001398 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001399 }
1400
1401 // The GCC manual says
1402 //
1403 // At present, a declaration to which `weakref' is attached can only
1404 // be `static'.
1405 //
1406 // It also says
1407 //
1408 // Without a TARGET,
1409 // given as an argument to `weakref' or to `alias', `weakref' is
1410 // equivalent to `weak'.
1411 //
1412 // gcc 4.4.1 will accept
1413 // int a7 __attribute__((weakref));
1414 // as
1415 // int a7 __attribute__((weak));
1416 // This looks like a bug in gcc. We reject that for now. We should revisit
1417 // it if this behaviour is actually used.
1418
Rafael Espindolac18086a2010-02-23 22:00:30 +00001419 // GCC rejects
1420 // static ((alias ("y"), weakref)).
1421 // Should we? How to check that weakref is before or after alias?
1422
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001423 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1424 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1425 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001426 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001427 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001428 // GCC will accept anything as the argument of weakref. Should we
1429 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001430 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1431 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001432
Michael Han99315932013-01-24 16:46:58 +00001433 D->addAttr(::new (S.Context)
1434 WeakRefAttr(Attr.getRange(), S.Context,
1435 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001436}
1437
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001438static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1439 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001440 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001441 return;
1442
Douglas Gregore8bbc122011-09-02 00:18:52 +00001443 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001444 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1445 return;
1446 }
1447
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001448 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001449
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001450 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001451 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001452}
1453
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001454static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001455 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001456 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001457
Michael Han99315932013-01-24 16:46:58 +00001458 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1459 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001460}
1461
1462static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001463 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001464 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001465
Michael Han99315932013-01-24 16:46:58 +00001466 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1467 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001468}
1469
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001470static void handleTLSModelAttr(Sema &S, Decl *D,
1471 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001472 StringRef Model;
1473 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001474 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001475 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001476 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001477
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001478 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001479 if (Model != "global-dynamic" && Model != "local-dynamic"
1480 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001481 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001482 return;
1483 }
1484
Michael Han99315932013-01-24 16:46:58 +00001485 D->addAttr(::new (S.Context)
1486 TLSModelAttr(Attr.getRange(), S.Context, Model,
1487 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001488}
1489
Chandler Carruthedc2c642011-07-02 00:01:44 +00001490static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001491 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001492 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001493 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001494 D->addAttr(::new (S.Context)
1495 MallocAttr(Attr.getRange(), S.Context,
1496 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001497 return;
1498 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001499 }
1500
Ted Kremenek08479ae2009-08-15 00:51:46 +00001501 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001502}
1503
Chandler Carruthedc2c642011-07-02 00:01:44 +00001504static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001505 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001506 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1507 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001508 return;
1509 }
1510
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001511 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1512 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001513}
1514
Chandler Carruthedc2c642011-07-02 00:01:44 +00001515static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001516 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001517
1518 if (S.CheckNoReturnAttr(attr)) return;
1519
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001520 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001521 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001522 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001523 return;
1524 }
1525
Michael Han99315932013-01-24 16:46:58 +00001526 D->addAttr(::new (S.Context)
1527 NoReturnAttr(attr.getRange(), S.Context,
1528 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001529}
1530
1531bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001532 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001533 attr.setInvalid();
1534 return true;
1535 }
1536
1537 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001538}
1539
Chandler Carruthedc2c642011-07-02 00:01:44 +00001540static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1541 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001542
1543 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1544 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001545 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1546 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001547 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1548 && !VD->getType()->isFunctionPointerType())) {
1549 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001550 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001551 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001552 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001553 return;
1554 }
1555 }
1556
Michael Han99315932013-01-24 16:46:58 +00001557 D->addAttr(::new (S.Context)
1558 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1559 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001560}
1561
John Thompsoncdb847ba2010-08-09 21:53:52 +00001562// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001563static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001564/*
1565 Returning a Vector Class in Registers
1566
Eric Christopherbc638a82010-12-01 22:13:54 +00001567 According to the PPU ABI specifications, a class with a single member of
1568 vector type is returned in memory when used as the return value of a function.
1569 This results in inefficient code when implementing vector classes. To return
1570 the value in a single vector register, add the vecreturn attribute to the
1571 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001572
1573 Example:
1574
1575 struct Vector
1576 {
1577 __vector float xyzw;
1578 } __attribute__((vecreturn));
1579
1580 Vector Add(Vector lhs, Vector rhs)
1581 {
1582 Vector result;
1583 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1584 return result; // This will be returned in a register
1585 }
1586*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001587 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1588 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001589 return;
1590 }
1591
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001592 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001593 int count = 0;
1594
1595 if (!isa<CXXRecordDecl>(record)) {
1596 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1597 return;
1598 }
1599
1600 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1602 return;
1603 }
1604
Eric Christopherbc638a82010-12-01 22:13:54 +00001605 for (RecordDecl::field_iterator iter = record->field_begin();
1606 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001607 if ((count == 1) || !iter->getType()->isVectorType()) {
1608 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1609 return;
1610 }
1611 count++;
1612 }
1613
Michael Han99315932013-01-24 16:46:58 +00001614 D->addAttr(::new (S.Context)
1615 VecReturnAttr(Attr.getRange(), S.Context,
1616 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001617}
1618
Richard Smithe233fbf2013-01-28 22:42:45 +00001619static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1620 const AttributeList &Attr) {
1621 if (isa<ParmVarDecl>(D)) {
1622 // [[carries_dependency]] can only be applied to a parameter if it is a
1623 // parameter of a function declaration or lambda.
1624 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1625 S.Diag(Attr.getLoc(),
1626 diag::err_carries_dependency_param_not_function_decl);
1627 return;
1628 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001629 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001630
1631 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1632 Attr.getRange(), S.Context,
1633 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001634}
1635
Chandler Carruthedc2c642011-07-02 00:01:44 +00001636static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001637 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001638 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001639 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001640 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001641 return;
1642 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001643
Michael Han99315932013-01-24 16:46:58 +00001644 D->addAttr(::new (S.Context)
1645 UnusedAttr(Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001647}
1648
Chandler Carruthedc2c642011-07-02 00:01:44 +00001649static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001650 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001651 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001652 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001653 return;
1654 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001655 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001656 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001657 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001658 return;
1659 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001660
Michael Han99315932013-01-24 16:46:58 +00001661 D->addAttr(::new (S.Context)
1662 UsedAttr(Attr.getRange(), S.Context,
1663 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001664}
1665
Chandler Carruthedc2c642011-07-02 00:01:44 +00001666static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001667 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001668 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001669 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1670 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001671 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001672 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001673
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001674 uint32_t priority = ConstructorAttr::DefaultPriority;
1675 if (Attr.getNumArgs() > 0 &&
1676 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1677 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001678
Michael Han99315932013-01-24 16:46:58 +00001679 D->addAttr(::new (S.Context)
1680 ConstructorAttr(Attr.getRange(), S.Context, priority,
1681 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001682}
1683
Chandler Carruthedc2c642011-07-02 00:01:44 +00001684static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001685 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001686 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001687 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1688 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001689 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001690 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001691
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001692 uint32_t priority = ConstructorAttr::DefaultPriority;
1693 if (Attr.getNumArgs() > 0 &&
1694 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1695 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001696
Michael Han99315932013-01-24 16:46:58 +00001697 D->addAttr(::new (S.Context)
1698 DestructorAttr(Attr.getRange(), S.Context, priority,
1699 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001700}
1701
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001702template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001703static void handleAttrWithMessage(Sema &S, Decl *D,
1704 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001705 unsigned NumArgs = Attr.getNumArgs();
1706 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001707 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1708 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001709 return;
1710 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001711
1712 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001713 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001714 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001715 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001716
Michael Han99315932013-01-24 16:46:58 +00001717 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1718 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001719}
1720
Ted Kremenek28eace62013-11-23 01:01:34 +00001721static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1722 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001723 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001724 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1725 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001726}
1727
Jordy Rose740b0c22012-05-08 03:27:22 +00001728static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1729 IdentifierInfo *Platform,
1730 VersionTuple Introduced,
1731 VersionTuple Deprecated,
1732 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001733 StringRef PlatformName
1734 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1735 if (PlatformName.empty())
1736 PlatformName = Platform->getName();
1737
1738 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1739 // of these steps are needed).
1740 if (!Introduced.empty() && !Deprecated.empty() &&
1741 !(Introduced <= Deprecated)) {
1742 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1743 << 1 << PlatformName << Deprecated.getAsString()
1744 << 0 << Introduced.getAsString();
1745 return true;
1746 }
1747
1748 if (!Introduced.empty() && !Obsoleted.empty() &&
1749 !(Introduced <= Obsoleted)) {
1750 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1751 << 2 << PlatformName << Obsoleted.getAsString()
1752 << 0 << Introduced.getAsString();
1753 return true;
1754 }
1755
1756 if (!Deprecated.empty() && !Obsoleted.empty() &&
1757 !(Deprecated <= Obsoleted)) {
1758 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1759 << 2 << PlatformName << Obsoleted.getAsString()
1760 << 1 << Deprecated.getAsString();
1761 return true;
1762 }
1763
1764 return false;
1765}
1766
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001767/// \brief Check whether the two versions match.
1768///
1769/// If either version tuple is empty, then they are assumed to match. If
1770/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1771static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1772 bool BeforeIsOkay) {
1773 if (X.empty() || Y.empty())
1774 return true;
1775
1776 if (X == Y)
1777 return true;
1778
1779 if (BeforeIsOkay && X < Y)
1780 return true;
1781
1782 return false;
1783}
1784
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001785AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001786 IdentifierInfo *Platform,
1787 VersionTuple Introduced,
1788 VersionTuple Deprecated,
1789 VersionTuple Obsoleted,
1790 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001791 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001792 bool Override,
1793 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001794 VersionTuple MergedIntroduced = Introduced;
1795 VersionTuple MergedDeprecated = Deprecated;
1796 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001797 bool FoundAny = false;
1798
Rafael Espindolac67f2232012-05-10 02:50:16 +00001799 if (D->hasAttrs()) {
1800 AttrVec &Attrs = D->getAttrs();
1801 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1802 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1803 if (!OldAA) {
1804 ++i;
1805 continue;
1806 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001807
Rafael Espindolac67f2232012-05-10 02:50:16 +00001808 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1809 if (OldPlatform != Platform) {
1810 ++i;
1811 continue;
1812 }
1813
1814 FoundAny = true;
1815 VersionTuple OldIntroduced = OldAA->getIntroduced();
1816 VersionTuple OldDeprecated = OldAA->getDeprecated();
1817 VersionTuple OldObsoleted = OldAA->getObsoleted();
1818 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001819
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001820 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1821 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1822 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1823 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001824 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001825 if (Override) {
1826 int Which = -1;
1827 VersionTuple FirstVersion;
1828 VersionTuple SecondVersion;
1829 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1830 Which = 0;
1831 FirstVersion = OldIntroduced;
1832 SecondVersion = Introduced;
1833 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1834 Which = 1;
1835 FirstVersion = Deprecated;
1836 SecondVersion = OldDeprecated;
1837 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1838 Which = 2;
1839 FirstVersion = Obsoleted;
1840 SecondVersion = OldObsoleted;
1841 }
1842
1843 if (Which == -1) {
1844 Diag(OldAA->getLocation(),
1845 diag::warn_mismatched_availability_override_unavail)
1846 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1847 } else {
1848 Diag(OldAA->getLocation(),
1849 diag::warn_mismatched_availability_override)
1850 << Which
1851 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1852 << FirstVersion.getAsString() << SecondVersion.getAsString();
1853 }
1854 Diag(Range.getBegin(), diag::note_overridden_method);
1855 } else {
1856 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1857 Diag(Range.getBegin(), diag::note_previous_attribute);
1858 }
1859
Rafael Espindolac67f2232012-05-10 02:50:16 +00001860 Attrs.erase(Attrs.begin() + i);
1861 --e;
1862 continue;
1863 }
1864
1865 VersionTuple MergedIntroduced2 = MergedIntroduced;
1866 VersionTuple MergedDeprecated2 = MergedDeprecated;
1867 VersionTuple MergedObsoleted2 = MergedObsoleted;
1868
1869 if (MergedIntroduced2.empty())
1870 MergedIntroduced2 = OldIntroduced;
1871 if (MergedDeprecated2.empty())
1872 MergedDeprecated2 = OldDeprecated;
1873 if (MergedObsoleted2.empty())
1874 MergedObsoleted2 = OldObsoleted;
1875
1876 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1877 MergedIntroduced2, MergedDeprecated2,
1878 MergedObsoleted2)) {
1879 Attrs.erase(Attrs.begin() + i);
1880 --e;
1881 continue;
1882 }
1883
1884 MergedIntroduced = MergedIntroduced2;
1885 MergedDeprecated = MergedDeprecated2;
1886 MergedObsoleted = MergedObsoleted2;
1887 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001888 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001889 }
1890
1891 if (FoundAny &&
1892 MergedIntroduced == Introduced &&
1893 MergedDeprecated == Deprecated &&
1894 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001895 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001896
Ted Kremenekb5445722013-04-06 00:34:27 +00001897 // Only create a new attribute if !Override, but we want to do
1898 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001899 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001900 MergedDeprecated, MergedObsoleted) &&
1901 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001902 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1903 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001904 Obsoleted, IsUnavailable, Message,
1905 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001906 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001907 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001908}
1909
Chandler Carruthedc2c642011-07-02 00:01:44 +00001910static void handleAvailabilityAttr(Sema &S, Decl *D,
1911 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001912 if (!checkAttributeNumArgs(S, Attr, 1))
1913 return;
1914 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001915 unsigned Index = Attr.getAttributeSpellingListIndex();
1916
Aaron Ballman00e99962013-08-31 01:11:41 +00001917 IdentifierInfo *II = Platform->Ident;
1918 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1919 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1920 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001921
Rafael Espindolac231fab2013-01-08 21:30:32 +00001922 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1923 if (!ND) {
1924 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1925 return;
1926 }
1927
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001928 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1929 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1930 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001931 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001932 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001933 if (const StringLiteral *SE =
1934 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001935 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001936
Aaron Ballman00e99962013-08-31 01:11:41 +00001937 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001938 Introduced.Version,
1939 Deprecated.Version,
1940 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001941 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001942 /*Override=*/false,
1943 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001944 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001945 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001946}
1947
John McCalld041a9b2013-02-20 01:54:26 +00001948template <class T>
1949static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1950 typename T::VisibilityType value,
1951 unsigned attrSpellingListIndex) {
1952 T *existingAttr = D->getAttr<T>();
1953 if (existingAttr) {
1954 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1955 if (existingValue == value)
1956 return NULL;
1957 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1958 S.Diag(range.getBegin(), diag::note_previous_attribute);
1959 D->dropAttr<T>();
1960 }
1961 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1962}
1963
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001964VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001965 VisibilityAttr::VisibilityType Vis,
1966 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001967 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1968 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001969}
1970
John McCalld041a9b2013-02-20 01:54:26 +00001971TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1972 TypeVisibilityAttr::VisibilityType Vis,
1973 unsigned AttrSpellingListIndex) {
1974 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1975 AttrSpellingListIndex);
1976}
1977
1978static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1979 bool isTypeVisibility) {
1980 // Visibility attributes don't mean anything on a typedef.
1981 if (isa<TypedefNameDecl>(D)) {
1982 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1983 << Attr.getName();
1984 return;
1985 }
1986
1987 // 'type_visibility' can only go on a type or namespace.
1988 if (isTypeVisibility &&
1989 !(isa<TagDecl>(D) ||
1990 isa<ObjCInterfaceDecl>(D) ||
1991 isa<NamespaceDecl>(D))) {
1992 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1993 << Attr.getName() << ExpectedTypeOrNamespace;
1994 return;
1995 }
1996
Benjamin Kramer70370212013-09-09 15:08:57 +00001997 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001998 StringRef TypeStr;
1999 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002000 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002001 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002002
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002003 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002004 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002005 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002006 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002007 return;
2008 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002009
2010 // Complain about attempts to use protected visibility on targets
2011 // (like Darwin) that don't support it.
2012 if (type == VisibilityAttr::Protected &&
2013 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2014 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2015 type = VisibilityAttr::Default;
2016 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002017
Michael Han99315932013-01-24 16:46:58 +00002018 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002019 clang::Attr *newAttr;
2020 if (isTypeVisibility) {
2021 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2022 (TypeVisibilityAttr::VisibilityType) type,
2023 Index);
2024 } else {
2025 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2026 }
2027 if (newAttr)
2028 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002029}
2030
Chandler Carruthedc2c642011-07-02 00:01:44 +00002031static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2032 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002033 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002034 if (!Attr.isArgIdent(0)) {
2035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2036 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002037 return;
2038 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002039
Aaron Ballman682ee422013-09-11 19:47:58 +00002040 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2041 ObjCMethodFamilyAttr::FamilyKind F;
2042 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2043 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2044 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002045 return;
2046 }
2047
Aaron Ballman682ee422013-09-11 19:47:58 +00002048 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002049 !method->getResultType()->isObjCObjectPointerType()) {
2050 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2051 << method->getResultType();
2052 // Ignore the attribute.
2053 return;
2054 }
2055
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002056 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002057 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002058}
2059
Chandler Carruthedc2c642011-07-02 00:01:44 +00002060static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002061 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002062 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002063 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002064 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2065 return;
2066 }
2067 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002068 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2069 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002070 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002071 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2072 return;
2073 }
2074 }
2075 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002076 // It is okay to include this attribute on properties, e.g.:
2077 //
2078 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2079 //
2080 // In this case it follows tradition and suppresses an error in the above
2081 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002082 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002083 }
Michael Han99315932013-01-24 16:46:58 +00002084 D->addAttr(::new (S.Context)
2085 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2086 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002087}
2088
Chandler Carruthedc2c642011-07-02 00:01:44 +00002089static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002090 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002091 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002092 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002093 return;
2094 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002095
Aaron Ballman00e99962013-08-31 01:11:41 +00002096 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002097 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002098 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2099 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2100 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002101 return;
2102 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002103
Michael Han99315932013-01-24 16:46:58 +00002104 D->addAttr(::new (S.Context)
2105 BlocksAttr(Attr.getRange(), S.Context, type,
2106 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002107}
2108
Chandler Carruthedc2c642011-07-02 00:01:44 +00002109static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002110 // check the attribute arguments.
2111 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002112 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2113 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002114 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002115 }
2116
Aaron Ballman18a78382013-11-21 00:28:23 +00002117 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002118 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002119 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002120 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002121 if (E->isTypeDependent() || E->isValueDependent() ||
2122 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002124 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002125 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002126 return;
2127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002128
John McCallb46f2872011-09-09 07:56:05 +00002129 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002130 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2131 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 return;
2133 }
John McCallb46f2872011-09-09 07:56:05 +00002134
2135 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 }
2137
Aaron Ballman18a78382013-11-21 00:28:23 +00002138 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002139 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002140 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002141 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002142 if (E->isTypeDependent() || E->isValueDependent() ||
2143 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002144 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002145 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002146 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002147 return;
2148 }
2149 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002150
John McCallb46f2872011-09-09 07:56:05 +00002151 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002152 // FIXME: This error message could be improved, it would be nice
2153 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002154 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2155 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002156 return;
2157 }
2158 }
2159
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002160 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002161 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002162 if (isa<FunctionNoProtoType>(FT)) {
2163 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2164 return;
2165 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002166
Chris Lattner9363e312009-03-17 23:03:47 +00002167 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002168 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002169 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002170 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002171 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002172 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002173 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002174 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002175 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002176 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2177 if (!BD->isVariadic()) {
2178 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2179 return;
2180 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002181 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002182 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002183 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002184 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002185 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002186 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002187 int m = Ty->isFunctionPointerType() ? 0 : 1;
2188 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002189 return;
2190 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002191 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002193 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002194 return;
2195 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002196 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002197 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002198 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002199 return;
2200 }
Michael Han99315932013-01-24 16:46:58 +00002201 D->addAttr(::new (S.Context)
2202 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2203 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002204}
2205
Chandler Carruthedc2c642011-07-02 00:01:44 +00002206static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002207 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002208 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002209 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002210 return;
2211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002212
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002213 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2214 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2215 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002216 return;
2217 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002218 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2219 if (MD->getResultType()->isVoidType()) {
2220 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2221 << Attr.getName() << 1;
2222 return;
2223 }
2224
Michael Han99315932013-01-24 16:46:58 +00002225 D->addAttr(::new (S.Context)
2226 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2227 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002228}
2229
Chandler Carruthedc2c642011-07-02 00:01:44 +00002230static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002231 // weak_import only applies to variable & function declarations.
2232 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002233 if (!D->canBeWeakImported(isDef)) {
2234 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002235 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2236 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002237 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002238 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002239 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002240 // Nothing to warn about here.
2241 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002242 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002243 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002244
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002245 return;
2246 }
2247
Michael Han99315932013-01-24 16:46:58 +00002248 D->addAttr(::new (S.Context)
2249 WeakImportAttr(Attr.getRange(), S.Context,
2250 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002251}
2252
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002253// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002254template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002255static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002256 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002257 uint32_t WGSize[3];
2258 for (unsigned i = 0; i < 3; ++i)
2259 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002260 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002261
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002262 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2263 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2264 Existing->getYDim() == WGSize[1] &&
2265 Existing->getZDim() == WGSize[2]))
2266 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002267
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002268 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2269 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002270 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002271}
2272
Joey Goulyaba589c2013-03-08 09:42:32 +00002273static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002274 if (!Attr.hasParsedType()) {
2275 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2276 << Attr.getName() << 1;
2277 return;
2278 }
2279
Richard Smithb87c4652013-10-31 21:23:20 +00002280 TypeSourceInfo *ParmTSI = 0;
2281 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2282 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002283
2284 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2285 (ParmType->isBooleanType() ||
2286 !ParmType->isIntegralType(S.getASTContext()))) {
2287 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2288 << ParmType;
2289 return;
2290 }
2291
Aaron Ballmana9e05402013-12-02 22:16:55 +00002292 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002293 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002294 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2295 return;
2296 }
2297 }
2298
2299 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002300 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002301}
2302
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002303SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002304 StringRef Name,
2305 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002306 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2307 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002308 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002309 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2310 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002311 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002312 }
Michael Han99315932013-01-24 16:46:58 +00002313 return ::new (Context) SectionAttr(Range, Context, Name,
2314 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002315}
2316
Chandler Carruthedc2c642011-07-02 00:01:44 +00002317static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002318 // Make sure that there is a string literal as the sections's single
2319 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002320 StringRef Str;
2321 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002322 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002323 return;
Mike Stump11289f42009-09-09 15:08:12 +00002324
Chris Lattner30ba6742009-08-10 19:03:04 +00002325 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002326 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002327 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002328 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002329 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002330 return;
2331 }
Mike Stump11289f42009-09-09 15:08:12 +00002332
Michael Han99315932013-01-24 16:46:58 +00002333 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002334 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002335 if (NewAttr)
2336 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002337}
2338
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002339
Chandler Carruthedc2c642011-07-02 00:01:44 +00002340static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002341 VarDecl *VD = cast<VarDecl>(D);
2342 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002343 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002344 return;
2345 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002346
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002347 Expr *E = Attr.getArgAsExpr(0);
2348 SourceLocation Loc = E->getExprLoc();
2349 FunctionDecl *FD = 0;
2350 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002351
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002352 // gcc only allows for simple identifiers. Since we support more than gcc, we
2353 // will warn the user.
2354 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2355 if (DRE->hasQualifier())
2356 S.Diag(Loc, diag::warn_cleanup_ext);
2357 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2358 NI = DRE->getNameInfo();
2359 if (!FD) {
2360 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2361 << NI.getName();
2362 return;
2363 }
2364 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2365 if (ULE->hasExplicitTemplateArgs())
2366 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002367 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2368 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002369 if (!FD) {
2370 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2371 << NI.getName();
2372 if (ULE->getType() == S.Context.OverloadTy)
2373 S.NoteAllOverloadCandidates(ULE);
2374 return;
2375 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002376 } else {
2377 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002378 return;
2379 }
2380
Anders Carlssond277d792009-01-31 01:16:18 +00002381 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002382 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2383 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002384 return;
2385 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002386
Anders Carlsson723f55d2009-02-07 23:16:50 +00002387 // We're currently more strict than GCC about what function types we accept.
2388 // If this ever proves to be a problem it should be easy to fix.
2389 QualType Ty = S.Context.getPointerType(VD->getType());
2390 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002391 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2392 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002393 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2394 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002395 return;
2396 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002397
Michael Han99315932013-01-24 16:46:58 +00002398 D->addAttr(::new (S.Context)
2399 CleanupAttr(Attr.getRange(), S.Context, FD,
2400 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002401}
2402
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002404/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002405static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002406 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002407 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002408 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002409 return;
2410 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002411
Aaron Ballman00e99962013-08-31 01:11:41 +00002412 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002413 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002414 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002415 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002416
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002417 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002418 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002419
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002420 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2421 if (not_nsstring_type &&
2422 !isCFStringType(Ty, S.Context) &&
2423 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002424 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002425 // FIXME: Should highlight the actual expression that has the wrong type.
2426 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002427 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002428 << IdxExpr->getSourceRange();
2429 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002430 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002431 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002432 if (!isNSStringType(Ty, S.Context) &&
2433 !isCFStringType(Ty, S.Context) &&
2434 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002435 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002436 // FIXME: Should highlight the actual expression that has the wrong type.
2437 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002438 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002439 << IdxExpr->getSourceRange();
2440 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002441 }
2442
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002443 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2444 // because that has corrected for the implicit this parameter, and is zero-
2445 // based. The attribute expects what the user wrote explicitly.
2446 llvm::APSInt Val;
2447 IdxExpr->EvaluateAsInt(Val, S.Context);
2448
Michael Han99315932013-01-24 16:46:58 +00002449 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002450 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002451 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002452}
2453
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002454enum FormatAttrKind {
2455 CFStringFormat,
2456 NSStringFormat,
2457 StrftimeFormat,
2458 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002459 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002460 InvalidFormat
2461};
2462
2463/// getFormatAttrKind - Map from format attribute names to supported format
2464/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002465static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002466 return llvm::StringSwitch<FormatAttrKind>(Format)
2467 // Check for formats that get handled specially.
2468 .Case("NSString", NSStringFormat)
2469 .Case("CFString", CFStringFormat)
2470 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002471
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002472 // Otherwise, check for supported formats.
2473 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2474 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2475 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002476
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002477 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2478 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002479}
2480
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002481/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002482/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002483static void handleInitPriorityAttr(Sema &S, Decl *D,
2484 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002485 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002486 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2487 return;
2488 }
2489
Aaron Ballman4a611152013-11-27 16:34:09 +00002490 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002491 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2492 Attr.setInvalid();
2493 return;
2494 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002495 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002496 if (S.Context.getAsArrayType(T))
2497 T = S.Context.getBaseElementType(T);
2498 if (!T->getAs<RecordType>()) {
2499 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2500 Attr.setInvalid();
2501 return;
2502 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002503
2504 Expr *E = Attr.getArgAsExpr(0);
2505 uint32_t prioritynum;
2506 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002507 Attr.setInvalid();
2508 return;
2509 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002510
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002511 if (prioritynum < 101 || prioritynum > 65535) {
2512 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002513 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002514 Attr.setInvalid();
2515 return;
2516 }
Michael Han99315932013-01-24 16:46:58 +00002517 D->addAttr(::new (S.Context)
2518 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2519 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002520}
2521
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002522FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2523 IdentifierInfo *Format, int FormatIdx,
2524 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002525 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002526 // Check whether we already have an equivalent format attribute.
2527 for (specific_attr_iterator<FormatAttr>
2528 i = D->specific_attr_begin<FormatAttr>(),
2529 e = D->specific_attr_end<FormatAttr>();
2530 i != e ; ++i) {
2531 FormatAttr *f = *i;
2532 if (f->getType() == Format &&
2533 f->getFormatIdx() == FormatIdx &&
2534 f->getFirstArg() == FirstArg) {
2535 // If we don't have a valid location for this attribute, adopt the
2536 // location.
2537 if (f->getLocation().isInvalid())
2538 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002539 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002540 }
2541 }
2542
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002543 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2544 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002545}
2546
Mike Stumpd3bb5572009-07-24 19:02:52 +00002547/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002548/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002549static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002550 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002552 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002553 return;
2554 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002555
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002556 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002557 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002558 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559 return;
2560 }
2561
Chandler Carruth743682b2010-11-16 08:35:43 +00002562 // In C++ the implicit 'this' function parameter also counts, and they are
2563 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002564 bool HasImplicitThisParam = isInstanceMethod(D);
2565 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566
Aaron Ballman00e99962013-08-31 01:11:41 +00002567 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2568 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569
2570 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002571 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002572 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002573 // If we've modified the string name, we need a new identifier for it.
2574 II = &S.Context.Idents.get(Format);
2575 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002577 // Check for supported formats.
2578 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002579
2580 if (Kind == IgnoredFormat)
2581 return;
2582
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002583 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002584 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002585 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002586 return;
2587 }
2588
2589 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002590 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002591 uint32_t Idx;
2592 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002594
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002595 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002597 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598 return;
2599 }
2600
2601 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002602 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002603
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002604 if (HasImplicitThisParam) {
2605 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002606 S.Diag(Attr.getLoc(),
2607 diag::err_format_attribute_implicit_this_format_string)
2608 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002609 return;
2610 }
2611 ArgIdx--;
2612 }
Mike Stump11289f42009-09-09 15:08:12 +00002613
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002615 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002617 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002618 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2620 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002621 return;
2622 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002623 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002624 // FIXME: do we need to check if the type is NSString*? What are the
2625 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002626 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002627 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002628 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2629 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002630 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002631 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002632 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002633 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002634 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002635 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2636 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637 return;
2638 }
2639
2640 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002641 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002642 uint32_t FirstArg;
2643 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002645
2646 // check if the function is variadic if the 3rd argument non-zero
2647 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002648 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649 ++NumArgs; // +1 for ...
2650 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002651 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002652 return;
2653 }
2654 }
2655
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002656 // strftime requires FirstArg to be 0 because it doesn't read from any
2657 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002658 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002659 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002660 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2661 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662 return;
2663 }
2664 // if 0 it disables parameter checking (to use with e.g. va_list)
2665 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002667 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002668 return;
2669 }
2670
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002671 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002672 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002673 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002674 if (NewAttr)
2675 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002676}
2677
Chandler Carruthedc2c642011-07-02 00:01:44 +00002678static void handleTransparentUnionAttr(Sema &S, Decl *D,
2679 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002680 // Try to find the underlying union declaration.
2681 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002682 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002683 if (TD && TD->getUnderlyingType()->isUnionType())
2684 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2685 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002686 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002687
2688 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002690 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002691 return;
2692 }
2693
John McCallf937c022011-10-07 06:10:15 +00002694 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002695 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002696 diag::warn_transparent_union_attribute_not_definition);
2697 return;
2698 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002699
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002700 RecordDecl::field_iterator Field = RD->field_begin(),
2701 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002702 if (Field == FieldEnd) {
2703 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2704 return;
2705 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002706
David Blaikie40ed2972012-06-06 20:45:41 +00002707 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002708 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002709 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002710 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002711 diag::warn_transparent_union_attribute_floating)
2712 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002713 return;
2714 }
2715
2716 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2717 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2718 for (; Field != FieldEnd; ++Field) {
2719 QualType FieldType = Field->getType();
2720 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2721 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2722 // Warn if we drop the attribute.
2723 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002724 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002725 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002726 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002727 diag::warn_transparent_union_attribute_field_size_align)
2728 << isSize << Field->getDeclName() << FieldBits;
2729 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002730 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 diag::note_transparent_union_first_field_size_align)
2732 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002733 return;
2734 }
2735 }
2736
Michael Han99315932013-01-24 16:46:58 +00002737 RD->addAttr(::new (S.Context)
2738 TransparentUnionAttr(Attr.getRange(), S.Context,
2739 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740}
2741
Chandler Carruthedc2c642011-07-02 00:01:44 +00002742static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002743 // Make sure that there is a string literal as the annotation's single
2744 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002745 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002746 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002748
2749 // Don't duplicate annotations that are already set.
2750 for (specific_attr_iterator<AnnotateAttr>
2751 i = D->specific_attr_begin<AnnotateAttr>(),
2752 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002753 if ((*i)->getAnnotation() == Str)
2754 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002755 }
Michael Han99315932013-01-24 16:46:58 +00002756
2757 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002758 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002759 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002760}
2761
Chandler Carruthedc2c642011-07-02 00:01:44 +00002762static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002763 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002764 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002765 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2766 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002767 return;
2768 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002769
Richard Smith848e1f12013-02-01 08:12:08 +00002770 if (Attr.getNumArgs() == 0) {
2771 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2772 true, 0, Attr.getAttributeSpellingListIndex()));
2773 return;
2774 }
2775
Aaron Ballman00e99962013-08-31 01:11:41 +00002776 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002777 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2778 S.Diag(Attr.getEllipsisLoc(),
2779 diag::err_pack_expansion_without_parameter_packs);
2780 return;
2781 }
2782
2783 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2784 return;
2785
2786 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2787 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002788}
2789
2790void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002791 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002792 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2793 SourceLocation AttrLoc = AttrRange.getBegin();
2794
Richard Smith1dba27c2013-01-29 09:02:09 +00002795 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002796 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002797 // C++11 [dcl.align]p1:
2798 // An alignment-specifier may be applied to a variable or to a class
2799 // data member, but it shall not be applied to a bit-field, a function
2800 // parameter, the formal parameter of a catch clause, or a variable
2801 // declared with the register storage class specifier. An
2802 // alignment-specifier may also be applied to the declaration of a class
2803 // or enumeration type.
2804 // C11 6.7.5/2:
2805 // An alignment attribute shall not be specified in a declaration of
2806 // a typedef, or a bit-field, or a function, or a parameter, or an
2807 // object declared with the register storage-class specifier.
2808 int DiagKind = -1;
2809 if (isa<ParmVarDecl>(D)) {
2810 DiagKind = 0;
2811 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2812 if (VD->getStorageClass() == SC_Register)
2813 DiagKind = 1;
2814 if (VD->isExceptionVariable())
2815 DiagKind = 2;
2816 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2817 if (FD->isBitField())
2818 DiagKind = 3;
2819 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002820 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002821 << (TmpAttr.isC11() ? ExpectedVariableOrField
2822 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002823 return;
2824 }
2825 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002826 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002827 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002828 return;
2829 }
2830 }
2831
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002832 if (E->isTypeDependent() || E->isValueDependent()) {
2833 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002834 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2835 AA->setPackExpansion(IsPackExpansion);
2836 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002837 return;
2838 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002839
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002840 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002841 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002842 ExprResult ICE
2843 = VerifyIntegerConstantExpression(E, &Alignment,
2844 diag::err_aligned_attribute_argument_not_int,
2845 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002846 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002847 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002848
2849 // C++11 [dcl.align]p2:
2850 // -- if the constant expression evaluates to zero, the alignment
2851 // specifier shall have no effect
2852 // C11 6.7.5p6:
2853 // An alignment specification of zero has no effect.
2854 if (!(TmpAttr.isAlignas() && !Alignment) &&
2855 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002856 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2857 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002858 return;
2859 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002860
Richard Smith848e1f12013-02-01 08:12:08 +00002861 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002862 // We've already verified it's a power of 2, now let's make sure it's
2863 // 8192 or less.
2864 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002865 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002866 << E->getSourceRange();
2867 return;
2868 }
2869 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002870
Richard Smith44c247f2013-02-22 08:32:16 +00002871 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2872 ICE.take(), SpellingListIndex);
2873 AA->setPackExpansion(IsPackExpansion);
2874 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002875}
2876
Michael Hanaf02bbe2013-02-01 01:19:17 +00002877void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002878 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002879 // FIXME: Cache the number on the Attr object if non-dependent?
2880 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002881 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2882 SpellingListIndex);
2883 AA->setPackExpansion(IsPackExpansion);
2884 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002885}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002886
Richard Smith848e1f12013-02-01 08:12:08 +00002887void Sema::CheckAlignasUnderalignment(Decl *D) {
2888 assert(D->hasAttrs() && "no attributes on decl");
2889
2890 QualType Ty;
2891 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2892 Ty = VD->getType();
2893 else
2894 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002895 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002896 return;
2897
2898 // C++11 [dcl.align]p5, C11 6.7.5/4:
2899 // The combined effect of all alignment attributes in a declaration shall
2900 // not specify an alignment that is less strict than the alignment that
2901 // would otherwise be required for the entity being declared.
2902 AlignedAttr *AlignasAttr = 0;
2903 unsigned Align = 0;
2904 for (specific_attr_iterator<AlignedAttr>
2905 I = D->specific_attr_begin<AlignedAttr>(),
2906 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2907 if (I->isAlignmentDependent())
2908 return;
2909 if (I->isAlignas())
2910 AlignasAttr = *I;
2911 Align = std::max(Align, I->getAlignment(Context));
2912 }
2913
2914 if (AlignasAttr && Align) {
2915 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2916 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2917 if (NaturalAlign > RequestedAlign)
2918 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2919 << Ty << (unsigned)NaturalAlign.getQuantity();
2920 }
2921}
2922
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002923/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002924/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002925///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002926/// Despite what would be logical, the mode attribute is a decl attribute, not a
2927/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2928/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002929static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002930 // This attribute isn't documented, but glibc uses it. It changes
2931 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002932 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002933 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2934 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002935 return;
2936 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002937
Aaron Ballman00e99962013-08-31 01:11:41 +00002938 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2939 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002940
2941 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002942 if (Str.startswith("__") && Str.endswith("__"))
2943 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002944
2945 unsigned DestWidth = 0;
2946 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002947 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002948 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002949 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002950 switch (Str[0]) {
2951 case 'Q': DestWidth = 8; break;
2952 case 'H': DestWidth = 16; break;
2953 case 'S': DestWidth = 32; break;
2954 case 'D': DestWidth = 64; break;
2955 case 'X': DestWidth = 96; break;
2956 case 'T': DestWidth = 128; break;
2957 }
2958 if (Str[1] == 'F') {
2959 IntegerMode = false;
2960 } else if (Str[1] == 'C') {
2961 IntegerMode = false;
2962 ComplexMode = true;
2963 } else if (Str[1] != 'I') {
2964 DestWidth = 0;
2965 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966 break;
2967 case 4:
2968 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2969 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002970 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002971 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002972 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002973 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002974 break;
2975 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002976 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002977 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002978 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002979 case 11:
2980 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002981 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002982 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002983 }
2984
2985 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002986 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002987 OldTy = TD->getUnderlyingType();
2988 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2989 OldTy = VD->getType();
2990 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002991 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002992 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002993 return;
2994 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002995
John McCall9dd450b2009-09-21 23:43:11 +00002996 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002997 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2998 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002999 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003000 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3001 } else if (ComplexMode) {
3002 if (!OldTy->isComplexType())
3003 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3004 } else {
3005 if (!OldTy->isFloatingType())
3006 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3007 }
3008
Mike Stump87c57ac2009-05-16 07:39:55 +00003009 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3010 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003011 // FIXME: Make sure floating-point mappings are accurate
3012 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003013 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003014 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003016 }
3017
3018 QualType NewTy;
3019
3020 if (IntegerMode)
3021 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3022 OldTy->isSignedIntegerType());
3023 else
3024 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3025
3026 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003027 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003028 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003029 }
3030
Eli Friedman4735374e2009-03-03 06:41:03 +00003031 if (ComplexMode) {
3032 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003033 }
3034
3035 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003036 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3037 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3038 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003039 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003040
3041 D->addAttr(::new (S.Context)
3042 ModeAttr(Attr.getRange(), S.Context, Name,
3043 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003044}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003045
Chandler Carruthedc2c642011-07-02 00:01:44 +00003046static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003047 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3048 if (!VD->hasGlobalStorage())
3049 S.Diag(Attr.getLoc(),
3050 diag::warn_attribute_requires_functions_or_static_globals)
3051 << Attr.getName();
3052 } else if (!isFunctionOrMethod(D)) {
3053 S.Diag(Attr.getLoc(),
3054 diag::warn_attribute_requires_functions_or_static_globals)
3055 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003056 return;
3057 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003058
Michael Han99315932013-01-24 16:46:58 +00003059 D->addAttr(::new (S.Context)
3060 NoDebugAttr(Attr.getRange(), S.Context,
3061 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003062}
3063
Chandler Carruthedc2c642011-07-02 00:01:44 +00003064static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003065 FunctionDecl *FD = cast<FunctionDecl>(D);
3066 if (!FD->getResultType()->isVoidType()) {
3067 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3068 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3069 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3070 << FD->getType()
3071 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3072 "void");
3073 } else {
3074 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3075 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003076 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003077 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003078 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003079
Aaron Ballman3aff6332013-12-02 19:30:36 +00003080 D->addAttr(::new (S.Context)
3081 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003082 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003083}
3084
Chandler Carruthedc2c642011-07-02 00:01:44 +00003085static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003086 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003087 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003088 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003089 return;
3090 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003091
Michael Han99315932013-01-24 16:46:58 +00003092 D->addAttr(::new (S.Context)
3093 GNUInlineAttr(Attr.getRange(), S.Context,
3094 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003095}
3096
Chandler Carruthedc2c642011-07-02 00:01:44 +00003097static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003098 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003099
Aaron Ballman02df2e02012-12-09 17:45:41 +00003100 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003101 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003102 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3103 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003104 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003105 return;
3106
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003107 if (!isa<ObjCMethodDecl>(D)) {
3108 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3109 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003110 return;
3111 }
3112
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003113 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003114 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003115 D->addAttr(::new (S.Context)
3116 FastCallAttr(Attr.getRange(), S.Context,
3117 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003118 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003119 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003120 D->addAttr(::new (S.Context)
3121 StdCallAttr(Attr.getRange(), S.Context,
3122 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003123 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003124 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003125 D->addAttr(::new (S.Context)
3126 ThisCallAttr(Attr.getRange(), S.Context,
3127 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003128 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003129 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003130 D->addAttr(::new (S.Context)
3131 CDeclAttr(Attr.getRange(), S.Context,
3132 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003133 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003134 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 PascalAttr(Attr.getRange(), S.Context,
3137 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003138 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003139 case AttributeList::AT_MSABI:
3140 D->addAttr(::new (S.Context)
3141 MSABIAttr(Attr.getRange(), S.Context,
3142 Attr.getAttributeSpellingListIndex()));
3143 return;
3144 case AttributeList::AT_SysVABI:
3145 D->addAttr(::new (S.Context)
3146 SysVABIAttr(Attr.getRange(), S.Context,
3147 Attr.getAttributeSpellingListIndex()));
3148 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003149 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003150 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003151 switch (CC) {
3152 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003153 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003154 break;
3155 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003156 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003157 break;
3158 default:
3159 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003160 }
3161
Michael Han99315932013-01-24 16:46:58 +00003162 D->addAttr(::new (S.Context)
3163 PcsAttr(Attr.getRange(), S.Context, PCS,
3164 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003165 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003166 }
Derek Schuffa2020962012-10-16 22:30:41 +00003167 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003168 D->addAttr(::new (S.Context)
3169 PnaclCallAttr(Attr.getRange(), S.Context,
3170 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003171 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003172 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003173 D->addAttr(::new (S.Context)
3174 IntelOclBiccAttr(Attr.getRange(), S.Context,
3175 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003176 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003177
Abramo Bagnara50099372010-04-30 13:10:51 +00003178 default:
3179 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003180 }
3181}
3182
Aaron Ballman02df2e02012-12-09 17:45:41 +00003183bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3184 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003185 if (attr.isInvalid())
3186 return true;
3187
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003188 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003189 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003190 attr.setInvalid();
3191 return true;
3192 }
3193
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003194 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003195 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003196 case AttributeList::AT_CDecl: CC = CC_C; break;
3197 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3198 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3199 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3200 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003201 case AttributeList::AT_MSABI:
3202 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3203 CC_X86_64Win64;
3204 break;
3205 case AttributeList::AT_SysVABI:
3206 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3207 CC_C;
3208 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003209 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003210 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003211 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003212 attr.setInvalid();
3213 return true;
3214 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003215 if (StrRef == "aapcs") {
3216 CC = CC_AAPCS;
3217 break;
3218 } else if (StrRef == "aapcs-vfp") {
3219 CC = CC_AAPCS_VFP;
3220 break;
3221 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003222
3223 attr.setInvalid();
3224 Diag(attr.getLoc(), diag::err_invalid_pcs);
3225 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003226 }
Derek Schuffa2020962012-10-16 22:30:41 +00003227 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003228 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003229 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003230 }
3231
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003232 const TargetInfo &TI = Context.getTargetInfo();
3233 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3234 if (A == TargetInfo::CCCR_Warning) {
3235 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003236
3237 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3238 if (FD)
3239 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3240 TargetInfo::CCMT_NonMember;
3241 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003242 }
3243
John McCall3882ace2011-01-05 12:14:39 +00003244 return false;
3245}
3246
John McCall3882ace2011-01-05 12:14:39 +00003247/// Checks a regparm attribute, returning true if it is ill-formed and
3248/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003249bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3250 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003251 return true;
3252
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003253 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003254 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003255 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003256 }
Eli Friedman7044b762009-03-27 21:06:47 +00003257
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003258 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003259 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003260 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003261 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003262 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003263 }
3264
Douglas Gregore8bbc122011-09-02 00:18:52 +00003265 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003266 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003267 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003268 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003269 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003270 }
3271
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003272 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003273 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003274 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003275 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003276 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003277 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003278 }
3279
John McCall3882ace2011-01-05 12:14:39 +00003280 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003281}
3282
Aaron Ballman66039932013-12-19 00:41:31 +00003283static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3284 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003285 // check the attribute arguments.
3286 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3287 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003288 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3289 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003290 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003291 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003292
3293 if (!isFunctionOrMethod(D)) {
3294 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3295 << Attr.getName() << ExpectedFunctionOrMethod;
3296 return;
3297 }
3298
Aaron Ballman66039932013-12-19 00:41:31 +00003299 uint32_t MaxThreads, MinBlocks = 0;
3300 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3301 return;
3302 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3303 Attr.getArgAsExpr(1),
3304 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003305 return;
3306
3307 D->addAttr(::new (S.Context)
3308 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3309 MaxThreads, MinBlocks,
3310 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003311}
3312
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003313static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3314 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003315 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003317 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003318 return;
3319 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003320
3321 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003322 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323
Aaron Ballman00e99962013-08-31 01:11:41 +00003324 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003325
3326 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3327 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3328 << Attr.getName() << ExpectedFunctionOrMethod;
3329 return;
3330 }
3331
3332 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003333 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3334 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003335 return;
3336
3337 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003338 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3339 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003340 return;
3341
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003342 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003343 if (IsPointer) {
3344 // Ensure that buffer has a pointer type.
3345 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3346 if (!BufferTy->isPointerType()) {
3347 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003348 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003349 }
3350 }
3351
Michael Han99315932013-01-24 16:46:58 +00003352 D->addAttr(::new (S.Context)
3353 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3354 ArgumentIdx, TypeTagIdx, IsPointer,
3355 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003356}
3357
3358static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3359 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003360 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003361 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003362 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003363 return;
3364 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003365
3366 if (!checkAttributeNumArgs(S, Attr, 1))
3367 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003368
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003369 if (!isa<VarDecl>(D)) {
3370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3371 << Attr.getName() << ExpectedVariable;
3372 return;
3373 }
3374
Aaron Ballman00e99962013-08-31 01:11:41 +00003375 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003376 TypeSourceInfo *MatchingCTypeLoc = 0;
3377 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3378 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003379
Michael Han99315932013-01-24 16:46:58 +00003380 D->addAttr(::new (S.Context)
3381 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003382 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003383 Attr.getLayoutCompatible(),
3384 Attr.getMustBeNull(),
3385 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003386}
3387
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003388//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003389// Checker-specific attribute handlers.
3390//===----------------------------------------------------------------------===//
3391
John McCalled433932011-01-25 03:31:58 +00003392static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003393 return type->isDependentType() ||
3394 type->isObjCObjectPointerType() ||
3395 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003396}
3397static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003398 return type->isDependentType() ||
3399 type->isPointerType() ||
3400 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003401}
3402
Chandler Carruthedc2c642011-07-02 00:01:44 +00003403static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003404 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003405 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003406
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003407 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003408 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3409 cf = false;
3410 } else {
3411 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3412 cf = true;
3413 }
3414
3415 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003416 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003417 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003418 return;
3419 }
3420
3421 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003422 param->addAttr(::new (S.Context)
3423 CFConsumedAttr(Attr.getRange(), S.Context,
3424 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003425 else
Michael Han99315932013-01-24 16:46:58 +00003426 param->addAttr(::new (S.Context)
3427 NSConsumedAttr(Attr.getRange(), S.Context,
3428 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003429}
3430
Chandler Carruthedc2c642011-07-02 00:01:44 +00003431static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3432 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003433
John McCalled433932011-01-25 03:31:58 +00003434 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003435
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003436 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003437 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003438 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003439 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003440 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003441 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3442 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003443 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003444 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003445 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003446 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003447 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003448 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003449 return;
3450 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003451
John McCalled433932011-01-25 03:31:58 +00003452 bool typeOK;
3453 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003454 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003455 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003456 case AttributeList::AT_NSReturnsAutoreleased:
3457 case AttributeList::AT_NSReturnsRetained:
3458 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003459 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3460 cf = false;
3461 break;
3462
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003463 case AttributeList::AT_CFReturnsRetained:
3464 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003465 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3466 cf = true;
3467 break;
3468 }
3469
3470 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003471 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003472 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003473 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003474 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003475
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003476 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003477 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003478 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003479 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003480 D->addAttr(::new (S.Context)
3481 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3482 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003483 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003484 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003485 D->addAttr(::new (S.Context)
3486 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3487 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003488 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003489 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003490 D->addAttr(::new (S.Context)
3491 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3492 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003493 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003494 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003495 D->addAttr(::new (S.Context)
3496 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3497 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003498 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003499 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003500 D->addAttr(::new (S.Context)
3501 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3502 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003503 return;
3504 };
3505}
3506
John McCallcf166702011-07-22 08:53:00 +00003507static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3508 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003509 const int EP_ObjCMethod = 1;
3510 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003511
John McCallcf166702011-07-22 08:53:00 +00003512 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003513 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003514 if (isa<ObjCMethodDecl>(D))
3515 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003516 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003517 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003518
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003519 if (!resultType->isReferenceType() &&
3520 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003521 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003522 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003523 << attr.getName()
3524 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003525 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003526
3527 // Drop the attribute.
3528 return;
3529 }
3530
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003531 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003532 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3533 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003534}
3535
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003536static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3537 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003538 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003539
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003540 DeclContext *DC = method->getDeclContext();
3541 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3542 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3543 << attr.getName() << 0;
3544 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3545 return;
3546 }
3547 if (method->getMethodFamily() == OMF_dealloc) {
3548 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3549 << attr.getName() << 1;
3550 return;
3551 }
3552
Michael Han99315932013-01-24 16:46:58 +00003553 method->addAttr(::new (S.Context)
3554 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3555 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003556}
3557
Aaron Ballmanfb763042013-12-02 18:05:46 +00003558static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3559 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003560 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003561 return;
John McCall32f5fe12011-09-30 05:12:12 +00003562
Aaron Ballmanfb763042013-12-02 18:05:46 +00003563 D->addAttr(::new (S.Context)
3564 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3565 Attr.getAttributeSpellingListIndex()));
3566}
3567
3568static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3569 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003570 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003571 return;
3572
3573 D->addAttr(::new (S.Context)
3574 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3575 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003576}
3577
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003578static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3579 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003580 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003581
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003582 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003583 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003584 return;
3585 }
3586
3587 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003588 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003589 Attr.getAttributeSpellingListIndex()));
3590}
3591
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003592static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3593 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003594 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003595
3596 if (!Parm) {
3597 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3598 return;
3599 }
3600
3601 D->addAttr(::new (S.Context)
3602 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3603 Attr.getAttributeSpellingListIndex()));
3604}
3605
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003606static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3607 const AttributeList &Attr) {
3608 IdentifierInfo *RelatedClass =
3609 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3610 if (!RelatedClass) {
3611 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3612 return;
3613 }
3614 IdentifierInfo *ClassMethod =
3615 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3616 IdentifierInfo *InstanceMethod =
3617 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3618 D->addAttr(::new (S.Context)
3619 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3620 ClassMethod, InstanceMethod,
3621 Attr.getAttributeSpellingListIndex()));
3622}
3623
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003624static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3625 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003626 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003627 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003628 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003629 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3630 Attr.getAttributeSpellingListIndex()));
3631}
3632
Chandler Carruthedc2c642011-07-02 00:01:44 +00003633static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3634 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003635 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003636
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003637 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003638 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003639}
3640
Chandler Carruthedc2c642011-07-02 00:01:44 +00003641static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3642 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003643 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003644 QualType type = vd->getType();
3645
3646 if (!type->isDependentType() &&
3647 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003648 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003649 << type;
3650 return;
3651 }
3652
3653 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3654
3655 // If we have no lifetime yet, check the lifetime we're presumably
3656 // going to infer.
3657 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3658 lifetime = type->getObjCARCImplicitLifetime();
3659
3660 switch (lifetime) {
3661 case Qualifiers::OCL_None:
3662 assert(type->isDependentType() &&
3663 "didn't infer lifetime for non-dependent type?");
3664 break;
3665
3666 case Qualifiers::OCL_Weak: // meaningful
3667 case Qualifiers::OCL_Strong: // meaningful
3668 break;
3669
3670 case Qualifiers::OCL_ExplicitNone:
3671 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003672 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003673 << (lifetime == Qualifiers::OCL_Autoreleasing);
3674 break;
3675 }
3676
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003677 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003678 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3679 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003680}
3681
Francois Picheta83957a2010-12-19 06:50:37 +00003682//===----------------------------------------------------------------------===//
3683// Microsoft specific attribute handlers.
3684//===----------------------------------------------------------------------===//
3685
Chandler Carruthedc2c642011-07-02 00:01:44 +00003686static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003687 if (!S.LangOpts.CPlusPlus) {
3688 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3689 << Attr.getName() << AttributeLangSupport::C;
3690 return;
3691 }
3692
Aaron Ballman60e705e2013-11-24 20:58:02 +00003693 if (!isa<CXXRecordDecl>(D)) {
3694 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3695 << Attr.getName() << ExpectedClass;
3696 return;
3697 }
3698
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003699 StringRef StrRef;
3700 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003701 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003702 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003703
David Majnemer89085342013-08-09 08:56:20 +00003704 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3705 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003706 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3707 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003708
Reid Kleckner140c4a72013-05-17 14:04:52 +00003709 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003710 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003711 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003712 return;
3713 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003714
David Majnemer89085342013-08-09 08:56:20 +00003715 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003716 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003717 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003718 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003719 return;
3720 }
David Majnemer89085342013-08-09 08:56:20 +00003721 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003722 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003723 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003724 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003725 }
Francois Picheta83957a2010-12-19 06:50:37 +00003726
David Majnemer89085342013-08-09 08:56:20 +00003727 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3728 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003729}
3730
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003731static void handleARMInterruptAttr(Sema &S, Decl *D,
3732 const AttributeList &Attr) {
3733 // Check the attribute arguments.
3734 if (Attr.getNumArgs() > 1) {
3735 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3736 << Attr.getName() << 1;
3737 return;
3738 }
3739
3740 StringRef Str;
3741 SourceLocation ArgLoc;
3742
3743 if (Attr.getNumArgs() == 0)
3744 Str = "";
3745 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3746 return;
3747
3748 ARMInterruptAttr::InterruptType Kind;
3749 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3750 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3751 << Attr.getName() << Str << ArgLoc;
3752 return;
3753 }
3754
3755 unsigned Index = Attr.getAttributeSpellingListIndex();
3756 D->addAttr(::new (S.Context)
3757 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3758}
3759
3760static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3761 const AttributeList &Attr) {
3762 if (!checkAttributeNumArgs(S, Attr, 1))
3763 return;
3764
3765 if (!Attr.isArgExpr(0)) {
3766 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3767 << AANT_ArgumentIntegerConstant;
3768 return;
3769 }
3770
3771 // FIXME: Check for decl - it should be void ()(void).
3772
3773 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3774 llvm::APSInt NumParams(32);
3775 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3776 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3777 << Attr.getName() << AANT_ArgumentIntegerConstant
3778 << NumParamsExpr->getSourceRange();
3779 return;
3780 }
3781
3782 unsigned Num = NumParams.getLimitedValue(255);
3783 if ((Num & 1) || Num > 30) {
3784 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3785 << Attr.getName() << (int)NumParams.getSExtValue()
3786 << NumParamsExpr->getSourceRange();
3787 return;
3788 }
3789
3790 D->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
3791 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
3792}
3793
3794static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3795 // Dispatch the interrupt attribute based on the current target.
3796 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3797 handleMSP430InterruptAttr(S, D, Attr);
3798 else
3799 handleARMInterruptAttr(S, D, Attr);
3800}
3801
3802static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3803 const AttributeList& Attr) {
3804 // If we try to apply it to a function pointer, don't warn, but don't
3805 // do anything, either. It doesn't matter anyway, because there's nothing
3806 // special about calling a force_align_arg_pointer function.
3807 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3808 if (VD && VD->getType()->isFunctionPointerType())
3809 return;
3810 // Also don't warn on function pointer typedefs.
3811 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3812 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3813 TD->getUnderlyingType()->isFunctionType()))
3814 return;
3815 // Attribute can only be applied to function types.
3816 if (!isa<FunctionDecl>(D)) {
3817 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3818 << Attr.getName() << /* function */0;
3819 return;
3820 }
3821
3822 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
3823 S.Context));
3824}
3825
3826DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3827 unsigned AttrSpellingListIndex) {
3828 if (D->hasAttr<DLLExportAttr>()) {
3829 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3830 return NULL;
3831 }
3832
3833 if (D->hasAttr<DLLImportAttr>())
3834 return NULL;
3835
3836 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3837 if (VD->hasDefinition()) {
3838 // dllimport cannot be applied to definitions.
3839 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3840 << "dllimport";
3841 return NULL;
3842 }
3843 }
3844
3845 return ::new (Context)DLLImportAttr(Range, Context,
3846 AttrSpellingListIndex);
3847}
3848
3849static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3850 // Attribute can be applied only to functions or variables.
3851 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3852 if (!FD && !isa<VarDecl>(D)) {
3853 // Apparently Visual C++ thinks it is okay to not emit a warning
3854 // in this case, so only emit a warning when -fms-extensions is not
3855 // specified.
3856 if (!S.getLangOpts().MicrosoftExt)
3857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3858 << Attr.getName() << 2 /*variable and function*/;
3859 return;
3860 }
3861
3862 // Currently, the dllimport attribute is ignored for inlined functions.
3863 // Warning is emitted.
3864 if (FD && FD->isInlineSpecified()) {
3865 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3866 return;
3867 }
3868
3869 unsigned Index = Attr.getAttributeSpellingListIndex();
3870 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3871 if (NewAttr)
3872 D->addAttr(NewAttr);
3873}
3874
3875DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3876 unsigned AttrSpellingListIndex) {
3877 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3878 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3879 D->dropAttr<DLLImportAttr>();
3880 }
3881
3882 if (D->hasAttr<DLLExportAttr>())
3883 return NULL;
3884
3885 return ::new (Context)DLLExportAttr(Range, Context,
3886 AttrSpellingListIndex);
3887}
3888
3889static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3890 // Currently, the dllexport attribute is ignored for inlined functions, unless
3891 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3892 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3893 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3894 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3895 return;
3896 }
3897
3898 unsigned Index = Attr.getAttributeSpellingListIndex();
3899 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3900 if (NewAttr)
3901 D->addAttr(NewAttr);
3902}
3903
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003904/// Handles semantic checking for features that are common to all attributes,
3905/// such as checking whether a parameter was properly specified, or the correct
3906/// number of arguments were passed, etc.
3907static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3908 const AttributeList &Attr) {
3909 // Several attributes carry different semantics than the parsing requires, so
3910 // those are opted out of the common handling.
3911 //
3912 // We also bail on unknown and ignored attributes because those are handled
3913 // as part of the target-specific handling logic.
3914 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003915 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003916 return false;
3917
Aaron Ballman3aff6332013-12-02 19:30:36 +00003918 // Check whether the attribute requires specific language extensions to be
3919 // enabled.
3920 if (!Attr.diagnoseLangOpts(S))
3921 return true;
3922
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003923 // If there are no optional arguments, then checking for the argument count
3924 // is trivial.
3925 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3926 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3927 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003928
3929 // Check whether the attribute appertains to the given subject.
3930 if (!Attr.diagnoseAppertainsTo(S, D))
3931 return true;
3932
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003933 return false;
3934}
3935
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003936//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003937// Top Level Sema Entry Points
3938//===----------------------------------------------------------------------===//
3939
Richard Smithf8a75c32013-08-29 00:47:48 +00003940/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3941/// the attribute applies to decls. If the attribute is a type attribute, just
3942/// silently ignore it if a GNU attribute.
3943static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3944 const AttributeList &Attr,
3945 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003946 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003947 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003948
Richard Smithf8a75c32013-08-29 00:47:48 +00003949 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3950 // instead.
3951 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3952 return;
3953
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003954 // Unknown attributes are automatically warned on. Target-specific attributes
3955 // which do not apply to the current target architecture are treated as
3956 // though they were unknown attributes.
3957 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3958 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3959 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3960 diag::warn_unhandled_ms_attribute_ignored :
3961 diag::warn_unknown_attribute_ignored) << Attr.getName();
3962 return;
3963 }
3964
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003965 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3966 return;
3967
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003968 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003969 default:
3970 // Type attributes are handled elsewhere; silently move on.
3971 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3972 break;
3973 case AttributeList::AT_Interrupt:
3974 handleInterruptAttr(S, D, Attr); break;
3975 case AttributeList::AT_X86ForceAlignArgPointer:
3976 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3977 case AttributeList::AT_DLLExport:
3978 handleDLLExportAttr(S, D, Attr); break;
3979 case AttributeList::AT_DLLImport:
3980 handleDLLImportAttr(S, D, Attr); break;
3981 case AttributeList::AT_Mips16:
3982 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3983 case AttributeList::AT_NoMips16:
3984 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003985 case AttributeList::AT_IBAction:
3986 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003987 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3988 case AttributeList::AT_IBOutletCollection:
3989 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003990 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3991 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003993 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003994 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003995 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003996 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3998 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3999 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004000 handleDependencyAttr(S, scope, D, Attr);
4001 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004002 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004003 case AttributeList::AT_CUDAConstant:
4004 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004005 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004006 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004007 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004008 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004009 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004010 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004011 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00004012 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004013 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004014 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004015 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004016 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004017 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004018 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004019 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4020 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4021 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004022 case AttributeList::AT_CUDADevice:
4023 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004024 case AttributeList::AT_CUDAHost:
4025 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004026 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4027 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004028 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004029 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004030 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004031 case AttributeList::AT_MayAlias:
4032 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004033 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004034 case AttributeList::AT_NoCommon:
4035 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004036 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004037 case AttributeList::AT_Overloadable:
4038 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004039 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004040 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4041 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004042 case AttributeList::AT_Naked:
4043 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004044 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004045 case AttributeList::AT_NoThrow:
4046 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004047 case AttributeList::AT_CUDAShared:
4048 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004050
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004051 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004052 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004053 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004054 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004055
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004056 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004057 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4058
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004059 case AttributeList::AT_ObjCRequiresSuper:
4060 handleObjCRequiresSuperAttr(S, D, Attr); break;
4061
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004062 case AttributeList::AT_ObjCBridge:
4063 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004064
4065 case AttributeList::AT_ObjCBridgeMutable:
4066 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004067
4068 case AttributeList::AT_ObjCBridgeRelated:
4069 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004070
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004071 case AttributeList::AT_ObjCDesignatedInitializer:
4072 handleObjCDesignatedInitializer(S, D, Attr); break;
4073
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004074 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004075 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004076 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004077 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004078
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004079 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004080 case AttributeList::AT_CFConsumed:
4081 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4082 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004083 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004084
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004085 case AttributeList::AT_NSReturnsAutoreleased:
4086 case AttributeList::AT_NSReturnsNotRetained:
4087 case AttributeList::AT_CFReturnsNotRetained:
4088 case AttributeList::AT_NSReturnsRetained:
4089 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004090 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004091 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004092 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004093 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004094 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004095 case AttributeList::AT_VecTypeHint:
4096 handleVecTypeHint(S, D, Attr); break;
4097
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004099 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004100
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004101 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4102 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4103 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004104 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004105 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004106 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004107 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004108 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004109 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004110 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004111 handleObjCSuppresProtocolAttr(S, D, Attr);
4112 break;
4113 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004114 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004115 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4116 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004117 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004118 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004119 case AttributeList::AT_Visibility:
4120 handleVisibilityAttr(S, D, Attr, false);
4121 break;
4122 case AttributeList::AT_TypeVisibility:
4123 handleVisibilityAttr(S, D, Attr, true);
4124 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004125 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004126 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004128 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004129 case AttributeList::AT_Weak:
4130 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004131 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4132 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4133 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004134 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004135 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004136 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004137 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004138 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004139 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004140 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004141 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4142 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4143 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004144 case AttributeList::AT_Const:
4145 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004146 case AttributeList::AT_Pure:
4147 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004148 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4149 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004150 case AttributeList::AT_NoInline:
4151 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004153 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004154 case AttributeList::AT_StdCall:
4155 case AttributeList::AT_CDecl:
4156 case AttributeList::AT_FastCall:
4157 case AttributeList::AT_ThisCall:
4158 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004159 case AttributeList::AT_MSABI:
4160 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004162 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004163 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004164 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004165 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004166 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004167 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004168 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004169 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004170
4171 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004172 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004173 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004174 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004175 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004176 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004177 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004178 case AttributeList::AT_MSInheritance:
4179 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004180 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004181 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004182 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004183 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004184
4185 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004186 case AttributeList::AT_AssertExclusiveLock:
4187 handleAssertExclusiveLockAttr(S, D, Attr);
4188 break;
4189 case AttributeList::AT_AssertSharedLock:
4190 handleAssertSharedLockAttr(S, D, Attr);
4191 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004192 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004193 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004194 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004195 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004196 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004197 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004198 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004199 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004200 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004201 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004202 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004203 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004204 break;
4205 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004206 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004207 break;
4208 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004209 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004210 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004211 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004212 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004213 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004214 handleGuardedByAttr(S, D, Attr);
4215 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004216 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004217 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004218 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004220 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004221 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004222 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004223 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004224 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004225 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004226 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004227 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004229 handleLockReturnedAttr(S, D, Attr);
4230 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004231 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004232 handleLocksExcludedAttr(S, D, Attr);
4233 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004234 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004235 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004236 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004237 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004238 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004239 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004240 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004241 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004242 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004243 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004244 handleUnlockFunAttr(S, D, Attr);
4245 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004246 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004247 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004248 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004249 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004250 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004251 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004252
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004253 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004254 case AttributeList::AT_Consumable:
4255 handleConsumableAttr(S, D, Attr);
4256 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004257 case AttributeList::AT_ConsumableAutoCast:
4258 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4259 break;
4260 case AttributeList::AT_ConsumableSetOnRead:
4261 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4262 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004263 case AttributeList::AT_CallableWhen:
4264 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004265 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004266 case AttributeList::AT_ParamTypestate:
4267 handleParamTypestateAttr(S, D, Attr);
4268 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004269 case AttributeList::AT_ReturnTypestate:
4270 handleReturnTypestateAttr(S, D, Attr);
4271 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004272 case AttributeList::AT_SetTypestate:
4273 handleSetTypestateAttr(S, D, Attr);
4274 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004275 case AttributeList::AT_TestTypestate:
4276 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004277 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004278
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004279 // Type safety attributes.
4280 case AttributeList::AT_ArgumentWithTypeTag:
4281 handleArgumentWithTypeTagAttr(S, D, Attr);
4282 break;
4283 case AttributeList::AT_TypeTagForDatatype:
4284 handleTypeTagForDatatypeAttr(S, D, Attr);
4285 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004286 }
4287}
4288
4289/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4290/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004291void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004292 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004293 bool IncludeCXX11Attributes) {
4294 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004295 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004296
Joey Gouly2cd9db12013-12-13 16:15:28 +00004297 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004298 // GCC accepts
4299 // static int a9 __attribute__((weakref));
4300 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004301 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004302 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4303 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004304 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004305 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004306 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004307
4308 if (!D->hasAttr<OpenCLKernelAttr>()) {
4309 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004310 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4311 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004312 D->setInvalidDecl();
4313 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004314 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4315 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004316 D->setInvalidDecl();
4317 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004318 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4319 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004320 D->setInvalidDecl();
4321 }
4322 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004323}
4324
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004325// Annotation attributes are the only attributes allowed after an access
4326// specifier.
4327bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4328 const AttributeList *AttrList) {
4329 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004330 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004331 handleAnnotateAttr(*this, ASDecl, *l);
4332 } else {
4333 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4334 return true;
4335 }
4336 }
4337
4338 return false;
4339}
4340
John McCall42856de2011-10-01 05:17:03 +00004341/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4342/// contains any decl attributes that we should warn about.
4343static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4344 for ( ; A; A = A->getNext()) {
4345 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004346 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004347 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4348
4349 if (A->getKind() == AttributeList::UnknownAttribute) {
4350 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4351 << A->getName() << A->getRange();
4352 } else {
4353 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4354 << A->getName() << A->getRange();
4355 }
4356 }
4357}
4358
4359/// checkUnusedDeclAttributes - Given a declarator which is not being
4360/// used to build a declaration, complain about any decl attributes
4361/// which might be lying around on it.
4362void Sema::checkUnusedDeclAttributes(Declarator &D) {
4363 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4364 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4365 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4366 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4367}
4368
Ryan Flynn7d470f32009-07-30 03:15:39 +00004369/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004370/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004371NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4372 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004373 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004374 NamedDecl *NewD = 0;
4375 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004376 FunctionDecl *NewFD;
4377 // FIXME: Missing call to CheckFunctionDeclaration().
4378 // FIXME: Mangling?
4379 // FIXME: Is the qualifier info correct?
4380 // FIXME: Is the DeclContext correct?
4381 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4382 Loc, Loc, DeclarationName(II),
4383 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004384 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004385 FD->hasPrototype(),
4386 false/*isConstexprSpecified*/);
4387 NewD = NewFD;
4388
4389 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004390 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004391
4392 // Fake up parameter variables; they are declared as if this were
4393 // a typedef.
4394 QualType FDTy = FD->getType();
4395 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4396 SmallVector<ParmVarDecl*, 16> Params;
4397 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4398 AE = FT->arg_type_end(); AI != AE; ++AI) {
4399 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4400 Param->setScopeInfo(0, Params.size());
4401 Params.push_back(Param);
4402 }
David Blaikie9c70e042011-09-21 18:16:56 +00004403 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004404 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004405 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4406 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004407 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004408 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004409 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004410 if (VD->getQualifier()) {
4411 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004412 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004413 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004414 }
4415 return NewD;
4416}
4417
James Dennett634962f2012-06-14 21:40:34 +00004418/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004419/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004420void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004421 if (W.getUsed()) return; // only do this once
4422 W.setUsed(true);
4423 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4424 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004425 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004426 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4427 NDId->getName()));
4428 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004429 WeakTopLevelDecl.push_back(NewD);
4430 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4431 // to insert Decl at TU scope, sorry.
4432 DeclContext *SavedContext = CurContext;
4433 CurContext = Context.getTranslationUnitDecl();
4434 PushOnScopeChains(NewD, S);
4435 CurContext = SavedContext;
4436 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004437 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004438 }
4439}
4440
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004441void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4442 // It's valid to "forward-declare" #pragma weak, in which case we
4443 // have to do this.
4444 LoadExternalWeakUndeclaredIdentifiers();
4445 if (!WeakUndeclaredIdentifiers.empty()) {
4446 NamedDecl *ND = NULL;
4447 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4448 if (VD->isExternC())
4449 ND = VD;
4450 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4451 if (FD->isExternC())
4452 ND = FD;
4453 if (ND) {
4454 if (IdentifierInfo *Id = ND->getIdentifier()) {
4455 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4456 = WeakUndeclaredIdentifiers.find(Id);
4457 if (I != WeakUndeclaredIdentifiers.end()) {
4458 WeakInfo W = I->second;
4459 DeclApplyPragmaWeak(S, ND, W);
4460 WeakUndeclaredIdentifiers[Id] = W;
4461 }
4462 }
4463 }
4464 }
4465}
4466
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004467/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4468/// it, apply them to D. This is a bit tricky because PD can have attributes
4469/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004470void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004471 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004472 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004473 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004474
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004475 // Walk the declarator structure, applying decl attributes that were in a type
4476 // position to the decl itself. This handles cases like:
4477 // int *__attr__(x)** D;
4478 // when X is a decl attribute.
4479 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4480 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004481 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004482
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004483 // Finally, apply any attributes on the decl itself.
4484 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004485 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004486}
John McCall28a6aea2009-11-04 02:18:39 +00004487
John McCall31168b02011-06-15 23:02:42 +00004488/// Is the given declaration allowed to use a forbidden type?
4489static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4490 // Private ivars are always okay. Unfortunately, people don't
4491 // always properly make their ivars private, even in system headers.
4492 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004493 // Function declarations in sys headers will be marked unavailable.
4494 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4495 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004496 return false;
4497
4498 // Require it to be declared in a system header.
4499 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4500}
4501
4502/// Handle a delayed forbidden-type diagnostic.
4503static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4504 Decl *decl) {
4505 if (decl && isForbiddenTypeAllowed(S, decl)) {
4506 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4507 "this system declaration uses an unsupported type"));
4508 return;
4509 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004510 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004511 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004512 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004513 // kind of forbidden type messages on unavailable functions.
4514 if (FD->hasAttr<UnavailableAttr>() &&
4515 diag.getForbiddenTypeDiagnostic() ==
4516 diag::err_arc_array_param_no_ownership) {
4517 diag.Triggered = true;
4518 return;
4519 }
4520 }
John McCall31168b02011-06-15 23:02:42 +00004521
4522 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4523 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4524 diag.Triggered = true;
4525}
4526
John McCall2ec85372012-05-07 06:16:41 +00004527void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4528 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004529 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004530 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004531
John McCall2ec85372012-05-07 06:16:41 +00004532 // When delaying diagnostics to run in the context of a parsed
4533 // declaration, we only want to actually emit anything if parsing
4534 // succeeds.
4535 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004536
John McCall2ec85372012-05-07 06:16:41 +00004537 // We emit all the active diagnostics in this pool or any of its
4538 // parents. In general, we'll get one pool for the decl spec
4539 // and a child pool for each declarator; in a decl group like:
4540 // deprecated_typedef foo, *bar, baz();
4541 // only the declarator pops will be passed decls. This is correct;
4542 // we really do need to consider delayed diagnostics from the decl spec
4543 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004544 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004545 do {
John McCall6347b682012-05-07 06:16:58 +00004546 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004547 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4548 // This const_cast is a bit lame. Really, Triggered should be mutable.
4549 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004550 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004551 continue;
4552
John McCallc1465822011-02-14 07:13:47 +00004553 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004554 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004555 case DelayedDiagnostic::Unavailable:
4556 // Don't bother giving deprecation/unavailable diagnostics if
4557 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004558 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004559 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004560 break;
4561
4562 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004563 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004564 break;
John McCall31168b02011-06-15 23:02:42 +00004565
4566 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004567 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004568 break;
John McCall86121512010-01-27 03:50:35 +00004569 }
4570 }
John McCall2ec85372012-05-07 06:16:41 +00004571 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004572}
4573
John McCall6347b682012-05-07 06:16:58 +00004574/// Given a set of delayed diagnostics, re-emit them as if they had
4575/// been delayed in the current context instead of in the given pool.
4576/// Essentially, this just moves them to the current pool.
4577void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4578 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4579 assert(curPool && "re-emitting in undelayed context not supported");
4580 curPool->steal(pool);
4581}
4582
John McCall28a6aea2009-11-04 02:18:39 +00004583static bool isDeclDeprecated(Decl *D) {
4584 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004585 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004586 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004587 // A category implicitly has the availability of the interface.
4588 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4589 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004590 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4591 return false;
4592}
4593
Ted Kremenekb79ee572013-12-18 23:30:06 +00004594static bool isDeclUnavailable(Decl *D) {
4595 do {
4596 if (D->isUnavailable())
4597 return true;
4598 // A category implicitly has the availability of the interface.
4599 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4600 return CatD->getClassInterface()->isUnavailable();
4601 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4602 return false;
4603}
4604
Eli Friedman971bfa12012-08-08 21:52:41 +00004605static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004606DoEmitAvailabilityWarning(Sema &S,
4607 DelayedDiagnostic::DDKind K,
4608 Decl *Ctx,
4609 const NamedDecl *D,
4610 StringRef Message,
4611 SourceLocation Loc,
4612 const ObjCInterfaceDecl *UnknownObjCClass,
4613 const ObjCPropertyDecl *ObjCProperty) {
4614
4615 // Diagnostics for deprecated or unavailable.
4616 unsigned diag, diag_message, diag_fwdclass_message;
4617
4618 // Matches 'diag::note_property_attribute' options.
4619 unsigned property_note_select;
4620
4621 // Matches diag::note_availability_specified_here.
4622 unsigned available_here_select_kind;
4623
4624 // Don't warn if our current context is deprecated or unavailable.
4625 switch (K) {
4626 case DelayedDiagnostic::Deprecation:
4627 if (isDeclDeprecated(Ctx))
4628 return;
4629 diag = diag::warn_deprecated;
4630 diag_message = diag::warn_deprecated_message;
4631 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4632 property_note_select = /* deprecated */ 0;
4633 available_here_select_kind = /* deprecated */ 2;
4634 break;
4635
4636 case DelayedDiagnostic::Unavailable:
4637 if (isDeclUnavailable(Ctx))
4638 return;
4639 diag = diag::err_unavailable;
4640 diag_message = diag::err_unavailable_message;
4641 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4642 property_note_select = /* unavailable */ 1;
4643 available_here_select_kind = /* unavailable */ 0;
4644 break;
4645
4646 default:
4647 llvm_unreachable("Neither a deprecation or unavailable kind");
4648 }
4649
Eli Friedman971bfa12012-08-08 21:52:41 +00004650 DeclarationName Name = D->getDeclName();
4651 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004652 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004653 if (ObjCProperty)
4654 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4655 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004656 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004657 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004658 if (ObjCProperty)
4659 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4660 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004661 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004662 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004663 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4664 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004665
4666 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4667 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004668}
4669
Ted Kremenekb79ee572013-12-18 23:30:06 +00004670void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4671 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004672 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004673 DoEmitAvailabilityWarning(*this,
4674 (DelayedDiagnostic::DDKind) DD.Kind,
4675 Ctx,
4676 DD.getDeprecationDecl(),
4677 DD.getDeprecationMessage(),
4678 DD.Loc,
4679 DD.getUnknownObjCClass(),
4680 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004681}
4682
Ted Kremenekb79ee572013-12-18 23:30:06 +00004683void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4684 NamedDecl *D, StringRef Message,
4685 SourceLocation Loc,
4686 const ObjCInterfaceDecl *UnknownObjCClass,
4687 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004688 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004689 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004690 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4691 UnknownObjCClass,
4692 ObjCProperty,
4693 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004694 return;
4695 }
4696
Ted Kremenekb79ee572013-12-18 23:30:06 +00004697 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4698 DelayedDiagnostic::DDKind K;
4699 switch (AD) {
4700 case AD_Deprecation:
4701 K = DelayedDiagnostic::Deprecation;
4702 break;
4703 case AD_Unavailable:
4704 K = DelayedDiagnostic::Unavailable;
4705 break;
4706 }
4707
4708 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4709 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004710}