blob: 49ab6ed1b82c9f43d2671f87efc5fbeaca83e474 [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
Aaron Ballman36a53502014-01-16 13:03:14 +0000568 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
569 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000570}
571
Michael Hana9171bc2012-08-03 17:40:43 +0000572static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000573 const AttributeList &Attr) {
574 Expr *Arg = 0;
575 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
576 return;
577
578 if (!threadSafetyCheckIsPointer(S, D, Attr))
579 return;
580
581 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000582 S.Context, Arg,
583 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000584}
585
Michael Hana9171bc2012-08-03 17:40:43 +0000586static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
587 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000588 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000591
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000592 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000593 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000594 if (!QT->isDependentType()) {
595 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000596 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000597 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000598 << Attr.getName();
599 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000600 }
601 }
602
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000603 // Check that all arguments are lockable objects.
604 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000605 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000606 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000607
Michael Han3be3b442012-07-23 18:48:41 +0000608 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000609}
610
Michael Hana9171bc2012-08-03 17:40:43 +0000611static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000612 const AttributeList &Attr) {
613 SmallVector<Expr*, 1> Args;
614 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
615 return;
616
617 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000618 D->addAttr(::new (S.Context)
619 AcquiredAfterAttr(Attr.getRange(), S.Context,
620 StartArg, Args.size(),
621 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000625 const AttributeList &Attr) {
626 SmallVector<Expr*, 1> Args;
627 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
628 return;
629
630 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000631 D->addAttr(::new (S.Context)
632 AcquiredBeforeAttr(Attr.getRange(), S.Context,
633 StartArg, Args.size(),
634 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000635}
636
Michael Hana9171bc2012-08-03 17:40:43 +0000637static bool checkLockFunAttrCommon(Sema &S, Decl *D,
638 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000639 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000640 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000641 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000642 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000643
Michael Han3be3b442012-07-23 18:48:41 +0000644 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000645}
646
Michael Hana9171bc2012-08-03 17:40:43 +0000647static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000648 const AttributeList &Attr) {
649 SmallVector<Expr*, 1> Args;
650 if (!checkLockFunAttrCommon(S, D, Attr, Args))
651 return;
652
653 unsigned Size = Args.size();
654 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000655 D->addAttr(::new (S.Context)
656 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
657 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000658}
659
Michael Hana9171bc2012-08-03 17:40:43 +0000660static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000661 const AttributeList &Attr) {
662 SmallVector<Expr*, 1> Args;
663 if (!checkLockFunAttrCommon(S, D, Attr, Args))
664 return;
665
666 unsigned Size = Args.size();
667 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000668 D->addAttr(::new (S.Context)
669 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
670 StartArg, Size,
671 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000672}
673
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000674static void handleAssertSharedLockAttr(Sema &S, Decl *D,
675 const AttributeList &Attr) {
676 SmallVector<Expr*, 1> Args;
677 if (!checkLockFunAttrCommon(S, D, Attr, Args))
678 return;
679
680 unsigned Size = Args.size();
681 Expr **StartArg = Size == 0 ? 0 : &Args[0];
682 D->addAttr(::new (S.Context)
683 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
684 Attr.getAttributeSpellingListIndex()));
685}
686
687static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
688 const AttributeList &Attr) {
689 SmallVector<Expr*, 1> Args;
690 if (!checkLockFunAttrCommon(S, D, Attr, Args))
691 return;
692
693 unsigned Size = Args.size();
694 Expr **StartArg = Size == 0 ? 0 : &Args[0];
695 D->addAttr(::new (S.Context)
696 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
697 StartArg, Size,
698 Attr.getAttributeSpellingListIndex()));
699}
700
701
Michael Hana9171bc2012-08-03 17:40:43 +0000702static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
703 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000704 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000706 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000707
Aaron Ballman00e99962013-08-31 01:11:41 +0000708 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000709 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000710 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000711 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000712 }
713
714 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000715 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000716
Michael Han3be3b442012-07-23 18:48:41 +0000717 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000718}
719
Michael Hana9171bc2012-08-03 17:40:43 +0000720static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000721 const AttributeList &Attr) {
722 SmallVector<Expr*, 2> Args;
723 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
724 return;
725
Michael Han99315932013-01-24 16:46:58 +0000726 D->addAttr(::new (S.Context)
727 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000728 Attr.getArgAsExpr(0),
729 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000730 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000731}
732
Michael Hana9171bc2012-08-03 17:40:43 +0000733static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000734 const AttributeList &Attr) {
735 SmallVector<Expr*, 2> Args;
736 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
737 return;
738
Michael Han99315932013-01-24 16:46:58 +0000739 D->addAttr(::new (S.Context)
740 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000741 Attr.getArgAsExpr(0),
742 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000743 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000744}
745
Michael Hana9171bc2012-08-03 17:40:43 +0000746static bool checkLocksRequiredCommon(Sema &S, Decl *D,
747 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000748 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000749 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000750 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000751
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000752 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000753 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000754 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000755 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000756
Michael Han3be3b442012-07-23 18:48:41 +0000757 return true;
758}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000759
Michael Hana9171bc2012-08-03 17:40:43 +0000760static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000761 const AttributeList &Attr) {
762 SmallVector<Expr*, 1> Args;
763 if (!checkLocksRequiredCommon(S, D, Attr, Args))
764 return;
765
766 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000767 D->addAttr(::new (S.Context)
768 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
769 StartArg, Args.size(),
770 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000771}
772
Michael Hana9171bc2012-08-03 17:40:43 +0000773static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000774 const AttributeList &Attr) {
775 SmallVector<Expr*, 1> Args;
776 if (!checkLocksRequiredCommon(S, D, Attr, Args))
777 return;
778
779 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000780 D->addAttr(::new (S.Context)
781 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
782 StartArg, Args.size(),
783 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784}
785
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000788 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000789 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000790 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000791 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000792 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000793 Expr **StartArg = Size == 0 ? 0 : &Args[0];
794
Michael Han99315932013-01-24 16:46:58 +0000795 D->addAttr(::new (S.Context)
796 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
797 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000798}
799
800static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000801 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000802 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000803 SmallVector<Expr*, 1> Args;
804 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
805 unsigned Size = Args.size();
806 if (Size == 0)
807 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000808
Michael Han99315932013-01-24 16:46:58 +0000809 D->addAttr(::new (S.Context)
810 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
811 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000812}
813
814static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000815 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000816 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000817 return;
818
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000819 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000820 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000821 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000822 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000823 if (Size == 0)
824 return;
825 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000826
Michael Han99315932013-01-24 16:46:58 +0000827 D->addAttr(::new (S.Context)
828 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
829 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000830}
831
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000832static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
833 Expr *Cond = Attr.getArgAsExpr(0);
834 if (!Cond->isTypeDependent()) {
835 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
836 if (Converted.isInvalid())
837 return;
838 Cond = Converted.take();
839 }
840
841 StringRef Msg;
842 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
843 return;
844
845 SmallVector<PartialDiagnosticAt, 8> Diags;
846 if (!Cond->isValueDependent() &&
847 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
848 Diags)) {
849 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
850 for (int I = 0, N = Diags.size(); I != N; ++I)
851 S.Diag(Diags[I].first, Diags[I].second);
852 return;
853 }
854
855 D->addAttr(::new (S.Context)
856 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
857 Attr.getAttributeSpellingListIndex()));
858}
859
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000860static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000861 ConsumableAttr::ConsumedState DefaultState;
862
863 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000864 IdentifierLoc *IL = Attr.getArgAsIdent(0);
865 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
866 DefaultState)) {
867 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
868 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000869 return;
870 }
David Blaikie16f76d22013-09-06 01:28:43 +0000871 } else {
872 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
873 << Attr.getName() << AANT_ArgumentIdentifier;
874 return;
875 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000876
877 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000878 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000879 Attr.getAttributeSpellingListIndex()));
880}
881
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000882
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000883static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
884 const AttributeList &Attr) {
885 ASTContext &CurrContext = S.getASTContext();
886 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
887
888 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
889 if (!RD->hasAttr<ConsumableAttr>()) {
890 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
891 RD->getNameAsString();
892
893 return false;
894 }
895 }
896
897 return true;
898}
899
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000900
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000901static void handleCallableWhenAttr(Sema &S, Decl *D,
902 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000903 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
904 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000905
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000906 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
907 return;
908
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000909 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
910 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
911 CallableWhenAttr::ConsumedState CallableState;
912
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000913 StringRef StateString;
914 SourceLocation Loc;
915 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
916 return;
917
918 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000919 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000920 S.Diag(Loc, diag::warn_attribute_type_not_supported)
921 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000922 return;
923 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000924
925 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000926 }
927
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000928 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000929 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
930 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000931}
932
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000933
DeLesley Hutchins69391772013-10-17 23:23:53 +0000934static void handleParamTypestateAttr(Sema &S, Decl *D,
935 const AttributeList &Attr) {
936 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000937
DeLesley Hutchins69391772013-10-17 23:23:53 +0000938 ParamTypestateAttr::ConsumedState ParamState;
939
940 if (Attr.isArgIdent(0)) {
941 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
942 StringRef StateString = Ident->Ident->getName();
943
944 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
945 ParamState)) {
946 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
947 << Attr.getName() << StateString;
948 return;
949 }
950 } else {
951 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
952 Attr.getName() << AANT_ArgumentIdentifier;
953 return;
954 }
955
956 // FIXME: This check is currently being done in the analysis. It can be
957 // enabled here only after the parser propagates attributes at
958 // template specialization definition, not declaration.
959 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
960 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
961 //
962 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
963 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
964 // ReturnType.getAsString();
965 // return;
966 //}
967
968 D->addAttr(::new (S.Context)
969 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
970 Attr.getAttributeSpellingListIndex()));
971}
972
973
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000974static void handleReturnTypestateAttr(Sema &S, Decl *D,
975 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000976 if (!checkAttributeNumArgs(S, Attr, 1)) return;
977
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000978 ReturnTypestateAttr::ConsumedState ReturnState;
979
980 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000981 IdentifierLoc *IL = Attr.getArgAsIdent(0);
982 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
983 ReturnState)) {
984 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
985 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000986 return;
987 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000988 } else {
989 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
990 Attr.getName() << AANT_ArgumentIdentifier;
991 return;
992 }
993
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000994 // FIXME: This check is currently being done in the analysis. It can be
995 // enabled here only after the parser propagates attributes at
996 // template specialization definition, not declaration.
997 //QualType ReturnType;
998 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000999 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1000 // ReturnType = Param->getType();
1001 //
1002 //} else if (const CXXConstructorDecl *Constructor =
1003 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001004 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1005 //
1006 //} else {
1007 //
1008 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1009 //}
1010 //
1011 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1012 //
1013 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1014 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1015 // ReturnType.getAsString();
1016 // return;
1017 //}
1018
1019 D->addAttr(::new (S.Context)
1020 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1021 Attr.getAttributeSpellingListIndex()));
1022}
1023
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001024
1025static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001026 if (!checkAttributeNumArgs(S, Attr, 1))
1027 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001028
1029 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1030 return;
1031
1032 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001033 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001034 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1035 StringRef Param = Ident->Ident->getName();
1036 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1037 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1038 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001039 return;
1040 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 } else {
1042 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1043 Attr.getName() << AANT_ArgumentIdentifier;
1044 return;
1045 }
1046
1047 D->addAttr(::new (S.Context)
1048 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1049 Attr.getAttributeSpellingListIndex()));
1050}
1051
Chris Wailes9385f9f2013-10-29 20:28:41 +00001052static void handleTestTypestateAttr(Sema &S, Decl *D,
1053 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001054 if (!checkAttributeNumArgs(S, Attr, 1))
1055 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001056
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001057 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1058 return;
1059
Chris Wailes9385f9f2013-10-29 20:28:41 +00001060 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001061 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001062 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1063 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001064 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001065 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1066 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001067 return;
1068 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001069 } else {
1070 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1071 Attr.getName() << AANT_ArgumentIdentifier;
1072 return;
1073 }
1074
1075 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001076 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001077 Attr.getAttributeSpellingListIndex()));
1078}
1079
Chandler Carruthedc2c642011-07-02 00:01:44 +00001080static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1081 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001082 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001083 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001084}
1085
Chandler Carruthedc2c642011-07-02 00:01:44 +00001086static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001087 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001088 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1089 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001090 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001091 // If the alignment is less than or equal to 8 bits, the packed attribute
1092 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001093 if (!FD->getType()->isDependentType() &&
1094 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001095 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001096 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001097 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001098 else
Michael Han99315932013-01-24 16:46:58 +00001099 FD->addAttr(::new (S.Context)
1100 PackedAttr(Attr.getRange(), S.Context,
1101 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001102 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001103 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001104}
1105
Ted Kremenek7fd17232011-09-29 07:02:25 +00001106static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1107 // The IBOutlet/IBOutletCollection attributes only apply to instance
1108 // variables or properties of Objective-C classes. The outlet must also
1109 // have an object reference type.
1110 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1111 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001112 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001113 << Attr.getName() << VD->getType() << 0;
1114 return false;
1115 }
1116 }
1117 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1118 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001119 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001120 << Attr.getName() << PD->getType() << 1;
1121 return false;
1122 }
1123 }
1124 else {
1125 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1126 return false;
1127 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001128
Ted Kremenek7fd17232011-09-29 07:02:25 +00001129 return true;
1130}
1131
Chandler Carruthedc2c642011-07-02 00:01:44 +00001132static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001133 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001134 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001135
Michael Han99315932013-01-24 16:46:58 +00001136 D->addAttr(::new (S.Context)
1137 IBOutletAttr(Attr.getRange(), S.Context,
1138 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001139}
1140
Chandler Carruthedc2c642011-07-02 00:01:44 +00001141static void handleIBOutletCollection(Sema &S, Decl *D,
1142 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001143
1144 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001145 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001146 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1147 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001148 return;
1149 }
1150
Ted Kremenek7fd17232011-09-29 07:02:25 +00001151 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001152 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001153
Richard Smithb1f9a282013-10-31 01:56:18 +00001154 ParsedType PT;
1155
1156 if (Attr.hasParsedType())
1157 PT = Attr.getTypeArg();
1158 else {
1159 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1160 S.getScopeForContext(D->getDeclContext()->getParent()));
1161 if (!PT) {
1162 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1163 return;
1164 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001165 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001166
Richard Smithb87c4652013-10-31 21:23:20 +00001167 TypeSourceInfo *QTLoc = 0;
1168 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1169 if (!QTLoc)
1170 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001171
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001172 // Diagnose use of non-object type in iboutletcollection attribute.
1173 // FIXME. Gnu attribute extension ignores use of builtin types in
1174 // attributes. So, __attribute__((iboutletcollection(char))) will be
1175 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001176 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001177 S.Diag(Attr.getLoc(),
1178 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1179 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001180 return;
1181 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001182
Michael Han99315932013-01-24 16:46:58 +00001183 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001184 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001185 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001186}
1187
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001188static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001189 if (const RecordType *UT = T->getAsUnionType())
1190 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1191 RecordDecl *UD = UT->getDecl();
1192 for (RecordDecl::field_iterator it = UD->field_begin(),
1193 itend = UD->field_end(); it != itend; ++it) {
1194 QualType QT = it->getType();
1195 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1196 T = QT;
1197 return;
1198 }
1199 }
1200 }
1201}
1202
Chandler Carruthedc2c642011-07-02 00:01:44 +00001203static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001204 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1205 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001206 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001207 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001208 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001209 return;
1210 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001211
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001212 SmallVector<unsigned, 8> NonNullArgs;
1213 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001214 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001215 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001216 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001217 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001218
1219 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001220 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001221 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001222
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001223 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001224 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001225 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1226 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001227 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001229
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001230 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001231 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001232
1233 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1234 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001235 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001236 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1237 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001238 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001239 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001240 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001241 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001242
Ted Kremenek22813f42010-10-21 18:49:36 +00001243 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001244 if (NonNullArgs.empty()) {
1245 // Warn the trivial case only if attribute is not coming from a
1246 // macro instantiation.
1247 if (Attr.getLoc().isFileID())
1248 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001249 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001250 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001251 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001252
Nick Lewyckye1121512013-01-24 01:12:16 +00001253 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001254 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001256 D->addAttr(::new (S.Context)
1257 NonNullAttr(Attr.getRange(), S.Context, start, size,
1258 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001259}
1260
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001261static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1262 switch (K) {
1263 case OwnershipAttr::Holds: return "'ownership_holds'";
1264 case OwnershipAttr::Takes: return "'ownership_takes'";
1265 case OwnershipAttr::Returns: return "'ownership_returns'";
1266 }
1267 llvm_unreachable("unknown ownership");
1268}
1269
Chandler Carruthedc2c642011-07-02 00:01:44 +00001270static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001271 // This attribute must be applied to a function declaration. The first
1272 // argument to the attribute must be an identifier, the name of the resource,
1273 // for example: malloc. The following arguments must be argument indexes, the
1274 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001275 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001276 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001277 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001278
Aaron Ballman00e99962013-08-31 01:11:41 +00001279 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001280 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001281 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001282 return;
1283 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001284
Richard Smith852e9ce2013-11-27 01:46:48 +00001285 // Figure out our Kind.
1286 OwnershipAttr::OwnershipKind K =
1287 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1288 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001289
Richard Smith852e9ce2013-11-27 01:46:48 +00001290 // Check arguments.
1291 switch (K) {
1292 case OwnershipAttr::Takes:
1293 case OwnershipAttr::Holds:
1294 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001295 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1296 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001297 return;
1298 }
1299 break;
1300 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001301 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001302 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1303 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001304 return;
1305 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001306 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001307 }
1308
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001309 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001310 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1311 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001312 return;
1313 }
1314
Richard Smith852e9ce2013-11-27 01:46:48 +00001315 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001316
1317 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001318 StringRef ModuleName = Module->getName();
1319 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1320 ModuleName.size() > 4) {
1321 ModuleName = ModuleName.drop_front(2).drop_back(2);
1322 Module = &S.PP.getIdentifierTable().get(ModuleName);
1323 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001324
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001325 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001326 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1327 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001328 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001329 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001330 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001331
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001332 // Is the function argument a pointer type?
1333 QualType T = getFunctionOrMethodArgType(D, Idx);
1334 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001335 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001336 case OwnershipAttr::Takes:
1337 case OwnershipAttr::Holds:
1338 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1339 Err = 0;
1340 break;
1341 case OwnershipAttr::Returns:
1342 if (!T->isIntegerType())
1343 Err = 1;
1344 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001345 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001346 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001347 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001348 << Ex->getSourceRange();
1349 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001350 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001351
1352 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001353 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001354 i = D->specific_attr_begin<OwnershipAttr>(),
1355 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001356 // FIXME: A returns attribute should conflict with any returns attribute
1357 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001358 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1359 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1360 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1361 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1362 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363 }
1364 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001365 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001366 }
1367
1368 unsigned* start = OwnershipArgs.data();
1369 unsigned size = OwnershipArgs.size();
1370 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001371
Michael Han99315932013-01-24 16:46:58 +00001372 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001373 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001374 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001375}
1376
Chandler Carruthedc2c642011-07-02 00:01:44 +00001377static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001378 // Check the attribute arguments.
1379 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1381 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001382 return;
1383 }
1384
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001385 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001386
Rafael Espindolac18086a2010-02-23 22:00:30 +00001387 // gcc rejects
1388 // class c {
1389 // static int a __attribute__((weakref ("v2")));
1390 // static int b() __attribute__((weakref ("f3")));
1391 // };
1392 // and ignores the attributes of
1393 // void f(void) {
1394 // static int a __attribute__((weakref ("v2")));
1395 // }
1396 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001397 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001398 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001399 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1400 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001401 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001402 }
1403
1404 // The GCC manual says
1405 //
1406 // At present, a declaration to which `weakref' is attached can only
1407 // be `static'.
1408 //
1409 // It also says
1410 //
1411 // Without a TARGET,
1412 // given as an argument to `weakref' or to `alias', `weakref' is
1413 // equivalent to `weak'.
1414 //
1415 // gcc 4.4.1 will accept
1416 // int a7 __attribute__((weakref));
1417 // as
1418 // int a7 __attribute__((weak));
1419 // This looks like a bug in gcc. We reject that for now. We should revisit
1420 // it if this behaviour is actually used.
1421
Rafael Espindolac18086a2010-02-23 22:00:30 +00001422 // GCC rejects
1423 // static ((alias ("y"), weakref)).
1424 // Should we? How to check that weakref is before or after alias?
1425
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001426 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1427 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1428 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001429 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001430 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001431 // GCC will accept anything as the argument of weakref. Should we
1432 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001433 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1434 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001435
Michael Han99315932013-01-24 16:46:58 +00001436 D->addAttr(::new (S.Context)
1437 WeakRefAttr(Attr.getRange(), S.Context,
1438 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001439}
1440
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001441static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1442 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001443 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001444 return;
1445
Douglas Gregore8bbc122011-09-02 00:18:52 +00001446 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001447 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1448 return;
1449 }
1450
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001451 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001452
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001453 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001454 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001455}
1456
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001457static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001458 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001459 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001460
Michael Han99315932013-01-24 16:46:58 +00001461 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1462 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001463}
1464
1465static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001466 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001467 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001468
Michael Han99315932013-01-24 16:46:58 +00001469 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1470 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001471}
1472
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001473static void handleTLSModelAttr(Sema &S, Decl *D,
1474 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001475 StringRef Model;
1476 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001477 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001478 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001479 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001480
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001481 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001482 if (Model != "global-dynamic" && Model != "local-dynamic"
1483 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001484 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001485 return;
1486 }
1487
Michael Han99315932013-01-24 16:46:58 +00001488 D->addAttr(::new (S.Context)
1489 TLSModelAttr(Attr.getRange(), S.Context, Model,
1490 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001491}
1492
Chandler Carruthedc2c642011-07-02 00:01:44 +00001493static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001494 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001495 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001496 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001497 D->addAttr(::new (S.Context)
1498 MallocAttr(Attr.getRange(), S.Context,
1499 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001500 return;
1501 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001502 }
1503
Ted Kremenek08479ae2009-08-15 00:51:46 +00001504 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001505}
1506
Chandler Carruthedc2c642011-07-02 00:01:44 +00001507static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001508 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001509 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1510 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001511 return;
1512 }
1513
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001514 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1515 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001516}
1517
Chandler Carruthedc2c642011-07-02 00:01:44 +00001518static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001519 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001520
1521 if (S.CheckNoReturnAttr(attr)) return;
1522
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001523 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001524 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001525 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001526 return;
1527 }
1528
Michael Han99315932013-01-24 16:46:58 +00001529 D->addAttr(::new (S.Context)
1530 NoReturnAttr(attr.getRange(), S.Context,
1531 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001532}
1533
1534bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001535 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001536 attr.setInvalid();
1537 return true;
1538 }
1539
1540 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001541}
1542
Chandler Carruthedc2c642011-07-02 00:01:44 +00001543static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1544 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001545
1546 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1547 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001548 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1549 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001550 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1551 && !VD->getType()->isFunctionPointerType())) {
1552 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001553 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001554 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001555 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001556 return;
1557 }
1558 }
1559
Michael Han99315932013-01-24 16:46:58 +00001560 D->addAttr(::new (S.Context)
1561 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1562 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001563}
1564
John Thompsoncdb847ba2010-08-09 21:53:52 +00001565// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001566static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001567/*
1568 Returning a Vector Class in Registers
1569
Eric Christopherbc638a82010-12-01 22:13:54 +00001570 According to the PPU ABI specifications, a class with a single member of
1571 vector type is returned in memory when used as the return value of a function.
1572 This results in inefficient code when implementing vector classes. To return
1573 the value in a single vector register, add the vecreturn attribute to the
1574 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001575
1576 Example:
1577
1578 struct Vector
1579 {
1580 __vector float xyzw;
1581 } __attribute__((vecreturn));
1582
1583 Vector Add(Vector lhs, Vector rhs)
1584 {
1585 Vector result;
1586 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1587 return result; // This will be returned in a register
1588 }
1589*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001590 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1591 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001592 return;
1593 }
1594
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001595 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001596 int count = 0;
1597
1598 if (!isa<CXXRecordDecl>(record)) {
1599 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1600 return;
1601 }
1602
1603 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1604 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1605 return;
1606 }
1607
Eric Christopherbc638a82010-12-01 22:13:54 +00001608 for (RecordDecl::field_iterator iter = record->field_begin();
1609 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001610 if ((count == 1) || !iter->getType()->isVectorType()) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1612 return;
1613 }
1614 count++;
1615 }
1616
Michael Han99315932013-01-24 16:46:58 +00001617 D->addAttr(::new (S.Context)
1618 VecReturnAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001620}
1621
Richard Smithe233fbf2013-01-28 22:42:45 +00001622static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1623 const AttributeList &Attr) {
1624 if (isa<ParmVarDecl>(D)) {
1625 // [[carries_dependency]] can only be applied to a parameter if it is a
1626 // parameter of a function declaration or lambda.
1627 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1628 S.Diag(Attr.getLoc(),
1629 diag::err_carries_dependency_param_not_function_decl);
1630 return;
1631 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001632 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001633
1634 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1635 Attr.getRange(), S.Context,
1636 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001637}
1638
Chandler Carruthedc2c642011-07-02 00:01:44 +00001639static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001640 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001641 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001643 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001644 return;
1645 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001646
Michael Han99315932013-01-24 16:46:58 +00001647 D->addAttr(::new (S.Context)
1648 UnusedAttr(Attr.getRange(), S.Context,
1649 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001650}
1651
Chandler Carruthedc2c642011-07-02 00:01:44 +00001652static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001653 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001654 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001656 return;
1657 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001658 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001660 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001661 return;
1662 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001663
Michael Han99315932013-01-24 16:46:58 +00001664 D->addAttr(::new (S.Context)
1665 UsedAttr(Attr.getRange(), S.Context,
1666 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001667}
1668
Chandler Carruthedc2c642011-07-02 00:01:44 +00001669static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001670 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001671 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001672 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1673 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001674 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001675 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001676
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001677 uint32_t priority = ConstructorAttr::DefaultPriority;
1678 if (Attr.getNumArgs() > 0 &&
1679 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1680 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001681
Michael Han99315932013-01-24 16:46:58 +00001682 D->addAttr(::new (S.Context)
1683 ConstructorAttr(Attr.getRange(), S.Context, priority,
1684 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001685}
1686
Chandler Carruthedc2c642011-07-02 00:01:44 +00001687static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001688 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001689 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001690 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1691 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001692 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001693 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001694
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001695 uint32_t priority = ConstructorAttr::DefaultPriority;
1696 if (Attr.getNumArgs() > 0 &&
1697 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1698 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001699
Michael Han99315932013-01-24 16:46:58 +00001700 D->addAttr(::new (S.Context)
1701 DestructorAttr(Attr.getRange(), S.Context, priority,
1702 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001703}
1704
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001705template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001706static void handleAttrWithMessage(Sema &S, Decl *D,
1707 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001708 unsigned NumArgs = Attr.getNumArgs();
1709 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001710 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1711 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001712 return;
1713 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001714
1715 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001716 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001717 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001718 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001719
Michael Han99315932013-01-24 16:46:58 +00001720 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1721 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001722}
1723
Ted Kremenek28eace62013-11-23 01:01:34 +00001724static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1725 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001726 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001727 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1728 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001729}
1730
Jordy Rose740b0c22012-05-08 03:27:22 +00001731static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1732 IdentifierInfo *Platform,
1733 VersionTuple Introduced,
1734 VersionTuple Deprecated,
1735 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001736 StringRef PlatformName
1737 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1738 if (PlatformName.empty())
1739 PlatformName = Platform->getName();
1740
1741 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1742 // of these steps are needed).
1743 if (!Introduced.empty() && !Deprecated.empty() &&
1744 !(Introduced <= Deprecated)) {
1745 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1746 << 1 << PlatformName << Deprecated.getAsString()
1747 << 0 << Introduced.getAsString();
1748 return true;
1749 }
1750
1751 if (!Introduced.empty() && !Obsoleted.empty() &&
1752 !(Introduced <= Obsoleted)) {
1753 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1754 << 2 << PlatformName << Obsoleted.getAsString()
1755 << 0 << Introduced.getAsString();
1756 return true;
1757 }
1758
1759 if (!Deprecated.empty() && !Obsoleted.empty() &&
1760 !(Deprecated <= Obsoleted)) {
1761 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1762 << 2 << PlatformName << Obsoleted.getAsString()
1763 << 1 << Deprecated.getAsString();
1764 return true;
1765 }
1766
1767 return false;
1768}
1769
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001770/// \brief Check whether the two versions match.
1771///
1772/// If either version tuple is empty, then they are assumed to match. If
1773/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1774static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1775 bool BeforeIsOkay) {
1776 if (X.empty() || Y.empty())
1777 return true;
1778
1779 if (X == Y)
1780 return true;
1781
1782 if (BeforeIsOkay && X < Y)
1783 return true;
1784
1785 return false;
1786}
1787
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001788AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001789 IdentifierInfo *Platform,
1790 VersionTuple Introduced,
1791 VersionTuple Deprecated,
1792 VersionTuple Obsoleted,
1793 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001794 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001795 bool Override,
1796 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001797 VersionTuple MergedIntroduced = Introduced;
1798 VersionTuple MergedDeprecated = Deprecated;
1799 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001800 bool FoundAny = false;
1801
Rafael Espindolac67f2232012-05-10 02:50:16 +00001802 if (D->hasAttrs()) {
1803 AttrVec &Attrs = D->getAttrs();
1804 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1805 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1806 if (!OldAA) {
1807 ++i;
1808 continue;
1809 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001810
Rafael Espindolac67f2232012-05-10 02:50:16 +00001811 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1812 if (OldPlatform != Platform) {
1813 ++i;
1814 continue;
1815 }
1816
1817 FoundAny = true;
1818 VersionTuple OldIntroduced = OldAA->getIntroduced();
1819 VersionTuple OldDeprecated = OldAA->getDeprecated();
1820 VersionTuple OldObsoleted = OldAA->getObsoleted();
1821 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001822
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001823 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1824 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1825 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1826 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001827 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001828 if (Override) {
1829 int Which = -1;
1830 VersionTuple FirstVersion;
1831 VersionTuple SecondVersion;
1832 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1833 Which = 0;
1834 FirstVersion = OldIntroduced;
1835 SecondVersion = Introduced;
1836 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1837 Which = 1;
1838 FirstVersion = Deprecated;
1839 SecondVersion = OldDeprecated;
1840 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1841 Which = 2;
1842 FirstVersion = Obsoleted;
1843 SecondVersion = OldObsoleted;
1844 }
1845
1846 if (Which == -1) {
1847 Diag(OldAA->getLocation(),
1848 diag::warn_mismatched_availability_override_unavail)
1849 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1850 } else {
1851 Diag(OldAA->getLocation(),
1852 diag::warn_mismatched_availability_override)
1853 << Which
1854 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1855 << FirstVersion.getAsString() << SecondVersion.getAsString();
1856 }
1857 Diag(Range.getBegin(), diag::note_overridden_method);
1858 } else {
1859 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1860 Diag(Range.getBegin(), diag::note_previous_attribute);
1861 }
1862
Rafael Espindolac67f2232012-05-10 02:50:16 +00001863 Attrs.erase(Attrs.begin() + i);
1864 --e;
1865 continue;
1866 }
1867
1868 VersionTuple MergedIntroduced2 = MergedIntroduced;
1869 VersionTuple MergedDeprecated2 = MergedDeprecated;
1870 VersionTuple MergedObsoleted2 = MergedObsoleted;
1871
1872 if (MergedIntroduced2.empty())
1873 MergedIntroduced2 = OldIntroduced;
1874 if (MergedDeprecated2.empty())
1875 MergedDeprecated2 = OldDeprecated;
1876 if (MergedObsoleted2.empty())
1877 MergedObsoleted2 = OldObsoleted;
1878
1879 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1880 MergedIntroduced2, MergedDeprecated2,
1881 MergedObsoleted2)) {
1882 Attrs.erase(Attrs.begin() + i);
1883 --e;
1884 continue;
1885 }
1886
1887 MergedIntroduced = MergedIntroduced2;
1888 MergedDeprecated = MergedDeprecated2;
1889 MergedObsoleted = MergedObsoleted2;
1890 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001891 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001892 }
1893
1894 if (FoundAny &&
1895 MergedIntroduced == Introduced &&
1896 MergedDeprecated == Deprecated &&
1897 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001898 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001899
Ted Kremenekb5445722013-04-06 00:34:27 +00001900 // Only create a new attribute if !Override, but we want to do
1901 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001902 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001903 MergedDeprecated, MergedObsoleted) &&
1904 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001905 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1906 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001907 Obsoleted, IsUnavailable, Message,
1908 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001909 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001910 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001911}
1912
Chandler Carruthedc2c642011-07-02 00:01:44 +00001913static void handleAvailabilityAttr(Sema &S, Decl *D,
1914 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001915 if (!checkAttributeNumArgs(S, Attr, 1))
1916 return;
1917 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001918 unsigned Index = Attr.getAttributeSpellingListIndex();
1919
Aaron Ballman00e99962013-08-31 01:11:41 +00001920 IdentifierInfo *II = Platform->Ident;
1921 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1922 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1923 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001924
Rafael Espindolac231fab2013-01-08 21:30:32 +00001925 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1926 if (!ND) {
1927 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1928 return;
1929 }
1930
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001931 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1932 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1933 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001934 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001935 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001936 if (const StringLiteral *SE =
1937 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001938 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001939
Aaron Ballman00e99962013-08-31 01:11:41 +00001940 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001941 Introduced.Version,
1942 Deprecated.Version,
1943 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001944 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001945 /*Override=*/false,
1946 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001947 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001948 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001949}
1950
John McCalld041a9b2013-02-20 01:54:26 +00001951template <class T>
1952static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1953 typename T::VisibilityType value,
1954 unsigned attrSpellingListIndex) {
1955 T *existingAttr = D->getAttr<T>();
1956 if (existingAttr) {
1957 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1958 if (existingValue == value)
1959 return NULL;
1960 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1961 S.Diag(range.getBegin(), diag::note_previous_attribute);
1962 D->dropAttr<T>();
1963 }
1964 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1965}
1966
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001967VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001968 VisibilityAttr::VisibilityType Vis,
1969 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001970 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1971 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001972}
1973
John McCalld041a9b2013-02-20 01:54:26 +00001974TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1975 TypeVisibilityAttr::VisibilityType Vis,
1976 unsigned AttrSpellingListIndex) {
1977 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1978 AttrSpellingListIndex);
1979}
1980
1981static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1982 bool isTypeVisibility) {
1983 // Visibility attributes don't mean anything on a typedef.
1984 if (isa<TypedefNameDecl>(D)) {
1985 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1986 << Attr.getName();
1987 return;
1988 }
1989
1990 // 'type_visibility' can only go on a type or namespace.
1991 if (isTypeVisibility &&
1992 !(isa<TagDecl>(D) ||
1993 isa<ObjCInterfaceDecl>(D) ||
1994 isa<NamespaceDecl>(D))) {
1995 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1996 << Attr.getName() << ExpectedTypeOrNamespace;
1997 return;
1998 }
1999
Benjamin Kramer70370212013-09-09 15:08:57 +00002000 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002001 StringRef TypeStr;
2002 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002003 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002004 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002005
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002006 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002007 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002008 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002009 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002010 return;
2011 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002012
2013 // Complain about attempts to use protected visibility on targets
2014 // (like Darwin) that don't support it.
2015 if (type == VisibilityAttr::Protected &&
2016 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2017 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2018 type = VisibilityAttr::Default;
2019 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002020
Michael Han99315932013-01-24 16:46:58 +00002021 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002022 clang::Attr *newAttr;
2023 if (isTypeVisibility) {
2024 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2025 (TypeVisibilityAttr::VisibilityType) type,
2026 Index);
2027 } else {
2028 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2029 }
2030 if (newAttr)
2031 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002032}
2033
Chandler Carruthedc2c642011-07-02 00:01:44 +00002034static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2035 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002036 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002037 if (!Attr.isArgIdent(0)) {
2038 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2039 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002040 return;
2041 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002042
Aaron Ballman682ee422013-09-11 19:47:58 +00002043 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2044 ObjCMethodFamilyAttr::FamilyKind F;
2045 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2046 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2047 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002048 return;
2049 }
2050
Aaron Ballman682ee422013-09-11 19:47:58 +00002051 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002052 !method->getResultType()->isObjCObjectPointerType()) {
2053 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2054 << method->getResultType();
2055 // Ignore the attribute.
2056 return;
2057 }
2058
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002059 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002060 S.Context, F,
2061 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002062}
2063
Chandler Carruthedc2c642011-07-02 00:01:44 +00002064static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002065 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002066 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002067 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002068 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2069 return;
2070 }
2071 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002072 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2073 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002074 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002075 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2076 return;
2077 }
2078 }
2079 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002080 // It is okay to include this attribute on properties, e.g.:
2081 //
2082 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2083 //
2084 // In this case it follows tradition and suppresses an error in the above
2085 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002086 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002087 }
Michael Han99315932013-01-24 16:46:58 +00002088 D->addAttr(::new (S.Context)
2089 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2090 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002091}
2092
Chandler Carruthedc2c642011-07-02 00:01:44 +00002093static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002094 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002095 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002096 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002097 return;
2098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002099
Aaron Ballman00e99962013-08-31 01:11:41 +00002100 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002101 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002102 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2103 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2104 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002105 return;
2106 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002107
Michael Han99315932013-01-24 16:46:58 +00002108 D->addAttr(::new (S.Context)
2109 BlocksAttr(Attr.getRange(), S.Context, type,
2110 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002111}
2112
Chandler Carruthedc2c642011-07-02 00:01:44 +00002113static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002114 // check the attribute arguments.
2115 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002116 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2117 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002118 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002119 }
2120
Aaron Ballman18a78382013-11-21 00:28:23 +00002121 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002122 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002123 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002124 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002125 if (E->isTypeDependent() || E->isValueDependent() ||
2126 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002127 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002128 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002129 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002130 return;
2131 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002132
John McCallb46f2872011-09-09 07:56:05 +00002133 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002134 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2135 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 return;
2137 }
John McCallb46f2872011-09-09 07:56:05 +00002138
2139 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002140 }
2141
Aaron Ballman18a78382013-11-21 00:28:23 +00002142 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002143 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002144 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002145 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002146 if (E->isTypeDependent() || E->isValueDependent() ||
2147 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002148 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002149 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002150 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002151 return;
2152 }
2153 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002154
John McCallb46f2872011-09-09 07:56:05 +00002155 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002156 // FIXME: This error message could be improved, it would be nice
2157 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002158 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2159 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002160 return;
2161 }
2162 }
2163
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002164 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002165 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002166 if (isa<FunctionNoProtoType>(FT)) {
2167 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2168 return;
2169 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002170
Chris Lattner9363e312009-03-17 23:03:47 +00002171 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002172 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002173 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002174 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002175 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002176 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002177 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002178 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002179 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002180 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2181 if (!BD->isVariadic()) {
2182 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2183 return;
2184 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002185 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002186 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002187 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002188 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002189 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002190 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002191 int m = Ty->isFunctionPointerType() ? 0 : 1;
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002193 return;
2194 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002195 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002197 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002198 return;
2199 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002200 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002202 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002203 return;
2204 }
Michael Han99315932013-01-24 16:46:58 +00002205 D->addAttr(::new (S.Context)
2206 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2207 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002208}
2209
Chandler Carruthedc2c642011-07-02 00:01:44 +00002210static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002211 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002212 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002213 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002214 return;
2215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002216
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002217 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2218 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2219 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002220 return;
2221 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002222 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2223 if (MD->getResultType()->isVoidType()) {
2224 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2225 << Attr.getName() << 1;
2226 return;
2227 }
2228
Michael Han99315932013-01-24 16:46:58 +00002229 D->addAttr(::new (S.Context)
2230 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2231 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002232}
2233
Chandler Carruthedc2c642011-07-02 00:01:44 +00002234static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002235 // weak_import only applies to variable & function declarations.
2236 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002237 if (!D->canBeWeakImported(isDef)) {
2238 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002239 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2240 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002241 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002242 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002243 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002244 // Nothing to warn about here.
2245 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002247 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002248
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002249 return;
2250 }
2251
Michael Han99315932013-01-24 16:46:58 +00002252 D->addAttr(::new (S.Context)
2253 WeakImportAttr(Attr.getRange(), S.Context,
2254 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002255}
2256
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002257// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002258template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002259static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002260 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002261 uint32_t WGSize[3];
2262 for (unsigned i = 0; i < 3; ++i)
2263 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002264 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002265
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002266 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2267 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2268 Existing->getYDim() == WGSize[1] &&
2269 Existing->getZDim() == WGSize[2]))
2270 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002271
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002272 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2273 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002274 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002275}
2276
Joey Goulyaba589c2013-03-08 09:42:32 +00002277static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002278 if (!Attr.hasParsedType()) {
2279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2280 << Attr.getName() << 1;
2281 return;
2282 }
2283
Richard Smithb87c4652013-10-31 21:23:20 +00002284 TypeSourceInfo *ParmTSI = 0;
2285 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2286 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002287
2288 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2289 (ParmType->isBooleanType() ||
2290 !ParmType->isIntegralType(S.getASTContext()))) {
2291 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2292 << ParmType;
2293 return;
2294 }
2295
Aaron Ballmana9e05402013-12-02 22:16:55 +00002296 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002297 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002298 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2299 return;
2300 }
2301 }
2302
2303 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002304 ParmTSI,
2305 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002306}
2307
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002308SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002309 StringRef Name,
2310 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002311 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2312 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002313 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002314 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2315 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002316 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002317 }
Michael Han99315932013-01-24 16:46:58 +00002318 return ::new (Context) SectionAttr(Range, Context, Name,
2319 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002320}
2321
Chandler Carruthedc2c642011-07-02 00:01:44 +00002322static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002323 // Make sure that there is a string literal as the sections's single
2324 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002325 StringRef Str;
2326 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002327 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002328 return;
Mike Stump11289f42009-09-09 15:08:12 +00002329
Chris Lattner30ba6742009-08-10 19:03:04 +00002330 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002331 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002332 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002333 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002334 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002335 return;
2336 }
Mike Stump11289f42009-09-09 15:08:12 +00002337
Michael Han99315932013-01-24 16:46:58 +00002338 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002339 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002340 if (NewAttr)
2341 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002342}
2343
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002344
Chandler Carruthedc2c642011-07-02 00:01:44 +00002345static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002346 VarDecl *VD = cast<VarDecl>(D);
2347 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002348 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002349 return;
2350 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002351
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002352 Expr *E = Attr.getArgAsExpr(0);
2353 SourceLocation Loc = E->getExprLoc();
2354 FunctionDecl *FD = 0;
2355 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002356
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002357 // gcc only allows for simple identifiers. Since we support more than gcc, we
2358 // will warn the user.
2359 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2360 if (DRE->hasQualifier())
2361 S.Diag(Loc, diag::warn_cleanup_ext);
2362 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2363 NI = DRE->getNameInfo();
2364 if (!FD) {
2365 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2366 << NI.getName();
2367 return;
2368 }
2369 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2370 if (ULE->hasExplicitTemplateArgs())
2371 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002372 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2373 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002374 if (!FD) {
2375 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2376 << NI.getName();
2377 if (ULE->getType() == S.Context.OverloadTy)
2378 S.NoteAllOverloadCandidates(ULE);
2379 return;
2380 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002381 } else {
2382 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002383 return;
2384 }
2385
Anders Carlssond277d792009-01-31 01:16:18 +00002386 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002387 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2388 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002389 return;
2390 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002391
Anders Carlsson723f55d2009-02-07 23:16:50 +00002392 // We're currently more strict than GCC about what function types we accept.
2393 // If this ever proves to be a problem it should be easy to fix.
2394 QualType Ty = S.Context.getPointerType(VD->getType());
2395 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002396 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2397 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002398 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2399 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002400 return;
2401 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002402
Michael Han99315932013-01-24 16:46:58 +00002403 D->addAttr(::new (S.Context)
2404 CleanupAttr(Attr.getRange(), S.Context, FD,
2405 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002406}
2407
Mike Stumpd3bb5572009-07-24 19:02:52 +00002408/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002409/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002410static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002411 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002413 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002414 return;
2415 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002416
Aaron Ballman00e99962013-08-31 01:11:41 +00002417 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002418 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002419 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002420 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002421
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002422 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002423 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002424
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002425 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2426 if (not_nsstring_type &&
2427 !isCFStringType(Ty, S.Context) &&
2428 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002429 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002430 // FIXME: Should highlight the actual expression that has the wrong type.
2431 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002432 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002433 << IdxExpr->getSourceRange();
2434 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002435 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002436 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002437 if (!isNSStringType(Ty, S.Context) &&
2438 !isCFStringType(Ty, S.Context) &&
2439 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002440 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002441 // FIXME: Should highlight the actual expression that has the wrong type.
2442 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002443 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002444 << IdxExpr->getSourceRange();
2445 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002446 }
2447
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002448 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2449 // because that has corrected for the implicit this parameter, and is zero-
2450 // based. The attribute expects what the user wrote explicitly.
2451 llvm::APSInt Val;
2452 IdxExpr->EvaluateAsInt(Val, S.Context);
2453
Michael Han99315932013-01-24 16:46:58 +00002454 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002455 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002456 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002457}
2458
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002459enum FormatAttrKind {
2460 CFStringFormat,
2461 NSStringFormat,
2462 StrftimeFormat,
2463 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002464 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002465 InvalidFormat
2466};
2467
2468/// getFormatAttrKind - Map from format attribute names to supported format
2469/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002470static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002471 return llvm::StringSwitch<FormatAttrKind>(Format)
2472 // Check for formats that get handled specially.
2473 .Case("NSString", NSStringFormat)
2474 .Case("CFString", CFStringFormat)
2475 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002476
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002477 // Otherwise, check for supported formats.
2478 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2479 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2480 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002481
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002482 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2483 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002484}
2485
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002486/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002487/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002488static void handleInitPriorityAttr(Sema &S, Decl *D,
2489 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002490 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002491 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2492 return;
2493 }
2494
Aaron Ballman4a611152013-11-27 16:34:09 +00002495 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002496 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2497 Attr.setInvalid();
2498 return;
2499 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002500 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002501 if (S.Context.getAsArrayType(T))
2502 T = S.Context.getBaseElementType(T);
2503 if (!T->getAs<RecordType>()) {
2504 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2505 Attr.setInvalid();
2506 return;
2507 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002508
2509 Expr *E = Attr.getArgAsExpr(0);
2510 uint32_t prioritynum;
2511 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002512 Attr.setInvalid();
2513 return;
2514 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002515
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002516 if (prioritynum < 101 || prioritynum > 65535) {
2517 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002518 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002519 Attr.setInvalid();
2520 return;
2521 }
Michael Han99315932013-01-24 16:46:58 +00002522 D->addAttr(::new (S.Context)
2523 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2524 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002525}
2526
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002527FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2528 IdentifierInfo *Format, int FormatIdx,
2529 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002530 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002531 // Check whether we already have an equivalent format attribute.
2532 for (specific_attr_iterator<FormatAttr>
2533 i = D->specific_attr_begin<FormatAttr>(),
2534 e = D->specific_attr_end<FormatAttr>();
2535 i != e ; ++i) {
2536 FormatAttr *f = *i;
2537 if (f->getType() == Format &&
2538 f->getFormatIdx() == FormatIdx &&
2539 f->getFirstArg() == FirstArg) {
2540 // If we don't have a valid location for this attribute, adopt the
2541 // location.
2542 if (f->getLocation().isInvalid())
2543 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002544 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002545 }
2546 }
2547
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002548 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2549 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002550}
2551
Mike Stumpd3bb5572009-07-24 19:02:52 +00002552/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002553/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002554static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002555 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002556 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002557 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002558 return;
2559 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002560
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002561 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002562 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002563 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002564 return;
2565 }
2566
Chandler Carruth743682b2010-11-16 08:35:43 +00002567 // In C++ the implicit 'this' function parameter also counts, and they are
2568 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002569 bool HasImplicitThisParam = isInstanceMethod(D);
2570 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002571
Aaron Ballman00e99962013-08-31 01:11:41 +00002572 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2573 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002574
2575 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002576 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002577 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002578 // If we've modified the string name, we need a new identifier for it.
2579 II = &S.Context.Idents.get(Format);
2580 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002581
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002582 // Check for supported formats.
2583 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002584
2585 if (Kind == IgnoredFormat)
2586 return;
2587
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002588 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002589 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002590 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 return;
2592 }
2593
2594 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002595 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002596 uint32_t Idx;
2597 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002599
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002600 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002601 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002602 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002603 return;
2604 }
2605
2606 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002607 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002608
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002609 if (HasImplicitThisParam) {
2610 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002611 S.Diag(Attr.getLoc(),
2612 diag::err_format_attribute_implicit_this_format_string)
2613 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002614 return;
2615 }
2616 ArgIdx--;
2617 }
Mike Stump11289f42009-09-09 15:08:12 +00002618
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002619 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002620 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002622 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002623 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002624 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2625 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002626 return;
2627 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002628 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002629 // FIXME: do we need to check if the type is NSString*? What are the
2630 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002631 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002632 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002633 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2634 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002636 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002638 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002639 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002640 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2641 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002642 return;
2643 }
2644
2645 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002646 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002647 uint32_t FirstArg;
2648 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650
2651 // check if the function is variadic if the 3rd argument non-zero
2652 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002653 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002654 ++NumArgs; // +1 for ...
2655 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002656 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 return;
2658 }
2659 }
2660
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002661 // strftime requires FirstArg to be 0 because it doesn't read from any
2662 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002663 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002664 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002665 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2666 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002667 return;
2668 }
2669 // if 0 it disables parameter checking (to use with e.g. va_list)
2670 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002671 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002672 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002673 return;
2674 }
2675
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002676 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002677 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002678 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002679 if (NewAttr)
2680 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002681}
2682
Chandler Carruthedc2c642011-07-02 00:01:44 +00002683static void handleTransparentUnionAttr(Sema &S, Decl *D,
2684 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002685 // Try to find the underlying union declaration.
2686 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002687 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002688 if (TD && TD->getUnderlyingType()->isUnionType())
2689 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2690 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002691 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002692
2693 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002694 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002695 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002696 return;
2697 }
2698
John McCallf937c022011-10-07 06:10:15 +00002699 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002700 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002701 diag::warn_transparent_union_attribute_not_definition);
2702 return;
2703 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002705 RecordDecl::field_iterator Field = RD->field_begin(),
2706 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002707 if (Field == FieldEnd) {
2708 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2709 return;
2710 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002711
David Blaikie40ed2972012-06-06 20:45:41 +00002712 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002713 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002714 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002715 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002716 diag::warn_transparent_union_attribute_floating)
2717 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002718 return;
2719 }
2720
2721 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2722 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2723 for (; Field != FieldEnd; ++Field) {
2724 QualType FieldType = Field->getType();
2725 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2726 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2727 // Warn if we drop the attribute.
2728 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002729 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002730 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002731 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002732 diag::warn_transparent_union_attribute_field_size_align)
2733 << isSize << Field->getDeclName() << FieldBits;
2734 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002735 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002736 diag::note_transparent_union_first_field_size_align)
2737 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002738 return;
2739 }
2740 }
2741
Michael Han99315932013-01-24 16:46:58 +00002742 RD->addAttr(::new (S.Context)
2743 TransparentUnionAttr(Attr.getRange(), S.Context,
2744 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002745}
2746
Chandler Carruthedc2c642011-07-02 00:01:44 +00002747static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002748 // Make sure that there is a string literal as the annotation's single
2749 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002750 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002751 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002752 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002753
2754 // Don't duplicate annotations that are already set.
2755 for (specific_attr_iterator<AnnotateAttr>
2756 i = D->specific_attr_begin<AnnotateAttr>(),
2757 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002758 if ((*i)->getAnnotation() == Str)
2759 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002760 }
Michael Han99315932013-01-24 16:46:58 +00002761
2762 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002763 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002764 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765}
2766
Chandler Carruthedc2c642011-07-02 00:01:44 +00002767static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002768 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002769 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2771 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002772 return;
2773 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002774
Richard Smith848e1f12013-02-01 08:12:08 +00002775 if (Attr.getNumArgs() == 0) {
2776 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2777 true, 0, Attr.getAttributeSpellingListIndex()));
2778 return;
2779 }
2780
Aaron Ballman00e99962013-08-31 01:11:41 +00002781 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002782 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2783 S.Diag(Attr.getEllipsisLoc(),
2784 diag::err_pack_expansion_without_parameter_packs);
2785 return;
2786 }
2787
2788 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2789 return;
2790
2791 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2792 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002793}
2794
2795void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002796 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002797 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2798 SourceLocation AttrLoc = AttrRange.getBegin();
2799
Richard Smith1dba27c2013-01-29 09:02:09 +00002800 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002801 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002802 // C++11 [dcl.align]p1:
2803 // An alignment-specifier may be applied to a variable or to a class
2804 // data member, but it shall not be applied to a bit-field, a function
2805 // parameter, the formal parameter of a catch clause, or a variable
2806 // declared with the register storage class specifier. An
2807 // alignment-specifier may also be applied to the declaration of a class
2808 // or enumeration type.
2809 // C11 6.7.5/2:
2810 // An alignment attribute shall not be specified in a declaration of
2811 // a typedef, or a bit-field, or a function, or a parameter, or an
2812 // object declared with the register storage-class specifier.
2813 int DiagKind = -1;
2814 if (isa<ParmVarDecl>(D)) {
2815 DiagKind = 0;
2816 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2817 if (VD->getStorageClass() == SC_Register)
2818 DiagKind = 1;
2819 if (VD->isExceptionVariable())
2820 DiagKind = 2;
2821 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2822 if (FD->isBitField())
2823 DiagKind = 3;
2824 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002825 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002826 << (TmpAttr.isC11() ? ExpectedVariableOrField
2827 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002828 return;
2829 }
2830 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002831 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002832 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002833 return;
2834 }
2835 }
2836
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002837 if (E->isTypeDependent() || E->isValueDependent()) {
2838 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002839 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2840 AA->setPackExpansion(IsPackExpansion);
2841 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002842 return;
2843 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002844
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002845 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002846 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002847 ExprResult ICE
2848 = VerifyIntegerConstantExpression(E, &Alignment,
2849 diag::err_aligned_attribute_argument_not_int,
2850 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002851 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002852 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002853
2854 // C++11 [dcl.align]p2:
2855 // -- if the constant expression evaluates to zero, the alignment
2856 // specifier shall have no effect
2857 // C11 6.7.5p6:
2858 // An alignment specification of zero has no effect.
2859 if (!(TmpAttr.isAlignas() && !Alignment) &&
2860 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002861 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2862 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002863 return;
2864 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002865
Richard Smith848e1f12013-02-01 08:12:08 +00002866 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002867 // We've already verified it's a power of 2, now let's make sure it's
2868 // 8192 or less.
2869 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002870 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002871 << E->getSourceRange();
2872 return;
2873 }
2874 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002875
Richard Smith44c247f2013-02-22 08:32:16 +00002876 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2877 ICE.take(), SpellingListIndex);
2878 AA->setPackExpansion(IsPackExpansion);
2879 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002880}
2881
Michael Hanaf02bbe2013-02-01 01:19:17 +00002882void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002883 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002884 // FIXME: Cache the number on the Attr object if non-dependent?
2885 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002886 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2887 SpellingListIndex);
2888 AA->setPackExpansion(IsPackExpansion);
2889 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002890}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002891
Richard Smith848e1f12013-02-01 08:12:08 +00002892void Sema::CheckAlignasUnderalignment(Decl *D) {
2893 assert(D->hasAttrs() && "no attributes on decl");
2894
2895 QualType Ty;
2896 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2897 Ty = VD->getType();
2898 else
2899 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002900 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002901 return;
2902
2903 // C++11 [dcl.align]p5, C11 6.7.5/4:
2904 // The combined effect of all alignment attributes in a declaration shall
2905 // not specify an alignment that is less strict than the alignment that
2906 // would otherwise be required for the entity being declared.
2907 AlignedAttr *AlignasAttr = 0;
2908 unsigned Align = 0;
2909 for (specific_attr_iterator<AlignedAttr>
2910 I = D->specific_attr_begin<AlignedAttr>(),
2911 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2912 if (I->isAlignmentDependent())
2913 return;
2914 if (I->isAlignas())
2915 AlignasAttr = *I;
2916 Align = std::max(Align, I->getAlignment(Context));
2917 }
2918
2919 if (AlignasAttr && Align) {
2920 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2921 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2922 if (NaturalAlign > RequestedAlign)
2923 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2924 << Ty << (unsigned)NaturalAlign.getQuantity();
2925 }
2926}
2927
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002928/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002929/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002930///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002931/// Despite what would be logical, the mode attribute is a decl attribute, not a
2932/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2933/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002934static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002935 // This attribute isn't documented, but glibc uses it. It changes
2936 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002937 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002938 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2939 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002940 return;
2941 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002942
Aaron Ballman00e99962013-08-31 01:11:41 +00002943 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2944 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002945
2946 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002947 if (Str.startswith("__") && Str.endswith("__"))
2948 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002949
2950 unsigned DestWidth = 0;
2951 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002952 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002953 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002954 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002955 switch (Str[0]) {
2956 case 'Q': DestWidth = 8; break;
2957 case 'H': DestWidth = 16; break;
2958 case 'S': DestWidth = 32; break;
2959 case 'D': DestWidth = 64; break;
2960 case 'X': DestWidth = 96; break;
2961 case 'T': DestWidth = 128; break;
2962 }
2963 if (Str[1] == 'F') {
2964 IntegerMode = false;
2965 } else if (Str[1] == 'C') {
2966 IntegerMode = false;
2967 ComplexMode = true;
2968 } else if (Str[1] != 'I') {
2969 DestWidth = 0;
2970 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002971 break;
2972 case 4:
2973 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2974 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002975 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002976 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002977 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002978 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002979 break;
2980 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002981 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002982 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002983 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002984 case 11:
2985 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002986 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002987 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002988 }
2989
2990 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002991 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002992 OldTy = TD->getUnderlyingType();
2993 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2994 OldTy = VD->getType();
2995 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002996 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002997 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002998 return;
2999 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003000
John McCall9dd450b2009-09-21 23:43:11 +00003001 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003002 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3003 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003004 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003005 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3006 } else if (ComplexMode) {
3007 if (!OldTy->isComplexType())
3008 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3009 } else {
3010 if (!OldTy->isFloatingType())
3011 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3012 }
3013
Mike Stump87c57ac2009-05-16 07:39:55 +00003014 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3015 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003016 // FIXME: Make sure floating-point mappings are accurate
3017 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003018 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003019 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003020 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003021 }
3022
3023 QualType NewTy;
3024
3025 if (IntegerMode)
3026 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3027 OldTy->isSignedIntegerType());
3028 else
3029 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3030
3031 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003032 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003033 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003034 }
3035
Eli Friedman4735374e2009-03-03 06:41:03 +00003036 if (ComplexMode) {
3037 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003038 }
3039
3040 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003041 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3042 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3043 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003044 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003045
3046 D->addAttr(::new (S.Context)
3047 ModeAttr(Attr.getRange(), S.Context, Name,
3048 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003049}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003050
Chandler Carruthedc2c642011-07-02 00:01:44 +00003051static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003052 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3053 if (!VD->hasGlobalStorage())
3054 S.Diag(Attr.getLoc(),
3055 diag::warn_attribute_requires_functions_or_static_globals)
3056 << Attr.getName();
3057 } else if (!isFunctionOrMethod(D)) {
3058 S.Diag(Attr.getLoc(),
3059 diag::warn_attribute_requires_functions_or_static_globals)
3060 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003061 return;
3062 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003063
Michael Han99315932013-01-24 16:46:58 +00003064 D->addAttr(::new (S.Context)
3065 NoDebugAttr(Attr.getRange(), S.Context,
3066 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003067}
3068
Chandler Carruthedc2c642011-07-02 00:01:44 +00003069static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003070 FunctionDecl *FD = cast<FunctionDecl>(D);
3071 if (!FD->getResultType()->isVoidType()) {
3072 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3073 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3074 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3075 << FD->getType()
3076 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3077 "void");
3078 } else {
3079 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3080 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003081 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003082 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003083 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003084
Aaron Ballman3aff6332013-12-02 19:30:36 +00003085 D->addAttr(::new (S.Context)
3086 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003087 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003088}
3089
Chandler Carruthedc2c642011-07-02 00:01:44 +00003090static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003091 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003092 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003093 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003094 return;
3095 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003096
Michael Han99315932013-01-24 16:46:58 +00003097 D->addAttr(::new (S.Context)
3098 GNUInlineAttr(Attr.getRange(), S.Context,
3099 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003100}
3101
Chandler Carruthedc2c642011-07-02 00:01:44 +00003102static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003103 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003104
Aaron Ballman02df2e02012-12-09 17:45:41 +00003105 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003106 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003107 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3108 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003109 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003110 return;
3111
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003112 if (!isa<ObjCMethodDecl>(D)) {
3113 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3114 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003115 return;
3116 }
3117
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003118 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003119 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003120 D->addAttr(::new (S.Context)
3121 FastCallAttr(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_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003125 D->addAttr(::new (S.Context)
3126 StdCallAttr(Attr.getRange(), S.Context,
3127 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003128 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003129 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003130 D->addAttr(::new (S.Context)
3131 ThisCallAttr(Attr.getRange(), S.Context,
3132 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003133 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003134 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 CDeclAttr(Attr.getRange(), S.Context,
3137 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003138 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003139 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003140 D->addAttr(::new (S.Context)
3141 PascalAttr(Attr.getRange(), S.Context,
3142 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003143 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003144 case AttributeList::AT_MSABI:
3145 D->addAttr(::new (S.Context)
3146 MSABIAttr(Attr.getRange(), S.Context,
3147 Attr.getAttributeSpellingListIndex()));
3148 return;
3149 case AttributeList::AT_SysVABI:
3150 D->addAttr(::new (S.Context)
3151 SysVABIAttr(Attr.getRange(), S.Context,
3152 Attr.getAttributeSpellingListIndex()));
3153 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003154 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003155 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003156 switch (CC) {
3157 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003158 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003159 break;
3160 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003161 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003162 break;
3163 default:
3164 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003165 }
3166
Michael Han99315932013-01-24 16:46:58 +00003167 D->addAttr(::new (S.Context)
3168 PcsAttr(Attr.getRange(), S.Context, PCS,
3169 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003170 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003171 }
Derek Schuffa2020962012-10-16 22:30:41 +00003172 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003173 D->addAttr(::new (S.Context)
3174 PnaclCallAttr(Attr.getRange(), S.Context,
3175 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003176 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003177 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003178 D->addAttr(::new (S.Context)
3179 IntelOclBiccAttr(Attr.getRange(), S.Context,
3180 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003181 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003182
Abramo Bagnara50099372010-04-30 13:10:51 +00003183 default:
3184 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003185 }
3186}
3187
Aaron Ballman02df2e02012-12-09 17:45:41 +00003188bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3189 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003190 if (attr.isInvalid())
3191 return true;
3192
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003193 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003194 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003195 attr.setInvalid();
3196 return true;
3197 }
3198
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003199 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003200 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003201 case AttributeList::AT_CDecl: CC = CC_C; break;
3202 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3203 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3204 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3205 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003206 case AttributeList::AT_MSABI:
3207 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3208 CC_X86_64Win64;
3209 break;
3210 case AttributeList::AT_SysVABI:
3211 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3212 CC_C;
3213 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003214 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003215 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003216 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003217 attr.setInvalid();
3218 return true;
3219 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003220 if (StrRef == "aapcs") {
3221 CC = CC_AAPCS;
3222 break;
3223 } else if (StrRef == "aapcs-vfp") {
3224 CC = CC_AAPCS_VFP;
3225 break;
3226 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003227
3228 attr.setInvalid();
3229 Diag(attr.getLoc(), diag::err_invalid_pcs);
3230 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003231 }
Derek Schuffa2020962012-10-16 22:30:41 +00003232 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003233 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003234 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003235 }
3236
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003237 const TargetInfo &TI = Context.getTargetInfo();
3238 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3239 if (A == TargetInfo::CCCR_Warning) {
3240 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003241
3242 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3243 if (FD)
3244 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3245 TargetInfo::CCMT_NonMember;
3246 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003247 }
3248
John McCall3882ace2011-01-05 12:14:39 +00003249 return false;
3250}
3251
John McCall3882ace2011-01-05 12:14:39 +00003252/// Checks a regparm attribute, returning true if it is ill-formed and
3253/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003254bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3255 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003256 return true;
3257
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003258 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003259 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003260 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003261 }
Eli Friedman7044b762009-03-27 21:06:47 +00003262
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003263 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003264 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003265 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003266 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003267 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003268 }
3269
Douglas Gregore8bbc122011-09-02 00:18:52 +00003270 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003271 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003272 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003273 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003274 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003275 }
3276
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003277 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003278 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003279 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003280 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003281 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003282 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003283 }
3284
John McCall3882ace2011-01-05 12:14:39 +00003285 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003286}
3287
Aaron Ballman66039932013-12-19 00:41:31 +00003288static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3289 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003290 // check the attribute arguments.
3291 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3292 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003293 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3294 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003295 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003296 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003297
3298 if (!isFunctionOrMethod(D)) {
3299 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3300 << Attr.getName() << ExpectedFunctionOrMethod;
3301 return;
3302 }
3303
Aaron Ballman66039932013-12-19 00:41:31 +00003304 uint32_t MaxThreads, MinBlocks = 0;
3305 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3306 return;
3307 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3308 Attr.getArgAsExpr(1),
3309 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003310 return;
3311
3312 D->addAttr(::new (S.Context)
3313 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3314 MaxThreads, MinBlocks,
3315 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003316}
3317
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003318static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3319 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003320 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003322 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323 return;
3324 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003325
3326 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003327 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003328
Aaron Ballman00e99962013-08-31 01:11:41 +00003329 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003330
3331 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3333 << Attr.getName() << ExpectedFunctionOrMethod;
3334 return;
3335 }
3336
3337 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003338 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3339 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003340 return;
3341
3342 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003343 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3344 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003345 return;
3346
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003347 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003348 if (IsPointer) {
3349 // Ensure that buffer has a pointer type.
3350 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3351 if (!BufferTy->isPointerType()) {
3352 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003353 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003354 }
3355 }
3356
Michael Han99315932013-01-24 16:46:58 +00003357 D->addAttr(::new (S.Context)
3358 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3359 ArgumentIdx, TypeTagIdx, IsPointer,
3360 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003361}
3362
3363static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3364 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003365 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003366 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003367 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003368 return;
3369 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003370
3371 if (!checkAttributeNumArgs(S, Attr, 1))
3372 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003373
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003374 if (!isa<VarDecl>(D)) {
3375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3376 << Attr.getName() << ExpectedVariable;
3377 return;
3378 }
3379
Aaron Ballman00e99962013-08-31 01:11:41 +00003380 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003381 TypeSourceInfo *MatchingCTypeLoc = 0;
3382 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3383 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003384
Michael Han99315932013-01-24 16:46:58 +00003385 D->addAttr(::new (S.Context)
3386 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003387 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003388 Attr.getLayoutCompatible(),
3389 Attr.getMustBeNull(),
3390 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003391}
3392
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003393//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003394// Checker-specific attribute handlers.
3395//===----------------------------------------------------------------------===//
3396
John McCalled433932011-01-25 03:31:58 +00003397static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003398 return type->isDependentType() ||
3399 type->isObjCObjectPointerType() ||
3400 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003401}
3402static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003403 return type->isDependentType() ||
3404 type->isPointerType() ||
3405 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003406}
3407
Chandler Carruthedc2c642011-07-02 00:01:44 +00003408static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003409 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003410 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003411
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003412 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003413 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3414 cf = false;
3415 } else {
3416 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3417 cf = true;
3418 }
3419
3420 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003421 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003422 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003423 return;
3424 }
3425
3426 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003427 param->addAttr(::new (S.Context)
3428 CFConsumedAttr(Attr.getRange(), S.Context,
3429 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003430 else
Michael Han99315932013-01-24 16:46:58 +00003431 param->addAttr(::new (S.Context)
3432 NSConsumedAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003434}
3435
Chandler Carruthedc2c642011-07-02 00:01:44 +00003436static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3437 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003438
John McCalled433932011-01-25 03:31:58 +00003439 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003440
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003441 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003442 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003443 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003444 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003445 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003446 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3447 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003448 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003449 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003450 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003451 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003452 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003453 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003454 return;
3455 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003456
John McCalled433932011-01-25 03:31:58 +00003457 bool typeOK;
3458 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003459 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003460 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003461 case AttributeList::AT_NSReturnsAutoreleased:
3462 case AttributeList::AT_NSReturnsRetained:
3463 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003464 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3465 cf = false;
3466 break;
3467
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003468 case AttributeList::AT_CFReturnsRetained:
3469 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003470 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3471 cf = true;
3472 break;
3473 }
3474
3475 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003476 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003477 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003478 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003479 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003480
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003481 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003482 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003483 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003484 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003485 D->addAttr(::new (S.Context)
3486 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3487 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003488 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003489 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003490 D->addAttr(::new (S.Context)
3491 CFReturnsNotRetainedAttr(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_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003495 D->addAttr(::new (S.Context)
3496 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3497 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003498 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003499 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003500 D->addAttr(::new (S.Context)
3501 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3502 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003503 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003504 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003505 D->addAttr(::new (S.Context)
3506 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3507 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003508 return;
3509 };
3510}
3511
John McCallcf166702011-07-22 08:53:00 +00003512static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3513 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003514 const int EP_ObjCMethod = 1;
3515 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003516
John McCallcf166702011-07-22 08:53:00 +00003517 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003518 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003519 if (isa<ObjCMethodDecl>(D))
3520 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003521 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003522 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003523
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003524 if (!resultType->isReferenceType() &&
3525 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003526 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003527 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003528 << attr.getName()
3529 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003530 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003531
3532 // Drop the attribute.
3533 return;
3534 }
3535
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003536 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003537 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3538 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003539}
3540
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003541static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3542 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003543 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003544
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003545 DeclContext *DC = method->getDeclContext();
3546 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3547 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3548 << attr.getName() << 0;
3549 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3550 return;
3551 }
3552 if (method->getMethodFamily() == OMF_dealloc) {
3553 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3554 << attr.getName() << 1;
3555 return;
3556 }
3557
Michael Han99315932013-01-24 16:46:58 +00003558 method->addAttr(::new (S.Context)
3559 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3560 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003561}
3562
Aaron Ballmanfb763042013-12-02 18:05:46 +00003563static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3564 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003565 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003566 return;
John McCall32f5fe12011-09-30 05:12:12 +00003567
Aaron Ballmanfb763042013-12-02 18:05:46 +00003568 D->addAttr(::new (S.Context)
3569 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3570 Attr.getAttributeSpellingListIndex()));
3571}
3572
3573static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3574 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003575 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003576 return;
3577
3578 D->addAttr(::new (S.Context)
3579 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3580 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003581}
3582
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003583static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3584 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003585 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003586
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003587 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003588 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003589 return;
3590 }
3591
3592 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003593 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003594 Attr.getAttributeSpellingListIndex()));
3595}
3596
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003597static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3598 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003599 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003600
3601 if (!Parm) {
3602 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3603 return;
3604 }
3605
3606 D->addAttr(::new (S.Context)
3607 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3608 Attr.getAttributeSpellingListIndex()));
3609}
3610
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003611static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3612 const AttributeList &Attr) {
3613 IdentifierInfo *RelatedClass =
3614 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3615 if (!RelatedClass) {
3616 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3617 return;
3618 }
3619 IdentifierInfo *ClassMethod =
3620 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3621 IdentifierInfo *InstanceMethod =
3622 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3623 D->addAttr(::new (S.Context)
3624 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3625 ClassMethod, InstanceMethod,
3626 Attr.getAttributeSpellingListIndex()));
3627}
3628
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003629static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3630 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003631 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003632 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003633 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003634 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3635 Attr.getAttributeSpellingListIndex()));
3636}
3637
Chandler Carruthedc2c642011-07-02 00:01:44 +00003638static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3639 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003640 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003641
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003642 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003643 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003644}
3645
Chandler Carruthedc2c642011-07-02 00:01:44 +00003646static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3647 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003648 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003649 QualType type = vd->getType();
3650
3651 if (!type->isDependentType() &&
3652 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003653 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003654 << type;
3655 return;
3656 }
3657
3658 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3659
3660 // If we have no lifetime yet, check the lifetime we're presumably
3661 // going to infer.
3662 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3663 lifetime = type->getObjCARCImplicitLifetime();
3664
3665 switch (lifetime) {
3666 case Qualifiers::OCL_None:
3667 assert(type->isDependentType() &&
3668 "didn't infer lifetime for non-dependent type?");
3669 break;
3670
3671 case Qualifiers::OCL_Weak: // meaningful
3672 case Qualifiers::OCL_Strong: // meaningful
3673 break;
3674
3675 case Qualifiers::OCL_ExplicitNone:
3676 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003677 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003678 << (lifetime == Qualifiers::OCL_Autoreleasing);
3679 break;
3680 }
3681
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003682 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003683 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3684 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003685}
3686
Francois Picheta83957a2010-12-19 06:50:37 +00003687//===----------------------------------------------------------------------===//
3688// Microsoft specific attribute handlers.
3689//===----------------------------------------------------------------------===//
3690
Chandler Carruthedc2c642011-07-02 00:01:44 +00003691static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003692 if (!S.LangOpts.CPlusPlus) {
3693 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3694 << Attr.getName() << AttributeLangSupport::C;
3695 return;
3696 }
3697
Aaron Ballman60e705e2013-11-24 20:58:02 +00003698 if (!isa<CXXRecordDecl>(D)) {
3699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3700 << Attr.getName() << ExpectedClass;
3701 return;
3702 }
3703
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003704 StringRef StrRef;
3705 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003706 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003707 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003708
David Majnemer89085342013-08-09 08:56:20 +00003709 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3710 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003711 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3712 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003713
Reid Kleckner140c4a72013-05-17 14:04:52 +00003714 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003715 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003716 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003717 return;
3718 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003719
David Majnemer89085342013-08-09 08:56:20 +00003720 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003721 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003722 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003723 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003724 return;
3725 }
David Majnemer89085342013-08-09 08:56:20 +00003726 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003727 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003728 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003729 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003730 }
Francois Picheta83957a2010-12-19 06:50:37 +00003731
David Majnemer89085342013-08-09 08:56:20 +00003732 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3733 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003734}
3735
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003736static void handleARMInterruptAttr(Sema &S, Decl *D,
3737 const AttributeList &Attr) {
3738 // Check the attribute arguments.
3739 if (Attr.getNumArgs() > 1) {
3740 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3741 << Attr.getName() << 1;
3742 return;
3743 }
3744
3745 StringRef Str;
3746 SourceLocation ArgLoc;
3747
3748 if (Attr.getNumArgs() == 0)
3749 Str = "";
3750 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3751 return;
3752
3753 ARMInterruptAttr::InterruptType Kind;
3754 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3755 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3756 << Attr.getName() << Str << ArgLoc;
3757 return;
3758 }
3759
3760 unsigned Index = Attr.getAttributeSpellingListIndex();
3761 D->addAttr(::new (S.Context)
3762 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3763}
3764
3765static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3766 const AttributeList &Attr) {
3767 if (!checkAttributeNumArgs(S, Attr, 1))
3768 return;
3769
3770 if (!Attr.isArgExpr(0)) {
3771 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3772 << AANT_ArgumentIntegerConstant;
3773 return;
3774 }
3775
3776 // FIXME: Check for decl - it should be void ()(void).
3777
3778 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3779 llvm::APSInt NumParams(32);
3780 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3781 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3782 << Attr.getName() << AANT_ArgumentIntegerConstant
3783 << NumParamsExpr->getSourceRange();
3784 return;
3785 }
3786
3787 unsigned Num = NumParams.getLimitedValue(255);
3788 if ((Num & 1) || Num > 30) {
3789 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3790 << Attr.getName() << (int)NumParams.getSExtValue()
3791 << NumParamsExpr->getSourceRange();
3792 return;
3793 }
3794
Aaron Ballman36a53502014-01-16 13:03:14 +00003795 D->addAttr(::new (S.Context)
3796 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3797 Attr.getAttributeSpellingListIndex()));
3798 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003799}
3800
3801static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3802 // Dispatch the interrupt attribute based on the current target.
3803 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3804 handleMSP430InterruptAttr(S, D, Attr);
3805 else
3806 handleARMInterruptAttr(S, D, Attr);
3807}
3808
3809static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3810 const AttributeList& Attr) {
3811 // If we try to apply it to a function pointer, don't warn, but don't
3812 // do anything, either. It doesn't matter anyway, because there's nothing
3813 // special about calling a force_align_arg_pointer function.
3814 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3815 if (VD && VD->getType()->isFunctionPointerType())
3816 return;
3817 // Also don't warn on function pointer typedefs.
3818 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3819 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3820 TD->getUnderlyingType()->isFunctionType()))
3821 return;
3822 // Attribute can only be applied to function types.
3823 if (!isa<FunctionDecl>(D)) {
3824 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3825 << Attr.getName() << /* function */0;
3826 return;
3827 }
3828
Aaron Ballman36a53502014-01-16 13:03:14 +00003829 D->addAttr(::new (S.Context)
3830 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3831 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003832}
3833
3834DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3835 unsigned AttrSpellingListIndex) {
3836 if (D->hasAttr<DLLExportAttr>()) {
3837 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3838 return NULL;
3839 }
3840
3841 if (D->hasAttr<DLLImportAttr>())
3842 return NULL;
3843
3844 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3845 if (VD->hasDefinition()) {
3846 // dllimport cannot be applied to definitions.
3847 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3848 << "dllimport";
3849 return NULL;
3850 }
3851 }
3852
3853 return ::new (Context)DLLImportAttr(Range, Context,
3854 AttrSpellingListIndex);
3855}
3856
3857static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3858 // Attribute can be applied only to functions or variables.
3859 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3860 if (!FD && !isa<VarDecl>(D)) {
3861 // Apparently Visual C++ thinks it is okay to not emit a warning
3862 // in this case, so only emit a warning when -fms-extensions is not
3863 // specified.
3864 if (!S.getLangOpts().MicrosoftExt)
3865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3866 << Attr.getName() << 2 /*variable and function*/;
3867 return;
3868 }
3869
3870 // Currently, the dllimport attribute is ignored for inlined functions.
3871 // Warning is emitted.
3872 if (FD && FD->isInlineSpecified()) {
3873 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3874 return;
3875 }
3876
3877 unsigned Index = Attr.getAttributeSpellingListIndex();
3878 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3879 if (NewAttr)
3880 D->addAttr(NewAttr);
3881}
3882
3883DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3884 unsigned AttrSpellingListIndex) {
3885 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3886 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3887 D->dropAttr<DLLImportAttr>();
3888 }
3889
3890 if (D->hasAttr<DLLExportAttr>())
3891 return NULL;
3892
3893 return ::new (Context)DLLExportAttr(Range, Context,
3894 AttrSpellingListIndex);
3895}
3896
3897static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3898 // Currently, the dllexport attribute is ignored for inlined functions, unless
3899 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3900 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3901 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3902 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3903 return;
3904 }
3905
3906 unsigned Index = Attr.getAttributeSpellingListIndex();
3907 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3908 if (NewAttr)
3909 D->addAttr(NewAttr);
3910}
3911
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003912/// Handles semantic checking for features that are common to all attributes,
3913/// such as checking whether a parameter was properly specified, or the correct
3914/// number of arguments were passed, etc.
3915static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3916 const AttributeList &Attr) {
3917 // Several attributes carry different semantics than the parsing requires, so
3918 // those are opted out of the common handling.
3919 //
3920 // We also bail on unknown and ignored attributes because those are handled
3921 // as part of the target-specific handling logic.
3922 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003923 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003924 return false;
3925
Aaron Ballman3aff6332013-12-02 19:30:36 +00003926 // Check whether the attribute requires specific language extensions to be
3927 // enabled.
3928 if (!Attr.diagnoseLangOpts(S))
3929 return true;
3930
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003931 // If there are no optional arguments, then checking for the argument count
3932 // is trivial.
3933 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3934 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3935 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003936
3937 // Check whether the attribute appertains to the given subject.
3938 if (!Attr.diagnoseAppertainsTo(S, D))
3939 return true;
3940
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003941 return false;
3942}
3943
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003944//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003945// Top Level Sema Entry Points
3946//===----------------------------------------------------------------------===//
3947
Richard Smithf8a75c32013-08-29 00:47:48 +00003948/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3949/// the attribute applies to decls. If the attribute is a type attribute, just
3950/// silently ignore it if a GNU attribute.
3951static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3952 const AttributeList &Attr,
3953 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003954 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003955 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003956
Richard Smithf8a75c32013-08-29 00:47:48 +00003957 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3958 // instead.
3959 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3960 return;
3961
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003962 // Unknown attributes are automatically warned on. Target-specific attributes
3963 // which do not apply to the current target architecture are treated as
3964 // though they were unknown attributes.
3965 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3966 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3967 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3968 diag::warn_unhandled_ms_attribute_ignored :
3969 diag::warn_unknown_attribute_ignored) << Attr.getName();
3970 return;
3971 }
3972
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003973 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3974 return;
3975
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003976 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003977 default:
3978 // Type attributes are handled elsewhere; silently move on.
3979 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3980 break;
3981 case AttributeList::AT_Interrupt:
3982 handleInterruptAttr(S, D, Attr); break;
3983 case AttributeList::AT_X86ForceAlignArgPointer:
3984 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3985 case AttributeList::AT_DLLExport:
3986 handleDLLExportAttr(S, D, Attr); break;
3987 case AttributeList::AT_DLLImport:
3988 handleDLLImportAttr(S, D, Attr); break;
3989 case AttributeList::AT_Mips16:
3990 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3991 case AttributeList::AT_NoMips16:
3992 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003993 case AttributeList::AT_IBAction:
3994 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003995 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3996 case AttributeList::AT_IBOutletCollection:
3997 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003998 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3999 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004000 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004001 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004002 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004003 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004004 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004005 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4006 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4007 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004008 handleDependencyAttr(S, scope, D, Attr);
4009 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004011 case AttributeList::AT_CUDAConstant:
4012 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004013 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004014 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004015 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004016 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004017 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004018 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004019 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00004020 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004021 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004022 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004023 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004024 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004025 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004026 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004027 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4028 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4029 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004030 case AttributeList::AT_CUDADevice:
4031 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004032 case AttributeList::AT_CUDAHost:
4033 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004034 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4035 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004036 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004037 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004038 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004039 case AttributeList::AT_MayAlias:
4040 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004041 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004042 case AttributeList::AT_NoCommon:
4043 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004044 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004045 case AttributeList::AT_Overloadable:
4046 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004047 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004048 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4049 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004050 case AttributeList::AT_Naked:
4051 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004052 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004053 case AttributeList::AT_NoThrow:
4054 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004055 case AttributeList::AT_CUDAShared:
4056 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004057 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004058
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004059 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004060 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004061 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004062 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004063
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004064 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004065 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4066
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004067 case AttributeList::AT_ObjCRequiresSuper:
4068 handleObjCRequiresSuperAttr(S, D, Attr); break;
4069
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004070 case AttributeList::AT_ObjCBridge:
4071 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004072
4073 case AttributeList::AT_ObjCBridgeMutable:
4074 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004075
4076 case AttributeList::AT_ObjCBridgeRelated:
4077 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004078
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004079 case AttributeList::AT_ObjCDesignatedInitializer:
4080 handleObjCDesignatedInitializer(S, D, Attr); break;
4081
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004083 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004084 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004085 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004086
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004087 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004088 case AttributeList::AT_CFConsumed:
4089 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4090 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004091 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004092
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004093 case AttributeList::AT_NSReturnsAutoreleased:
4094 case AttributeList::AT_NSReturnsNotRetained:
4095 case AttributeList::AT_CFReturnsNotRetained:
4096 case AttributeList::AT_NSReturnsRetained:
4097 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004098 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004099 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004100 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004101 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004102 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004103 case AttributeList::AT_VecTypeHint:
4104 handleVecTypeHint(S, D, Attr); break;
4105
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004106 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004107 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004108
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004109 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4110 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4111 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004112 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004113 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004114 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004115 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004116 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004117 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004118 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004119 handleObjCSuppresProtocolAttr(S, D, Attr);
4120 break;
4121 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004122 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004123 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4124 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004125 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004126 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004127 case AttributeList::AT_Visibility:
4128 handleVisibilityAttr(S, D, Attr, false);
4129 break;
4130 case AttributeList::AT_TypeVisibility:
4131 handleVisibilityAttr(S, D, Attr, true);
4132 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004133 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004134 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004135 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004136 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004137 case AttributeList::AT_Weak:
4138 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004139 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4140 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4141 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004142 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004143 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004144 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004145 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004147 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004148 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004149 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4150 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4151 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004152 case AttributeList::AT_Const:
4153 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004154 case AttributeList::AT_Pure:
4155 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004156 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4157 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004158 case AttributeList::AT_NoInline:
4159 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004161 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004162 case AttributeList::AT_StdCall:
4163 case AttributeList::AT_CDecl:
4164 case AttributeList::AT_FastCall:
4165 case AttributeList::AT_ThisCall:
4166 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004167 case AttributeList::AT_MSABI:
4168 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004170 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004171 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004172 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004173 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004174 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004175 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004176 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman26891332014-01-14 17:41:53 +00004177 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break;
John McCall8d32c052012-05-22 21:28:12 +00004178
4179 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004180 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004181 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004182 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004183 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004184 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004185 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004186 case AttributeList::AT_MSInheritance:
4187 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004189 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004190 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004191 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004192
4193 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004194 case AttributeList::AT_AssertExclusiveLock:
4195 handleAssertExclusiveLockAttr(S, D, Attr);
4196 break;
4197 case AttributeList::AT_AssertSharedLock:
4198 handleAssertSharedLockAttr(S, D, Attr);
4199 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004200 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004201 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004202 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004203 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004204 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004206 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004207 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004208 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004209 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004210 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004211 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004212 break;
4213 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004214 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004215 break;
4216 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004217 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004218 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004220 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004221 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004222 handleGuardedByAttr(S, D, Attr);
4223 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004225 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004226 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004227 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004228 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004229 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004230 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004231 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004232 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004233 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004234 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004235 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004236 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004237 handleLockReturnedAttr(S, D, Attr);
4238 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004239 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004240 handleLocksExcludedAttr(S, D, Attr);
4241 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004242 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004243 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004244 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004245 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004246 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004247 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004248 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004249 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004250 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004251 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004252 handleUnlockFunAttr(S, D, Attr);
4253 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004254 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004255 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004256 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004257 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004258 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004259 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004260
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004261 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004262 case AttributeList::AT_Consumable:
4263 handleConsumableAttr(S, D, Attr);
4264 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004265 case AttributeList::AT_ConsumableAutoCast:
4266 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break;
4267 break;
4268 case AttributeList::AT_ConsumableSetOnRead:
4269 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break;
4270 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004271 case AttributeList::AT_CallableWhen:
4272 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004273 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004274 case AttributeList::AT_ParamTypestate:
4275 handleParamTypestateAttr(S, D, Attr);
4276 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004277 case AttributeList::AT_ReturnTypestate:
4278 handleReturnTypestateAttr(S, D, Attr);
4279 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004280 case AttributeList::AT_SetTypestate:
4281 handleSetTypestateAttr(S, D, Attr);
4282 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004283 case AttributeList::AT_TestTypestate:
4284 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004285 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004286
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004287 // Type safety attributes.
4288 case AttributeList::AT_ArgumentWithTypeTag:
4289 handleArgumentWithTypeTagAttr(S, D, Attr);
4290 break;
4291 case AttributeList::AT_TypeTagForDatatype:
4292 handleTypeTagForDatatypeAttr(S, D, Attr);
4293 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004294 }
4295}
4296
4297/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4298/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004299void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004300 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004301 bool IncludeCXX11Attributes) {
4302 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004303 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004304
Joey Gouly2cd9db12013-12-13 16:15:28 +00004305 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004306 // GCC accepts
4307 // static int a9 __attribute__((weakref));
4308 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004309 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004310 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4311 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004312 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004313 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004314 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004315
4316 if (!D->hasAttr<OpenCLKernelAttr>()) {
4317 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004318 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4319 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004320 D->setInvalidDecl();
4321 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004322 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4323 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004324 D->setInvalidDecl();
4325 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004326 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4327 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004328 D->setInvalidDecl();
4329 }
4330 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004331}
4332
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004333// Annotation attributes are the only attributes allowed after an access
4334// specifier.
4335bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4336 const AttributeList *AttrList) {
4337 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004338 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004339 handleAnnotateAttr(*this, ASDecl, *l);
4340 } else {
4341 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4342 return true;
4343 }
4344 }
4345
4346 return false;
4347}
4348
John McCall42856de2011-10-01 05:17:03 +00004349/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4350/// contains any decl attributes that we should warn about.
4351static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4352 for ( ; A; A = A->getNext()) {
4353 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004354 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004355 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4356
4357 if (A->getKind() == AttributeList::UnknownAttribute) {
4358 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4359 << A->getName() << A->getRange();
4360 } else {
4361 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4362 << A->getName() << A->getRange();
4363 }
4364 }
4365}
4366
4367/// checkUnusedDeclAttributes - Given a declarator which is not being
4368/// used to build a declaration, complain about any decl attributes
4369/// which might be lying around on it.
4370void Sema::checkUnusedDeclAttributes(Declarator &D) {
4371 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4372 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4373 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4374 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4375}
4376
Ryan Flynn7d470f32009-07-30 03:15:39 +00004377/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004378/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004379NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4380 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004381 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004382 NamedDecl *NewD = 0;
4383 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004384 FunctionDecl *NewFD;
4385 // FIXME: Missing call to CheckFunctionDeclaration().
4386 // FIXME: Mangling?
4387 // FIXME: Is the qualifier info correct?
4388 // FIXME: Is the DeclContext correct?
4389 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4390 Loc, Loc, DeclarationName(II),
4391 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004392 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004393 FD->hasPrototype(),
4394 false/*isConstexprSpecified*/);
4395 NewD = NewFD;
4396
4397 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004398 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004399
4400 // Fake up parameter variables; they are declared as if this were
4401 // a typedef.
4402 QualType FDTy = FD->getType();
4403 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4404 SmallVector<ParmVarDecl*, 16> Params;
4405 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4406 AE = FT->arg_type_end(); AI != AE; ++AI) {
4407 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4408 Param->setScopeInfo(0, Params.size());
4409 Params.push_back(Param);
4410 }
David Blaikie9c70e042011-09-21 18:16:56 +00004411 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004412 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004413 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4414 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004415 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004416 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004417 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004418 if (VD->getQualifier()) {
4419 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004420 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004421 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004422 }
4423 return NewD;
4424}
4425
James Dennett634962f2012-06-14 21:40:34 +00004426/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004427/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004428void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004429 if (W.getUsed()) return; // only do this once
4430 W.setUsed(true);
4431 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4432 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004433 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004434 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4435 W.getLocation()));
4436 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004437 WeakTopLevelDecl.push_back(NewD);
4438 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4439 // to insert Decl at TU scope, sorry.
4440 DeclContext *SavedContext = CurContext;
4441 CurContext = Context.getTranslationUnitDecl();
4442 PushOnScopeChains(NewD, S);
4443 CurContext = SavedContext;
4444 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004445 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004446 }
4447}
4448
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004449void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4450 // It's valid to "forward-declare" #pragma weak, in which case we
4451 // have to do this.
4452 LoadExternalWeakUndeclaredIdentifiers();
4453 if (!WeakUndeclaredIdentifiers.empty()) {
4454 NamedDecl *ND = NULL;
4455 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4456 if (VD->isExternC())
4457 ND = VD;
4458 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4459 if (FD->isExternC())
4460 ND = FD;
4461 if (ND) {
4462 if (IdentifierInfo *Id = ND->getIdentifier()) {
4463 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4464 = WeakUndeclaredIdentifiers.find(Id);
4465 if (I != WeakUndeclaredIdentifiers.end()) {
4466 WeakInfo W = I->second;
4467 DeclApplyPragmaWeak(S, ND, W);
4468 WeakUndeclaredIdentifiers[Id] = W;
4469 }
4470 }
4471 }
4472 }
4473}
4474
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004475/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4476/// it, apply them to D. This is a bit tricky because PD can have attributes
4477/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004478void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004479 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004480 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004481 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004482
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004483 // Walk the declarator structure, applying decl attributes that were in a type
4484 // position to the decl itself. This handles cases like:
4485 // int *__attr__(x)** D;
4486 // when X is a decl attribute.
4487 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4488 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004489 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004490
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004491 // Finally, apply any attributes on the decl itself.
4492 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004493 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004494}
John McCall28a6aea2009-11-04 02:18:39 +00004495
John McCall31168b02011-06-15 23:02:42 +00004496/// Is the given declaration allowed to use a forbidden type?
4497static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4498 // Private ivars are always okay. Unfortunately, people don't
4499 // always properly make their ivars private, even in system headers.
4500 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004501 // Function declarations in sys headers will be marked unavailable.
4502 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4503 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004504 return false;
4505
4506 // Require it to be declared in a system header.
4507 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4508}
4509
4510/// Handle a delayed forbidden-type diagnostic.
4511static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4512 Decl *decl) {
4513 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004514 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4515 "this system declaration uses an unsupported type",
4516 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004517 return;
4518 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004519 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004520 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004521 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004522 // kind of forbidden type messages on unavailable functions.
4523 if (FD->hasAttr<UnavailableAttr>() &&
4524 diag.getForbiddenTypeDiagnostic() ==
4525 diag::err_arc_array_param_no_ownership) {
4526 diag.Triggered = true;
4527 return;
4528 }
4529 }
John McCall31168b02011-06-15 23:02:42 +00004530
4531 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4532 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4533 diag.Triggered = true;
4534}
4535
John McCall2ec85372012-05-07 06:16:41 +00004536void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4537 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004538 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004539 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004540
John McCall2ec85372012-05-07 06:16:41 +00004541 // When delaying diagnostics to run in the context of a parsed
4542 // declaration, we only want to actually emit anything if parsing
4543 // succeeds.
4544 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004545
John McCall2ec85372012-05-07 06:16:41 +00004546 // We emit all the active diagnostics in this pool or any of its
4547 // parents. In general, we'll get one pool for the decl spec
4548 // and a child pool for each declarator; in a decl group like:
4549 // deprecated_typedef foo, *bar, baz();
4550 // only the declarator pops will be passed decls. This is correct;
4551 // we really do need to consider delayed diagnostics from the decl spec
4552 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004553 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004554 do {
John McCall6347b682012-05-07 06:16:58 +00004555 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004556 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4557 // This const_cast is a bit lame. Really, Triggered should be mutable.
4558 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004559 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004560 continue;
4561
John McCallc1465822011-02-14 07:13:47 +00004562 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004563 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004564 case DelayedDiagnostic::Unavailable:
4565 // Don't bother giving deprecation/unavailable diagnostics if
4566 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004567 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004568 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004569 break;
4570
4571 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004572 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004573 break;
John McCall31168b02011-06-15 23:02:42 +00004574
4575 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004576 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004577 break;
John McCall86121512010-01-27 03:50:35 +00004578 }
4579 }
John McCall2ec85372012-05-07 06:16:41 +00004580 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004581}
4582
John McCall6347b682012-05-07 06:16:58 +00004583/// Given a set of delayed diagnostics, re-emit them as if they had
4584/// been delayed in the current context instead of in the given pool.
4585/// Essentially, this just moves them to the current pool.
4586void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4587 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4588 assert(curPool && "re-emitting in undelayed context not supported");
4589 curPool->steal(pool);
4590}
4591
John McCall28a6aea2009-11-04 02:18:39 +00004592static bool isDeclDeprecated(Decl *D) {
4593 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004594 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004595 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004596 // A category implicitly has the availability of the interface.
4597 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4598 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004599 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4600 return false;
4601}
4602
Ted Kremenekb79ee572013-12-18 23:30:06 +00004603static bool isDeclUnavailable(Decl *D) {
4604 do {
4605 if (D->isUnavailable())
4606 return true;
4607 // A category implicitly has the availability of the interface.
4608 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4609 return CatD->getClassInterface()->isUnavailable();
4610 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4611 return false;
4612}
4613
Eli Friedman971bfa12012-08-08 21:52:41 +00004614static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004615DoEmitAvailabilityWarning(Sema &S,
4616 DelayedDiagnostic::DDKind K,
4617 Decl *Ctx,
4618 const NamedDecl *D,
4619 StringRef Message,
4620 SourceLocation Loc,
4621 const ObjCInterfaceDecl *UnknownObjCClass,
4622 const ObjCPropertyDecl *ObjCProperty) {
4623
4624 // Diagnostics for deprecated or unavailable.
4625 unsigned diag, diag_message, diag_fwdclass_message;
4626
4627 // Matches 'diag::note_property_attribute' options.
4628 unsigned property_note_select;
4629
4630 // Matches diag::note_availability_specified_here.
4631 unsigned available_here_select_kind;
4632
4633 // Don't warn if our current context is deprecated or unavailable.
4634 switch (K) {
4635 case DelayedDiagnostic::Deprecation:
4636 if (isDeclDeprecated(Ctx))
4637 return;
4638 diag = diag::warn_deprecated;
4639 diag_message = diag::warn_deprecated_message;
4640 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4641 property_note_select = /* deprecated */ 0;
4642 available_here_select_kind = /* deprecated */ 2;
4643 break;
4644
4645 case DelayedDiagnostic::Unavailable:
4646 if (isDeclUnavailable(Ctx))
4647 return;
4648 diag = diag::err_unavailable;
4649 diag_message = diag::err_unavailable_message;
4650 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4651 property_note_select = /* unavailable */ 1;
4652 available_here_select_kind = /* unavailable */ 0;
4653 break;
4654
4655 default:
4656 llvm_unreachable("Neither a deprecation or unavailable kind");
4657 }
4658
Eli Friedman971bfa12012-08-08 21:52:41 +00004659 DeclarationName Name = D->getDeclName();
4660 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004661 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004662 if (ObjCProperty)
4663 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4664 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004665 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004666 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004667 if (ObjCProperty)
4668 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4669 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004670 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004671 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004672 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4673 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004674
4675 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4676 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004677}
4678
Ted Kremenekb79ee572013-12-18 23:30:06 +00004679void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4680 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004681 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004682 DoEmitAvailabilityWarning(*this,
4683 (DelayedDiagnostic::DDKind) DD.Kind,
4684 Ctx,
4685 DD.getDeprecationDecl(),
4686 DD.getDeprecationMessage(),
4687 DD.Loc,
4688 DD.getUnknownObjCClass(),
4689 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004690}
4691
Ted Kremenekb79ee572013-12-18 23:30:06 +00004692void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4693 NamedDecl *D, StringRef Message,
4694 SourceLocation Loc,
4695 const ObjCInterfaceDecl *UnknownObjCClass,
4696 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004697 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004698 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004699 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4700 UnknownObjCClass,
4701 ObjCProperty,
4702 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004703 return;
4704 }
4705
Ted Kremenekb79ee572013-12-18 23:30:06 +00004706 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4707 DelayedDiagnostic::DDKind K;
4708 switch (AD) {
4709 case AD_Deprecation:
4710 K = DelayedDiagnostic::Deprecation;
4711 break;
4712 case AD_Unavailable:
4713 K = DelayedDiagnostic::Unavailable;
4714 break;
4715 }
4716
4717 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4718 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004719}