blob: 23649c6694030e6d60ace23db4f1de20bf69b616 [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"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000035namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000036 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000037 C,
38 Cpp,
39 ObjC
40 };
41}
42
Chris Lattner58418ff2008-06-29 00:16:31 +000043//===----------------------------------------------------------------------===//
44// Helper functions
45//===----------------------------------------------------------------------===//
46
Chandler Carruthff4c4f02011-07-01 23:49:12 +000047static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000048 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000049 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000050 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000051 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000053 Ty = decl->getUnderlyingType();
54 else
55 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000056
Chris Lattner2c6fcf52008-06-26 18:38:35 +000057 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000058 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000059 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000060 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061
John McCall9dd450b2009-09-21 23:43:11 +000062 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000063}
64
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000065// FIXME: We should provide an abstraction around a method or function
66// to provide the following bits of information.
67
Nuno Lopes518e3702009-12-20 23:11:08 +000068/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000069/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070static bool isFunction(const Decl *D) {
71 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000072}
73
74/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000075/// type (function or function-typed variable) or an Objective-C
76/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000078 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000079}
80
Fariborz Jahanian4447e172009-05-15 23:15:03 +000081/// isFunctionOrMethodOrBlock - Return true if the given decl has function
82/// type (function or function-typed variable) or an Objective-C
83/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084static bool isFunctionOrMethodOrBlock(const Decl *D) {
85 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000086 return true;
87 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000088 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000089 QualType Ty = V->getType();
90 return Ty->isBlockPointerType();
91 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000093}
94
John McCall3882ace2011-01-05 12:14:39 +000095/// Return true if the given decl has a declarator that should have
96/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000097static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000098 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
100 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000101}
102
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000103/// hasFunctionProto - Return true if the given decl has a argument
104/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000105/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106static bool hasFunctionProto(const Decl *D) {
107 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000108 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000109 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000110 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000111 return true;
112 }
113}
114
115/// getFunctionOrMethodNumArgs - Return number of function or method
116/// arguments. It is an error to call this on a K&R function (use
117/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000118static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
119 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000120 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000122 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000124}
125
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000128 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000130 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000131
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000133}
134
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodResultType(const Decl *D) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000137 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000138 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000139}
140
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141static bool isFunctionOrMethodVariadic(const Decl *D) {
142 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000143 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000144 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000146 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000147 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000149 }
150}
151
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152static bool isInstanceMethod(const Decl *D) {
153 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000154 return MethodDecl->isInstance();
155 return false;
156}
157
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000158static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000159 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000160 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000161 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000162
John McCall96fa4842010-05-17 21:00:27 +0000163 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
164 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000165 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166
John McCall96fa4842010-05-17 21:00:27 +0000167 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000168
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000169 // FIXME: Should we walk the chain of classes?
170 return ClsName == &Ctx.Idents.get("NSString") ||
171 ClsName == &Ctx.Idents.get("NSMutableString");
172}
173
Daniel Dunbar980c6692008-09-26 03:32:58 +0000174static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000175 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000176 if (!PT)
177 return false;
178
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000179 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000180 if (!RT)
181 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000182
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000184 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000185 return false;
186
187 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
188}
189
Richard Smithb87c4652013-10-31 21:23:20 +0000190static unsigned getNumAttributeArgs(const AttributeList &Attr) {
191 // FIXME: Include the type in the argument list.
192 return Attr.getNumArgs() + Attr.hasParsedType();
193}
194
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000195/// \brief Check if the attribute has exactly as many args as Num. May
196/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000197static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000198 unsigned Num) {
199 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
201 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000202 return false;
203 }
204
205 return true;
206}
207
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000208
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000209/// \brief Check if the attribute has at least as many args as Num. May
210/// output an error.
211static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000212 unsigned Num) {
213 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000214 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
215 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216 return false;
217 }
218
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000219 return true;
220}
221
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000222/// \brief If Expr is a valid integer constant, get the value of the integer
223/// expression and return success or failure. May output an error.
224static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
225 const Expr *Expr, uint32_t &Val,
226 unsigned Idx = UINT_MAX) {
227 llvm::APSInt I(32);
228 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
229 !Expr->isIntegerConstantExpr(I, S.Context)) {
230 if (Idx != UINT_MAX)
231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
232 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
233 << Expr->getSourceRange();
234 else
235 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
236 << Attr.getName() << AANT_ArgumentIntegerConstant
237 << Expr->getSourceRange();
238 return false;
239 }
240 Val = (uint32_t)I.getZExtValue();
241 return true;
242}
243
Aaron Ballmanfb763042013-12-02 18:05:46 +0000244/// \brief Diagnose mutually exclusive attributes when present on a given
245/// declaration. Returns true if diagnosed.
246template <typename AttrTy>
247static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000248 const AttributeList &Attr) {
249 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000250 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000251 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000252 return true;
253 }
254 return false;
255}
256
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000257/// \brief Check if IdxExpr is a valid argument index for a function or
258/// instance method D. May output an error.
259///
260/// \returns true if IdxExpr is a valid index.
261static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000262 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000263 unsigned AttrArgNum,
264 const Expr *IdxExpr,
265 uint64_t &Idx)
266{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000267 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000268
269 // In C++ the implicit 'this' function parameter also counts.
270 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000271 bool HP = hasFunctionProto(D);
272 bool HasImplicitThisParam = isInstanceMethod(D);
273 bool IV = HP && isFunctionOrMethodVariadic(D);
274 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
275 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000276
277 llvm::APSInt IdxInt;
278 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
279 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000280 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
281 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
282 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000283 return false;
284 }
285
286 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000287 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
289 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000290 return false;
291 }
292 Idx--; // Convert to zero-based.
293 if (HasImplicitThisParam) {
294 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000295 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000296 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000297 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000298 return false;
299 }
300 --Idx;
301 }
302
303 return true;
304}
305
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000306/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
307/// If not emit an error and return false. If the argument is an identifier it
308/// will emit an error with a fixit hint and treat it as if it was a string
309/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000310bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
311 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000312 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000313 // Look for identifiers. If we have one emit a hint to fix it to a literal.
314 if (Attr.isArgIdent(ArgNum)) {
315 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000316 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000317 << Attr.getName() << AANT_ArgumentString
318 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000319 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000320 Str = Loc->Ident->getName();
321 if (ArgLocation)
322 *ArgLocation = Loc->Loc;
323 return true;
324 }
325
326 // Now check for an actual string literal.
327 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
328 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
329 if (ArgLocation)
330 *ArgLocation = ArgExpr->getLocStart();
331
332 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000333 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000334 << Attr.getName() << AANT_ArgumentString;
335 return false;
336 }
337
338 Str = Literal->getString();
339 return true;
340}
341
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000342/// \brief Applies the given attribute to the Decl without performing any
343/// additional semantic checking.
344template <typename AttrType>
345static void handleSimpleAttribute(Sema &S, Decl *D,
346 const AttributeList &Attr) {
347 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
348 Attr.getAttributeSpellingListIndex()));
349}
350
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000351/// \brief Check if the passed-in expression is of type int or bool.
352static bool isIntOrBool(Expr *Exp) {
353 QualType QT = Exp->getType();
354 return QT->isBooleanType() || QT->isIntegerType();
355}
356
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000357
358// Check to see if the type is a smart pointer of some kind. We assume
359// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000360static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
361 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
362 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000363 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000364 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000365
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000366 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
367 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000368 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000369 return false;
370
371 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000372}
373
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000374/// \brief Check if passed in Decl is a pointer type.
375/// Note that this function may produce an error message.
376/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000377static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
378 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000379 const ValueDecl *vd = cast<ValueDecl>(D);
380 QualType QT = vd->getType();
381 if (QT->isAnyPointerType())
382 return true;
383
384 if (const RecordType *RT = QT->getAs<RecordType>()) {
385 // If it's an incomplete type, it could be a smart pointer; skip it.
386 // (We don't want to force template instantiation if we can avoid it,
387 // since that would alter the order in which templates are instantiated.)
388 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000389 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000390
Aaron Ballman553e6812013-12-26 14:54:11 +0000391 if (threadSafetyCheckIsSmartPointer(S, RT))
392 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000393 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000394
395 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000396 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000397 return false;
398}
399
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000400/// \brief Checks that the passed in QualType either is of RecordType or points
401/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000402static const RecordType *getRecordType(QualType QT) {
403 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000405
406 // Now check if we point to record type.
407 if (const PointerType *PT = QT->getAs<PointerType>())
408 return PT->getPointeeType()->getAs<RecordType>();
409
410 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000411}
412
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000413
Jordy Rose740b0c22012-05-08 03:27:22 +0000414static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
415 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000416 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000417 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000418}
419
420
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000421/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000422/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000423static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
424 QualType Ty) {
425 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000426
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000427 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000428 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000429 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000430 << Attr.getName() << Ty;
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000431 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000432 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000433
Michael Hana9171bc2012-08-03 17:40:43 +0000434 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000435 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000436 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000437
438 // Allow smart pointers to be used as lockable objects.
439 // FIXME -- Check the type that the smart pointer points to.
440 if (threadSafetyCheckIsSmartPointer(S, RT))
441 return;
442
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000443 // Check if the type is lockable.
444 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000445 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000446 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000447
448 // Else check if any base classes are lockable.
449 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
450 CXXBasePaths BPaths(false, false);
451 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
452 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000453 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000454
455 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
Aaron Ballman1da282a2014-01-02 23:15:58 +0000456 << Attr.getName() << Ty;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000457}
458
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000459/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000460/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461/// \param Sidx The attribute argument index to start checking with.
462/// \param ParamIdxOk Whether an argument can be indexing into a function
463/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000464static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000465 const AttributeList &Attr,
466 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000467 int Sidx = 0,
468 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000470 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000471
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000472 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000473 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000474 Args.push_back(ArgExp);
475 continue;
476 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000477
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000478 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000479 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000480 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000481 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000482 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000483 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000484 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000485 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000486
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000487 // We allow constant strings to be used as a placeholder for expressions
488 // that are not valid C++ syntax, but warn that they are ignored.
489 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
490 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000491 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000492 continue;
493 }
494
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000495 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000496
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000497 // A pointer to member expression of the form &MyClass::mu is treated
498 // specially -- we need to look at the type of the member.
499 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
500 if (UOp->getOpcode() == UO_AddrOf)
501 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
502 if (DRE->getDecl()->isCXXInstanceMember())
503 ArgTy = DRE->getDecl()->getType();
504
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000505 // First see if we can just cast to record type, or point to record type.
506 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000507
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000508 // Now check if we index into a record type function param.
509 if(!RT && ParamIdxOk) {
510 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000511 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
512 if(FD && IL) {
513 unsigned int NumParams = FD->getNumParams();
514 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
516 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
517 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
519 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000522 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000523 }
524 }
525
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000526 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530}
531
Chris Lattner58418ff2008-06-29 00:16:31 +0000532//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000533// Attribute Implementations
534//===----------------------------------------------------------------------===//
535
Daniel Dunbar032db472008-07-31 22:40:48 +0000536// FIXME: All this manual attribute parsing code is gross. At the
537// least add some helper functions to check most argument patterns (#
538// and types of args).
539
Michael Hana9171bc2012-08-03 17:40:43 +0000540static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000541 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000542 if (!threadSafetyCheckIsPointer(S, D, Attr))
543 return;
544
Michael Han99315932013-01-24 16:46:58 +0000545 D->addAttr(::new (S.Context)
546 PtGuardedVarAttr(Attr.getRange(), S.Context,
547 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000548}
549
Michael Hana9171bc2012-08-03 17:40:43 +0000550static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
551 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000552 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000553 SmallVector<Expr*, 1> Args;
554 // check that all arguments are lockable objects
555 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
556 unsigned Size = Args.size();
557 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000558 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000559
Michael Han3be3b442012-07-23 18:48:41 +0000560 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000561
Michael Han3be3b442012-07-23 18:48:41 +0000562 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000563}
564
Michael Han3be3b442012-07-23 18:48:41 +0000565static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
566 Expr *Arg = 0;
567 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
568 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000569
Michael Han3be3b442012-07-23 18:48:41 +0000570 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
571}
572
Michael Hana9171bc2012-08-03 17:40:43 +0000573static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000574 const AttributeList &Attr) {
575 Expr *Arg = 0;
576 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
577 return;
578
579 if (!threadSafetyCheckIsPointer(S, D, Attr))
580 return;
581
582 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
583 S.Context, Arg));
584}
585
Michael Hana9171bc2012-08-03 17:40:43 +0000586static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
587 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000588 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000591
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000592 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000593 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000594 if (!QT->isDependentType()) {
595 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000596 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000597 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000598 << Attr.getName();
599 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000600 }
601 }
602
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000603 // Check that all arguments are lockable objects.
604 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000605 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000606 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000607
Michael Han3be3b442012-07-23 18:48:41 +0000608 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000609}
610
Michael Hana9171bc2012-08-03 17:40:43 +0000611static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000612 const AttributeList &Attr) {
613 SmallVector<Expr*, 1> Args;
614 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
615 return;
616
617 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000618 D->addAttr(::new (S.Context)
619 AcquiredAfterAttr(Attr.getRange(), S.Context,
620 StartArg, Args.size(),
621 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000625 const AttributeList &Attr) {
626 SmallVector<Expr*, 1> Args;
627 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
628 return;
629
630 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000631 D->addAttr(::new (S.Context)
632 AcquiredBeforeAttr(Attr.getRange(), S.Context,
633 StartArg, Args.size(),
634 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000635}
636
Michael Hana9171bc2012-08-03 17:40:43 +0000637static bool checkLockFunAttrCommon(Sema &S, Decl *D,
638 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000639 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000640 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000641 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000642 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000643
Michael Han3be3b442012-07-23 18:48:41 +0000644 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000645}
646
Michael Hana9171bc2012-08-03 17:40:43 +0000647static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000648 const AttributeList &Attr) {
649 SmallVector<Expr*, 1> Args;
650 if (!checkLockFunAttrCommon(S, D, Attr, Args))
651 return;
652
653 unsigned Size = Args.size();
654 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000655 D->addAttr(::new (S.Context)
656 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
657 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000658}
659
Michael Hana9171bc2012-08-03 17:40:43 +0000660static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000661 const AttributeList &Attr) {
662 SmallVector<Expr*, 1> Args;
663 if (!checkLockFunAttrCommon(S, D, Attr, Args))
664 return;
665
666 unsigned Size = Args.size();
667 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000668 D->addAttr(::new (S.Context)
669 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
670 StartArg, Size,
671 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000672}
673
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000674static void handleAssertSharedLockAttr(Sema &S, Decl *D,
675 const AttributeList &Attr) {
676 SmallVector<Expr*, 1> Args;
677 if (!checkLockFunAttrCommon(S, D, Attr, Args))
678 return;
679
680 unsigned Size = Args.size();
681 Expr **StartArg = Size == 0 ? 0 : &Args[0];
682 D->addAttr(::new (S.Context)
683 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
684 Attr.getAttributeSpellingListIndex()));
685}
686
687static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
688 const AttributeList &Attr) {
689 SmallVector<Expr*, 1> Args;
690 if (!checkLockFunAttrCommon(S, D, Attr, Args))
691 return;
692
693 unsigned Size = Args.size();
694 Expr **StartArg = Size == 0 ? 0 : &Args[0];
695 D->addAttr(::new (S.Context)
696 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
697 StartArg, Size,
698 Attr.getAttributeSpellingListIndex()));
699}
700
701
Michael Hana9171bc2012-08-03 17:40:43 +0000702static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
703 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000704 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000706 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000707
Aaron Ballman00e99962013-08-31 01:11:41 +0000708 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000709 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000710 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000711 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000712 }
713
714 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000715 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000716
Michael Han3be3b442012-07-23 18:48:41 +0000717 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000718}
719
Michael Hana9171bc2012-08-03 17:40:43 +0000720static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000721 const AttributeList &Attr) {
722 SmallVector<Expr*, 2> Args;
723 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
724 return;
725
Michael Han99315932013-01-24 16:46:58 +0000726 D->addAttr(::new (S.Context)
727 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000728 Attr.getArgAsExpr(0),
729 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000730 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000731}
732
Michael Hana9171bc2012-08-03 17:40:43 +0000733static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000734 const AttributeList &Attr) {
735 SmallVector<Expr*, 2> Args;
736 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
737 return;
738
Michael Han99315932013-01-24 16:46:58 +0000739 D->addAttr(::new (S.Context)
740 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000741 Attr.getArgAsExpr(0),
742 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000743 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000744}
745
Michael Hana9171bc2012-08-03 17:40:43 +0000746static bool checkLocksRequiredCommon(Sema &S, Decl *D,
747 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000748 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000749 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000750 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000751
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000752 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000753 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000754 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000755 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000756
Michael Han3be3b442012-07-23 18:48:41 +0000757 return true;
758}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000759
Michael Hana9171bc2012-08-03 17:40:43 +0000760static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000761 const AttributeList &Attr) {
762 SmallVector<Expr*, 1> Args;
763 if (!checkLocksRequiredCommon(S, D, Attr, Args))
764 return;
765
766 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000767 D->addAttr(::new (S.Context)
768 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
769 StartArg, Args.size(),
770 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000771}
772
Michael Hana9171bc2012-08-03 17:40:43 +0000773static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000774 const AttributeList &Attr) {
775 SmallVector<Expr*, 1> Args;
776 if (!checkLocksRequiredCommon(S, D, Attr, Args))
777 return;
778
779 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000780 D->addAttr(::new (S.Context)
781 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
782 StartArg, Args.size(),
783 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784}
785
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000788 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000789 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000790 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000791 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000792 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000793 Expr **StartArg = Size == 0 ? 0 : &Args[0];
794
Michael Han99315932013-01-24 16:46:58 +0000795 D->addAttr(::new (S.Context)
796 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
797 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000798}
799
800static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000801 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000802 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000803 SmallVector<Expr*, 1> Args;
804 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
805 unsigned Size = Args.size();
806 if (Size == 0)
807 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000808
Michael Han99315932013-01-24 16:46:58 +0000809 D->addAttr(::new (S.Context)
810 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
811 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000812}
813
814static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000815 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000816 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000817 return;
818
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000819 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000820 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000821 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000822 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000823 if (Size == 0)
824 return;
825 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000826
Michael Han99315932013-01-24 16:46:58 +0000827 D->addAttr(::new (S.Context)
828 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
829 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000830}
831
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000832static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000833 ConsumableAttr::ConsumedState DefaultState;
834
835 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000836 IdentifierLoc *IL = Attr.getArgAsIdent(0);
837 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
838 DefaultState)) {
839 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
840 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000841 return;
842 }
David Blaikie16f76d22013-09-06 01:28:43 +0000843 } else {
844 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
845 << Attr.getName() << AANT_ArgumentIdentifier;
846 return;
847 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000848
849 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000850 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000851 Attr.getAttributeSpellingListIndex()));
852}
853
854static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
855 const AttributeList &Attr) {
856 ASTContext &CurrContext = S.getASTContext();
857 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
858
859 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
860 if (!RD->hasAttr<ConsumableAttr>()) {
861 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
862 RD->getNameAsString();
863
864 return false;
865 }
866 }
867
868 return true;
869}
870
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000871
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000872static void handleCallableWhenAttr(Sema &S, Decl *D,
873 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000874 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
875 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000876
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000877 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
878 return;
879
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000880 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
881 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
882 CallableWhenAttr::ConsumedState CallableState;
883
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000884 StringRef StateString;
885 SourceLocation Loc;
886 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
887 return;
888
889 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000890 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000891 S.Diag(Loc, diag::warn_attribute_type_not_supported)
892 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000893 return;
894 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000895
896 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000897 }
898
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000899 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000900 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
901 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000902}
903
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000904
DeLesley Hutchins69391772013-10-17 23:23:53 +0000905static void handleParamTypestateAttr(Sema &S, Decl *D,
906 const AttributeList &Attr) {
907 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000908
DeLesley Hutchins69391772013-10-17 23:23:53 +0000909 ParamTypestateAttr::ConsumedState ParamState;
910
911 if (Attr.isArgIdent(0)) {
912 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
913 StringRef StateString = Ident->Ident->getName();
914
915 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
916 ParamState)) {
917 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
918 << Attr.getName() << StateString;
919 return;
920 }
921 } else {
922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
923 Attr.getName() << AANT_ArgumentIdentifier;
924 return;
925 }
926
927 // FIXME: This check is currently being done in the analysis. It can be
928 // enabled here only after the parser propagates attributes at
929 // template specialization definition, not declaration.
930 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
931 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
932 //
933 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
934 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
935 // ReturnType.getAsString();
936 // return;
937 //}
938
939 D->addAttr(::new (S.Context)
940 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
941 Attr.getAttributeSpellingListIndex()));
942}
943
944
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000945static void handleReturnTypestateAttr(Sema &S, Decl *D,
946 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000947 if (!checkAttributeNumArgs(S, Attr, 1)) return;
948
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000949 ReturnTypestateAttr::ConsumedState ReturnState;
950
951 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000952 IdentifierLoc *IL = Attr.getArgAsIdent(0);
953 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
954 ReturnState)) {
955 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
956 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000957 return;
958 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000959 } else {
960 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
961 Attr.getName() << AANT_ArgumentIdentifier;
962 return;
963 }
964
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000965 // FIXME: This check is currently being done in the analysis. It can be
966 // enabled here only after the parser propagates attributes at
967 // template specialization definition, not declaration.
968 //QualType ReturnType;
969 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000970 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
971 // ReturnType = Param->getType();
972 //
973 //} else if (const CXXConstructorDecl *Constructor =
974 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000975 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
976 //
977 //} else {
978 //
979 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
980 //}
981 //
982 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
983 //
984 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
985 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
986 // ReturnType.getAsString();
987 // return;
988 //}
989
990 D->addAttr(::new (S.Context)
991 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
992 Attr.getAttributeSpellingListIndex()));
993}
994
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000995
996static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000997 if (!checkAttributeNumArgs(S, Attr, 1))
998 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000999
1000 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1001 return;
1002
1003 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001004 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001005 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1006 StringRef Param = Ident->Ident->getName();
1007 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1008 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1009 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001010 return;
1011 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012 } else {
1013 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1014 Attr.getName() << AANT_ArgumentIdentifier;
1015 return;
1016 }
1017
1018 D->addAttr(::new (S.Context)
1019 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1020 Attr.getAttributeSpellingListIndex()));
1021}
1022
Chris Wailes9385f9f2013-10-29 20:28:41 +00001023static void handleTestTypestateAttr(Sema &S, Decl *D,
1024 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001025 if (!checkAttributeNumArgs(S, Attr, 1))
1026 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001027
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001028 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1029 return;
1030
Chris Wailes9385f9f2013-10-29 20:28:41 +00001031 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001032 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001033 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1034 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001035 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001036 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1037 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001038 return;
1039 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001040 } else {
1041 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1042 Attr.getName() << AANT_ArgumentIdentifier;
1043 return;
1044 }
1045
1046 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001047 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001048 Attr.getAttributeSpellingListIndex()));
1049}
1050
Chandler Carruthedc2c642011-07-02 00:01:44 +00001051static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1052 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001053 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001054 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001055}
1056
Chandler Carruthedc2c642011-07-02 00:01:44 +00001057static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001058 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001059 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001060 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001061 // If the alignment is less than or equal to 8 bits, the packed attribute
1062 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001063 if (!FD->getType()->isDependentType() &&
1064 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001065 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001066 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001067 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001068 else
Michael Han99315932013-01-24 16:46:58 +00001069 FD->addAttr(::new (S.Context)
1070 PackedAttr(Attr.getRange(), S.Context,
1071 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001072 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001073 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001074}
1075
Ted Kremenek7fd17232011-09-29 07:02:25 +00001076static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1077 // The IBOutlet/IBOutletCollection attributes only apply to instance
1078 // variables or properties of Objective-C classes. The outlet must also
1079 // have an object reference type.
1080 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1081 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001082 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001083 << Attr.getName() << VD->getType() << 0;
1084 return false;
1085 }
1086 }
1087 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1088 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001089 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001090 << Attr.getName() << PD->getType() << 1;
1091 return false;
1092 }
1093 }
1094 else {
1095 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1096 return false;
1097 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001098
Ted Kremenek7fd17232011-09-29 07:02:25 +00001099 return true;
1100}
1101
Chandler Carruthedc2c642011-07-02 00:01:44 +00001102static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001103 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001104 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001105
Michael Han99315932013-01-24 16:46:58 +00001106 D->addAttr(::new (S.Context)
1107 IBOutletAttr(Attr.getRange(), S.Context,
1108 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001109}
1110
Chandler Carruthedc2c642011-07-02 00:01:44 +00001111static void handleIBOutletCollection(Sema &S, Decl *D,
1112 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001113
1114 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001115 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1117 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001118 return;
1119 }
1120
Ted Kremenek7fd17232011-09-29 07:02:25 +00001121 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001122 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001123
Richard Smithb1f9a282013-10-31 01:56:18 +00001124 ParsedType PT;
1125
1126 if (Attr.hasParsedType())
1127 PT = Attr.getTypeArg();
1128 else {
1129 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1130 S.getScopeForContext(D->getDeclContext()->getParent()));
1131 if (!PT) {
1132 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1133 return;
1134 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001135 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001136
Richard Smithb87c4652013-10-31 21:23:20 +00001137 TypeSourceInfo *QTLoc = 0;
1138 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1139 if (!QTLoc)
1140 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001141
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001142 // Diagnose use of non-object type in iboutletcollection attribute.
1143 // FIXME. Gnu attribute extension ignores use of builtin types in
1144 // attributes. So, __attribute__((iboutletcollection(char))) will be
1145 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001146 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001147 S.Diag(Attr.getLoc(),
1148 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1149 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001150 return;
1151 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001152
Michael Han99315932013-01-24 16:46:58 +00001153 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001154 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001155 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001156}
1157
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001158static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001159 if (const RecordType *UT = T->getAsUnionType())
1160 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1161 RecordDecl *UD = UT->getDecl();
1162 for (RecordDecl::field_iterator it = UD->field_begin(),
1163 itend = UD->field_end(); it != itend; ++it) {
1164 QualType QT = it->getType();
1165 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1166 T = QT;
1167 return;
1168 }
1169 }
1170 }
1171}
1172
Chandler Carruthedc2c642011-07-02 00:01:44 +00001173static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001174 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1175 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001176 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001178 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001179 return;
1180 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001181
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001182 SmallVector<unsigned, 8> NonNullArgs;
1183 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001184 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001185 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001186 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001187 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001188
1189 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001190 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001191 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001192
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001193 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001194 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001195 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1196 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001197 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001198 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001199
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001200 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001201 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001202
1203 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1204 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001205 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001206 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1207 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001208 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001209 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001210 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001212
Ted Kremenek22813f42010-10-21 18:49:36 +00001213 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001214 if (NonNullArgs.empty()) {
1215 // Warn the trivial case only if attribute is not coming from a
1216 // macro instantiation.
1217 if (Attr.getLoc().isFileID())
1218 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001219 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001220 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001221 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001222
Nick Lewyckye1121512013-01-24 01:12:16 +00001223 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001224 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001225 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001226 D->addAttr(::new (S.Context)
1227 NonNullAttr(Attr.getRange(), S.Context, start, size,
1228 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001229}
1230
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001231static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1232 switch (K) {
1233 case OwnershipAttr::Holds: return "'ownership_holds'";
1234 case OwnershipAttr::Takes: return "'ownership_takes'";
1235 case OwnershipAttr::Returns: return "'ownership_returns'";
1236 }
1237 llvm_unreachable("unknown ownership");
1238}
1239
Chandler Carruthedc2c642011-07-02 00:01:44 +00001240static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001241 // This attribute must be applied to a function declaration. The first
1242 // argument to the attribute must be an identifier, the name of the resource,
1243 // for example: malloc. The following arguments must be argument indexes, the
1244 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001245 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001246 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001247 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001248
Aaron Ballman00e99962013-08-31 01:11:41 +00001249 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001250 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001251 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001252 return;
1253 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001254
Richard Smith852e9ce2013-11-27 01:46:48 +00001255 // Figure out our Kind.
1256 OwnershipAttr::OwnershipKind K =
1257 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1258 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001259
Richard Smith852e9ce2013-11-27 01:46:48 +00001260 // Check arguments.
1261 switch (K) {
1262 case OwnershipAttr::Takes:
1263 case OwnershipAttr::Holds:
1264 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001265 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1266 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001267 return;
1268 }
1269 break;
1270 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001271 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001272 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1273 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001274 return;
1275 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001276 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001277 }
1278
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001279 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001280 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1281 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001282 return;
1283 }
1284
Richard Smith852e9ce2013-11-27 01:46:48 +00001285 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001286
1287 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001288 StringRef ModuleName = Module->getName();
1289 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1290 ModuleName.size() > 4) {
1291 ModuleName = ModuleName.drop_front(2).drop_back(2);
1292 Module = &S.PP.getIdentifierTable().get(ModuleName);
1293 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001294
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001295 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001296 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1297 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001298 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001299 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001300 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001301
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001302 // Is the function argument a pointer type?
1303 QualType T = getFunctionOrMethodArgType(D, Idx);
1304 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001305 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001306 case OwnershipAttr::Takes:
1307 case OwnershipAttr::Holds:
1308 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1309 Err = 0;
1310 break;
1311 case OwnershipAttr::Returns:
1312 if (!T->isIntegerType())
1313 Err = 1;
1314 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001315 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001316 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001317 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001318 << Ex->getSourceRange();
1319 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001320 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321
1322 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001323 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001324 i = D->specific_attr_begin<OwnershipAttr>(),
1325 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001326 // FIXME: A returns attribute should conflict with any returns attribute
1327 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001328 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1329 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1330 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1331 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1332 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001333 }
1334 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001335 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001336 }
1337
1338 unsigned* start = OwnershipArgs.data();
1339 unsigned size = OwnershipArgs.size();
1340 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001341
Michael Han99315932013-01-24 16:46:58 +00001342 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001343 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001344 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001345}
1346
Chandler Carruthedc2c642011-07-02 00:01:44 +00001347static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001348 // Check the attribute arguments.
1349 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001350 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1351 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001352 return;
1353 }
1354
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001355 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001356
Rafael Espindolac18086a2010-02-23 22:00:30 +00001357 // gcc rejects
1358 // class c {
1359 // static int a __attribute__((weakref ("v2")));
1360 // static int b() __attribute__((weakref ("f3")));
1361 // };
1362 // and ignores the attributes of
1363 // void f(void) {
1364 // static int a __attribute__((weakref ("v2")));
1365 // }
1366 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001367 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001368 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001369 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1370 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001371 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001372 }
1373
1374 // The GCC manual says
1375 //
1376 // At present, a declaration to which `weakref' is attached can only
1377 // be `static'.
1378 //
1379 // It also says
1380 //
1381 // Without a TARGET,
1382 // given as an argument to `weakref' or to `alias', `weakref' is
1383 // equivalent to `weak'.
1384 //
1385 // gcc 4.4.1 will accept
1386 // int a7 __attribute__((weakref));
1387 // as
1388 // int a7 __attribute__((weak));
1389 // This looks like a bug in gcc. We reject that for now. We should revisit
1390 // it if this behaviour is actually used.
1391
Rafael Espindolac18086a2010-02-23 22:00:30 +00001392 // GCC rejects
1393 // static ((alias ("y"), weakref)).
1394 // Should we? How to check that weakref is before or after alias?
1395
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001396 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1397 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1398 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001399 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001400 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001401 // GCC will accept anything as the argument of weakref. Should we
1402 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001403 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1404 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001405
Michael Han99315932013-01-24 16:46:58 +00001406 D->addAttr(::new (S.Context)
1407 WeakRefAttr(Attr.getRange(), S.Context,
1408 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001409}
1410
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001411static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1412 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001413 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001414 return;
1415
Douglas Gregore8bbc122011-09-02 00:18:52 +00001416 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001417 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1418 return;
1419 }
1420
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001421 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001422
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001423 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001424 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001425}
1426
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001427static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001428 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001429 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001430
Michael Han99315932013-01-24 16:46:58 +00001431 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1432 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001433}
1434
1435static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001436 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001437 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001438
Michael Han99315932013-01-24 16:46:58 +00001439 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1440 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001441}
1442
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001443static void handleTLSModelAttr(Sema &S, Decl *D,
1444 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001445 StringRef Model;
1446 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001447 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001448 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001449 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001450
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001451 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001452 if (Model != "global-dynamic" && Model != "local-dynamic"
1453 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001454 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001455 return;
1456 }
1457
Michael Han99315932013-01-24 16:46:58 +00001458 D->addAttr(::new (S.Context)
1459 TLSModelAttr(Attr.getRange(), S.Context, Model,
1460 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001461}
1462
Chandler Carruthedc2c642011-07-02 00:01:44 +00001463static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001464 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001465 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001466 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001467 D->addAttr(::new (S.Context)
1468 MallocAttr(Attr.getRange(), S.Context,
1469 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001470 return;
1471 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001472 }
1473
Ted Kremenek08479ae2009-08-15 00:51:46 +00001474 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001475}
1476
Chandler Carruthedc2c642011-07-02 00:01:44 +00001477static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001478 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001479 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1480 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001481 return;
1482 }
1483
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001484 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1485 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001486}
1487
Chandler Carruthedc2c642011-07-02 00:01:44 +00001488static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001489 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001490
1491 if (S.CheckNoReturnAttr(attr)) return;
1492
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001493 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001494 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001495 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001496 return;
1497 }
1498
Michael Han99315932013-01-24 16:46:58 +00001499 D->addAttr(::new (S.Context)
1500 NoReturnAttr(attr.getRange(), S.Context,
1501 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001502}
1503
1504bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001505 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001506 attr.setInvalid();
1507 return true;
1508 }
1509
1510 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001511}
1512
Chandler Carruthedc2c642011-07-02 00:01:44 +00001513static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1514 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001515
1516 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1517 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001518 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1519 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001520 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1521 && !VD->getType()->isFunctionPointerType())) {
1522 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001523 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001524 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001525 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001526 return;
1527 }
1528 }
1529
Michael Han99315932013-01-24 16:46:58 +00001530 D->addAttr(::new (S.Context)
1531 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1532 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001533}
1534
John Thompsoncdb847ba2010-08-09 21:53:52 +00001535// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001536static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001537/*
1538 Returning a Vector Class in Registers
1539
Eric Christopherbc638a82010-12-01 22:13:54 +00001540 According to the PPU ABI specifications, a class with a single member of
1541 vector type is returned in memory when used as the return value of a function.
1542 This results in inefficient code when implementing vector classes. To return
1543 the value in a single vector register, add the vecreturn attribute to the
1544 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001545
1546 Example:
1547
1548 struct Vector
1549 {
1550 __vector float xyzw;
1551 } __attribute__((vecreturn));
1552
1553 Vector Add(Vector lhs, Vector rhs)
1554 {
1555 Vector result;
1556 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1557 return result; // This will be returned in a register
1558 }
1559*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001560 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1561 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001562 return;
1563 }
1564
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001565 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001566 int count = 0;
1567
1568 if (!isa<CXXRecordDecl>(record)) {
1569 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1570 return;
1571 }
1572
1573 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1574 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1575 return;
1576 }
1577
Eric Christopherbc638a82010-12-01 22:13:54 +00001578 for (RecordDecl::field_iterator iter = record->field_begin();
1579 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001580 if ((count == 1) || !iter->getType()->isVectorType()) {
1581 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1582 return;
1583 }
1584 count++;
1585 }
1586
Michael Han99315932013-01-24 16:46:58 +00001587 D->addAttr(::new (S.Context)
1588 VecReturnAttr(Attr.getRange(), S.Context,
1589 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001590}
1591
Richard Smithe233fbf2013-01-28 22:42:45 +00001592static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1593 const AttributeList &Attr) {
1594 if (isa<ParmVarDecl>(D)) {
1595 // [[carries_dependency]] can only be applied to a parameter if it is a
1596 // parameter of a function declaration or lambda.
1597 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1598 S.Diag(Attr.getLoc(),
1599 diag::err_carries_dependency_param_not_function_decl);
1600 return;
1601 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001602 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001603
1604 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1605 Attr.getRange(), S.Context,
1606 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001607}
1608
Chandler Carruthedc2c642011-07-02 00:01:44 +00001609static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001610 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001611 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001613 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001614 return;
1615 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001616
Michael Han99315932013-01-24 16:46:58 +00001617 D->addAttr(::new (S.Context)
1618 UnusedAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001620}
1621
Chandler Carruthedc2c642011-07-02 00:01:44 +00001622static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001623 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001624 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001625 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001626 return;
1627 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001628 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001630 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001631 return;
1632 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001633
Michael Han99315932013-01-24 16:46:58 +00001634 D->addAttr(::new (S.Context)
1635 UsedAttr(Attr.getRange(), S.Context,
1636 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001637}
1638
Chandler Carruthedc2c642011-07-02 00:01:44 +00001639static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001640 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001641 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001642 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1643 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001644 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001645 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001646
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001647 uint32_t priority = ConstructorAttr::DefaultPriority;
1648 if (Attr.getNumArgs() > 0 &&
1649 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1650 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001651
Michael Han99315932013-01-24 16:46:58 +00001652 D->addAttr(::new (S.Context)
1653 ConstructorAttr(Attr.getRange(), S.Context, priority,
1654 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001655}
1656
Chandler Carruthedc2c642011-07-02 00:01:44 +00001657static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001658 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001659 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001660 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1661 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001662 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001663 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001664
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001665 uint32_t priority = ConstructorAttr::DefaultPriority;
1666 if (Attr.getNumArgs() > 0 &&
1667 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1668 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001669
Michael Han99315932013-01-24 16:46:58 +00001670 D->addAttr(::new (S.Context)
1671 DestructorAttr(Attr.getRange(), S.Context, priority,
1672 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001673}
1674
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001675template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001676static void handleAttrWithMessage(Sema &S, Decl *D,
1677 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001678 unsigned NumArgs = Attr.getNumArgs();
1679 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001680 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1681 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001682 return;
1683 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001684
1685 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001686 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001687 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001688 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001689
Michael Han99315932013-01-24 16:46:58 +00001690 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1691 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001692}
1693
Ted Kremenek28eace62013-11-23 01:01:34 +00001694static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1695 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001696 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001697 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1698 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001699}
1700
Jordy Rose740b0c22012-05-08 03:27:22 +00001701static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1702 IdentifierInfo *Platform,
1703 VersionTuple Introduced,
1704 VersionTuple Deprecated,
1705 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001706 StringRef PlatformName
1707 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1708 if (PlatformName.empty())
1709 PlatformName = Platform->getName();
1710
1711 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1712 // of these steps are needed).
1713 if (!Introduced.empty() && !Deprecated.empty() &&
1714 !(Introduced <= Deprecated)) {
1715 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1716 << 1 << PlatformName << Deprecated.getAsString()
1717 << 0 << Introduced.getAsString();
1718 return true;
1719 }
1720
1721 if (!Introduced.empty() && !Obsoleted.empty() &&
1722 !(Introduced <= Obsoleted)) {
1723 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1724 << 2 << PlatformName << Obsoleted.getAsString()
1725 << 0 << Introduced.getAsString();
1726 return true;
1727 }
1728
1729 if (!Deprecated.empty() && !Obsoleted.empty() &&
1730 !(Deprecated <= Obsoleted)) {
1731 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1732 << 2 << PlatformName << Obsoleted.getAsString()
1733 << 1 << Deprecated.getAsString();
1734 return true;
1735 }
1736
1737 return false;
1738}
1739
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001740/// \brief Check whether the two versions match.
1741///
1742/// If either version tuple is empty, then they are assumed to match. If
1743/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1744static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1745 bool BeforeIsOkay) {
1746 if (X.empty() || Y.empty())
1747 return true;
1748
1749 if (X == Y)
1750 return true;
1751
1752 if (BeforeIsOkay && X < Y)
1753 return true;
1754
1755 return false;
1756}
1757
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001758AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001759 IdentifierInfo *Platform,
1760 VersionTuple Introduced,
1761 VersionTuple Deprecated,
1762 VersionTuple Obsoleted,
1763 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001764 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001765 bool Override,
1766 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001767 VersionTuple MergedIntroduced = Introduced;
1768 VersionTuple MergedDeprecated = Deprecated;
1769 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001770 bool FoundAny = false;
1771
Rafael Espindolac67f2232012-05-10 02:50:16 +00001772 if (D->hasAttrs()) {
1773 AttrVec &Attrs = D->getAttrs();
1774 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1775 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1776 if (!OldAA) {
1777 ++i;
1778 continue;
1779 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001780
Rafael Espindolac67f2232012-05-10 02:50:16 +00001781 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1782 if (OldPlatform != Platform) {
1783 ++i;
1784 continue;
1785 }
1786
1787 FoundAny = true;
1788 VersionTuple OldIntroduced = OldAA->getIntroduced();
1789 VersionTuple OldDeprecated = OldAA->getDeprecated();
1790 VersionTuple OldObsoleted = OldAA->getObsoleted();
1791 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001792
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001793 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1794 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1795 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1796 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001797 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001798 if (Override) {
1799 int Which = -1;
1800 VersionTuple FirstVersion;
1801 VersionTuple SecondVersion;
1802 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1803 Which = 0;
1804 FirstVersion = OldIntroduced;
1805 SecondVersion = Introduced;
1806 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1807 Which = 1;
1808 FirstVersion = Deprecated;
1809 SecondVersion = OldDeprecated;
1810 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1811 Which = 2;
1812 FirstVersion = Obsoleted;
1813 SecondVersion = OldObsoleted;
1814 }
1815
1816 if (Which == -1) {
1817 Diag(OldAA->getLocation(),
1818 diag::warn_mismatched_availability_override_unavail)
1819 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1820 } else {
1821 Diag(OldAA->getLocation(),
1822 diag::warn_mismatched_availability_override)
1823 << Which
1824 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1825 << FirstVersion.getAsString() << SecondVersion.getAsString();
1826 }
1827 Diag(Range.getBegin(), diag::note_overridden_method);
1828 } else {
1829 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1830 Diag(Range.getBegin(), diag::note_previous_attribute);
1831 }
1832
Rafael Espindolac67f2232012-05-10 02:50:16 +00001833 Attrs.erase(Attrs.begin() + i);
1834 --e;
1835 continue;
1836 }
1837
1838 VersionTuple MergedIntroduced2 = MergedIntroduced;
1839 VersionTuple MergedDeprecated2 = MergedDeprecated;
1840 VersionTuple MergedObsoleted2 = MergedObsoleted;
1841
1842 if (MergedIntroduced2.empty())
1843 MergedIntroduced2 = OldIntroduced;
1844 if (MergedDeprecated2.empty())
1845 MergedDeprecated2 = OldDeprecated;
1846 if (MergedObsoleted2.empty())
1847 MergedObsoleted2 = OldObsoleted;
1848
1849 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1850 MergedIntroduced2, MergedDeprecated2,
1851 MergedObsoleted2)) {
1852 Attrs.erase(Attrs.begin() + i);
1853 --e;
1854 continue;
1855 }
1856
1857 MergedIntroduced = MergedIntroduced2;
1858 MergedDeprecated = MergedDeprecated2;
1859 MergedObsoleted = MergedObsoleted2;
1860 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001861 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001862 }
1863
1864 if (FoundAny &&
1865 MergedIntroduced == Introduced &&
1866 MergedDeprecated == Deprecated &&
1867 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001868 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001869
Ted Kremenekb5445722013-04-06 00:34:27 +00001870 // Only create a new attribute if !Override, but we want to do
1871 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001872 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001873 MergedDeprecated, MergedObsoleted) &&
1874 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001875 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1876 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001877 Obsoleted, IsUnavailable, Message,
1878 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001879 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001880 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001881}
1882
Chandler Carruthedc2c642011-07-02 00:01:44 +00001883static void handleAvailabilityAttr(Sema &S, Decl *D,
1884 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001885 if (!checkAttributeNumArgs(S, Attr, 1))
1886 return;
1887 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001888 unsigned Index = Attr.getAttributeSpellingListIndex();
1889
Aaron Ballman00e99962013-08-31 01:11:41 +00001890 IdentifierInfo *II = Platform->Ident;
1891 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1892 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1893 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001894
Rafael Espindolac231fab2013-01-08 21:30:32 +00001895 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1896 if (!ND) {
1897 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1898 return;
1899 }
1900
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001901 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1902 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1903 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001904 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001905 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001906 if (const StringLiteral *SE =
1907 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001908 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001909
Aaron Ballman00e99962013-08-31 01:11:41 +00001910 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001911 Introduced.Version,
1912 Deprecated.Version,
1913 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001914 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001915 /*Override=*/false,
1916 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001917 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001918 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001919}
1920
John McCalld041a9b2013-02-20 01:54:26 +00001921template <class T>
1922static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1923 typename T::VisibilityType value,
1924 unsigned attrSpellingListIndex) {
1925 T *existingAttr = D->getAttr<T>();
1926 if (existingAttr) {
1927 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1928 if (existingValue == value)
1929 return NULL;
1930 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1931 S.Diag(range.getBegin(), diag::note_previous_attribute);
1932 D->dropAttr<T>();
1933 }
1934 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1935}
1936
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001937VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001938 VisibilityAttr::VisibilityType Vis,
1939 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001940 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1941 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001942}
1943
John McCalld041a9b2013-02-20 01:54:26 +00001944TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1945 TypeVisibilityAttr::VisibilityType Vis,
1946 unsigned AttrSpellingListIndex) {
1947 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1948 AttrSpellingListIndex);
1949}
1950
1951static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1952 bool isTypeVisibility) {
1953 // Visibility attributes don't mean anything on a typedef.
1954 if (isa<TypedefNameDecl>(D)) {
1955 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1956 << Attr.getName();
1957 return;
1958 }
1959
1960 // 'type_visibility' can only go on a type or namespace.
1961 if (isTypeVisibility &&
1962 !(isa<TagDecl>(D) ||
1963 isa<ObjCInterfaceDecl>(D) ||
1964 isa<NamespaceDecl>(D))) {
1965 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1966 << Attr.getName() << ExpectedTypeOrNamespace;
1967 return;
1968 }
1969
Benjamin Kramer70370212013-09-09 15:08:57 +00001970 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001971 StringRef TypeStr;
1972 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001973 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001974 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001975
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001976 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001977 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001978 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001979 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001980 return;
1981 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001982
1983 // Complain about attempts to use protected visibility on targets
1984 // (like Darwin) that don't support it.
1985 if (type == VisibilityAttr::Protected &&
1986 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1987 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1988 type = VisibilityAttr::Default;
1989 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001990
Michael Han99315932013-01-24 16:46:58 +00001991 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001992 clang::Attr *newAttr;
1993 if (isTypeVisibility) {
1994 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1995 (TypeVisibilityAttr::VisibilityType) type,
1996 Index);
1997 } else {
1998 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1999 }
2000 if (newAttr)
2001 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002002}
2003
Chandler Carruthedc2c642011-07-02 00:01:44 +00002004static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2005 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002006 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002007 if (!Attr.isArgIdent(0)) {
2008 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2009 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002010 return;
2011 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002012
Aaron Ballman682ee422013-09-11 19:47:58 +00002013 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2014 ObjCMethodFamilyAttr::FamilyKind F;
2015 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2016 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2017 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002018 return;
2019 }
2020
Aaron Ballman682ee422013-09-11 19:47:58 +00002021 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002022 !method->getResultType()->isObjCObjectPointerType()) {
2023 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2024 << method->getResultType();
2025 // Ignore the attribute.
2026 return;
2027 }
2028
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002029 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002030 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002031}
2032
Chandler Carruthedc2c642011-07-02 00:01:44 +00002033static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002034 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002035 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002036 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002037 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2038 return;
2039 }
2040 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002041 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2042 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002043 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002044 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2045 return;
2046 }
2047 }
2048 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002049 // It is okay to include this attribute on properties, e.g.:
2050 //
2051 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2052 //
2053 // In this case it follows tradition and suppresses an error in the above
2054 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002055 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002056 }
Michael Han99315932013-01-24 16:46:58 +00002057 D->addAttr(::new (S.Context)
2058 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2059 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002060}
2061
Chandler Carruthedc2c642011-07-02 00:01:44 +00002062static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002063 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002065 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002066 return;
2067 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002068
Aaron Ballman00e99962013-08-31 01:11:41 +00002069 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002070 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002071 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2072 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2073 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002074 return;
2075 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002076
Michael Han99315932013-01-24 16:46:58 +00002077 D->addAttr(::new (S.Context)
2078 BlocksAttr(Attr.getRange(), S.Context, type,
2079 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002080}
2081
Chandler Carruthedc2c642011-07-02 00:01:44 +00002082static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002083 // check the attribute arguments.
2084 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002085 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2086 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002087 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002088 }
2089
Aaron Ballman18a78382013-11-21 00:28:23 +00002090 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002091 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002092 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002093 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002094 if (E->isTypeDependent() || E->isValueDependent() ||
2095 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002096 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002097 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002098 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002099 return;
2100 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002101
John McCallb46f2872011-09-09 07:56:05 +00002102 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002103 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2104 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002105 return;
2106 }
John McCallb46f2872011-09-09 07:56:05 +00002107
2108 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002109 }
2110
Aaron Ballman18a78382013-11-21 00:28:23 +00002111 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002112 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002113 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002114 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002115 if (E->isTypeDependent() || E->isValueDependent() ||
2116 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002118 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002119 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002120 return;
2121 }
2122 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002123
John McCallb46f2872011-09-09 07:56:05 +00002124 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002125 // FIXME: This error message could be improved, it would be nice
2126 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002127 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2128 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002129 return;
2130 }
2131 }
2132
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002133 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002134 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002135 if (isa<FunctionNoProtoType>(FT)) {
2136 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2137 return;
2138 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002139
Chris Lattner9363e312009-03-17 23:03:47 +00002140 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002141 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002142 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002143 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002144 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002145 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002146 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002147 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002148 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002149 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2150 if (!BD->isVariadic()) {
2151 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2152 return;
2153 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002154 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002155 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002156 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002157 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002158 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002159 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002160 int m = Ty->isFunctionPointerType() ? 0 : 1;
2161 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002162 return;
2163 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002164 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002165 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002166 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002167 return;
2168 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002169 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002171 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002172 return;
2173 }
Michael Han99315932013-01-24 16:46:58 +00002174 D->addAttr(::new (S.Context)
2175 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2176 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002177}
2178
Chandler Carruthedc2c642011-07-02 00:01:44 +00002179static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002180 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002181 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002182 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002183 return;
2184 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002185
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002186 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2187 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2188 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002189 return;
2190 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002191 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2192 if (MD->getResultType()->isVoidType()) {
2193 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2194 << Attr.getName() << 1;
2195 return;
2196 }
2197
Michael Han99315932013-01-24 16:46:58 +00002198 D->addAttr(::new (S.Context)
2199 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2200 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002201}
2202
Chandler Carruthedc2c642011-07-02 00:01:44 +00002203static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002204 // weak_import only applies to variable & function declarations.
2205 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002206 if (!D->canBeWeakImported(isDef)) {
2207 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002208 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2209 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002210 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002211 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002212 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002213 // Nothing to warn about here.
2214 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002215 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002216 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002217
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002218 return;
2219 }
2220
Michael Han99315932013-01-24 16:46:58 +00002221 D->addAttr(::new (S.Context)
2222 WeakImportAttr(Attr.getRange(), S.Context,
2223 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002224}
2225
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002226// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002227template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002228static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002229 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002230 uint32_t WGSize[3];
2231 for (unsigned i = 0; i < 3; ++i)
2232 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002233 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002234
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002235 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2236 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2237 Existing->getYDim() == WGSize[1] &&
2238 Existing->getZDim() == WGSize[2]))
2239 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002240
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002241 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2242 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002243 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002244}
2245
Joey Goulyaba589c2013-03-08 09:42:32 +00002246static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002247 if (!Attr.hasParsedType()) {
2248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2249 << Attr.getName() << 1;
2250 return;
2251 }
2252
Richard Smithb87c4652013-10-31 21:23:20 +00002253 TypeSourceInfo *ParmTSI = 0;
2254 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2255 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002256
2257 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2258 (ParmType->isBooleanType() ||
2259 !ParmType->isIntegralType(S.getASTContext()))) {
2260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2261 << ParmType;
2262 return;
2263 }
2264
Aaron Ballmana9e05402013-12-02 22:16:55 +00002265 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002266 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002267 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2268 return;
2269 }
2270 }
2271
2272 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002273 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002274}
2275
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002276SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002277 StringRef Name,
2278 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002279 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2280 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002281 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002282 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2283 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002284 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002285 }
Michael Han99315932013-01-24 16:46:58 +00002286 return ::new (Context) SectionAttr(Range, Context, Name,
2287 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002288}
2289
Chandler Carruthedc2c642011-07-02 00:01:44 +00002290static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002291 // Make sure that there is a string literal as the sections's single
2292 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002293 StringRef Str;
2294 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002295 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002296 return;
Mike Stump11289f42009-09-09 15:08:12 +00002297
Chris Lattner30ba6742009-08-10 19:03:04 +00002298 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002299 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002300 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002301 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002302 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002303 return;
2304 }
Mike Stump11289f42009-09-09 15:08:12 +00002305
Michael Han99315932013-01-24 16:46:58 +00002306 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002307 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002308 if (NewAttr)
2309 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002310}
2311
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002312
Chandler Carruthedc2c642011-07-02 00:01:44 +00002313static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002314 VarDecl *VD = cast<VarDecl>(D);
2315 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002316 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002317 return;
2318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002319
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002320 Expr *E = Attr.getArgAsExpr(0);
2321 SourceLocation Loc = E->getExprLoc();
2322 FunctionDecl *FD = 0;
2323 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002324
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002325 // gcc only allows for simple identifiers. Since we support more than gcc, we
2326 // will warn the user.
2327 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2328 if (DRE->hasQualifier())
2329 S.Diag(Loc, diag::warn_cleanup_ext);
2330 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2331 NI = DRE->getNameInfo();
2332 if (!FD) {
2333 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2334 << NI.getName();
2335 return;
2336 }
2337 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2338 if (ULE->hasExplicitTemplateArgs())
2339 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002340 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2341 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002342 if (!FD) {
2343 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2344 << NI.getName();
2345 if (ULE->getType() == S.Context.OverloadTy)
2346 S.NoteAllOverloadCandidates(ULE);
2347 return;
2348 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002349 } else {
2350 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002351 return;
2352 }
2353
Anders Carlssond277d792009-01-31 01:16:18 +00002354 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002355 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2356 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002357 return;
2358 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002359
Anders Carlsson723f55d2009-02-07 23:16:50 +00002360 // We're currently more strict than GCC about what function types we accept.
2361 // If this ever proves to be a problem it should be easy to fix.
2362 QualType Ty = S.Context.getPointerType(VD->getType());
2363 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002364 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2365 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002366 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2367 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002368 return;
2369 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002370
Michael Han99315932013-01-24 16:46:58 +00002371 D->addAttr(::new (S.Context)
2372 CleanupAttr(Attr.getRange(), S.Context, FD,
2373 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002374}
2375
Mike Stumpd3bb5572009-07-24 19:02:52 +00002376/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002377/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002378static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002379 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002380 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002381 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002382 return;
2383 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002384
Aaron Ballman00e99962013-08-31 01:11:41 +00002385 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002386 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002387 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002388 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002389
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002390 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002391 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002392
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002393 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2394 if (not_nsstring_type &&
2395 !isCFStringType(Ty, S.Context) &&
2396 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002397 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002398 // FIXME: Should highlight the actual expression that has the wrong type.
2399 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002400 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002401 << IdxExpr->getSourceRange();
2402 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002404 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002405 if (!isNSStringType(Ty, S.Context) &&
2406 !isCFStringType(Ty, S.Context) &&
2407 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002408 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002409 // FIXME: Should highlight the actual expression that has the wrong type.
2410 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002411 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002412 << IdxExpr->getSourceRange();
2413 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002414 }
2415
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002416 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2417 // because that has corrected for the implicit this parameter, and is zero-
2418 // based. The attribute expects what the user wrote explicitly.
2419 llvm::APSInt Val;
2420 IdxExpr->EvaluateAsInt(Val, S.Context);
2421
Michael Han99315932013-01-24 16:46:58 +00002422 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002423 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002424 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002425}
2426
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002427enum FormatAttrKind {
2428 CFStringFormat,
2429 NSStringFormat,
2430 StrftimeFormat,
2431 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002432 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002433 InvalidFormat
2434};
2435
2436/// getFormatAttrKind - Map from format attribute names to supported format
2437/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002438static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002439 return llvm::StringSwitch<FormatAttrKind>(Format)
2440 // Check for formats that get handled specially.
2441 .Case("NSString", NSStringFormat)
2442 .Case("CFString", CFStringFormat)
2443 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002444
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002445 // Otherwise, check for supported formats.
2446 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2447 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2448 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002449
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002450 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2451 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002452}
2453
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002454/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002455/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002456static void handleInitPriorityAttr(Sema &S, Decl *D,
2457 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002458 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002459 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2460 return;
2461 }
2462
Aaron Ballman4a611152013-11-27 16:34:09 +00002463 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002464 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2465 Attr.setInvalid();
2466 return;
2467 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002468 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002469 if (S.Context.getAsArrayType(T))
2470 T = S.Context.getBaseElementType(T);
2471 if (!T->getAs<RecordType>()) {
2472 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2473 Attr.setInvalid();
2474 return;
2475 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002476
2477 Expr *E = Attr.getArgAsExpr(0);
2478 uint32_t prioritynum;
2479 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002480 Attr.setInvalid();
2481 return;
2482 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002483
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002484 if (prioritynum < 101 || prioritynum > 65535) {
2485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002486 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002487 Attr.setInvalid();
2488 return;
2489 }
Michael Han99315932013-01-24 16:46:58 +00002490 D->addAttr(::new (S.Context)
2491 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2492 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002493}
2494
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002495FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2496 IdentifierInfo *Format, int FormatIdx,
2497 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002498 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002499 // Check whether we already have an equivalent format attribute.
2500 for (specific_attr_iterator<FormatAttr>
2501 i = D->specific_attr_begin<FormatAttr>(),
2502 e = D->specific_attr_end<FormatAttr>();
2503 i != e ; ++i) {
2504 FormatAttr *f = *i;
2505 if (f->getType() == Format &&
2506 f->getFormatIdx() == FormatIdx &&
2507 f->getFirstArg() == FirstArg) {
2508 // If we don't have a valid location for this attribute, adopt the
2509 // location.
2510 if (f->getLocation().isInvalid())
2511 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002512 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002513 }
2514 }
2515
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002516 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2517 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002518}
2519
Mike Stumpd3bb5572009-07-24 19:02:52 +00002520/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002521/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002522static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002523 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002525 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002526 return;
2527 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002529 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002531 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002532 return;
2533 }
2534
Chandler Carruth743682b2010-11-16 08:35:43 +00002535 // In C++ the implicit 'this' function parameter also counts, and they are
2536 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002537 bool HasImplicitThisParam = isInstanceMethod(D);
2538 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002539
Aaron Ballman00e99962013-08-31 01:11:41 +00002540 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2541 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002542
2543 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002544 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002545 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002546 // If we've modified the string name, we need a new identifier for it.
2547 II = &S.Context.Idents.get(Format);
2548 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002549
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002550 // Check for supported formats.
2551 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002552
2553 if (Kind == IgnoredFormat)
2554 return;
2555
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002556 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002557 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002558 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559 return;
2560 }
2561
2562 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002563 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002564 uint32_t Idx;
2565 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002568 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002570 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002571 return;
2572 }
2573
2574 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002575 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002576
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002577 if (HasImplicitThisParam) {
2578 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002579 S.Diag(Attr.getLoc(),
2580 diag::err_format_attribute_implicit_this_format_string)
2581 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002582 return;
2583 }
2584 ArgIdx--;
2585 }
Mike Stump11289f42009-09-09 15:08:12 +00002586
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002588 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002589
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002590 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002591 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002592 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2593 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002594 return;
2595 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002596 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002597 // FIXME: do we need to check if the type is NSString*? What are the
2598 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002599 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002600 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002601 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2602 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002603 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002604 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002605 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002606 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002607 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002608 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2609 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610 return;
2611 }
2612
2613 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002614 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002615 uint32_t FirstArg;
2616 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002617 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618
2619 // check if the function is variadic if the 3rd argument non-zero
2620 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002621 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002622 ++NumArgs; // +1 for ...
2623 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002624 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625 return;
2626 }
2627 }
2628
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002629 // strftime requires FirstArg to be 0 because it doesn't read from any
2630 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002631 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002632 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002633 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2634 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635 return;
2636 }
2637 // if 0 it disables parameter checking (to use with e.g. va_list)
2638 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002639 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002640 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641 return;
2642 }
2643
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002644 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002645 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002646 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002647 if (NewAttr)
2648 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649}
2650
Chandler Carruthedc2c642011-07-02 00:01:44 +00002651static void handleTransparentUnionAttr(Sema &S, Decl *D,
2652 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002653 // Try to find the underlying union declaration.
2654 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002655 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002656 if (TD && TD->getUnderlyingType()->isUnionType())
2657 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2658 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002659 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002660
2661 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002663 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002664 return;
2665 }
2666
John McCallf937c022011-10-07 06:10:15 +00002667 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002668 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002669 diag::warn_transparent_union_attribute_not_definition);
2670 return;
2671 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002673 RecordDecl::field_iterator Field = RD->field_begin(),
2674 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002675 if (Field == FieldEnd) {
2676 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2677 return;
2678 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002679
David Blaikie40ed2972012-06-06 20:45:41 +00002680 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002681 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002682 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002683 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002684 diag::warn_transparent_union_attribute_floating)
2685 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002686 return;
2687 }
2688
2689 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2690 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2691 for (; Field != FieldEnd; ++Field) {
2692 QualType FieldType = Field->getType();
2693 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2694 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2695 // Warn if we drop the attribute.
2696 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002697 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002698 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002699 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002700 diag::warn_transparent_union_attribute_field_size_align)
2701 << isSize << Field->getDeclName() << FieldBits;
2702 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002703 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002704 diag::note_transparent_union_first_field_size_align)
2705 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002706 return;
2707 }
2708 }
2709
Michael Han99315932013-01-24 16:46:58 +00002710 RD->addAttr(::new (S.Context)
2711 TransparentUnionAttr(Attr.getRange(), S.Context,
2712 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002713}
2714
Chandler Carruthedc2c642011-07-02 00:01:44 +00002715static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716 // Make sure that there is a string literal as the annotation's single
2717 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002718 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002719 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002720 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002721
2722 // Don't duplicate annotations that are already set.
2723 for (specific_attr_iterator<AnnotateAttr>
2724 i = D->specific_attr_begin<AnnotateAttr>(),
2725 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002726 if ((*i)->getAnnotation() == Str)
2727 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002728 }
Michael Han99315932013-01-24 16:46:58 +00002729
2730 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002731 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002732 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002733}
2734
Chandler Carruthedc2c642011-07-02 00:01:44 +00002735static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002736 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002737 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002738 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2739 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740 return;
2741 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002742
Richard Smith848e1f12013-02-01 08:12:08 +00002743 if (Attr.getNumArgs() == 0) {
2744 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2745 true, 0, Attr.getAttributeSpellingListIndex()));
2746 return;
2747 }
2748
Aaron Ballman00e99962013-08-31 01:11:41 +00002749 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002750 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2751 S.Diag(Attr.getEllipsisLoc(),
2752 diag::err_pack_expansion_without_parameter_packs);
2753 return;
2754 }
2755
2756 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2757 return;
2758
2759 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2760 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002761}
2762
2763void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002764 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002765 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2766 SourceLocation AttrLoc = AttrRange.getBegin();
2767
Richard Smith1dba27c2013-01-29 09:02:09 +00002768 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002769 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002770 // C++11 [dcl.align]p1:
2771 // An alignment-specifier may be applied to a variable or to a class
2772 // data member, but it shall not be applied to a bit-field, a function
2773 // parameter, the formal parameter of a catch clause, or a variable
2774 // declared with the register storage class specifier. An
2775 // alignment-specifier may also be applied to the declaration of a class
2776 // or enumeration type.
2777 // C11 6.7.5/2:
2778 // An alignment attribute shall not be specified in a declaration of
2779 // a typedef, or a bit-field, or a function, or a parameter, or an
2780 // object declared with the register storage-class specifier.
2781 int DiagKind = -1;
2782 if (isa<ParmVarDecl>(D)) {
2783 DiagKind = 0;
2784 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2785 if (VD->getStorageClass() == SC_Register)
2786 DiagKind = 1;
2787 if (VD->isExceptionVariable())
2788 DiagKind = 2;
2789 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2790 if (FD->isBitField())
2791 DiagKind = 3;
2792 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002793 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002794 << (TmpAttr.isC11() ? ExpectedVariableOrField
2795 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002796 return;
2797 }
2798 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002799 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002800 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002801 return;
2802 }
2803 }
2804
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002805 if (E->isTypeDependent() || E->isValueDependent()) {
2806 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002807 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2808 AA->setPackExpansion(IsPackExpansion);
2809 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002810 return;
2811 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002812
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002813 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002814 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002815 ExprResult ICE
2816 = VerifyIntegerConstantExpression(E, &Alignment,
2817 diag::err_aligned_attribute_argument_not_int,
2818 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002819 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002820 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002821
2822 // C++11 [dcl.align]p2:
2823 // -- if the constant expression evaluates to zero, the alignment
2824 // specifier shall have no effect
2825 // C11 6.7.5p6:
2826 // An alignment specification of zero has no effect.
2827 if (!(TmpAttr.isAlignas() && !Alignment) &&
2828 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002829 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2830 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002831 return;
2832 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002833
Richard Smith848e1f12013-02-01 08:12:08 +00002834 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002835 // We've already verified it's a power of 2, now let's make sure it's
2836 // 8192 or less.
2837 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002838 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002839 << E->getSourceRange();
2840 return;
2841 }
2842 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002843
Richard Smith44c247f2013-02-22 08:32:16 +00002844 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2845 ICE.take(), SpellingListIndex);
2846 AA->setPackExpansion(IsPackExpansion);
2847 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002848}
2849
Michael Hanaf02bbe2013-02-01 01:19:17 +00002850void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002851 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002852 // FIXME: Cache the number on the Attr object if non-dependent?
2853 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002854 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2855 SpellingListIndex);
2856 AA->setPackExpansion(IsPackExpansion);
2857 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002858}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002859
Richard Smith848e1f12013-02-01 08:12:08 +00002860void Sema::CheckAlignasUnderalignment(Decl *D) {
2861 assert(D->hasAttrs() && "no attributes on decl");
2862
2863 QualType Ty;
2864 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2865 Ty = VD->getType();
2866 else
2867 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002868 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002869 return;
2870
2871 // C++11 [dcl.align]p5, C11 6.7.5/4:
2872 // The combined effect of all alignment attributes in a declaration shall
2873 // not specify an alignment that is less strict than the alignment that
2874 // would otherwise be required for the entity being declared.
2875 AlignedAttr *AlignasAttr = 0;
2876 unsigned Align = 0;
2877 for (specific_attr_iterator<AlignedAttr>
2878 I = D->specific_attr_begin<AlignedAttr>(),
2879 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2880 if (I->isAlignmentDependent())
2881 return;
2882 if (I->isAlignas())
2883 AlignasAttr = *I;
2884 Align = std::max(Align, I->getAlignment(Context));
2885 }
2886
2887 if (AlignasAttr && Align) {
2888 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2889 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2890 if (NaturalAlign > RequestedAlign)
2891 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2892 << Ty << (unsigned)NaturalAlign.getQuantity();
2893 }
2894}
2895
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002896/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002897/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002898///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002899/// Despite what would be logical, the mode attribute is a decl attribute, not a
2900/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2901/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002902static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002903 // This attribute isn't documented, but glibc uses it. It changes
2904 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002905 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002906 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2907 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002908 return;
2909 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002910
Aaron Ballman00e99962013-08-31 01:11:41 +00002911 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2912 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002913
2914 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002915 if (Str.startswith("__") && Str.endswith("__"))
2916 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002917
2918 unsigned DestWidth = 0;
2919 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002920 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002921 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002922 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002923 switch (Str[0]) {
2924 case 'Q': DestWidth = 8; break;
2925 case 'H': DestWidth = 16; break;
2926 case 'S': DestWidth = 32; break;
2927 case 'D': DestWidth = 64; break;
2928 case 'X': DestWidth = 96; break;
2929 case 'T': DestWidth = 128; break;
2930 }
2931 if (Str[1] == 'F') {
2932 IntegerMode = false;
2933 } else if (Str[1] == 'C') {
2934 IntegerMode = false;
2935 ComplexMode = true;
2936 } else if (Str[1] != 'I') {
2937 DestWidth = 0;
2938 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939 break;
2940 case 4:
2941 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2942 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002943 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002944 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002945 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002946 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002947 break;
2948 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002949 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002950 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002951 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002952 case 11:
2953 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002954 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002955 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002956 }
2957
2958 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002959 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002960 OldTy = TD->getUnderlyingType();
2961 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2962 OldTy = VD->getType();
2963 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002964 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002965 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966 return;
2967 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002968
John McCall9dd450b2009-09-21 23:43:11 +00002969 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002970 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2971 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002972 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002973 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2974 } else if (ComplexMode) {
2975 if (!OldTy->isComplexType())
2976 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2977 } else {
2978 if (!OldTy->isFloatingType())
2979 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2980 }
2981
Mike Stump87c57ac2009-05-16 07:39:55 +00002982 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2983 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002984 // FIXME: Make sure floating-point mappings are accurate
2985 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002986 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002987 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002988 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002989 }
2990
2991 QualType NewTy;
2992
2993 if (IntegerMode)
2994 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2995 OldTy->isSignedIntegerType());
2996 else
2997 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2998
2999 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003000 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003001 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003002 }
3003
Eli Friedman4735374e2009-03-03 06:41:03 +00003004 if (ComplexMode) {
3005 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003006 }
3007
3008 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003009 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3010 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3011 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003012 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003013
3014 D->addAttr(::new (S.Context)
3015 ModeAttr(Attr.getRange(), S.Context, Name,
3016 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003017}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003018
Chandler Carruthedc2c642011-07-02 00:01:44 +00003019static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003020 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3021 if (!VD->hasGlobalStorage())
3022 S.Diag(Attr.getLoc(),
3023 diag::warn_attribute_requires_functions_or_static_globals)
3024 << Attr.getName();
3025 } else if (!isFunctionOrMethod(D)) {
3026 S.Diag(Attr.getLoc(),
3027 diag::warn_attribute_requires_functions_or_static_globals)
3028 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003029 return;
3030 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003031
Michael Han99315932013-01-24 16:46:58 +00003032 D->addAttr(::new (S.Context)
3033 NoDebugAttr(Attr.getRange(), S.Context,
3034 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003035}
3036
Chandler Carruthedc2c642011-07-02 00:01:44 +00003037static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003038 FunctionDecl *FD = cast<FunctionDecl>(D);
3039 if (!FD->getResultType()->isVoidType()) {
3040 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3041 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3042 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3043 << FD->getType()
3044 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3045 "void");
3046 } else {
3047 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3048 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003049 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003050 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003051 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003052
Aaron Ballman3aff6332013-12-02 19:30:36 +00003053 D->addAttr(::new (S.Context)
3054 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003055 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003056}
3057
Chandler Carruthedc2c642011-07-02 00:01:44 +00003058static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003059 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003060 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003061 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003062 return;
3063 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003064
Michael Han99315932013-01-24 16:46:58 +00003065 D->addAttr(::new (S.Context)
3066 GNUInlineAttr(Attr.getRange(), S.Context,
3067 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003068}
3069
Chandler Carruthedc2c642011-07-02 00:01:44 +00003070static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003071 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003072
Aaron Ballman02df2e02012-12-09 17:45:41 +00003073 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003074 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003075 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3076 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003077 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003078 return;
3079
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003080 if (!isa<ObjCMethodDecl>(D)) {
3081 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3082 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003083 return;
3084 }
3085
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003086 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003087 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003088 D->addAttr(::new (S.Context)
3089 FastCallAttr(Attr.getRange(), S.Context,
3090 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003091 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003092 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003093 D->addAttr(::new (S.Context)
3094 StdCallAttr(Attr.getRange(), S.Context,
3095 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003096 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003097 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003098 D->addAttr(::new (S.Context)
3099 ThisCallAttr(Attr.getRange(), S.Context,
3100 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003101 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003102 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003103 D->addAttr(::new (S.Context)
3104 CDeclAttr(Attr.getRange(), S.Context,
3105 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003106 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003107 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003108 D->addAttr(::new (S.Context)
3109 PascalAttr(Attr.getRange(), S.Context,
3110 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003111 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003112 case AttributeList::AT_MSABI:
3113 D->addAttr(::new (S.Context)
3114 MSABIAttr(Attr.getRange(), S.Context,
3115 Attr.getAttributeSpellingListIndex()));
3116 return;
3117 case AttributeList::AT_SysVABI:
3118 D->addAttr(::new (S.Context)
3119 SysVABIAttr(Attr.getRange(), S.Context,
3120 Attr.getAttributeSpellingListIndex()));
3121 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003122 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003123 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003124 switch (CC) {
3125 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003126 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003127 break;
3128 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003129 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003130 break;
3131 default:
3132 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003133 }
3134
Michael Han99315932013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 PcsAttr(Attr.getRange(), S.Context, PCS,
3137 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003138 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003139 }
Derek Schuffa2020962012-10-16 22:30:41 +00003140 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003141 D->addAttr(::new (S.Context)
3142 PnaclCallAttr(Attr.getRange(), S.Context,
3143 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003144 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003145 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003146 D->addAttr(::new (S.Context)
3147 IntelOclBiccAttr(Attr.getRange(), S.Context,
3148 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003149 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003150
Abramo Bagnara50099372010-04-30 13:10:51 +00003151 default:
3152 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003153 }
3154}
3155
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003156static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3157 const AttributeList &Attr) {
3158 uint32_t ArgNum;
3159 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003160 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003161
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003162 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3163 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003164}
3165
Aaron Ballman02df2e02012-12-09 17:45:41 +00003166bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3167 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003168 if (attr.isInvalid())
3169 return true;
3170
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003171 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003172 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003173 attr.setInvalid();
3174 return true;
3175 }
3176
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003177 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3178 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003179 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003180 case AttributeList::AT_CDecl: CC = CC_C; break;
3181 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3182 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3183 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3184 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003185 case AttributeList::AT_MSABI:
3186 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3187 CC_X86_64Win64;
3188 break;
3189 case AttributeList::AT_SysVABI:
3190 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3191 CC_C;
3192 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003193 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003194 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003195 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003196 attr.setInvalid();
3197 return true;
3198 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003199 if (StrRef == "aapcs") {
3200 CC = CC_AAPCS;
3201 break;
3202 } else if (StrRef == "aapcs-vfp") {
3203 CC = CC_AAPCS_VFP;
3204 break;
3205 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003206
3207 attr.setInvalid();
3208 Diag(attr.getLoc(), diag::err_invalid_pcs);
3209 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003210 }
Derek Schuffa2020962012-10-16 22:30:41 +00003211 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003212 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003213 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003214 }
3215
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003216 const TargetInfo &TI = Context.getTargetInfo();
3217 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3218 if (A == TargetInfo::CCCR_Warning) {
3219 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003220
3221 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3222 if (FD)
3223 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3224 TargetInfo::CCMT_NonMember;
3225 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003226 }
3227
John McCall3882ace2011-01-05 12:14:39 +00003228 return false;
3229}
3230
John McCall3882ace2011-01-05 12:14:39 +00003231/// Checks a regparm attribute, returning true if it is ill-formed and
3232/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003233bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3234 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003235 return true;
3236
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003237 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003238 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003239 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003240 }
Eli Friedman7044b762009-03-27 21:06:47 +00003241
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003242 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003243 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003244 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003245 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003246 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003247 }
3248
Douglas Gregore8bbc122011-09-02 00:18:52 +00003249 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003250 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003251 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003252 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003253 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003254 }
3255
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003256 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003257 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003258 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003259 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003260 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003261 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003262 }
3263
John McCall3882ace2011-01-05 12:14:39 +00003264 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003265}
3266
Aaron Ballman66039932013-12-19 00:41:31 +00003267static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3268 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003269 // check the attribute arguments.
3270 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3271 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003272 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3273 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003274 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003275 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003276
3277 if (!isFunctionOrMethod(D)) {
3278 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3279 << Attr.getName() << ExpectedFunctionOrMethod;
3280 return;
3281 }
3282
Aaron Ballman66039932013-12-19 00:41:31 +00003283 uint32_t MaxThreads, MinBlocks = 0;
3284 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3285 return;
3286 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3287 Attr.getArgAsExpr(1),
3288 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003289 return;
3290
3291 D->addAttr(::new (S.Context)
3292 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3293 MaxThreads, MinBlocks,
3294 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003295}
3296
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003297static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3298 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003299 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003300 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003301 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003302 return;
3303 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003304
3305 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003306 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003307
Aaron Ballman00e99962013-08-31 01:11:41 +00003308 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003309
3310 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3311 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3312 << Attr.getName() << ExpectedFunctionOrMethod;
3313 return;
3314 }
3315
3316 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003317 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3318 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003319 return;
3320
3321 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003322 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3323 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003324 return;
3325
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003326 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003327 if (IsPointer) {
3328 // Ensure that buffer has a pointer type.
3329 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3330 if (!BufferTy->isPointerType()) {
3331 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003332 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003333 }
3334 }
3335
Michael Han99315932013-01-24 16:46:58 +00003336 D->addAttr(::new (S.Context)
3337 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3338 ArgumentIdx, TypeTagIdx, IsPointer,
3339 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003340}
3341
3342static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3343 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003344 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003346 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003347 return;
3348 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003349
3350 if (!checkAttributeNumArgs(S, Attr, 1))
3351 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003352
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003353 if (!isa<VarDecl>(D)) {
3354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3355 << Attr.getName() << ExpectedVariable;
3356 return;
3357 }
3358
Aaron Ballman00e99962013-08-31 01:11:41 +00003359 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003360 TypeSourceInfo *MatchingCTypeLoc = 0;
3361 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3362 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003363
Michael Han99315932013-01-24 16:46:58 +00003364 D->addAttr(::new (S.Context)
3365 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003366 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003367 Attr.getLayoutCompatible(),
3368 Attr.getMustBeNull(),
3369 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003370}
3371
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003372//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003373// Checker-specific attribute handlers.
3374//===----------------------------------------------------------------------===//
3375
John McCalled433932011-01-25 03:31:58 +00003376static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003377 return type->isDependentType() ||
3378 type->isObjCObjectPointerType() ||
3379 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003380}
3381static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003382 return type->isDependentType() ||
3383 type->isPointerType() ||
3384 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003385}
3386
Chandler Carruthedc2c642011-07-02 00:01:44 +00003387static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003388 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003389 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003390
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003391 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003392 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3393 cf = false;
3394 } else {
3395 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3396 cf = true;
3397 }
3398
3399 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003400 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003401 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003402 return;
3403 }
3404
3405 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003406 param->addAttr(::new (S.Context)
3407 CFConsumedAttr(Attr.getRange(), S.Context,
3408 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003409 else
Michael Han99315932013-01-24 16:46:58 +00003410 param->addAttr(::new (S.Context)
3411 NSConsumedAttr(Attr.getRange(), S.Context,
3412 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003413}
3414
Chandler Carruthedc2c642011-07-02 00:01:44 +00003415static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3416 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003417
John McCalled433932011-01-25 03:31:58 +00003418 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003419
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003420 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003421 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003422 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003423 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003424 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003425 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3426 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003427 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003428 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003429 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003430 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003431 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003432 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003433 return;
3434 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003435
John McCalled433932011-01-25 03:31:58 +00003436 bool typeOK;
3437 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003438 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003439 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003440 case AttributeList::AT_NSReturnsAutoreleased:
3441 case AttributeList::AT_NSReturnsRetained:
3442 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003443 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3444 cf = false;
3445 break;
3446
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003447 case AttributeList::AT_CFReturnsRetained:
3448 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003449 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3450 cf = true;
3451 break;
3452 }
3453
3454 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003455 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003456 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003457 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003458 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003459
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003460 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003461 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003462 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003463 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003464 D->addAttr(::new (S.Context)
3465 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3466 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003467 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003468 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003469 D->addAttr(::new (S.Context)
3470 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3471 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003472 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003473 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003474 D->addAttr(::new (S.Context)
3475 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3476 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003477 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003478 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003479 D->addAttr(::new (S.Context)
3480 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3481 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003482 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003483 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003484 D->addAttr(::new (S.Context)
3485 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3486 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003487 return;
3488 };
3489}
3490
John McCallcf166702011-07-22 08:53:00 +00003491static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3492 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003493 const int EP_ObjCMethod = 1;
3494 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003495
John McCallcf166702011-07-22 08:53:00 +00003496 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003497 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003498 if (isa<ObjCMethodDecl>(D))
3499 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003500 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003501 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003502
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003503 if (!resultType->isReferenceType() &&
3504 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003505 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003506 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003507 << attr.getName()
3508 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003509 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003510
3511 // Drop the attribute.
3512 return;
3513 }
3514
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003515 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003516 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3517 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003518}
3519
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003520static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3521 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003522 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003523
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003524 DeclContext *DC = method->getDeclContext();
3525 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3526 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3527 << attr.getName() << 0;
3528 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3529 return;
3530 }
3531 if (method->getMethodFamily() == OMF_dealloc) {
3532 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3533 << attr.getName() << 1;
3534 return;
3535 }
3536
Michael Han99315932013-01-24 16:46:58 +00003537 method->addAttr(::new (S.Context)
3538 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3539 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003540}
3541
Aaron Ballmanfb763042013-12-02 18:05:46 +00003542static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3543 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003544 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003545 return;
John McCall32f5fe12011-09-30 05:12:12 +00003546
Aaron Ballmanfb763042013-12-02 18:05:46 +00003547 D->addAttr(::new (S.Context)
3548 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3549 Attr.getAttributeSpellingListIndex()));
3550}
3551
3552static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3553 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003554 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003555 return;
3556
3557 D->addAttr(::new (S.Context)
3558 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3559 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003560}
3561
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003562static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3563 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003564 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003565
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003566 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003567 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003568 return;
3569 }
3570
3571 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003572 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003573 Attr.getAttributeSpellingListIndex()));
3574}
3575
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003576static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3577 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003578 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003579
3580 if (!Parm) {
3581 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3582 return;
3583 }
3584
3585 D->addAttr(::new (S.Context)
3586 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3587 Attr.getAttributeSpellingListIndex()));
3588}
3589
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003590static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3591 const AttributeList &Attr) {
3592 IdentifierInfo *RelatedClass =
3593 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3594 if (!RelatedClass) {
3595 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3596 return;
3597 }
3598 IdentifierInfo *ClassMethod =
3599 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3600 IdentifierInfo *InstanceMethod =
3601 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3602 D->addAttr(::new (S.Context)
3603 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3604 ClassMethod, InstanceMethod,
3605 Attr.getAttributeSpellingListIndex()));
3606}
3607
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003608static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3609 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003610 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003611 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003612 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003613 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3614 Attr.getAttributeSpellingListIndex()));
3615}
3616
Chandler Carruthedc2c642011-07-02 00:01:44 +00003617static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3618 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003619 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003620
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003621 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003622 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003623}
3624
Chandler Carruthedc2c642011-07-02 00:01:44 +00003625static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3626 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003627 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003628 QualType type = vd->getType();
3629
3630 if (!type->isDependentType() &&
3631 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003632 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003633 << type;
3634 return;
3635 }
3636
3637 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3638
3639 // If we have no lifetime yet, check the lifetime we're presumably
3640 // going to infer.
3641 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3642 lifetime = type->getObjCARCImplicitLifetime();
3643
3644 switch (lifetime) {
3645 case Qualifiers::OCL_None:
3646 assert(type->isDependentType() &&
3647 "didn't infer lifetime for non-dependent type?");
3648 break;
3649
3650 case Qualifiers::OCL_Weak: // meaningful
3651 case Qualifiers::OCL_Strong: // meaningful
3652 break;
3653
3654 case Qualifiers::OCL_ExplicitNone:
3655 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003656 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003657 << (lifetime == Qualifiers::OCL_Autoreleasing);
3658 break;
3659 }
3660
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003661 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003662 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3663 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003664}
3665
Francois Picheta83957a2010-12-19 06:50:37 +00003666//===----------------------------------------------------------------------===//
3667// Microsoft specific attribute handlers.
3668//===----------------------------------------------------------------------===//
3669
Chandler Carruthedc2c642011-07-02 00:01:44 +00003670static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003671 if (!S.LangOpts.CPlusPlus) {
3672 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3673 << Attr.getName() << AttributeLangSupport::C;
3674 return;
3675 }
3676
Aaron Ballman60e705e2013-11-24 20:58:02 +00003677 if (!isa<CXXRecordDecl>(D)) {
3678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3679 << Attr.getName() << ExpectedClass;
3680 return;
3681 }
3682
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003683 StringRef StrRef;
3684 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003685 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003686 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003687
David Majnemer89085342013-08-09 08:56:20 +00003688 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3689 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003690 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3691 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003692
Reid Kleckner140c4a72013-05-17 14:04:52 +00003693 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003694 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003695 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003696 return;
3697 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003698
David Majnemer89085342013-08-09 08:56:20 +00003699 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003700 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003701 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003702 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003703 return;
3704 }
David Majnemer89085342013-08-09 08:56:20 +00003705 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003706 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003707 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003708 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003709 }
Francois Picheta83957a2010-12-19 06:50:37 +00003710
David Majnemer89085342013-08-09 08:56:20 +00003711 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3712 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003713}
3714
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003715/// Handles semantic checking for features that are common to all attributes,
3716/// such as checking whether a parameter was properly specified, or the correct
3717/// number of arguments were passed, etc.
3718static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3719 const AttributeList &Attr) {
3720 // Several attributes carry different semantics than the parsing requires, so
3721 // those are opted out of the common handling.
3722 //
3723 // We also bail on unknown and ignored attributes because those are handled
3724 // as part of the target-specific handling logic.
3725 if (Attr.hasCustomParsing() ||
3726 Attr.getKind() == AttributeList::UnknownAttribute ||
3727 Attr.getKind() == AttributeList::IgnoredAttribute)
3728 return false;
3729
Aaron Ballman3aff6332013-12-02 19:30:36 +00003730 // Check whether the attribute requires specific language extensions to be
3731 // enabled.
3732 if (!Attr.diagnoseLangOpts(S))
3733 return true;
3734
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003735 // If there are no optional arguments, then checking for the argument count
3736 // is trivial.
3737 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3738 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3739 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003740
3741 // Check whether the attribute appertains to the given subject.
3742 if (!Attr.diagnoseAppertainsTo(S, D))
3743 return true;
3744
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003745 return false;
3746}
3747
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003748//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003749// Top Level Sema Entry Points
3750//===----------------------------------------------------------------------===//
3751
Richard Smithf8a75c32013-08-29 00:47:48 +00003752/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3753/// the attribute applies to decls. If the attribute is a type attribute, just
3754/// silently ignore it if a GNU attribute.
3755static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3756 const AttributeList &Attr,
3757 bool IncludeCXX11Attributes) {
3758 if (Attr.isInvalid())
3759 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003760
Richard Smithf8a75c32013-08-29 00:47:48 +00003761 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3762 // instead.
3763 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3764 return;
3765
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003766 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3767 return;
3768
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003769 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003770 case AttributeList::AT_IBAction:
3771 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003772 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3773 case AttributeList::AT_IBOutletCollection:
3774 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003775 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003776 case AttributeList::AT_ObjCGC:
3777 case AttributeList::AT_VectorSize:
3778 case AttributeList::AT_NeonVectorType:
3779 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003780 case AttributeList::AT_Ptr32:
3781 case AttributeList::AT_Ptr64:
3782 case AttributeList::AT_SPtr:
3783 case AttributeList::AT_UPtr:
Aaron Ballmanb8c0adea2014-01-08 13:23:01 +00003784 case AttributeList::AT_Regparm:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003785 // Ignore these, these are type attributes, handled by
3786 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003787 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003788 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3789 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003790 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003791 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003792 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003793 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003794 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003795 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3796 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3797 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003798 handleDependencyAttr(S, scope, D, Attr);
3799 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003800 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003801 case AttributeList::AT_CUDAConstant:
3802 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003803 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003804 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003805 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003806 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003807 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003808 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003809 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3810 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003811 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003812 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003813 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003814 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003815 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003816 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3817 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3818 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003819 case AttributeList::AT_CUDADevice:
3820 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003821 case AttributeList::AT_CUDAHost:
3822 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003823 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3824 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003825 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003826 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003827 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003828 case AttributeList::AT_MayAlias:
3829 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003830 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003831 case AttributeList::AT_NoCommon:
3832 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003833 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003834 case AttributeList::AT_Overloadable:
3835 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003836 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003837 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3838 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003839 case AttributeList::AT_Naked:
3840 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003841 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003842 case AttributeList::AT_NoThrow:
3843 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003844 case AttributeList::AT_CUDAShared:
3845 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003846 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003847
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003848 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003849 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003850 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003851 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003852
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003853 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003854 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3855
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003856 case AttributeList::AT_ObjCRequiresSuper:
3857 handleObjCRequiresSuperAttr(S, D, Attr); break;
3858
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003859 case AttributeList::AT_ObjCBridge:
3860 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003861
3862 case AttributeList::AT_ObjCBridgeMutable:
3863 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003864
3865 case AttributeList::AT_ObjCBridgeRelated:
3866 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003867
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003868 case AttributeList::AT_ObjCDesignatedInitializer:
3869 handleObjCDesignatedInitializer(S, D, Attr); break;
3870
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003871 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003872 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003873 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003874 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003875
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003876 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003877 case AttributeList::AT_CFConsumed:
3878 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3879 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003880 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003881
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003882 case AttributeList::AT_NSReturnsAutoreleased:
3883 case AttributeList::AT_NSReturnsNotRetained:
3884 case AttributeList::AT_CFReturnsNotRetained:
3885 case AttributeList::AT_NSReturnsRetained:
3886 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003887 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00003888 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003889 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003890 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003891 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00003892 case AttributeList::AT_VecTypeHint:
3893 handleVecTypeHint(S, D, Attr); break;
3894
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003895 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003896 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003897
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003898 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
3899 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
3900 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003901 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003902 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003903 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003904 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003905 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003906 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00003907 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00003908 handleObjCSuppresProtocolAttr(S, D, Attr);
3909 break;
3910 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003911 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003912 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
3913 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003914 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003915 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00003916 case AttributeList::AT_Visibility:
3917 handleVisibilityAttr(S, D, Attr, false);
3918 break;
3919 case AttributeList::AT_TypeVisibility:
3920 handleVisibilityAttr(S, D, Attr, true);
3921 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00003922 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00003923 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003924 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003925 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00003926 case AttributeList::AT_Weak:
3927 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003928 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
3929 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
3930 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003931 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003932 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003933 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003934 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003935 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003936 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003937 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003938 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
3939 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
3940 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003941 case AttributeList::AT_Const:
3942 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003943 case AttributeList::AT_Pure:
3944 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003945 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
3946 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003947 case AttributeList::AT_NoInline:
3948 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003949 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003950 // Just ignore
3951 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003952 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003953 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003954 case AttributeList::AT_StdCall:
3955 case AttributeList::AT_CDecl:
3956 case AttributeList::AT_FastCall:
3957 case AttributeList::AT_ThisCall:
3958 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00003959 case AttributeList::AT_MSABI:
3960 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003961 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00003962 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00003963 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003964 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003965 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003966 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003967 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003968 case AttributeList::AT_OpenCLImageAccess:
3969 handleOpenCLImageAccessAttr(S, D, Attr);
3970 break;
John McCall8d32c052012-05-22 21:28:12 +00003971
3972 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003973 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003974 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00003975 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003976 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003977 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003978 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00003979 case AttributeList::AT_MSInheritance:
3980 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003981 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00003982 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00003983 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00003984 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003985
3986 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00003987 case AttributeList::AT_AssertExclusiveLock:
3988 handleAssertExclusiveLockAttr(S, D, Attr);
3989 break;
3990 case AttributeList::AT_AssertSharedLock:
3991 handleAssertSharedLockAttr(S, D, Attr);
3992 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003993 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00003994 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003995 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00003996 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003997 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003998 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00003999 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004000 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004001 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004002 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004003 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004004 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004005 break;
4006 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004007 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004008 break;
4009 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004010 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004011 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004012 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004013 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004014 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004015 handleGuardedByAttr(S, D, Attr);
4016 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004017 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004018 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004019 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004021 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004022 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004023 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004024 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004025 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004026 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004027 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004028 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004030 handleLockReturnedAttr(S, D, Attr);
4031 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004032 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004033 handleLocksExcludedAttr(S, D, Attr);
4034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004035 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004036 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004037 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004038 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004039 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004040 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004041 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004042 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004043 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004044 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004045 handleUnlockFunAttr(S, D, Attr);
4046 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004047 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004048 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004049 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004051 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004052 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004053
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004054 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004055 case AttributeList::AT_Consumable:
4056 handleConsumableAttr(S, D, Attr);
4057 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004058 case AttributeList::AT_CallableWhen:
4059 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004060 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004061 case AttributeList::AT_ParamTypestate:
4062 handleParamTypestateAttr(S, D, Attr);
4063 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004064 case AttributeList::AT_ReturnTypestate:
4065 handleReturnTypestateAttr(S, D, Attr);
4066 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004067 case AttributeList::AT_SetTypestate:
4068 handleSetTypestateAttr(S, D, Attr);
4069 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004070 case AttributeList::AT_TestTypestate:
4071 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004072 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004073
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004074 // Type safety attributes.
4075 case AttributeList::AT_ArgumentWithTypeTag:
4076 handleArgumentWithTypeTagAttr(S, D, Attr);
4077 break;
4078 case AttributeList::AT_TypeTagForDatatype:
4079 handleTypeTagForDatatypeAttr(S, D, Attr);
4080 break;
4081
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004082 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004083 // Ask target about the attribute.
4084 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4085 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004086 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4087 diag::warn_unhandled_ms_attribute_ignored :
4088 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004089 break;
4090 }
4091}
4092
4093/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4094/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004095void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004096 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004097 bool IncludeCXX11Attributes) {
4098 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004099 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004100
Joey Gouly2cd9db12013-12-13 16:15:28 +00004101 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004102 // GCC accepts
4103 // static int a9 __attribute__((weakref));
4104 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004105 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004106 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4107 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004108 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004109 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004110 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004111
4112 if (!D->hasAttr<OpenCLKernelAttr>()) {
4113 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004114 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4115 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004116 D->setInvalidDecl();
4117 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004118 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4119 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004120 D->setInvalidDecl();
4121 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004122 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4123 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004124 D->setInvalidDecl();
4125 }
4126 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004127}
4128
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004129// Annotation attributes are the only attributes allowed after an access
4130// specifier.
4131bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4132 const AttributeList *AttrList) {
4133 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004135 handleAnnotateAttr(*this, ASDecl, *l);
4136 } else {
4137 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4138 return true;
4139 }
4140 }
4141
4142 return false;
4143}
4144
John McCall42856de2011-10-01 05:17:03 +00004145/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4146/// contains any decl attributes that we should warn about.
4147static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4148 for ( ; A; A = A->getNext()) {
4149 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004150 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004151 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4152
4153 if (A->getKind() == AttributeList::UnknownAttribute) {
4154 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4155 << A->getName() << A->getRange();
4156 } else {
4157 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4158 << A->getName() << A->getRange();
4159 }
4160 }
4161}
4162
4163/// checkUnusedDeclAttributes - Given a declarator which is not being
4164/// used to build a declaration, complain about any decl attributes
4165/// which might be lying around on it.
4166void Sema::checkUnusedDeclAttributes(Declarator &D) {
4167 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4168 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4169 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4170 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4171}
4172
Ryan Flynn7d470f32009-07-30 03:15:39 +00004173/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004174/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004175NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4176 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004177 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004178 NamedDecl *NewD = 0;
4179 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004180 FunctionDecl *NewFD;
4181 // FIXME: Missing call to CheckFunctionDeclaration().
4182 // FIXME: Mangling?
4183 // FIXME: Is the qualifier info correct?
4184 // FIXME: Is the DeclContext correct?
4185 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4186 Loc, Loc, DeclarationName(II),
4187 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004188 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004189 FD->hasPrototype(),
4190 false/*isConstexprSpecified*/);
4191 NewD = NewFD;
4192
4193 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004194 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004195
4196 // Fake up parameter variables; they are declared as if this were
4197 // a typedef.
4198 QualType FDTy = FD->getType();
4199 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4200 SmallVector<ParmVarDecl*, 16> Params;
4201 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4202 AE = FT->arg_type_end(); AI != AE; ++AI) {
4203 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4204 Param->setScopeInfo(0, Params.size());
4205 Params.push_back(Param);
4206 }
David Blaikie9c70e042011-09-21 18:16:56 +00004207 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004208 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004209 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4210 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004211 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004212 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004213 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004214 if (VD->getQualifier()) {
4215 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004216 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004217 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004218 }
4219 return NewD;
4220}
4221
James Dennett634962f2012-06-14 21:40:34 +00004222/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004223/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004224void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004225 if (W.getUsed()) return; // only do this once
4226 W.setUsed(true);
4227 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4228 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004229 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004230 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4231 NDId->getName()));
4232 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004233 WeakTopLevelDecl.push_back(NewD);
4234 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4235 // to insert Decl at TU scope, sorry.
4236 DeclContext *SavedContext = CurContext;
4237 CurContext = Context.getTranslationUnitDecl();
4238 PushOnScopeChains(NewD, S);
4239 CurContext = SavedContext;
4240 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004241 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004242 }
4243}
4244
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004245void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4246 // It's valid to "forward-declare" #pragma weak, in which case we
4247 // have to do this.
4248 LoadExternalWeakUndeclaredIdentifiers();
4249 if (!WeakUndeclaredIdentifiers.empty()) {
4250 NamedDecl *ND = NULL;
4251 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4252 if (VD->isExternC())
4253 ND = VD;
4254 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4255 if (FD->isExternC())
4256 ND = FD;
4257 if (ND) {
4258 if (IdentifierInfo *Id = ND->getIdentifier()) {
4259 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4260 = WeakUndeclaredIdentifiers.find(Id);
4261 if (I != WeakUndeclaredIdentifiers.end()) {
4262 WeakInfo W = I->second;
4263 DeclApplyPragmaWeak(S, ND, W);
4264 WeakUndeclaredIdentifiers[Id] = W;
4265 }
4266 }
4267 }
4268 }
4269}
4270
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004271/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4272/// it, apply them to D. This is a bit tricky because PD can have attributes
4273/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004274void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004275 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004276 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004277 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004278
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004279 // Walk the declarator structure, applying decl attributes that were in a type
4280 // position to the decl itself. This handles cases like:
4281 // int *__attr__(x)** D;
4282 // when X is a decl attribute.
4283 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4284 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004285 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004286
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004287 // Finally, apply any attributes on the decl itself.
4288 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004289 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004290}
John McCall28a6aea2009-11-04 02:18:39 +00004291
John McCall31168b02011-06-15 23:02:42 +00004292/// Is the given declaration allowed to use a forbidden type?
4293static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4294 // Private ivars are always okay. Unfortunately, people don't
4295 // always properly make their ivars private, even in system headers.
4296 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004297 // Function declarations in sys headers will be marked unavailable.
4298 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4299 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004300 return false;
4301
4302 // Require it to be declared in a system header.
4303 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4304}
4305
4306/// Handle a delayed forbidden-type diagnostic.
4307static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4308 Decl *decl) {
4309 if (decl && isForbiddenTypeAllowed(S, decl)) {
4310 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4311 "this system declaration uses an unsupported type"));
4312 return;
4313 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004314 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004315 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004316 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004317 // kind of forbidden type messages on unavailable functions.
4318 if (FD->hasAttr<UnavailableAttr>() &&
4319 diag.getForbiddenTypeDiagnostic() ==
4320 diag::err_arc_array_param_no_ownership) {
4321 diag.Triggered = true;
4322 return;
4323 }
4324 }
John McCall31168b02011-06-15 23:02:42 +00004325
4326 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4327 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4328 diag.Triggered = true;
4329}
4330
John McCall2ec85372012-05-07 06:16:41 +00004331void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4332 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004333 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004334 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004335
John McCall2ec85372012-05-07 06:16:41 +00004336 // When delaying diagnostics to run in the context of a parsed
4337 // declaration, we only want to actually emit anything if parsing
4338 // succeeds.
4339 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004340
John McCall2ec85372012-05-07 06:16:41 +00004341 // We emit all the active diagnostics in this pool or any of its
4342 // parents. In general, we'll get one pool for the decl spec
4343 // and a child pool for each declarator; in a decl group like:
4344 // deprecated_typedef foo, *bar, baz();
4345 // only the declarator pops will be passed decls. This is correct;
4346 // we really do need to consider delayed diagnostics from the decl spec
4347 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004348 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004349 do {
John McCall6347b682012-05-07 06:16:58 +00004350 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004351 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4352 // This const_cast is a bit lame. Really, Triggered should be mutable.
4353 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004354 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004355 continue;
4356
John McCallc1465822011-02-14 07:13:47 +00004357 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004358 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004359 case DelayedDiagnostic::Unavailable:
4360 // Don't bother giving deprecation/unavailable diagnostics if
4361 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004362 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004363 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004364 break;
4365
4366 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004367 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004368 break;
John McCall31168b02011-06-15 23:02:42 +00004369
4370 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004371 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004372 break;
John McCall86121512010-01-27 03:50:35 +00004373 }
4374 }
John McCall2ec85372012-05-07 06:16:41 +00004375 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004376}
4377
John McCall6347b682012-05-07 06:16:58 +00004378/// Given a set of delayed diagnostics, re-emit them as if they had
4379/// been delayed in the current context instead of in the given pool.
4380/// Essentially, this just moves them to the current pool.
4381void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4382 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4383 assert(curPool && "re-emitting in undelayed context not supported");
4384 curPool->steal(pool);
4385}
4386
John McCall28a6aea2009-11-04 02:18:39 +00004387static bool isDeclDeprecated(Decl *D) {
4388 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004389 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004390 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004391 // A category implicitly has the availability of the interface.
4392 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4393 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004394 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4395 return false;
4396}
4397
Ted Kremenekb79ee572013-12-18 23:30:06 +00004398static bool isDeclUnavailable(Decl *D) {
4399 do {
4400 if (D->isUnavailable())
4401 return true;
4402 // A category implicitly has the availability of the interface.
4403 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4404 return CatD->getClassInterface()->isUnavailable();
4405 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4406 return false;
4407}
4408
Eli Friedman971bfa12012-08-08 21:52:41 +00004409static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004410DoEmitAvailabilityWarning(Sema &S,
4411 DelayedDiagnostic::DDKind K,
4412 Decl *Ctx,
4413 const NamedDecl *D,
4414 StringRef Message,
4415 SourceLocation Loc,
4416 const ObjCInterfaceDecl *UnknownObjCClass,
4417 const ObjCPropertyDecl *ObjCProperty) {
4418
4419 // Diagnostics for deprecated or unavailable.
4420 unsigned diag, diag_message, diag_fwdclass_message;
4421
4422 // Matches 'diag::note_property_attribute' options.
4423 unsigned property_note_select;
4424
4425 // Matches diag::note_availability_specified_here.
4426 unsigned available_here_select_kind;
4427
4428 // Don't warn if our current context is deprecated or unavailable.
4429 switch (K) {
4430 case DelayedDiagnostic::Deprecation:
4431 if (isDeclDeprecated(Ctx))
4432 return;
4433 diag = diag::warn_deprecated;
4434 diag_message = diag::warn_deprecated_message;
4435 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4436 property_note_select = /* deprecated */ 0;
4437 available_here_select_kind = /* deprecated */ 2;
4438 break;
4439
4440 case DelayedDiagnostic::Unavailable:
4441 if (isDeclUnavailable(Ctx))
4442 return;
4443 diag = diag::err_unavailable;
4444 diag_message = diag::err_unavailable_message;
4445 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4446 property_note_select = /* unavailable */ 1;
4447 available_here_select_kind = /* unavailable */ 0;
4448 break;
4449
4450 default:
4451 llvm_unreachable("Neither a deprecation or unavailable kind");
4452 }
4453
Eli Friedman971bfa12012-08-08 21:52:41 +00004454 DeclarationName Name = D->getDeclName();
4455 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004456 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004457 if (ObjCProperty)
4458 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4459 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004460 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004461 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004462 if (ObjCProperty)
4463 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4464 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004465 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004466 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004467 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4468 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004469
4470 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4471 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004472}
4473
Ted Kremenekb79ee572013-12-18 23:30:06 +00004474void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4475 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004476 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004477 DoEmitAvailabilityWarning(*this,
4478 (DelayedDiagnostic::DDKind) DD.Kind,
4479 Ctx,
4480 DD.getDeprecationDecl(),
4481 DD.getDeprecationMessage(),
4482 DD.Loc,
4483 DD.getUnknownObjCClass(),
4484 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004485}
4486
Ted Kremenekb79ee572013-12-18 23:30:06 +00004487void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4488 NamedDecl *D, StringRef Message,
4489 SourceLocation Loc,
4490 const ObjCInterfaceDecl *UnknownObjCClass,
4491 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004492 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004493 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004494 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4495 UnknownObjCClass,
4496 ObjCProperty,
4497 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004498 return;
4499 }
4500
Ted Kremenekb79ee572013-12-18 23:30:06 +00004501 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4502 DelayedDiagnostic::DDKind K;
4503 switch (AD) {
4504 case AD_Deprecation:
4505 K = DelayedDiagnostic::Deprecation;
4506 break;
4507 case AD_Unavailable:
4508 K = DelayedDiagnostic::Unavailable;
4509 break;
4510 }
4511
4512 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4513 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004514}