blob: 6555a3d27fcb506961fa4f40ddf9fa9aa1ac7fe3 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000021#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000025#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000034namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000035 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036 C,
37 Cpp,
38 ObjC
39 };
40}
41
Chris Lattner58418ff2008-06-29 00:16:31 +000042//===----------------------------------------------------------------------===//
43// Helper functions
44//===----------------------------------------------------------------------===//
45
Chandler Carruthff4c4f02011-07-01 23:49:12 +000046static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000047 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000048 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000049 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000050 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000051 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000052 Ty = decl->getUnderlyingType();
53 else
54 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000055
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000057 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000058 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000059 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000060
John McCall9dd450b2009-09-21 23:43:11 +000061 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000062}
63
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000064// FIXME: We should provide an abstraction around a method or function
65// to provide the following bits of information.
66
Nuno Lopes518e3702009-12-20 23:11:08 +000067/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000068/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069static bool isFunction(const Decl *D) {
70 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000071}
72
73/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000074/// type (function or function-typed variable) or an Objective-C
75/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000077 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000078}
79
Fariborz Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethodOrBlock - Return true if the given decl has function
81/// type (function or function-typed variable) or an Objective-C
82/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000083static bool isFunctionOrMethodOrBlock(const Decl *D) {
84 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000085 return true;
86 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088 QualType Ty = V->getType();
89 return Ty->isBlockPointerType();
90 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000092}
93
John McCall3882ace2011-01-05 12:14:39 +000094/// Return true if the given decl has a declarator that should have
95/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000097 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000098 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
99 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000100}
101
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000102/// hasFunctionProto - Return true if the given decl has a argument
103/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000105static bool hasFunctionProto(const Decl *D) {
106 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000107 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000108 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000109 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110 return true;
111 }
112}
113
114/// getFunctionOrMethodNumArgs - Return number of function or method
115/// arguments. It is an error to call this on a K&R function (use
116/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
118 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000119 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000120 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000121 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123}
124
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000130
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000131 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static QualType getFunctionOrMethodResultType(const Decl *D) {
135 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000136 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000138}
139
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140static bool isFunctionOrMethodVariadic(const Decl *D) {
141 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000142 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000143 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000145 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000146 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000148 }
149}
150
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000151static bool isInstanceMethod(const Decl *D) {
152 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000153 return MethodDecl->isInstance();
154 return false;
155}
156
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000157static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000158 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000159 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000160 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000161
John McCall96fa4842010-05-17 21:00:27 +0000162 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
163 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000164 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000165
John McCall96fa4842010-05-17 21:00:27 +0000166 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 // FIXME: Should we walk the chain of classes?
169 return ClsName == &Ctx.Idents.get("NSString") ||
170 ClsName == &Ctx.Idents.get("NSMutableString");
171}
172
Daniel Dunbar980c6692008-09-26 03:32:58 +0000173static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000174 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000175 if (!PT)
176 return false;
177
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000178 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000179 if (!RT)
180 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000181
Daniel Dunbar980c6692008-09-26 03:32:58 +0000182 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000183 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000184 return false;
185
186 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
187}
188
Richard Smithb87c4652013-10-31 21:23:20 +0000189static unsigned getNumAttributeArgs(const AttributeList &Attr) {
190 // FIXME: Include the type in the argument list.
191 return Attr.getNumArgs() + Attr.hasParsedType();
192}
193
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000194/// \brief Check if the attribute has exactly as many args as Num. May
195/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000196static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000197 unsigned Num) {
198 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
200 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000201 return false;
202 }
203
204 return true;
205}
206
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000207/// \brief Check if the attribute has at least as many args as Num. May
208/// output an error.
209static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000210 unsigned Num) {
211 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000212 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
213 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214 return false;
215 }
216
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217 return true;
218}
219
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000220/// \brief If Expr is a valid integer constant, get the value of the integer
221/// expression and return success or failure. May output an error.
222static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
223 const Expr *Expr, uint32_t &Val,
224 unsigned Idx = UINT_MAX) {
225 llvm::APSInt I(32);
226 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
227 !Expr->isIntegerConstantExpr(I, S.Context)) {
228 if (Idx != UINT_MAX)
229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
230 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
231 << Expr->getSourceRange();
232 else
233 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
234 << Attr.getName() << AANT_ArgumentIntegerConstant
235 << Expr->getSourceRange();
236 return false;
237 }
238 Val = (uint32_t)I.getZExtValue();
239 return true;
240}
241
Aaron Ballmanfb763042013-12-02 18:05:46 +0000242/// \brief Diagnose mutually exclusive attributes when present on a given
243/// declaration. Returns true if diagnosed.
244template <typename AttrTy>
245static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000246 const AttributeList &Attr) {
247 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000248 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000249 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000250 return true;
251 }
252 return false;
253}
254
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000255/// \brief Check if IdxExpr is a valid argument index for a function or
256/// instance method D. May output an error.
257///
258/// \returns true if IdxExpr is a valid index.
259static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000260 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000261 unsigned AttrArgNum,
262 const Expr *IdxExpr,
263 uint64_t &Idx)
264{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000265 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266
267 // In C++ the implicit 'this' function parameter also counts.
268 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000269 bool HP = hasFunctionProto(D);
270 bool HasImplicitThisParam = isInstanceMethod(D);
271 bool IV = HP && isFunctionOrMethodVariadic(D);
272 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
273 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000274
275 llvm::APSInt IdxInt;
276 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
277 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
279 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
280 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000281 return false;
282 }
283
284 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000285 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
287 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000288 return false;
289 }
290 Idx--; // Convert to zero-based.
291 if (HasImplicitThisParam) {
292 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000293 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000295 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000296 return false;
297 }
298 --Idx;
299 }
300
301 return true;
302}
303
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000304/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
305/// If not emit an error and return false. If the argument is an identifier it
306/// will emit an error with a fixit hint and treat it as if it was a string
307/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000308bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
309 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000310 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000311 // Look for identifiers. If we have one emit a hint to fix it to a literal.
312 if (Attr.isArgIdent(ArgNum)) {
313 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000314 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000315 << Attr.getName() << AANT_ArgumentString
316 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000317 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000318 Str = Loc->Ident->getName();
319 if (ArgLocation)
320 *ArgLocation = Loc->Loc;
321 return true;
322 }
323
324 // Now check for an actual string literal.
325 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
326 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
327 if (ArgLocation)
328 *ArgLocation = ArgExpr->getLocStart();
329
330 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000331 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000332 << Attr.getName() << AANT_ArgumentString;
333 return false;
334 }
335
336 Str = Literal->getString();
337 return true;
338}
339
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000340/// \brief Applies the given attribute to the Decl without performing any
341/// additional semantic checking.
342template <typename AttrType>
343static void handleSimpleAttribute(Sema &S, Decl *D,
344 const AttributeList &Attr) {
345 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
346 Attr.getAttributeSpellingListIndex()));
347}
348
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000349/// \brief Check if the passed-in expression is of type int or bool.
350static bool isIntOrBool(Expr *Exp) {
351 QualType QT = Exp->getType();
352 return QT->isBooleanType() || QT->isIntegerType();
353}
354
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000355
356// Check to see if the type is a smart pointer of some kind. We assume
357// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000358static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
359 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
360 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000361 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000363
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000364 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
365 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000366 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000367 return false;
368
369 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000370}
371
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000372/// \brief Check if passed in Decl is a pointer type.
373/// Note that this function may produce an error message.
374/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
376 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000377 const ValueDecl *vd = cast<ValueDecl>(D);
378 QualType QT = vd->getType();
379 if (QT->isAnyPointerType())
380 return true;
381
382 if (const RecordType *RT = QT->getAs<RecordType>()) {
383 // If it's an incomplete type, it could be a smart pointer; skip it.
384 // (We don't want to force template instantiation if we can avoid it,
385 // since that would alter the order in which templates are instantiated.)
386 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000387 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000388
Aaron Ballman553e6812013-12-26 14:54:11 +0000389 if (threadSafetyCheckIsSmartPointer(S, RT))
390 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000391 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000392
393 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000394 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000395 return false;
396}
397
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000398/// \brief Checks that the passed in QualType either is of RecordType or points
399/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000400static const RecordType *getRecordType(QualType QT) {
401 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000402 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000403
404 // Now check if we point to record type.
405 if (const PointerType *PT = QT->getAs<PointerType>())
406 return PT->getPointeeType()->getAs<RecordType>();
407
408 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000409}
410
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000411
Jordy Rose740b0c22012-05-08 03:27:22 +0000412static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
413 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000414 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000415 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000416}
417
418
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000420/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000421static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
422 QualType Ty) {
423 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000424
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000425 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000426 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000427 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000428 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000429 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000430 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000431
Michael Hana9171bc2012-08-03 17:40:43 +0000432 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000433 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435
436 // Allow smart pointers to be used as lockable objects.
437 // FIXME -- Check the type that the smart pointer points to.
438 if (threadSafetyCheckIsSmartPointer(S, RT))
439 return;
440
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000441 // Check if the type is lockable.
442 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000443 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000444 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000445
446 // Else check if any base classes are lockable.
447 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
448 CXXBasePaths BPaths(false, false);
449 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
450 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000451 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452
453 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000454 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000455}
456
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000458/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459/// \param Sidx The attribute argument index to start checking with.
460/// \param ParamIdxOk Whether an argument can be indexing into a function
461/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000462static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 const AttributeList &Attr,
464 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 int Sidx = 0,
466 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000468 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000470 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000471 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000472 Args.push_back(ArgExp);
473 continue;
474 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000476 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000477 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000478 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000479 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000480 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000481 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000482 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000483 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000484
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000485 // We allow constant strings to be used as a placeholder for expressions
486 // that are not valid C++ syntax, but warn that they are ignored.
487 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
488 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000489 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000490 continue;
491 }
492
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000493 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000494
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000495 // A pointer to member expression of the form &MyClass::mu is treated
496 // specially -- we need to look at the type of the member.
497 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
498 if (UOp->getOpcode() == UO_AddrOf)
499 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
500 if (DRE->getDecl()->isCXXInstanceMember())
501 ArgTy = DRE->getDecl()->getType();
502
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000503 // First see if we can just cast to record type, or point to record type.
504 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000506 // Now check if we index into a record type function param.
507 if(!RT && ParamIdxOk) {
508 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
510 if(FD && IL) {
511 unsigned int NumParams = FD->getNumParams();
512 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000513 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
514 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
515 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
517 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000518 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000519 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000520 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 }
522 }
523
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000524 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000526 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528}
529
Chris Lattner58418ff2008-06-29 00:16:31 +0000530//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000531// Attribute Implementations
532//===----------------------------------------------------------------------===//
533
Daniel Dunbar032db472008-07-31 22:40:48 +0000534// FIXME: All this manual attribute parsing code is gross. At the
535// least add some helper functions to check most argument patterns (#
536// and types of args).
537
Michael Hana9171bc2012-08-03 17:40:43 +0000538static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000539 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000540 if (!threadSafetyCheckIsPointer(S, D, Attr))
541 return;
542
Michael Han99315932013-01-24 16:46:58 +0000543 D->addAttr(::new (S.Context)
544 PtGuardedVarAttr(Attr.getRange(), S.Context,
545 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000546}
547
Michael Hana9171bc2012-08-03 17:40:43 +0000548static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
549 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000550 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000551 SmallVector<Expr*, 1> Args;
552 // check that all arguments are lockable objects
553 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
554 unsigned Size = Args.size();
555 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000556 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000557
Michael Han3be3b442012-07-23 18:48:41 +0000558 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000559
Michael Han3be3b442012-07-23 18:48:41 +0000560 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000561}
562
Michael Han3be3b442012-07-23 18:48:41 +0000563static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
564 Expr *Arg = 0;
565 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
566 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000567
Michael Han3be3b442012-07-23 18:48:41 +0000568 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
569}
570
Michael Hana9171bc2012-08-03 17:40:43 +0000571static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000572 const AttributeList &Attr) {
573 Expr *Arg = 0;
574 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
575 return;
576
577 if (!threadSafetyCheckIsPointer(S, D, Attr))
578 return;
579
580 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
581 S.Context, Arg));
582}
583
Michael Hana9171bc2012-08-03 17:40:43 +0000584static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
585 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000586 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000587 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000588 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000589
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000590 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000591 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 if (!QT->isDependentType()) {
593 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000594 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000595 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000596 << Attr.getName();
597 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000598 }
599 }
600
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000601 // Check that all arguments are lockable objects.
602 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000603 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000604 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000605
Michael Han3be3b442012-07-23 18:48:41 +0000606 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000607}
608
Michael Hana9171bc2012-08-03 17:40:43 +0000609static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
611 SmallVector<Expr*, 1> Args;
612 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
613 return;
614
615 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000616 D->addAttr(::new (S.Context)
617 AcquiredAfterAttr(Attr.getRange(), S.Context,
618 StartArg, Args.size(),
619 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000620}
621
Michael Hana9171bc2012-08-03 17:40:43 +0000622static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000623 const AttributeList &Attr) {
624 SmallVector<Expr*, 1> Args;
625 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
626 return;
627
628 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000629 D->addAttr(::new (S.Context)
630 AcquiredBeforeAttr(Attr.getRange(), S.Context,
631 StartArg, Args.size(),
632 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000633}
634
Michael Hana9171bc2012-08-03 17:40:43 +0000635static bool checkLockFunAttrCommon(Sema &S, Decl *D,
636 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000637 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000638 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000639 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000640 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000641
Michael Han3be3b442012-07-23 18:48:41 +0000642 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643}
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000646 const AttributeList &Attr) {
647 SmallVector<Expr*, 1> Args;
648 if (!checkLockFunAttrCommon(S, D, Attr, Args))
649 return;
650
651 unsigned Size = Args.size();
652 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000653 D->addAttr(::new (S.Context)
654 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
655 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000656}
657
Michael Hana9171bc2012-08-03 17:40:43 +0000658static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000659 const AttributeList &Attr) {
660 SmallVector<Expr*, 1> Args;
661 if (!checkLockFunAttrCommon(S, D, Attr, Args))
662 return;
663
664 unsigned Size = Args.size();
665 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000666 D->addAttr(::new (S.Context)
667 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
668 StartArg, Size,
669 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000670}
671
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000672static void handleAssertSharedLockAttr(Sema &S, Decl *D,
673 const AttributeList &Attr) {
674 SmallVector<Expr*, 1> Args;
675 if (!checkLockFunAttrCommon(S, D, Attr, Args))
676 return;
677
678 unsigned Size = Args.size();
679 Expr **StartArg = Size == 0 ? 0 : &Args[0];
680 D->addAttr(::new (S.Context)
681 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
682 Attr.getAttributeSpellingListIndex()));
683}
684
685static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
686 const AttributeList &Attr) {
687 SmallVector<Expr*, 1> Args;
688 if (!checkLockFunAttrCommon(S, D, Attr, Args))
689 return;
690
691 unsigned Size = Args.size();
692 Expr **StartArg = Size == 0 ? 0 : &Args[0];
693 D->addAttr(::new (S.Context)
694 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
695 StartArg, Size,
696 Attr.getAttributeSpellingListIndex()));
697}
698
699
Michael Hana9171bc2012-08-03 17:40:43 +0000700static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
701 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000702 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000703 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000705
Aaron Ballman00e99962013-08-31 01:11:41 +0000706 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000708 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000709 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000710 }
711
712 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000713 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000714
Michael Han3be3b442012-07-23 18:48:41 +0000715 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000716}
717
Michael Hana9171bc2012-08-03 17:40:43 +0000718static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000719 const AttributeList &Attr) {
720 SmallVector<Expr*, 2> Args;
721 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
722 return;
723
Michael Han99315932013-01-24 16:46:58 +0000724 D->addAttr(::new (S.Context)
725 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000726 Attr.getArgAsExpr(0),
727 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000728 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000729}
730
Michael Hana9171bc2012-08-03 17:40:43 +0000731static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000732 const AttributeList &Attr) {
733 SmallVector<Expr*, 2> Args;
734 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
735 return;
736
Michael Han99315932013-01-24 16:46:58 +0000737 D->addAttr(::new (S.Context)
738 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000739 Attr.getArgAsExpr(0),
740 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000741 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000742}
743
Michael Hana9171bc2012-08-03 17:40:43 +0000744static bool checkLocksRequiredCommon(Sema &S, Decl *D,
745 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000746 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000747 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000750 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000751 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000752 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000753 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000754
Michael Han3be3b442012-07-23 18:48:41 +0000755 return true;
756}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000757
Michael Hana9171bc2012-08-03 17:40:43 +0000758static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000759 const AttributeList &Attr) {
760 SmallVector<Expr*, 1> Args;
761 if (!checkLocksRequiredCommon(S, D, Attr, Args))
762 return;
763
764 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000765 D->addAttr(::new (S.Context)
766 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
767 StartArg, Args.size(),
768 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000769}
770
Michael Hana9171bc2012-08-03 17:40:43 +0000771static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000772 const AttributeList &Attr) {
773 SmallVector<Expr*, 1> Args;
774 if (!checkLocksRequiredCommon(S, D, Attr, Args))
775 return;
776
777 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000778 D->addAttr(::new (S.Context)
779 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
780 StartArg, Args.size(),
781 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000782}
783
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000785 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000788 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000789 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000790 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000791 Expr **StartArg = Size == 0 ? 0 : &Args[0];
792
Michael Han99315932013-01-24 16:46:58 +0000793 D->addAttr(::new (S.Context)
794 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
795 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000796}
797
798static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000799 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000800 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000801 SmallVector<Expr*, 1> Args;
802 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
803 unsigned Size = Args.size();
804 if (Size == 0)
805 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000806
Michael Han99315932013-01-24 16:46:58 +0000807 D->addAttr(::new (S.Context)
808 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
809 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000810}
811
812static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000813 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000814 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000815 return;
816
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000817 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000818 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000819 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000820 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000821 if (Size == 0)
822 return;
823 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000824
Michael Han99315932013-01-24 16:46:58 +0000825 D->addAttr(::new (S.Context)
826 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
827 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000828}
829
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000830static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
831 Expr *Cond = Attr.getArgAsExpr(0);
832 if (!Cond->isTypeDependent()) {
833 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
834 if (Converted.isInvalid())
835 return;
836 Cond = Converted.take();
837 }
838
839 StringRef Msg;
840 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
841 return;
842
843 SmallVector<PartialDiagnosticAt, 8> Diags;
844 if (!Cond->isValueDependent() &&
845 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
846 Diags)) {
847 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
848 for (int I = 0, N = Diags.size(); I != N; ++I)
849 S.Diag(Diags[I].first, Diags[I].second);
850 return;
851 }
852
853 D->addAttr(::new (S.Context)
854 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
855 Attr.getAttributeSpellingListIndex()));
856}
857
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000858static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000859 ConsumableAttr::ConsumedState DefaultState;
860
861 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000862 IdentifierLoc *IL = Attr.getArgAsIdent(0);
863 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
864 DefaultState)) {
865 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
866 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000867 return;
868 }
David Blaikie16f76d22013-09-06 01:28:43 +0000869 } else {
870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
871 << Attr.getName() << AANT_ArgumentIdentifier;
872 return;
873 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000874
875 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000876 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000877 Attr.getAttributeSpellingListIndex()));
878}
879
880static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
881 const AttributeList &Attr) {
882 ASTContext &CurrContext = S.getASTContext();
883 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
884
885 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
886 if (!RD->hasAttr<ConsumableAttr>()) {
887 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
888 RD->getNameAsString();
889
890 return false;
891 }
892 }
893
894 return true;
895}
896
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000897
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000898static void handleCallableWhenAttr(Sema &S, Decl *D,
899 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000900 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
901 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000902
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000903 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
904 return;
905
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000906 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
907 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
908 CallableWhenAttr::ConsumedState CallableState;
909
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000910 StringRef StateString;
911 SourceLocation Loc;
912 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
913 return;
914
915 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000916 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000917 S.Diag(Loc, diag::warn_attribute_type_not_supported)
918 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000919 return;
920 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000921
922 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000923 }
924
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000925 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000926 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
927 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000928}
929
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000930
DeLesley Hutchins69391772013-10-17 23:23:53 +0000931static void handleParamTypestateAttr(Sema &S, Decl *D,
932 const AttributeList &Attr) {
933 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000934
DeLesley Hutchins69391772013-10-17 23:23:53 +0000935 ParamTypestateAttr::ConsumedState ParamState;
936
937 if (Attr.isArgIdent(0)) {
938 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
939 StringRef StateString = Ident->Ident->getName();
940
941 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
942 ParamState)) {
943 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
944 << Attr.getName() << StateString;
945 return;
946 }
947 } else {
948 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
949 Attr.getName() << AANT_ArgumentIdentifier;
950 return;
951 }
952
953 // FIXME: This check is currently being done in the analysis. It can be
954 // enabled here only after the parser propagates attributes at
955 // template specialization definition, not declaration.
956 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
957 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
958 //
959 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
960 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
961 // ReturnType.getAsString();
962 // return;
963 //}
964
965 D->addAttr(::new (S.Context)
966 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
967 Attr.getAttributeSpellingListIndex()));
968}
969
970
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000971static void handleReturnTypestateAttr(Sema &S, Decl *D,
972 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000973 if (!checkAttributeNumArgs(S, Attr, 1)) return;
974
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000975 ReturnTypestateAttr::ConsumedState ReturnState;
976
977 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000978 IdentifierLoc *IL = Attr.getArgAsIdent(0);
979 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
980 ReturnState)) {
981 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
982 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000983 return;
984 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000985 } else {
986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
987 Attr.getName() << AANT_ArgumentIdentifier;
988 return;
989 }
990
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000991 // FIXME: This check is currently being done in the analysis. It can be
992 // enabled here only after the parser propagates attributes at
993 // template specialization definition, not declaration.
994 //QualType ReturnType;
995 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000996 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
997 // ReturnType = Param->getType();
998 //
999 //} else if (const CXXConstructorDecl *Constructor =
1000 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001001 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1002 //
1003 //} else {
1004 //
1005 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1006 //}
1007 //
1008 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1009 //
1010 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1011 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1012 // ReturnType.getAsString();
1013 // return;
1014 //}
1015
1016 D->addAttr(::new (S.Context)
1017 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1018 Attr.getAttributeSpellingListIndex()));
1019}
1020
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001021
1022static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001023 if (!checkAttributeNumArgs(S, Attr, 1))
1024 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001025
1026 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1027 return;
1028
1029 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001030 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001031 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1032 StringRef Param = Ident->Ident->getName();
1033 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1034 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1035 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001036 return;
1037 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001038 } else {
1039 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1040 Attr.getName() << AANT_ArgumentIdentifier;
1041 return;
1042 }
1043
1044 D->addAttr(::new (S.Context)
1045 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1046 Attr.getAttributeSpellingListIndex()));
1047}
1048
Chris Wailes9385f9f2013-10-29 20:28:41 +00001049static void handleTestTypestateAttr(Sema &S, Decl *D,
1050 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001051 if (!checkAttributeNumArgs(S, Attr, 1))
1052 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001053
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001054 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1055 return;
1056
Chris Wailes9385f9f2013-10-29 20:28:41 +00001057 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001058 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001059 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1060 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001061 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001062 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1063 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001064 return;
1065 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001066 } else {
1067 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1068 Attr.getName() << AANT_ArgumentIdentifier;
1069 return;
1070 }
1071
1072 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001073 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001074 Attr.getAttributeSpellingListIndex()));
1075}
1076
Chandler Carruthedc2c642011-07-02 00:01:44 +00001077static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1078 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001079 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001080 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081}
1082
Chandler Carruthedc2c642011-07-02 00:01:44 +00001083static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001084 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001085 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001086 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001087 // If the alignment is less than or equal to 8 bits, the packed attribute
1088 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001089 if (!FD->getType()->isDependentType() &&
1090 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001091 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001092 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001093 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001094 else
Michael Han99315932013-01-24 16:46:58 +00001095 FD->addAttr(::new (S.Context)
1096 PackedAttr(Attr.getRange(), S.Context,
1097 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001098 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001099 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001100}
1101
Ted Kremenek7fd17232011-09-29 07:02:25 +00001102static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1103 // The IBOutlet/IBOutletCollection attributes only apply to instance
1104 // variables or properties of Objective-C classes. The outlet must also
1105 // have an object reference type.
1106 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1107 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001108 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001109 << Attr.getName() << VD->getType() << 0;
1110 return false;
1111 }
1112 }
1113 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1114 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001115 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001116 << Attr.getName() << PD->getType() << 1;
1117 return false;
1118 }
1119 }
1120 else {
1121 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1122 return false;
1123 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001124
Ted Kremenek7fd17232011-09-29 07:02:25 +00001125 return true;
1126}
1127
Chandler Carruthedc2c642011-07-02 00:01:44 +00001128static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001129 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001130 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001131
Michael Han99315932013-01-24 16:46:58 +00001132 D->addAttr(::new (S.Context)
1133 IBOutletAttr(Attr.getRange(), S.Context,
1134 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001135}
1136
Chandler Carruthedc2c642011-07-02 00:01:44 +00001137static void handleIBOutletCollection(Sema &S, Decl *D,
1138 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001139
1140 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001141 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1143 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001144 return;
1145 }
1146
Ted Kremenek7fd17232011-09-29 07:02:25 +00001147 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001148 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001149
Richard Smithb1f9a282013-10-31 01:56:18 +00001150 ParsedType PT;
1151
1152 if (Attr.hasParsedType())
1153 PT = Attr.getTypeArg();
1154 else {
1155 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1156 S.getScopeForContext(D->getDeclContext()->getParent()));
1157 if (!PT) {
1158 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1159 return;
1160 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001161 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001162
Richard Smithb87c4652013-10-31 21:23:20 +00001163 TypeSourceInfo *QTLoc = 0;
1164 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1165 if (!QTLoc)
1166 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001167
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001168 // Diagnose use of non-object type in iboutletcollection attribute.
1169 // FIXME. Gnu attribute extension ignores use of builtin types in
1170 // attributes. So, __attribute__((iboutletcollection(char))) will be
1171 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001172 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001173 S.Diag(Attr.getLoc(),
1174 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1175 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001176 return;
1177 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001178
Michael Han99315932013-01-24 16:46:58 +00001179 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001180 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001181 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001182}
1183
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001184static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001185 if (const RecordType *UT = T->getAsUnionType())
1186 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1187 RecordDecl *UD = UT->getDecl();
1188 for (RecordDecl::field_iterator it = UD->field_begin(),
1189 itend = UD->field_end(); it != itend; ++it) {
1190 QualType QT = it->getType();
1191 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1192 T = QT;
1193 return;
1194 }
1195 }
1196 }
1197}
1198
Chandler Carruthedc2c642011-07-02 00:01:44 +00001199static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001200 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1201 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001202 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001203 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001204 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001205 return;
1206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001207
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001208 SmallVector<unsigned, 8> NonNullArgs;
1209 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001210 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001211 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001212 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001213 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001214
1215 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001216 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001217 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001218
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001219 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001220 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001221 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1222 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001223 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001224 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001225
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001226 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001228
1229 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1230 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001231 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001232 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1233 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001234 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001235 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001236 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001237 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001238
Ted Kremenek22813f42010-10-21 18:49:36 +00001239 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001240 if (NonNullArgs.empty()) {
1241 // Warn the trivial case only if attribute is not coming from a
1242 // macro instantiation.
1243 if (Attr.getLoc().isFileID())
1244 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001245 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001246 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001247 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001248
Nick Lewyckye1121512013-01-24 01:12:16 +00001249 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001250 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001252 D->addAttr(::new (S.Context)
1253 NonNullAttr(Attr.getRange(), S.Context, start, size,
1254 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001255}
1256
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001257static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1258 switch (K) {
1259 case OwnershipAttr::Holds: return "'ownership_holds'";
1260 case OwnershipAttr::Takes: return "'ownership_takes'";
1261 case OwnershipAttr::Returns: return "'ownership_returns'";
1262 }
1263 llvm_unreachable("unknown ownership");
1264}
1265
Chandler Carruthedc2c642011-07-02 00:01:44 +00001266static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001267 // This attribute must be applied to a function declaration. The first
1268 // argument to the attribute must be an identifier, the name of the resource,
1269 // for example: malloc. The following arguments must be argument indexes, the
1270 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001271 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001272 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001273 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001274
Aaron Ballman00e99962013-08-31 01:11:41 +00001275 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001276 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001277 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001278 return;
1279 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001280
Richard Smith852e9ce2013-11-27 01:46:48 +00001281 // Figure out our Kind.
1282 OwnershipAttr::OwnershipKind K =
1283 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1284 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001285
Richard Smith852e9ce2013-11-27 01:46:48 +00001286 // Check arguments.
1287 switch (K) {
1288 case OwnershipAttr::Takes:
1289 case OwnershipAttr::Holds:
1290 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001291 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1292 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001293 return;
1294 }
1295 break;
1296 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001297 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001298 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1299 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001300 return;
1301 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001302 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303 }
1304
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001305 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001306 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1307 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001308 return;
1309 }
1310
Richard Smith852e9ce2013-11-27 01:46:48 +00001311 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001312
1313 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001314 StringRef ModuleName = Module->getName();
1315 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1316 ModuleName.size() > 4) {
1317 ModuleName = ModuleName.drop_front(2).drop_back(2);
1318 Module = &S.PP.getIdentifierTable().get(ModuleName);
1319 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001320
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001321 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001322 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1323 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001324 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001325 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001326 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001327
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001328 // Is the function argument a pointer type?
1329 QualType T = getFunctionOrMethodArgType(D, Idx);
1330 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001331 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001332 case OwnershipAttr::Takes:
1333 case OwnershipAttr::Holds:
1334 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1335 Err = 0;
1336 break;
1337 case OwnershipAttr::Returns:
1338 if (!T->isIntegerType())
1339 Err = 1;
1340 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001341 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001342 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001343 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001344 << Ex->getSourceRange();
1345 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001346 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001347
1348 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001349 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001350 i = D->specific_attr_begin<OwnershipAttr>(),
1351 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001352 // FIXME: A returns attribute should conflict with any returns attribute
1353 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001354 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1355 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1356 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1357 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1358 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001359 }
1360 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001361 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001362 }
1363
1364 unsigned* start = OwnershipArgs.data();
1365 unsigned size = OwnershipArgs.size();
1366 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001367
Michael Han99315932013-01-24 16:46:58 +00001368 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001369 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001370 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001371}
1372
Chandler Carruthedc2c642011-07-02 00:01:44 +00001373static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001374 // Check the attribute arguments.
1375 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001376 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1377 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001378 return;
1379 }
1380
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001381 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001382
Rafael Espindolac18086a2010-02-23 22:00:30 +00001383 // gcc rejects
1384 // class c {
1385 // static int a __attribute__((weakref ("v2")));
1386 // static int b() __attribute__((weakref ("f3")));
1387 // };
1388 // and ignores the attributes of
1389 // void f(void) {
1390 // static int a __attribute__((weakref ("v2")));
1391 // }
1392 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001393 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001394 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001395 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1396 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001397 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001398 }
1399
1400 // The GCC manual says
1401 //
1402 // At present, a declaration to which `weakref' is attached can only
1403 // be `static'.
1404 //
1405 // It also says
1406 //
1407 // Without a TARGET,
1408 // given as an argument to `weakref' or to `alias', `weakref' is
1409 // equivalent to `weak'.
1410 //
1411 // gcc 4.4.1 will accept
1412 // int a7 __attribute__((weakref));
1413 // as
1414 // int a7 __attribute__((weak));
1415 // This looks like a bug in gcc. We reject that for now. We should revisit
1416 // it if this behaviour is actually used.
1417
Rafael Espindolac18086a2010-02-23 22:00:30 +00001418 // GCC rejects
1419 // static ((alias ("y"), weakref)).
1420 // Should we? How to check that weakref is before or after alias?
1421
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001422 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1423 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1424 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001425 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001426 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001427 // GCC will accept anything as the argument of weakref. Should we
1428 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001429 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1430 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001431
Michael Han99315932013-01-24 16:46:58 +00001432 D->addAttr(::new (S.Context)
1433 WeakRefAttr(Attr.getRange(), S.Context,
1434 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001435}
1436
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001437static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1438 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001439 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001440 return;
1441
Douglas Gregore8bbc122011-09-02 00:18:52 +00001442 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001443 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1444 return;
1445 }
1446
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001447 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001448
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001449 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001450 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001451}
1452
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001453static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001454 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001455 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001456
Michael Han99315932013-01-24 16:46:58 +00001457 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1458 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001459}
1460
1461static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001462 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001463 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001464
Michael Han99315932013-01-24 16:46:58 +00001465 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1466 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001467}
1468
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001469static void handleTLSModelAttr(Sema &S, Decl *D,
1470 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001471 StringRef Model;
1472 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001473 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001474 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001475 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001476
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001477 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001478 if (Model != "global-dynamic" && Model != "local-dynamic"
1479 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001480 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001481 return;
1482 }
1483
Michael Han99315932013-01-24 16:46:58 +00001484 D->addAttr(::new (S.Context)
1485 TLSModelAttr(Attr.getRange(), S.Context, Model,
1486 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001487}
1488
Chandler Carruthedc2c642011-07-02 00:01:44 +00001489static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001490 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001491 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001492 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001493 D->addAttr(::new (S.Context)
1494 MallocAttr(Attr.getRange(), S.Context,
1495 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001496 return;
1497 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001498 }
1499
Ted Kremenek08479ae2009-08-15 00:51:46 +00001500 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001501}
1502
Chandler Carruthedc2c642011-07-02 00:01:44 +00001503static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001504 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001505 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1506 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001507 return;
1508 }
1509
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001510 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1511 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001512}
1513
Chandler Carruthedc2c642011-07-02 00:01:44 +00001514static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001515 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001516
1517 if (S.CheckNoReturnAttr(attr)) return;
1518
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001519 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001520 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001521 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001522 return;
1523 }
1524
Michael Han99315932013-01-24 16:46:58 +00001525 D->addAttr(::new (S.Context)
1526 NoReturnAttr(attr.getRange(), S.Context,
1527 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001528}
1529
1530bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001531 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001532 attr.setInvalid();
1533 return true;
1534 }
1535
1536 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001537}
1538
Chandler Carruthedc2c642011-07-02 00:01:44 +00001539static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1540 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001541
1542 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1543 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001544 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1545 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001546 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1547 && !VD->getType()->isFunctionPointerType())) {
1548 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001549 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001550 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001551 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001552 return;
1553 }
1554 }
1555
Michael Han99315932013-01-24 16:46:58 +00001556 D->addAttr(::new (S.Context)
1557 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1558 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001559}
1560
John Thompsoncdb847ba2010-08-09 21:53:52 +00001561// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001562static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001563/*
1564 Returning a Vector Class in Registers
1565
Eric Christopherbc638a82010-12-01 22:13:54 +00001566 According to the PPU ABI specifications, a class with a single member of
1567 vector type is returned in memory when used as the return value of a function.
1568 This results in inefficient code when implementing vector classes. To return
1569 the value in a single vector register, add the vecreturn attribute to the
1570 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001571
1572 Example:
1573
1574 struct Vector
1575 {
1576 __vector float xyzw;
1577 } __attribute__((vecreturn));
1578
1579 Vector Add(Vector lhs, Vector rhs)
1580 {
1581 Vector result;
1582 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1583 return result; // This will be returned in a register
1584 }
1585*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001586 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1587 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001588 return;
1589 }
1590
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001591 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001592 int count = 0;
1593
1594 if (!isa<CXXRecordDecl>(record)) {
1595 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1596 return;
1597 }
1598
1599 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1600 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1601 return;
1602 }
1603
Eric Christopherbc638a82010-12-01 22:13:54 +00001604 for (RecordDecl::field_iterator iter = record->field_begin();
1605 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001606 if ((count == 1) || !iter->getType()->isVectorType()) {
1607 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1608 return;
1609 }
1610 count++;
1611 }
1612
Michael Han99315932013-01-24 16:46:58 +00001613 D->addAttr(::new (S.Context)
1614 VecReturnAttr(Attr.getRange(), S.Context,
1615 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001616}
1617
Richard Smithe233fbf2013-01-28 22:42:45 +00001618static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1619 const AttributeList &Attr) {
1620 if (isa<ParmVarDecl>(D)) {
1621 // [[carries_dependency]] can only be applied to a parameter if it is a
1622 // parameter of a function declaration or lambda.
1623 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1624 S.Diag(Attr.getLoc(),
1625 diag::err_carries_dependency_param_not_function_decl);
1626 return;
1627 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001628 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001629
1630 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1631 Attr.getRange(), S.Context,
1632 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001633}
1634
Chandler Carruthedc2c642011-07-02 00:01:44 +00001635static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001636 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001637 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001638 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001639 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001640 return;
1641 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001642
Michael Han99315932013-01-24 16:46:58 +00001643 D->addAttr(::new (S.Context)
1644 UnusedAttr(Attr.getRange(), S.Context,
1645 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001646}
1647
Chandler Carruthedc2c642011-07-02 00:01:44 +00001648static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001649 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001650 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001652 return;
1653 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001654 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001656 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001657 return;
1658 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001659
Michael Han99315932013-01-24 16:46:58 +00001660 D->addAttr(::new (S.Context)
1661 UsedAttr(Attr.getRange(), S.Context,
1662 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001663}
1664
Chandler Carruthedc2c642011-07-02 00:01:44 +00001665static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001666 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001667 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001668 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1669 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001670 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001671 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001672
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001673 uint32_t priority = ConstructorAttr::DefaultPriority;
1674 if (Attr.getNumArgs() > 0 &&
1675 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1676 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001677
Michael Han99315932013-01-24 16:46:58 +00001678 D->addAttr(::new (S.Context)
1679 ConstructorAttr(Attr.getRange(), S.Context, priority,
1680 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001681}
1682
Chandler Carruthedc2c642011-07-02 00:01:44 +00001683static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001684 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001685 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001686 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1687 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001688 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001689 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001690
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001691 uint32_t priority = ConstructorAttr::DefaultPriority;
1692 if (Attr.getNumArgs() > 0 &&
1693 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1694 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001695
Michael Han99315932013-01-24 16:46:58 +00001696 D->addAttr(::new (S.Context)
1697 DestructorAttr(Attr.getRange(), S.Context, priority,
1698 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001699}
1700
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001701template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001702static void handleAttrWithMessage(Sema &S, Decl *D,
1703 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001704 unsigned NumArgs = Attr.getNumArgs();
1705 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001706 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1707 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001708 return;
1709 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001710
1711 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001712 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001713 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001714 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Michael Han99315932013-01-24 16:46:58 +00001716 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1717 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001718}
1719
Ted Kremenek28eace62013-11-23 01:01:34 +00001720static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1721 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001722 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001723 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1724 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001725}
1726
Jordy Rose740b0c22012-05-08 03:27:22 +00001727static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1728 IdentifierInfo *Platform,
1729 VersionTuple Introduced,
1730 VersionTuple Deprecated,
1731 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001732 StringRef PlatformName
1733 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1734 if (PlatformName.empty())
1735 PlatformName = Platform->getName();
1736
1737 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1738 // of these steps are needed).
1739 if (!Introduced.empty() && !Deprecated.empty() &&
1740 !(Introduced <= Deprecated)) {
1741 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1742 << 1 << PlatformName << Deprecated.getAsString()
1743 << 0 << Introduced.getAsString();
1744 return true;
1745 }
1746
1747 if (!Introduced.empty() && !Obsoleted.empty() &&
1748 !(Introduced <= Obsoleted)) {
1749 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1750 << 2 << PlatformName << Obsoleted.getAsString()
1751 << 0 << Introduced.getAsString();
1752 return true;
1753 }
1754
1755 if (!Deprecated.empty() && !Obsoleted.empty() &&
1756 !(Deprecated <= Obsoleted)) {
1757 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1758 << 2 << PlatformName << Obsoleted.getAsString()
1759 << 1 << Deprecated.getAsString();
1760 return true;
1761 }
1762
1763 return false;
1764}
1765
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001766/// \brief Check whether the two versions match.
1767///
1768/// If either version tuple is empty, then they are assumed to match. If
1769/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1770static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1771 bool BeforeIsOkay) {
1772 if (X.empty() || Y.empty())
1773 return true;
1774
1775 if (X == Y)
1776 return true;
1777
1778 if (BeforeIsOkay && X < Y)
1779 return true;
1780
1781 return false;
1782}
1783
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001784AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001785 IdentifierInfo *Platform,
1786 VersionTuple Introduced,
1787 VersionTuple Deprecated,
1788 VersionTuple Obsoleted,
1789 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001790 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001791 bool Override,
1792 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001793 VersionTuple MergedIntroduced = Introduced;
1794 VersionTuple MergedDeprecated = Deprecated;
1795 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001796 bool FoundAny = false;
1797
Rafael Espindolac67f2232012-05-10 02:50:16 +00001798 if (D->hasAttrs()) {
1799 AttrVec &Attrs = D->getAttrs();
1800 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1801 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1802 if (!OldAA) {
1803 ++i;
1804 continue;
1805 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001806
Rafael Espindolac67f2232012-05-10 02:50:16 +00001807 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1808 if (OldPlatform != Platform) {
1809 ++i;
1810 continue;
1811 }
1812
1813 FoundAny = true;
1814 VersionTuple OldIntroduced = OldAA->getIntroduced();
1815 VersionTuple OldDeprecated = OldAA->getDeprecated();
1816 VersionTuple OldObsoleted = OldAA->getObsoleted();
1817 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001818
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001819 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1820 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1821 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1822 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001823 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001824 if (Override) {
1825 int Which = -1;
1826 VersionTuple FirstVersion;
1827 VersionTuple SecondVersion;
1828 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1829 Which = 0;
1830 FirstVersion = OldIntroduced;
1831 SecondVersion = Introduced;
1832 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1833 Which = 1;
1834 FirstVersion = Deprecated;
1835 SecondVersion = OldDeprecated;
1836 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1837 Which = 2;
1838 FirstVersion = Obsoleted;
1839 SecondVersion = OldObsoleted;
1840 }
1841
1842 if (Which == -1) {
1843 Diag(OldAA->getLocation(),
1844 diag::warn_mismatched_availability_override_unavail)
1845 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1846 } else {
1847 Diag(OldAA->getLocation(),
1848 diag::warn_mismatched_availability_override)
1849 << Which
1850 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1851 << FirstVersion.getAsString() << SecondVersion.getAsString();
1852 }
1853 Diag(Range.getBegin(), diag::note_overridden_method);
1854 } else {
1855 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1856 Diag(Range.getBegin(), diag::note_previous_attribute);
1857 }
1858
Rafael Espindolac67f2232012-05-10 02:50:16 +00001859 Attrs.erase(Attrs.begin() + i);
1860 --e;
1861 continue;
1862 }
1863
1864 VersionTuple MergedIntroduced2 = MergedIntroduced;
1865 VersionTuple MergedDeprecated2 = MergedDeprecated;
1866 VersionTuple MergedObsoleted2 = MergedObsoleted;
1867
1868 if (MergedIntroduced2.empty())
1869 MergedIntroduced2 = OldIntroduced;
1870 if (MergedDeprecated2.empty())
1871 MergedDeprecated2 = OldDeprecated;
1872 if (MergedObsoleted2.empty())
1873 MergedObsoleted2 = OldObsoleted;
1874
1875 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1876 MergedIntroduced2, MergedDeprecated2,
1877 MergedObsoleted2)) {
1878 Attrs.erase(Attrs.begin() + i);
1879 --e;
1880 continue;
1881 }
1882
1883 MergedIntroduced = MergedIntroduced2;
1884 MergedDeprecated = MergedDeprecated2;
1885 MergedObsoleted = MergedObsoleted2;
1886 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001887 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001888 }
1889
1890 if (FoundAny &&
1891 MergedIntroduced == Introduced &&
1892 MergedDeprecated == Deprecated &&
1893 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001894 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001895
Ted Kremenekb5445722013-04-06 00:34:27 +00001896 // Only create a new attribute if !Override, but we want to do
1897 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001898 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001899 MergedDeprecated, MergedObsoleted) &&
1900 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001901 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1902 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001903 Obsoleted, IsUnavailable, Message,
1904 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001905 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001906 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907}
1908
Chandler Carruthedc2c642011-07-02 00:01:44 +00001909static void handleAvailabilityAttr(Sema &S, Decl *D,
1910 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001911 if (!checkAttributeNumArgs(S, Attr, 1))
1912 return;
1913 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001914 unsigned Index = Attr.getAttributeSpellingListIndex();
1915
Aaron Ballman00e99962013-08-31 01:11:41 +00001916 IdentifierInfo *II = Platform->Ident;
1917 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1918 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1919 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001920
Rafael Espindolac231fab2013-01-08 21:30:32 +00001921 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1922 if (!ND) {
1923 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1924 return;
1925 }
1926
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001927 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1928 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1929 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001930 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001931 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001932 if (const StringLiteral *SE =
1933 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001934 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001935
Aaron Ballman00e99962013-08-31 01:11:41 +00001936 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001937 Introduced.Version,
1938 Deprecated.Version,
1939 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001940 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001941 /*Override=*/false,
1942 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001943 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001944 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001945}
1946
John McCalld041a9b2013-02-20 01:54:26 +00001947template <class T>
1948static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1949 typename T::VisibilityType value,
1950 unsigned attrSpellingListIndex) {
1951 T *existingAttr = D->getAttr<T>();
1952 if (existingAttr) {
1953 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1954 if (existingValue == value)
1955 return NULL;
1956 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1957 S.Diag(range.getBegin(), diag::note_previous_attribute);
1958 D->dropAttr<T>();
1959 }
1960 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1961}
1962
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001963VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001964 VisibilityAttr::VisibilityType Vis,
1965 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001966 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1967 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001968}
1969
John McCalld041a9b2013-02-20 01:54:26 +00001970TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1971 TypeVisibilityAttr::VisibilityType Vis,
1972 unsigned AttrSpellingListIndex) {
1973 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1974 AttrSpellingListIndex);
1975}
1976
1977static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1978 bool isTypeVisibility) {
1979 // Visibility attributes don't mean anything on a typedef.
1980 if (isa<TypedefNameDecl>(D)) {
1981 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1982 << Attr.getName();
1983 return;
1984 }
1985
1986 // 'type_visibility' can only go on a type or namespace.
1987 if (isTypeVisibility &&
1988 !(isa<TagDecl>(D) ||
1989 isa<ObjCInterfaceDecl>(D) ||
1990 isa<NamespaceDecl>(D))) {
1991 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1992 << Attr.getName() << ExpectedTypeOrNamespace;
1993 return;
1994 }
1995
Benjamin Kramer70370212013-09-09 15:08:57 +00001996 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001997 StringRef TypeStr;
1998 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001999 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002000 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002001
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002002 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002003 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002004 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002005 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002006 return;
2007 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002008
2009 // Complain about attempts to use protected visibility on targets
2010 // (like Darwin) that don't support it.
2011 if (type == VisibilityAttr::Protected &&
2012 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2013 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2014 type = VisibilityAttr::Default;
2015 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002016
Michael Han99315932013-01-24 16:46:58 +00002017 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002018 clang::Attr *newAttr;
2019 if (isTypeVisibility) {
2020 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2021 (TypeVisibilityAttr::VisibilityType) type,
2022 Index);
2023 } else {
2024 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2025 }
2026 if (newAttr)
2027 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002028}
2029
Chandler Carruthedc2c642011-07-02 00:01:44 +00002030static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2031 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002032 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002033 if (!Attr.isArgIdent(0)) {
2034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2035 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002036 return;
2037 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002038
Aaron Ballman682ee422013-09-11 19:47:58 +00002039 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2040 ObjCMethodFamilyAttr::FamilyKind F;
2041 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2042 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2043 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002044 return;
2045 }
2046
Aaron Ballman682ee422013-09-11 19:47:58 +00002047 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002048 !method->getResultType()->isObjCObjectPointerType()) {
2049 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2050 << method->getResultType();
2051 // Ignore the attribute.
2052 return;
2053 }
2054
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002055 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002056 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002057}
2058
Chandler Carruthedc2c642011-07-02 00:01:44 +00002059static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002060 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002061 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002062 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002063 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2064 return;
2065 }
2066 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002067 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2068 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002069 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002070 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2071 return;
2072 }
2073 }
2074 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002075 // It is okay to include this attribute on properties, e.g.:
2076 //
2077 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2078 //
2079 // In this case it follows tradition and suppresses an error in the above
2080 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002081 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002082 }
Michael Han99315932013-01-24 16:46:58 +00002083 D->addAttr(::new (S.Context)
2084 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2085 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002086}
2087
Chandler Carruthedc2c642011-07-02 00:01:44 +00002088static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002089 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002090 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002091 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002092 return;
2093 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002094
Aaron Ballman00e99962013-08-31 01:11:41 +00002095 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002096 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002097 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2098 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2099 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002100 return;
2101 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002102
Michael Han99315932013-01-24 16:46:58 +00002103 D->addAttr(::new (S.Context)
2104 BlocksAttr(Attr.getRange(), S.Context, type,
2105 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002106}
2107
Chandler Carruthedc2c642011-07-02 00:01:44 +00002108static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002109 // check the attribute arguments.
2110 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002111 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2112 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002113 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002114 }
2115
Aaron Ballman18a78382013-11-21 00:28:23 +00002116 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002117 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002118 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002119 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002120 if (E->isTypeDependent() || E->isValueDependent() ||
2121 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002122 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002123 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002124 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002125 return;
2126 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002127
John McCallb46f2872011-09-09 07:56:05 +00002128 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002129 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2130 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002131 return;
2132 }
John McCallb46f2872011-09-09 07:56:05 +00002133
2134 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002135 }
2136
Aaron Ballman18a78382013-11-21 00:28:23 +00002137 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002138 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002139 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002140 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002141 if (E->isTypeDependent() || E->isValueDependent() ||
2142 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002144 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002145 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002146 return;
2147 }
2148 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002149
John McCallb46f2872011-09-09 07:56:05 +00002150 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002151 // FIXME: This error message could be improved, it would be nice
2152 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002153 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2154 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002155 return;
2156 }
2157 }
2158
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002159 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002160 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002161 if (isa<FunctionNoProtoType>(FT)) {
2162 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2163 return;
2164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002165
Chris Lattner9363e312009-03-17 23:03:47 +00002166 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002167 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002168 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002169 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002170 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002171 if (!MD->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;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002174 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002175 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2176 if (!BD->isVariadic()) {
2177 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2178 return;
2179 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002180 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002181 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002182 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002183 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002184 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002185 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002186 int m = Ty->isFunctionPointerType() ? 0 : 1;
2187 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002188 return;
2189 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002190 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002192 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002193 return;
2194 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002195 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002197 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002198 return;
2199 }
Michael Han99315932013-01-24 16:46:58 +00002200 D->addAttr(::new (S.Context)
2201 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2202 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002203}
2204
Chandler Carruthedc2c642011-07-02 00:01:44 +00002205static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002206 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002207 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002208 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002209 return;
2210 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002211
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002212 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2213 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2214 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002215 return;
2216 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002217 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2218 if (MD->getResultType()->isVoidType()) {
2219 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2220 << Attr.getName() << 1;
2221 return;
2222 }
2223
Michael Han99315932013-01-24 16:46:58 +00002224 D->addAttr(::new (S.Context)
2225 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2226 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002227}
2228
Chandler Carruthedc2c642011-07-02 00:01:44 +00002229static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002230 // weak_import only applies to variable & function declarations.
2231 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002232 if (!D->canBeWeakImported(isDef)) {
2233 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002234 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2235 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002236 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002237 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002238 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002239 // Nothing to warn about here.
2240 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002241 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002242 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002243
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002244 return;
2245 }
2246
Michael Han99315932013-01-24 16:46:58 +00002247 D->addAttr(::new (S.Context)
2248 WeakImportAttr(Attr.getRange(), S.Context,
2249 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002250}
2251
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002252// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002253template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002254static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002255 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002256 uint32_t WGSize[3];
2257 for (unsigned i = 0; i < 3; ++i)
2258 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002259 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002260
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002261 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2262 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2263 Existing->getYDim() == WGSize[1] &&
2264 Existing->getZDim() == WGSize[2]))
2265 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002266
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002267 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2268 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002269 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002270}
2271
Joey Goulyaba589c2013-03-08 09:42:32 +00002272static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002273 if (!Attr.hasParsedType()) {
2274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2275 << Attr.getName() << 1;
2276 return;
2277 }
2278
Richard Smithb87c4652013-10-31 21:23:20 +00002279 TypeSourceInfo *ParmTSI = 0;
2280 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2281 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002282
2283 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2284 (ParmType->isBooleanType() ||
2285 !ParmType->isIntegralType(S.getASTContext()))) {
2286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2287 << ParmType;
2288 return;
2289 }
2290
Aaron Ballmana9e05402013-12-02 22:16:55 +00002291 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002292 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002293 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2294 return;
2295 }
2296 }
2297
2298 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002299 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002300}
2301
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002302SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002303 StringRef Name,
2304 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002305 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2306 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002307 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002308 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2309 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002310 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002311 }
Michael Han99315932013-01-24 16:46:58 +00002312 return ::new (Context) SectionAttr(Range, Context, Name,
2313 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002314}
2315
Chandler Carruthedc2c642011-07-02 00:01:44 +00002316static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002317 // Make sure that there is a string literal as the sections's single
2318 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002319 StringRef Str;
2320 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002321 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002322 return;
Mike Stump11289f42009-09-09 15:08:12 +00002323
Chris Lattner30ba6742009-08-10 19:03:04 +00002324 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002325 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002326 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002327 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002328 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002329 return;
2330 }
Mike Stump11289f42009-09-09 15:08:12 +00002331
Michael Han99315932013-01-24 16:46:58 +00002332 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002333 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002334 if (NewAttr)
2335 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002336}
2337
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002338
Chandler Carruthedc2c642011-07-02 00:01:44 +00002339static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002340 VarDecl *VD = cast<VarDecl>(D);
2341 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002342 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002343 return;
2344 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002345
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002346 Expr *E = Attr.getArgAsExpr(0);
2347 SourceLocation Loc = E->getExprLoc();
2348 FunctionDecl *FD = 0;
2349 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002350
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002351 // gcc only allows for simple identifiers. Since we support more than gcc, we
2352 // will warn the user.
2353 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2354 if (DRE->hasQualifier())
2355 S.Diag(Loc, diag::warn_cleanup_ext);
2356 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2357 NI = DRE->getNameInfo();
2358 if (!FD) {
2359 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2360 << NI.getName();
2361 return;
2362 }
2363 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2364 if (ULE->hasExplicitTemplateArgs())
2365 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002366 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2367 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002368 if (!FD) {
2369 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2370 << NI.getName();
2371 if (ULE->getType() == S.Context.OverloadTy)
2372 S.NoteAllOverloadCandidates(ULE);
2373 return;
2374 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002375 } else {
2376 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002377 return;
2378 }
2379
Anders Carlssond277d792009-01-31 01:16:18 +00002380 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002381 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2382 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002383 return;
2384 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002385
Anders Carlsson723f55d2009-02-07 23:16:50 +00002386 // We're currently more strict than GCC about what function types we accept.
2387 // If this ever proves to be a problem it should be easy to fix.
2388 QualType Ty = S.Context.getPointerType(VD->getType());
2389 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002390 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2391 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002392 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2393 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002394 return;
2395 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002396
Michael Han99315932013-01-24 16:46:58 +00002397 D->addAttr(::new (S.Context)
2398 CleanupAttr(Attr.getRange(), S.Context, FD,
2399 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002400}
2401
Mike Stumpd3bb5572009-07-24 19:02:52 +00002402/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002403/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002404static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002405 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002406 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002407 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002408 return;
2409 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002410
Aaron Ballman00e99962013-08-31 01:11:41 +00002411 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002412 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002413 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002414 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002415
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002416 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002417 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002418
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002419 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2420 if (not_nsstring_type &&
2421 !isCFStringType(Ty, S.Context) &&
2422 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002423 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002424 // FIXME: Should highlight the actual expression that has the wrong type.
2425 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002426 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002427 << IdxExpr->getSourceRange();
2428 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002429 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002430 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002431 if (!isNSStringType(Ty, S.Context) &&
2432 !isCFStringType(Ty, S.Context) &&
2433 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002434 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002435 // FIXME: Should highlight the actual expression that has the wrong type.
2436 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002437 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002438 << IdxExpr->getSourceRange();
2439 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002440 }
2441
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002442 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2443 // because that has corrected for the implicit this parameter, and is zero-
2444 // based. The attribute expects what the user wrote explicitly.
2445 llvm::APSInt Val;
2446 IdxExpr->EvaluateAsInt(Val, S.Context);
2447
Michael Han99315932013-01-24 16:46:58 +00002448 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002449 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002450 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002451}
2452
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002453enum FormatAttrKind {
2454 CFStringFormat,
2455 NSStringFormat,
2456 StrftimeFormat,
2457 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002458 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002459 InvalidFormat
2460};
2461
2462/// getFormatAttrKind - Map from format attribute names to supported format
2463/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002464static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002465 return llvm::StringSwitch<FormatAttrKind>(Format)
2466 // Check for formats that get handled specially.
2467 .Case("NSString", NSStringFormat)
2468 .Case("CFString", CFStringFormat)
2469 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002470
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002471 // Otherwise, check for supported formats.
2472 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2473 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2474 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002475
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002476 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2477 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002478}
2479
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002480/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002481/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002482static void handleInitPriorityAttr(Sema &S, Decl *D,
2483 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002484 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002485 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2486 return;
2487 }
2488
Aaron Ballman4a611152013-11-27 16:34:09 +00002489 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002490 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2491 Attr.setInvalid();
2492 return;
2493 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002494 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002495 if (S.Context.getAsArrayType(T))
2496 T = S.Context.getBaseElementType(T);
2497 if (!T->getAs<RecordType>()) {
2498 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2499 Attr.setInvalid();
2500 return;
2501 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002502
2503 Expr *E = Attr.getArgAsExpr(0);
2504 uint32_t prioritynum;
2505 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002506 Attr.setInvalid();
2507 return;
2508 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002509
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002510 if (prioritynum < 101 || prioritynum > 65535) {
2511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002512 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002513 Attr.setInvalid();
2514 return;
2515 }
Michael Han99315932013-01-24 16:46:58 +00002516 D->addAttr(::new (S.Context)
2517 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2518 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002519}
2520
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002521FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2522 IdentifierInfo *Format, int FormatIdx,
2523 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002524 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002525 // Check whether we already have an equivalent format attribute.
2526 for (specific_attr_iterator<FormatAttr>
2527 i = D->specific_attr_begin<FormatAttr>(),
2528 e = D->specific_attr_end<FormatAttr>();
2529 i != e ; ++i) {
2530 FormatAttr *f = *i;
2531 if (f->getType() == Format &&
2532 f->getFormatIdx() == FormatIdx &&
2533 f->getFirstArg() == FirstArg) {
2534 // If we don't have a valid location for this attribute, adopt the
2535 // location.
2536 if (f->getLocation().isInvalid())
2537 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002538 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002539 }
2540 }
2541
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002542 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2543 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002544}
2545
Mike Stumpd3bb5572009-07-24 19:02:52 +00002546/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002547/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002548static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002549 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002550 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002551 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002552 return;
2553 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002555 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002557 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002558 return;
2559 }
2560
Chandler Carruth743682b2010-11-16 08:35:43 +00002561 // In C++ the implicit 'this' function parameter also counts, and they are
2562 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002563 bool HasImplicitThisParam = isInstanceMethod(D);
2564 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002565
Aaron Ballman00e99962013-08-31 01:11:41 +00002566 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2567 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002568
2569 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002570 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002571 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002572 // If we've modified the string name, we need a new identifier for it.
2573 II = &S.Context.Idents.get(Format);
2574 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002575
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002576 // Check for supported formats.
2577 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002578
2579 if (Kind == IgnoredFormat)
2580 return;
2581
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002582 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002583 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002584 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585 return;
2586 }
2587
2588 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002589 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002590 uint32_t Idx;
2591 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002594 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002595 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002596 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002597 return;
2598 }
2599
2600 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002601 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002603 if (HasImplicitThisParam) {
2604 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002605 S.Diag(Attr.getLoc(),
2606 diag::err_format_attribute_implicit_this_format_string)
2607 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002608 return;
2609 }
2610 ArgIdx--;
2611 }
Mike Stump11289f42009-09-09 15:08:12 +00002612
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002613 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002614 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002615
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002616 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002617 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002618 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2619 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002620 return;
2621 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002622 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002623 // FIXME: do we need to check if the type is NSString*? What are the
2624 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002625 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002626 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002627 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2628 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002629 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002630 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002632 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002633 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002634 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2635 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002636 return;
2637 }
2638
2639 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002640 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002641 uint32_t FirstArg;
2642 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002643 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644
2645 // check if the function is variadic if the 3rd argument non-zero
2646 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002647 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002648 ++NumArgs; // +1 for ...
2649 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002650 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002651 return;
2652 }
2653 }
2654
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002655 // strftime requires FirstArg to be 0 because it doesn't read from any
2656 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002657 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002658 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002659 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2660 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002661 return;
2662 }
2663 // if 0 it disables parameter checking (to use with e.g. va_list)
2664 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002666 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002667 return;
2668 }
2669
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002670 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002671 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002672 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002673 if (NewAttr)
2674 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675}
2676
Chandler Carruthedc2c642011-07-02 00:01:44 +00002677static void handleTransparentUnionAttr(Sema &S, Decl *D,
2678 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002679 // Try to find the underlying union declaration.
2680 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002681 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002682 if (TD && TD->getUnderlyingType()->isUnionType())
2683 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2684 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002685 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002686
2687 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002689 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002690 return;
2691 }
2692
John McCallf937c022011-10-07 06:10:15 +00002693 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002694 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002695 diag::warn_transparent_union_attribute_not_definition);
2696 return;
2697 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002698
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002699 RecordDecl::field_iterator Field = RD->field_begin(),
2700 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002701 if (Field == FieldEnd) {
2702 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2703 return;
2704 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002705
David Blaikie40ed2972012-06-06 20:45:41 +00002706 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002707 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002708 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002709 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002710 diag::warn_transparent_union_attribute_floating)
2711 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002712 return;
2713 }
2714
2715 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2716 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2717 for (; Field != FieldEnd; ++Field) {
2718 QualType FieldType = Field->getType();
2719 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2720 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2721 // Warn if we drop the attribute.
2722 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002723 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002724 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002725 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002726 diag::warn_transparent_union_attribute_field_size_align)
2727 << isSize << Field->getDeclName() << FieldBits;
2728 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002729 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002730 diag::note_transparent_union_first_field_size_align)
2731 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002732 return;
2733 }
2734 }
2735
Michael Han99315932013-01-24 16:46:58 +00002736 RD->addAttr(::new (S.Context)
2737 TransparentUnionAttr(Attr.getRange(), S.Context,
2738 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002739}
2740
Chandler Carruthedc2c642011-07-02 00:01:44 +00002741static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002742 // Make sure that there is a string literal as the annotation's single
2743 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002744 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002745 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002746 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002747
2748 // Don't duplicate annotations that are already set.
2749 for (specific_attr_iterator<AnnotateAttr>
2750 i = D->specific_attr_begin<AnnotateAttr>(),
2751 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002752 if ((*i)->getAnnotation() == Str)
2753 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002754 }
Michael Han99315932013-01-24 16:46:58 +00002755
2756 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002757 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002758 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002759}
2760
Chandler Carruthedc2c642011-07-02 00:01:44 +00002761static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002762 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002763 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002764 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2765 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002766 return;
2767 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002768
Richard Smith848e1f12013-02-01 08:12:08 +00002769 if (Attr.getNumArgs() == 0) {
2770 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2771 true, 0, Attr.getAttributeSpellingListIndex()));
2772 return;
2773 }
2774
Aaron Ballman00e99962013-08-31 01:11:41 +00002775 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002776 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2777 S.Diag(Attr.getEllipsisLoc(),
2778 diag::err_pack_expansion_without_parameter_packs);
2779 return;
2780 }
2781
2782 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2783 return;
2784
2785 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2786 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002787}
2788
2789void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002790 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002791 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2792 SourceLocation AttrLoc = AttrRange.getBegin();
2793
Richard Smith1dba27c2013-01-29 09:02:09 +00002794 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002795 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002796 // C++11 [dcl.align]p1:
2797 // An alignment-specifier may be applied to a variable or to a class
2798 // data member, but it shall not be applied to a bit-field, a function
2799 // parameter, the formal parameter of a catch clause, or a variable
2800 // declared with the register storage class specifier. An
2801 // alignment-specifier may also be applied to the declaration of a class
2802 // or enumeration type.
2803 // C11 6.7.5/2:
2804 // An alignment attribute shall not be specified in a declaration of
2805 // a typedef, or a bit-field, or a function, or a parameter, or an
2806 // object declared with the register storage-class specifier.
2807 int DiagKind = -1;
2808 if (isa<ParmVarDecl>(D)) {
2809 DiagKind = 0;
2810 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2811 if (VD->getStorageClass() == SC_Register)
2812 DiagKind = 1;
2813 if (VD->isExceptionVariable())
2814 DiagKind = 2;
2815 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2816 if (FD->isBitField())
2817 DiagKind = 3;
2818 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002819 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002820 << (TmpAttr.isC11() ? ExpectedVariableOrField
2821 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002822 return;
2823 }
2824 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002825 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002826 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002827 return;
2828 }
2829 }
2830
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002831 if (E->isTypeDependent() || E->isValueDependent()) {
2832 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002833 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2834 AA->setPackExpansion(IsPackExpansion);
2835 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002836 return;
2837 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002838
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002839 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002840 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002841 ExprResult ICE
2842 = VerifyIntegerConstantExpression(E, &Alignment,
2843 diag::err_aligned_attribute_argument_not_int,
2844 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002845 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002846 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002847
2848 // C++11 [dcl.align]p2:
2849 // -- if the constant expression evaluates to zero, the alignment
2850 // specifier shall have no effect
2851 // C11 6.7.5p6:
2852 // An alignment specification of zero has no effect.
2853 if (!(TmpAttr.isAlignas() && !Alignment) &&
2854 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002855 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2856 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002857 return;
2858 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002859
Richard Smith848e1f12013-02-01 08:12:08 +00002860 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002861 // We've already verified it's a power of 2, now let's make sure it's
2862 // 8192 or less.
2863 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002864 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002865 << E->getSourceRange();
2866 return;
2867 }
2868 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002869
Richard Smith44c247f2013-02-22 08:32:16 +00002870 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2871 ICE.take(), SpellingListIndex);
2872 AA->setPackExpansion(IsPackExpansion);
2873 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002874}
2875
Michael Hanaf02bbe2013-02-01 01:19:17 +00002876void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002877 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002878 // FIXME: Cache the number on the Attr object if non-dependent?
2879 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002880 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2881 SpellingListIndex);
2882 AA->setPackExpansion(IsPackExpansion);
2883 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002884}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002885
Richard Smith848e1f12013-02-01 08:12:08 +00002886void Sema::CheckAlignasUnderalignment(Decl *D) {
2887 assert(D->hasAttrs() && "no attributes on decl");
2888
2889 QualType Ty;
2890 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2891 Ty = VD->getType();
2892 else
2893 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002894 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002895 return;
2896
2897 // C++11 [dcl.align]p5, C11 6.7.5/4:
2898 // The combined effect of all alignment attributes in a declaration shall
2899 // not specify an alignment that is less strict than the alignment that
2900 // would otherwise be required for the entity being declared.
2901 AlignedAttr *AlignasAttr = 0;
2902 unsigned Align = 0;
2903 for (specific_attr_iterator<AlignedAttr>
2904 I = D->specific_attr_begin<AlignedAttr>(),
2905 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2906 if (I->isAlignmentDependent())
2907 return;
2908 if (I->isAlignas())
2909 AlignasAttr = *I;
2910 Align = std::max(Align, I->getAlignment(Context));
2911 }
2912
2913 if (AlignasAttr && Align) {
2914 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2915 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2916 if (NaturalAlign > RequestedAlign)
2917 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2918 << Ty << (unsigned)NaturalAlign.getQuantity();
2919 }
2920}
2921
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002922/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002923/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002924///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002925/// Despite what would be logical, the mode attribute is a decl attribute, not a
2926/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2927/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002928static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002929 // This attribute isn't documented, but glibc uses it. It changes
2930 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002931 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2933 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002934 return;
2935 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002936
Aaron Ballman00e99962013-08-31 01:11:41 +00002937 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2938 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939
2940 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002941 if (Str.startswith("__") && Str.endswith("__"))
2942 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002943
2944 unsigned DestWidth = 0;
2945 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002946 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002947 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002948 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002949 switch (Str[0]) {
2950 case 'Q': DestWidth = 8; break;
2951 case 'H': DestWidth = 16; break;
2952 case 'S': DestWidth = 32; break;
2953 case 'D': DestWidth = 64; break;
2954 case 'X': DestWidth = 96; break;
2955 case 'T': DestWidth = 128; break;
2956 }
2957 if (Str[1] == 'F') {
2958 IntegerMode = false;
2959 } else if (Str[1] == 'C') {
2960 IntegerMode = false;
2961 ComplexMode = true;
2962 } else if (Str[1] != 'I') {
2963 DestWidth = 0;
2964 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002965 break;
2966 case 4:
2967 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2968 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002969 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002970 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002971 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002972 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002973 break;
2974 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002975 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002976 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002977 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002978 case 11:
2979 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002980 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002981 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002982 }
2983
2984 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002985 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002986 OldTy = TD->getUnderlyingType();
2987 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2988 OldTy = VD->getType();
2989 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002990 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002991 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002992 return;
2993 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002994
John McCall9dd450b2009-09-21 23:43:11 +00002995 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002996 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2997 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002998 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002999 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3000 } else if (ComplexMode) {
3001 if (!OldTy->isComplexType())
3002 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3003 } else {
3004 if (!OldTy->isFloatingType())
3005 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3006 }
3007
Mike Stump87c57ac2009-05-16 07:39:55 +00003008 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3009 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003010 // FIXME: Make sure floating-point mappings are accurate
3011 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003012 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003013 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003014 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003015 }
3016
3017 QualType NewTy;
3018
3019 if (IntegerMode)
3020 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3021 OldTy->isSignedIntegerType());
3022 else
3023 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3024
3025 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003026 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003027 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003028 }
3029
Eli Friedman4735374e2009-03-03 06:41:03 +00003030 if (ComplexMode) {
3031 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003032 }
3033
3034 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003035 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3036 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3037 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003038 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003039
3040 D->addAttr(::new (S.Context)
3041 ModeAttr(Attr.getRange(), S.Context, Name,
3042 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003043}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003044
Chandler Carruthedc2c642011-07-02 00:01:44 +00003045static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003046 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3047 if (!VD->hasGlobalStorage())
3048 S.Diag(Attr.getLoc(),
3049 diag::warn_attribute_requires_functions_or_static_globals)
3050 << Attr.getName();
3051 } else if (!isFunctionOrMethod(D)) {
3052 S.Diag(Attr.getLoc(),
3053 diag::warn_attribute_requires_functions_or_static_globals)
3054 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003055 return;
3056 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003057
Michael Han99315932013-01-24 16:46:58 +00003058 D->addAttr(::new (S.Context)
3059 NoDebugAttr(Attr.getRange(), S.Context,
3060 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003061}
3062
Chandler Carruthedc2c642011-07-02 00:01:44 +00003063static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003064 FunctionDecl *FD = cast<FunctionDecl>(D);
3065 if (!FD->getResultType()->isVoidType()) {
3066 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3067 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3068 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3069 << FD->getType()
3070 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3071 "void");
3072 } else {
3073 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3074 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003075 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003076 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003077 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003078
Aaron Ballman3aff6332013-12-02 19:30:36 +00003079 D->addAttr(::new (S.Context)
3080 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003081 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003082}
3083
Chandler Carruthedc2c642011-07-02 00:01:44 +00003084static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003085 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003086 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003087 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003088 return;
3089 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003090
Michael Han99315932013-01-24 16:46:58 +00003091 D->addAttr(::new (S.Context)
3092 GNUInlineAttr(Attr.getRange(), S.Context,
3093 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003094}
3095
Chandler Carruthedc2c642011-07-02 00:01:44 +00003096static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003097 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003098
Aaron Ballman02df2e02012-12-09 17:45:41 +00003099 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003100 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003101 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3102 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003103 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003104 return;
3105
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003106 if (!isa<ObjCMethodDecl>(D)) {
3107 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3108 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003109 return;
3110 }
3111
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003112 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003113 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003114 D->addAttr(::new (S.Context)
3115 FastCallAttr(Attr.getRange(), S.Context,
3116 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003117 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003118 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003119 D->addAttr(::new (S.Context)
3120 StdCallAttr(Attr.getRange(), S.Context,
3121 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003122 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003123 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003124 D->addAttr(::new (S.Context)
3125 ThisCallAttr(Attr.getRange(), S.Context,
3126 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003127 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003128 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003129 D->addAttr(::new (S.Context)
3130 CDeclAttr(Attr.getRange(), S.Context,
3131 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003132 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003133 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003134 D->addAttr(::new (S.Context)
3135 PascalAttr(Attr.getRange(), S.Context,
3136 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003137 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003138 case AttributeList::AT_MSABI:
3139 D->addAttr(::new (S.Context)
3140 MSABIAttr(Attr.getRange(), S.Context,
3141 Attr.getAttributeSpellingListIndex()));
3142 return;
3143 case AttributeList::AT_SysVABI:
3144 D->addAttr(::new (S.Context)
3145 SysVABIAttr(Attr.getRange(), S.Context,
3146 Attr.getAttributeSpellingListIndex()));
3147 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003148 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003149 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003150 switch (CC) {
3151 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003152 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003153 break;
3154 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003155 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003156 break;
3157 default:
3158 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003159 }
3160
Michael Han99315932013-01-24 16:46:58 +00003161 D->addAttr(::new (S.Context)
3162 PcsAttr(Attr.getRange(), S.Context, PCS,
3163 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003164 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003165 }
Derek Schuffa2020962012-10-16 22:30:41 +00003166 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003167 D->addAttr(::new (S.Context)
3168 PnaclCallAttr(Attr.getRange(), S.Context,
3169 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003170 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003171 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003172 D->addAttr(::new (S.Context)
3173 IntelOclBiccAttr(Attr.getRange(), S.Context,
3174 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003175 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003176
Abramo Bagnara50099372010-04-30 13:10:51 +00003177 default:
3178 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003179 }
3180}
3181
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003182static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3183 const AttributeList &Attr) {
3184 uint32_t ArgNum;
3185 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003186 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003187
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003188 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3189 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003190}
3191
Aaron Ballman02df2e02012-12-09 17:45:41 +00003192bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3193 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003194 if (attr.isInvalid())
3195 return true;
3196
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003197 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003198 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003199 attr.setInvalid();
3200 return true;
3201 }
3202
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003203 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003204 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003205 case AttributeList::AT_CDecl: CC = CC_C; break;
3206 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3207 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3208 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3209 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003210 case AttributeList::AT_MSABI:
3211 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3212 CC_X86_64Win64;
3213 break;
3214 case AttributeList::AT_SysVABI:
3215 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3216 CC_C;
3217 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003218 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003219 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003220 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003221 attr.setInvalid();
3222 return true;
3223 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003224 if (StrRef == "aapcs") {
3225 CC = CC_AAPCS;
3226 break;
3227 } else if (StrRef == "aapcs-vfp") {
3228 CC = CC_AAPCS_VFP;
3229 break;
3230 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003231
3232 attr.setInvalid();
3233 Diag(attr.getLoc(), diag::err_invalid_pcs);
3234 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003235 }
Derek Schuffa2020962012-10-16 22:30:41 +00003236 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003237 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003238 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003239 }
3240
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003241 const TargetInfo &TI = Context.getTargetInfo();
3242 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3243 if (A == TargetInfo::CCCR_Warning) {
3244 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003245
3246 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3247 if (FD)
3248 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3249 TargetInfo::CCMT_NonMember;
3250 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003251 }
3252
John McCall3882ace2011-01-05 12:14:39 +00003253 return false;
3254}
3255
John McCall3882ace2011-01-05 12:14:39 +00003256/// Checks a regparm attribute, returning true if it is ill-formed and
3257/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003258bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3259 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003260 return true;
3261
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003262 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003263 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003264 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003265 }
Eli Friedman7044b762009-03-27 21:06:47 +00003266
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003267 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003268 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003269 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003270 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003271 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003272 }
3273
Douglas Gregore8bbc122011-09-02 00:18:52 +00003274 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003275 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003276 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003277 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003278 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003279 }
3280
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003281 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003282 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003283 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003284 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003285 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003286 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003287 }
3288
John McCall3882ace2011-01-05 12:14:39 +00003289 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003290}
3291
Aaron Ballman66039932013-12-19 00:41:31 +00003292static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3293 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003294 // check the attribute arguments.
3295 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3296 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003297 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3298 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003299 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003300 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003301
3302 if (!isFunctionOrMethod(D)) {
3303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3304 << Attr.getName() << ExpectedFunctionOrMethod;
3305 return;
3306 }
3307
Aaron Ballman66039932013-12-19 00:41:31 +00003308 uint32_t MaxThreads, MinBlocks = 0;
3309 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3310 return;
3311 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3312 Attr.getArgAsExpr(1),
3313 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003314 return;
3315
3316 D->addAttr(::new (S.Context)
3317 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3318 MaxThreads, MinBlocks,
3319 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003320}
3321
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003322static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3323 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003324 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003325 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003326 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003327 return;
3328 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003329
3330 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003331 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003332
Aaron Ballman00e99962013-08-31 01:11:41 +00003333 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003334
3335 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3336 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3337 << Attr.getName() << ExpectedFunctionOrMethod;
3338 return;
3339 }
3340
3341 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003342 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3343 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003344 return;
3345
3346 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003347 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3348 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003349 return;
3350
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003351 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003352 if (IsPointer) {
3353 // Ensure that buffer has a pointer type.
3354 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3355 if (!BufferTy->isPointerType()) {
3356 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003357 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003358 }
3359 }
3360
Michael Han99315932013-01-24 16:46:58 +00003361 D->addAttr(::new (S.Context)
3362 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3363 ArgumentIdx, TypeTagIdx, IsPointer,
3364 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003365}
3366
3367static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3368 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003369 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003371 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003372 return;
3373 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003374
3375 if (!checkAttributeNumArgs(S, Attr, 1))
3376 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003377
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003378 if (!isa<VarDecl>(D)) {
3379 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3380 << Attr.getName() << ExpectedVariable;
3381 return;
3382 }
3383
Aaron Ballman00e99962013-08-31 01:11:41 +00003384 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003385 TypeSourceInfo *MatchingCTypeLoc = 0;
3386 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3387 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003388
Michael Han99315932013-01-24 16:46:58 +00003389 D->addAttr(::new (S.Context)
3390 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003391 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003392 Attr.getLayoutCompatible(),
3393 Attr.getMustBeNull(),
3394 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003395}
3396
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003397//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003398// Checker-specific attribute handlers.
3399//===----------------------------------------------------------------------===//
3400
John McCalled433932011-01-25 03:31:58 +00003401static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003402 return type->isDependentType() ||
3403 type->isObjCObjectPointerType() ||
3404 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003405}
3406static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003407 return type->isDependentType() ||
3408 type->isPointerType() ||
3409 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003410}
3411
Chandler Carruthedc2c642011-07-02 00:01:44 +00003412static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003413 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003414 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003415
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003416 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003417 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3418 cf = false;
3419 } else {
3420 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3421 cf = true;
3422 }
3423
3424 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003425 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003426 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003427 return;
3428 }
3429
3430 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003431 param->addAttr(::new (S.Context)
3432 CFConsumedAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003434 else
Michael Han99315932013-01-24 16:46:58 +00003435 param->addAttr(::new (S.Context)
3436 NSConsumedAttr(Attr.getRange(), S.Context,
3437 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003438}
3439
Chandler Carruthedc2c642011-07-02 00:01:44 +00003440static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3441 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003442
John McCalled433932011-01-25 03:31:58 +00003443 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003444
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003445 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003446 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003447 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003448 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003449 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003450 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3451 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003453 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003454 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003455 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003456 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003457 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003458 return;
3459 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003460
John McCalled433932011-01-25 03:31:58 +00003461 bool typeOK;
3462 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003463 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003464 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003465 case AttributeList::AT_NSReturnsAutoreleased:
3466 case AttributeList::AT_NSReturnsRetained:
3467 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003468 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3469 cf = false;
3470 break;
3471
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003472 case AttributeList::AT_CFReturnsRetained:
3473 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003474 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3475 cf = true;
3476 break;
3477 }
3478
3479 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003480 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003481 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003482 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003483 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003484
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003485 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003486 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003487 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003488 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003489 D->addAttr(::new (S.Context)
3490 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3491 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003492 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003493 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003494 D->addAttr(::new (S.Context)
3495 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3496 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003497 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003498 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003499 D->addAttr(::new (S.Context)
3500 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3501 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003502 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003503 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003504 D->addAttr(::new (S.Context)
3505 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3506 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003507 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003508 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003509 D->addAttr(::new (S.Context)
3510 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3511 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003512 return;
3513 };
3514}
3515
John McCallcf166702011-07-22 08:53:00 +00003516static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3517 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003518 const int EP_ObjCMethod = 1;
3519 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003520
John McCallcf166702011-07-22 08:53:00 +00003521 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003522 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003523 if (isa<ObjCMethodDecl>(D))
3524 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003525 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003526 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003527
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003528 if (!resultType->isReferenceType() &&
3529 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003530 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003531 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003532 << attr.getName()
3533 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003534 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003535
3536 // Drop the attribute.
3537 return;
3538 }
3539
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003540 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003541 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3542 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003543}
3544
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003545static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3546 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003547 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003548
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003549 DeclContext *DC = method->getDeclContext();
3550 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3551 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3552 << attr.getName() << 0;
3553 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3554 return;
3555 }
3556 if (method->getMethodFamily() == OMF_dealloc) {
3557 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3558 << attr.getName() << 1;
3559 return;
3560 }
3561
Michael Han99315932013-01-24 16:46:58 +00003562 method->addAttr(::new (S.Context)
3563 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3564 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003565}
3566
Aaron Ballmanfb763042013-12-02 18:05:46 +00003567static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3568 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003569 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003570 return;
John McCall32f5fe12011-09-30 05:12:12 +00003571
Aaron Ballmanfb763042013-12-02 18:05:46 +00003572 D->addAttr(::new (S.Context)
3573 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3574 Attr.getAttributeSpellingListIndex()));
3575}
3576
3577static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3578 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003579 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003580 return;
3581
3582 D->addAttr(::new (S.Context)
3583 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3584 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003585}
3586
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003587static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3588 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003589 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003590
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003591 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003592 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003593 return;
3594 }
3595
3596 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003597 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003598 Attr.getAttributeSpellingListIndex()));
3599}
3600
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003601static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3602 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003603 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003604
3605 if (!Parm) {
3606 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3607 return;
3608 }
3609
3610 D->addAttr(::new (S.Context)
3611 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3612 Attr.getAttributeSpellingListIndex()));
3613}
3614
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003615static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3616 const AttributeList &Attr) {
3617 IdentifierInfo *RelatedClass =
3618 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3619 if (!RelatedClass) {
3620 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3621 return;
3622 }
3623 IdentifierInfo *ClassMethod =
3624 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3625 IdentifierInfo *InstanceMethod =
3626 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3627 D->addAttr(::new (S.Context)
3628 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3629 ClassMethod, InstanceMethod,
3630 Attr.getAttributeSpellingListIndex()));
3631}
3632
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003633static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3634 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003635 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003636 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003637 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003638 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3639 Attr.getAttributeSpellingListIndex()));
3640}
3641
Chandler Carruthedc2c642011-07-02 00:01:44 +00003642static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3643 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003644 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003645
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003646 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003647 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003648}
3649
Chandler Carruthedc2c642011-07-02 00:01:44 +00003650static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3651 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003652 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003653 QualType type = vd->getType();
3654
3655 if (!type->isDependentType() &&
3656 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003657 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003658 << type;
3659 return;
3660 }
3661
3662 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3663
3664 // If we have no lifetime yet, check the lifetime we're presumably
3665 // going to infer.
3666 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3667 lifetime = type->getObjCARCImplicitLifetime();
3668
3669 switch (lifetime) {
3670 case Qualifiers::OCL_None:
3671 assert(type->isDependentType() &&
3672 "didn't infer lifetime for non-dependent type?");
3673 break;
3674
3675 case Qualifiers::OCL_Weak: // meaningful
3676 case Qualifiers::OCL_Strong: // meaningful
3677 break;
3678
3679 case Qualifiers::OCL_ExplicitNone:
3680 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003681 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003682 << (lifetime == Qualifiers::OCL_Autoreleasing);
3683 break;
3684 }
3685
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003686 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003687 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3688 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003689}
3690
Francois Picheta83957a2010-12-19 06:50:37 +00003691//===----------------------------------------------------------------------===//
3692// Microsoft specific attribute handlers.
3693//===----------------------------------------------------------------------===//
3694
Chandler Carruthedc2c642011-07-02 00:01:44 +00003695static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003696 if (!S.LangOpts.CPlusPlus) {
3697 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3698 << Attr.getName() << AttributeLangSupport::C;
3699 return;
3700 }
3701
Aaron Ballman60e705e2013-11-24 20:58:02 +00003702 if (!isa<CXXRecordDecl>(D)) {
3703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3704 << Attr.getName() << ExpectedClass;
3705 return;
3706 }
3707
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003708 StringRef StrRef;
3709 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003710 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003711 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003712
David Majnemer89085342013-08-09 08:56:20 +00003713 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3714 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003715 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3716 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003717
Reid Kleckner140c4a72013-05-17 14:04:52 +00003718 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003719 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003720 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003721 return;
3722 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003723
David Majnemer89085342013-08-09 08:56:20 +00003724 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003725 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003726 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003727 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003728 return;
3729 }
David Majnemer89085342013-08-09 08:56:20 +00003730 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003731 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003732 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003733 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003734 }
Francois Picheta83957a2010-12-19 06:50:37 +00003735
David Majnemer89085342013-08-09 08:56:20 +00003736 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3737 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003738}
3739
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003740static void handleARMInterruptAttr(Sema &S, Decl *D,
3741 const AttributeList &Attr) {
3742 // Check the attribute arguments.
3743 if (Attr.getNumArgs() > 1) {
3744 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3745 << Attr.getName() << 1;
3746 return;
3747 }
3748
3749 StringRef Str;
3750 SourceLocation ArgLoc;
3751
3752 if (Attr.getNumArgs() == 0)
3753 Str = "";
3754 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3755 return;
3756
3757 ARMInterruptAttr::InterruptType Kind;
3758 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3759 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3760 << Attr.getName() << Str << ArgLoc;
3761 return;
3762 }
3763
3764 unsigned Index = Attr.getAttributeSpellingListIndex();
3765 D->addAttr(::new (S.Context)
3766 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3767}
3768
3769static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3770 const AttributeList &Attr) {
3771 if (!checkAttributeNumArgs(S, Attr, 1))
3772 return;
3773
3774 if (!Attr.isArgExpr(0)) {
3775 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3776 << AANT_ArgumentIntegerConstant;
3777 return;
3778 }
3779
3780 // FIXME: Check for decl - it should be void ()(void).
3781
3782 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3783 llvm::APSInt NumParams(32);
3784 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3785 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3786 << Attr.getName() << AANT_ArgumentIntegerConstant
3787 << NumParamsExpr->getSourceRange();
3788 return;
3789 }
3790
3791 unsigned Num = NumParams.getLimitedValue(255);
3792 if ((Num & 1) || Num > 30) {
3793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3794 << Attr.getName() << (int)NumParams.getSExtValue()
3795 << NumParamsExpr->getSourceRange();
3796 return;
3797 }
3798
3799 D->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
3800 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
3801}
3802
3803static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3804 // Dispatch the interrupt attribute based on the current target.
3805 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3806 handleMSP430InterruptAttr(S, D, Attr);
3807 else
3808 handleARMInterruptAttr(S, D, Attr);
3809}
3810
3811static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3812 const AttributeList& Attr) {
3813 // If we try to apply it to a function pointer, don't warn, but don't
3814 // do anything, either. It doesn't matter anyway, because there's nothing
3815 // special about calling a force_align_arg_pointer function.
3816 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3817 if (VD && VD->getType()->isFunctionPointerType())
3818 return;
3819 // Also don't warn on function pointer typedefs.
3820 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3821 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3822 TD->getUnderlyingType()->isFunctionType()))
3823 return;
3824 // Attribute can only be applied to function types.
3825 if (!isa<FunctionDecl>(D)) {
3826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3827 << Attr.getName() << /* function */0;
3828 return;
3829 }
3830
3831 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
3832 S.Context));
3833}
3834
3835DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3836 unsigned AttrSpellingListIndex) {
3837 if (D->hasAttr<DLLExportAttr>()) {
3838 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
3839 return NULL;
3840 }
3841
3842 if (D->hasAttr<DLLImportAttr>())
3843 return NULL;
3844
3845 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3846 if (VD->hasDefinition()) {
3847 // dllimport cannot be applied to definitions.
3848 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
3849 << "dllimport";
3850 return NULL;
3851 }
3852 }
3853
3854 return ::new (Context)DLLImportAttr(Range, Context,
3855 AttrSpellingListIndex);
3856}
3857
3858static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3859 // Attribute can be applied only to functions or variables.
3860 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3861 if (!FD && !isa<VarDecl>(D)) {
3862 // Apparently Visual C++ thinks it is okay to not emit a warning
3863 // in this case, so only emit a warning when -fms-extensions is not
3864 // specified.
3865 if (!S.getLangOpts().MicrosoftExt)
3866 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3867 << Attr.getName() << 2 /*variable and function*/;
3868 return;
3869 }
3870
3871 // Currently, the dllimport attribute is ignored for inlined functions.
3872 // Warning is emitted.
3873 if (FD && FD->isInlineSpecified()) {
3874 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3875 return;
3876 }
3877
3878 unsigned Index = Attr.getAttributeSpellingListIndex();
3879 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3880 if (NewAttr)
3881 D->addAttr(NewAttr);
3882}
3883
3884DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3885 unsigned AttrSpellingListIndex) {
3886 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
3887 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
3888 D->dropAttr<DLLImportAttr>();
3889 }
3890
3891 if (D->hasAttr<DLLExportAttr>())
3892 return NULL;
3893
3894 return ::new (Context)DLLExportAttr(Range, Context,
3895 AttrSpellingListIndex);
3896}
3897
3898static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3899 // Currently, the dllexport attribute is ignored for inlined functions, unless
3900 // the -fkeep-inline-functions flag has been used. Warning is emitted;
3901 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) {
3902 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
3903 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3904 return;
3905 }
3906
3907 unsigned Index = Attr.getAttributeSpellingListIndex();
3908 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3909 if (NewAttr)
3910 D->addAttr(NewAttr);
3911}
3912
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003913/// Handles semantic checking for features that are common to all attributes,
3914/// such as checking whether a parameter was properly specified, or the correct
3915/// number of arguments were passed, etc.
3916static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3917 const AttributeList &Attr) {
3918 // Several attributes carry different semantics than the parsing requires, so
3919 // those are opted out of the common handling.
3920 //
3921 // We also bail on unknown and ignored attributes because those are handled
3922 // as part of the target-specific handling logic.
3923 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003924 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003925 return false;
3926
Aaron Ballman3aff6332013-12-02 19:30:36 +00003927 // Check whether the attribute requires specific language extensions to be
3928 // enabled.
3929 if (!Attr.diagnoseLangOpts(S))
3930 return true;
3931
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003932 // If there are no optional arguments, then checking for the argument count
3933 // is trivial.
3934 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3935 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3936 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003937
3938 // Check whether the attribute appertains to the given subject.
3939 if (!Attr.diagnoseAppertainsTo(S, D))
3940 return true;
3941
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003942 return false;
3943}
3944
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003945//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003946// Top Level Sema Entry Points
3947//===----------------------------------------------------------------------===//
3948
Richard Smithf8a75c32013-08-29 00:47:48 +00003949/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3950/// the attribute applies to decls. If the attribute is a type attribute, just
3951/// silently ignore it if a GNU attribute.
3952static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3953 const AttributeList &Attr,
3954 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003955 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00003956 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003957
Richard Smithf8a75c32013-08-29 00:47:48 +00003958 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3959 // instead.
3960 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3961 return;
3962
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003963 // Unknown attributes are automatically warned on. Target-specific attributes
3964 // which do not apply to the current target architecture are treated as
3965 // though they were unknown attributes.
3966 if (Attr.getKind() == AttributeList::UnknownAttribute ||
3967 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
3968 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
3969 diag::warn_unhandled_ms_attribute_ignored :
3970 diag::warn_unknown_attribute_ignored) << Attr.getName();
3971 return;
3972 }
3973
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003974 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3975 return;
3976
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003977 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003978 default:
3979 // Type attributes are handled elsewhere; silently move on.
3980 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
3981 break;
3982 case AttributeList::AT_Interrupt:
3983 handleInterruptAttr(S, D, Attr); break;
3984 case AttributeList::AT_X86ForceAlignArgPointer:
3985 handleX86ForceAlignArgPointerAttr(S, D, Attr); break;
3986 case AttributeList::AT_DLLExport:
3987 handleDLLExportAttr(S, D, Attr); break;
3988 case AttributeList::AT_DLLImport:
3989 handleDLLImportAttr(S, D, Attr); break;
3990 case AttributeList::AT_Mips16:
3991 handleSimpleAttribute<Mips16Attr>(S, D, Attr); break;
3992 case AttributeList::AT_NoMips16:
3993 handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00003994 case AttributeList::AT_IBAction:
3995 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003996 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3997 case AttributeList::AT_IBOutletCollection:
3998 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4000 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004001 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004002 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004003 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004004 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004005 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004006 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4007 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4008 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004009 handleDependencyAttr(S, scope, D, Attr);
4010 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004011 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004012 case AttributeList::AT_CUDAConstant:
4013 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004014 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004015 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004016 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004017 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004018 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004019 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
Nick Lewycky35a6ef42014-01-11 02:50:57 +00004021 case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004022 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004023 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004024 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004025 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004026 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004027 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004028 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4029 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4030 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004031 case AttributeList::AT_CUDADevice:
4032 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004033 case AttributeList::AT_CUDAHost:
4034 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004035 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4036 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004037 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004038 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004039 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004040 case AttributeList::AT_MayAlias:
4041 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004042 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004043 case AttributeList::AT_NoCommon:
4044 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004046 case AttributeList::AT_Overloadable:
4047 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004048 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4050 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004051 case AttributeList::AT_Naked:
4052 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004053 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004054 case AttributeList::AT_NoThrow:
4055 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004056 case AttributeList::AT_CUDAShared:
4057 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004058 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004059
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004060 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004061 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004062 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004063 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004064
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004065 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004066 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4067
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004068 case AttributeList::AT_ObjCRequiresSuper:
4069 handleObjCRequiresSuperAttr(S, D, Attr); break;
4070
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004071 case AttributeList::AT_ObjCBridge:
4072 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004073
4074 case AttributeList::AT_ObjCBridgeMutable:
4075 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004076
4077 case AttributeList::AT_ObjCBridgeRelated:
4078 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004079
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004080 case AttributeList::AT_ObjCDesignatedInitializer:
4081 handleObjCDesignatedInitializer(S, D, Attr); break;
4082
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004083 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004084 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004085 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004086 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004087
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004088 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004089 case AttributeList::AT_CFConsumed:
4090 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4091 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004092 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004093
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004094 case AttributeList::AT_NSReturnsAutoreleased:
4095 case AttributeList::AT_NSReturnsNotRetained:
4096 case AttributeList::AT_CFReturnsNotRetained:
4097 case AttributeList::AT_NSReturnsRetained:
4098 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004099 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004100 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004101 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004102 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004103 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004104 case AttributeList::AT_VecTypeHint:
4105 handleVecTypeHint(S, D, Attr); break;
4106
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004108 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004109
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4111 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4112 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004113 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004114 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004115 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004116 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004118 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004119 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004120 handleObjCSuppresProtocolAttr(S, D, Attr);
4121 break;
4122 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004123 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004124 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4125 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004126 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004128 case AttributeList::AT_Visibility:
4129 handleVisibilityAttr(S, D, Attr, false);
4130 break;
4131 case AttributeList::AT_TypeVisibility:
4132 handleVisibilityAttr(S, D, Attr, true);
4133 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004134 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004135 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004136 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004137 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004138 case AttributeList::AT_Weak:
4139 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004140 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4141 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4142 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004143 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004144 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004145 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004146 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004147 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004148 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004149 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004150 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4151 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4152 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004153 case AttributeList::AT_Const:
4154 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004155 case AttributeList::AT_Pure:
4156 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004157 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4158 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004159 case AttributeList::AT_NoInline:
4160 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004162 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004163 case AttributeList::AT_StdCall:
4164 case AttributeList::AT_CDecl:
4165 case AttributeList::AT_FastCall:
4166 case AttributeList::AT_ThisCall:
4167 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004168 case AttributeList::AT_MSABI:
4169 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004170 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004171 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004172 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004173 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004174 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004175 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004176 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004177 case AttributeList::AT_OpenCLImageAccess:
4178 handleOpenCLImageAccessAttr(S, D, Attr);
4179 break;
John McCall8d32c052012-05-22 21:28:12 +00004180
4181 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004183 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004184 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004185 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004186 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004187 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004188 case AttributeList::AT_MSInheritance:
4189 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004190 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004191 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004192 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004193 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004194
4195 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004196 case AttributeList::AT_AssertExclusiveLock:
4197 handleAssertExclusiveLockAttr(S, D, Attr);
4198 break;
4199 case AttributeList::AT_AssertSharedLock:
4200 handleAssertSharedLockAttr(S, D, Attr);
4201 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004202 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004203 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004204 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004205 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004206 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004207 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004208 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004209 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004210 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004211 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004212 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004213 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004214 break;
4215 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004216 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004217 break;
4218 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004219 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004220 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004221 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004222 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004223 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004224 handleGuardedByAttr(S, D, Attr);
4225 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004226 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004227 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004228 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004229 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004230 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004231 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004232 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004233 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004234 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004235 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004236 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004237 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004239 handleLockReturnedAttr(S, D, Attr);
4240 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004241 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004242 handleLocksExcludedAttr(S, D, Attr);
4243 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004244 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004245 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004246 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004247 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004248 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004249 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004251 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004252 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004253 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004254 handleUnlockFunAttr(S, D, Attr);
4255 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004256 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004257 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004258 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004259 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004260 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004261 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004262
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004263 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004264 case AttributeList::AT_Consumable:
4265 handleConsumableAttr(S, D, Attr);
4266 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004267 case AttributeList::AT_CallableWhen:
4268 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004269 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004270 case AttributeList::AT_ParamTypestate:
4271 handleParamTypestateAttr(S, D, Attr);
4272 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004273 case AttributeList::AT_ReturnTypestate:
4274 handleReturnTypestateAttr(S, D, Attr);
4275 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004276 case AttributeList::AT_SetTypestate:
4277 handleSetTypestateAttr(S, D, Attr);
4278 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004279 case AttributeList::AT_TestTypestate:
4280 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004281 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004282
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004283 // Type safety attributes.
4284 case AttributeList::AT_ArgumentWithTypeTag:
4285 handleArgumentWithTypeTagAttr(S, D, Attr);
4286 break;
4287 case AttributeList::AT_TypeTagForDatatype:
4288 handleTypeTagForDatatypeAttr(S, D, Attr);
4289 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004290 }
4291}
4292
4293/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4294/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004295void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004296 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004297 bool IncludeCXX11Attributes) {
4298 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004299 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004300
Joey Gouly2cd9db12013-12-13 16:15:28 +00004301 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004302 // GCC accepts
4303 // static int a9 __attribute__((weakref));
4304 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004305 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004306 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4307 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004308 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004309 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004310 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004311
4312 if (!D->hasAttr<OpenCLKernelAttr>()) {
4313 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004314 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4315 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004316 D->setInvalidDecl();
4317 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004318 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
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<VecTypeHintAttr>()) {
4323 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004324 D->setInvalidDecl();
4325 }
4326 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004327}
4328
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004329// Annotation attributes are the only attributes allowed after an access
4330// specifier.
4331bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4332 const AttributeList *AttrList) {
4333 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004334 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004335 handleAnnotateAttr(*this, ASDecl, *l);
4336 } else {
4337 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4338 return true;
4339 }
4340 }
4341
4342 return false;
4343}
4344
John McCall42856de2011-10-01 05:17:03 +00004345/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4346/// contains any decl attributes that we should warn about.
4347static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4348 for ( ; A; A = A->getNext()) {
4349 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004350 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004351 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4352
4353 if (A->getKind() == AttributeList::UnknownAttribute) {
4354 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4355 << A->getName() << A->getRange();
4356 } else {
4357 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4358 << A->getName() << A->getRange();
4359 }
4360 }
4361}
4362
4363/// checkUnusedDeclAttributes - Given a declarator which is not being
4364/// used to build a declaration, complain about any decl attributes
4365/// which might be lying around on it.
4366void Sema::checkUnusedDeclAttributes(Declarator &D) {
4367 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4368 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4369 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4370 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4371}
4372
Ryan Flynn7d470f32009-07-30 03:15:39 +00004373/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004374/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004375NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4376 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004377 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004378 NamedDecl *NewD = 0;
4379 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004380 FunctionDecl *NewFD;
4381 // FIXME: Missing call to CheckFunctionDeclaration().
4382 // FIXME: Mangling?
4383 // FIXME: Is the qualifier info correct?
4384 // FIXME: Is the DeclContext correct?
4385 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4386 Loc, Loc, DeclarationName(II),
4387 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004388 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004389 FD->hasPrototype(),
4390 false/*isConstexprSpecified*/);
4391 NewD = NewFD;
4392
4393 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004394 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004395
4396 // Fake up parameter variables; they are declared as if this were
4397 // a typedef.
4398 QualType FDTy = FD->getType();
4399 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4400 SmallVector<ParmVarDecl*, 16> Params;
4401 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4402 AE = FT->arg_type_end(); AI != AE; ++AI) {
4403 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4404 Param->setScopeInfo(0, Params.size());
4405 Params.push_back(Param);
4406 }
David Blaikie9c70e042011-09-21 18:16:56 +00004407 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004408 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004409 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4410 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004411 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004412 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004413 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004414 if (VD->getQualifier()) {
4415 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004416 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004417 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004418 }
4419 return NewD;
4420}
4421
James Dennett634962f2012-06-14 21:40:34 +00004422/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004423/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004424void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004425 if (W.getUsed()) return; // only do this once
4426 W.setUsed(true);
4427 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4428 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004429 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004430 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4431 NDId->getName()));
4432 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004433 WeakTopLevelDecl.push_back(NewD);
4434 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4435 // to insert Decl at TU scope, sorry.
4436 DeclContext *SavedContext = CurContext;
4437 CurContext = Context.getTranslationUnitDecl();
4438 PushOnScopeChains(NewD, S);
4439 CurContext = SavedContext;
4440 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004441 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004442 }
4443}
4444
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004445void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4446 // It's valid to "forward-declare" #pragma weak, in which case we
4447 // have to do this.
4448 LoadExternalWeakUndeclaredIdentifiers();
4449 if (!WeakUndeclaredIdentifiers.empty()) {
4450 NamedDecl *ND = NULL;
4451 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4452 if (VD->isExternC())
4453 ND = VD;
4454 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4455 if (FD->isExternC())
4456 ND = FD;
4457 if (ND) {
4458 if (IdentifierInfo *Id = ND->getIdentifier()) {
4459 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4460 = WeakUndeclaredIdentifiers.find(Id);
4461 if (I != WeakUndeclaredIdentifiers.end()) {
4462 WeakInfo W = I->second;
4463 DeclApplyPragmaWeak(S, ND, W);
4464 WeakUndeclaredIdentifiers[Id] = W;
4465 }
4466 }
4467 }
4468 }
4469}
4470
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004471/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4472/// it, apply them to D. This is a bit tricky because PD can have attributes
4473/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004474void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004475 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004476 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004477 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004478
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004479 // Walk the declarator structure, applying decl attributes that were in a type
4480 // position to the decl itself. This handles cases like:
4481 // int *__attr__(x)** D;
4482 // when X is a decl attribute.
4483 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4484 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004485 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004486
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004487 // Finally, apply any attributes on the decl itself.
4488 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004489 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004490}
John McCall28a6aea2009-11-04 02:18:39 +00004491
John McCall31168b02011-06-15 23:02:42 +00004492/// Is the given declaration allowed to use a forbidden type?
4493static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4494 // Private ivars are always okay. Unfortunately, people don't
4495 // always properly make their ivars private, even in system headers.
4496 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004497 // Function declarations in sys headers will be marked unavailable.
4498 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4499 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004500 return false;
4501
4502 // Require it to be declared in a system header.
4503 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4504}
4505
4506/// Handle a delayed forbidden-type diagnostic.
4507static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4508 Decl *decl) {
4509 if (decl && isForbiddenTypeAllowed(S, decl)) {
4510 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4511 "this system declaration uses an unsupported type"));
4512 return;
4513 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004514 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004515 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004516 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004517 // kind of forbidden type messages on unavailable functions.
4518 if (FD->hasAttr<UnavailableAttr>() &&
4519 diag.getForbiddenTypeDiagnostic() ==
4520 diag::err_arc_array_param_no_ownership) {
4521 diag.Triggered = true;
4522 return;
4523 }
4524 }
John McCall31168b02011-06-15 23:02:42 +00004525
4526 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4527 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4528 diag.Triggered = true;
4529}
4530
John McCall2ec85372012-05-07 06:16:41 +00004531void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4532 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004533 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004534 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004535
John McCall2ec85372012-05-07 06:16:41 +00004536 // When delaying diagnostics to run in the context of a parsed
4537 // declaration, we only want to actually emit anything if parsing
4538 // succeeds.
4539 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004540
John McCall2ec85372012-05-07 06:16:41 +00004541 // We emit all the active diagnostics in this pool or any of its
4542 // parents. In general, we'll get one pool for the decl spec
4543 // and a child pool for each declarator; in a decl group like:
4544 // deprecated_typedef foo, *bar, baz();
4545 // only the declarator pops will be passed decls. This is correct;
4546 // we really do need to consider delayed diagnostics from the decl spec
4547 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004548 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004549 do {
John McCall6347b682012-05-07 06:16:58 +00004550 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004551 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4552 // This const_cast is a bit lame. Really, Triggered should be mutable.
4553 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004554 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004555 continue;
4556
John McCallc1465822011-02-14 07:13:47 +00004557 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004558 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004559 case DelayedDiagnostic::Unavailable:
4560 // Don't bother giving deprecation/unavailable diagnostics if
4561 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004562 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004563 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004564 break;
4565
4566 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004567 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004568 break;
John McCall31168b02011-06-15 23:02:42 +00004569
4570 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004571 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004572 break;
John McCall86121512010-01-27 03:50:35 +00004573 }
4574 }
John McCall2ec85372012-05-07 06:16:41 +00004575 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004576}
4577
John McCall6347b682012-05-07 06:16:58 +00004578/// Given a set of delayed diagnostics, re-emit them as if they had
4579/// been delayed in the current context instead of in the given pool.
4580/// Essentially, this just moves them to the current pool.
4581void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4582 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4583 assert(curPool && "re-emitting in undelayed context not supported");
4584 curPool->steal(pool);
4585}
4586
John McCall28a6aea2009-11-04 02:18:39 +00004587static bool isDeclDeprecated(Decl *D) {
4588 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004589 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004590 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004591 // A category implicitly has the availability of the interface.
4592 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4593 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004594 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4595 return false;
4596}
4597
Ted Kremenekb79ee572013-12-18 23:30:06 +00004598static bool isDeclUnavailable(Decl *D) {
4599 do {
4600 if (D->isUnavailable())
4601 return true;
4602 // A category implicitly has the availability of the interface.
4603 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4604 return CatD->getClassInterface()->isUnavailable();
4605 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4606 return false;
4607}
4608
Eli Friedman971bfa12012-08-08 21:52:41 +00004609static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004610DoEmitAvailabilityWarning(Sema &S,
4611 DelayedDiagnostic::DDKind K,
4612 Decl *Ctx,
4613 const NamedDecl *D,
4614 StringRef Message,
4615 SourceLocation Loc,
4616 const ObjCInterfaceDecl *UnknownObjCClass,
4617 const ObjCPropertyDecl *ObjCProperty) {
4618
4619 // Diagnostics for deprecated or unavailable.
4620 unsigned diag, diag_message, diag_fwdclass_message;
4621
4622 // Matches 'diag::note_property_attribute' options.
4623 unsigned property_note_select;
4624
4625 // Matches diag::note_availability_specified_here.
4626 unsigned available_here_select_kind;
4627
4628 // Don't warn if our current context is deprecated or unavailable.
4629 switch (K) {
4630 case DelayedDiagnostic::Deprecation:
4631 if (isDeclDeprecated(Ctx))
4632 return;
4633 diag = diag::warn_deprecated;
4634 diag_message = diag::warn_deprecated_message;
4635 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4636 property_note_select = /* deprecated */ 0;
4637 available_here_select_kind = /* deprecated */ 2;
4638 break;
4639
4640 case DelayedDiagnostic::Unavailable:
4641 if (isDeclUnavailable(Ctx))
4642 return;
4643 diag = diag::err_unavailable;
4644 diag_message = diag::err_unavailable_message;
4645 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4646 property_note_select = /* unavailable */ 1;
4647 available_here_select_kind = /* unavailable */ 0;
4648 break;
4649
4650 default:
4651 llvm_unreachable("Neither a deprecation or unavailable kind");
4652 }
4653
Eli Friedman971bfa12012-08-08 21:52:41 +00004654 DeclarationName Name = D->getDeclName();
4655 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004656 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004657 if (ObjCProperty)
4658 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4659 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004660 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004661 S.Diag(Loc, diag) << Name;
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 {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004666 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004667 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4668 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004669
4670 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4671 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004672}
4673
Ted Kremenekb79ee572013-12-18 23:30:06 +00004674void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4675 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004676 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004677 DoEmitAvailabilityWarning(*this,
4678 (DelayedDiagnostic::DDKind) DD.Kind,
4679 Ctx,
4680 DD.getDeprecationDecl(),
4681 DD.getDeprecationMessage(),
4682 DD.Loc,
4683 DD.getUnknownObjCClass(),
4684 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004685}
4686
Ted Kremenekb79ee572013-12-18 23:30:06 +00004687void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4688 NamedDecl *D, StringRef Message,
4689 SourceLocation Loc,
4690 const ObjCInterfaceDecl *UnknownObjCClass,
4691 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004692 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004693 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004694 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4695 UnknownObjCClass,
4696 ObjCProperty,
4697 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004698 return;
4699 }
4700
Ted Kremenekb79ee572013-12-18 23:30:06 +00004701 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4702 DelayedDiagnostic::DDKind K;
4703 switch (AD) {
4704 case AD_Deprecation:
4705 K = DelayedDiagnostic::Deprecation;
4706 break;
4707 case AD_Unavailable:
4708 K = DelayedDiagnostic::Unavailable;
4709 break;
4710 }
4711
4712 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4713 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004714}