blob: aaaa2b3552cbac9125814e8cac474ec6816ee801 [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) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
215 return false;
216 }
217
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000218 return true;
219}
220
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000221/// \brief If Expr is a valid integer constant, get the value of the integer
222/// expression and return success or failure. May output an error.
223static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
224 const Expr *Expr, uint32_t &Val,
225 unsigned Idx = UINT_MAX) {
226 llvm::APSInt I(32);
227 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
228 !Expr->isIntegerConstantExpr(I, S.Context)) {
229 if (Idx != UINT_MAX)
230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
231 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
232 << Expr->getSourceRange();
233 else
234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
235 << Attr.getName() << AANT_ArgumentIntegerConstant
236 << Expr->getSourceRange();
237 return false;
238 }
239 Val = (uint32_t)I.getZExtValue();
240 return true;
241}
242
Aaron Ballmanfb763042013-12-02 18:05:46 +0000243/// \brief Diagnose mutually exclusive attributes when present on a given
244/// declaration. Returns true if diagnosed.
245template <typename AttrTy>
246static bool checkAttrMutualExclusion(Sema &S, Decl *D,
247 const AttributeList &Attr,
248 const char *OtherName) {
249 // FIXME: it would be nice if OtherName did not have to be passed in, but was
250 // instead determined based on the AttrTy template parameter.
251 if (D->hasAttr<AttrTy>()) {
252 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
253 << Attr.getName() << OtherName;
254 return true;
255 }
256 return false;
257}
258
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000259/// \brief Check if IdxExpr is a valid argument index for a function or
260/// instance method D. May output an error.
261///
262/// \returns true if IdxExpr is a valid index.
263static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000264 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000265 unsigned AttrArgNum,
266 const Expr *IdxExpr,
267 uint64_t &Idx)
268{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000269 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000270
271 // In C++ the implicit 'this' function parameter also counts.
272 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000273 bool HP = hasFunctionProto(D);
274 bool HasImplicitThisParam = isInstanceMethod(D);
275 bool IV = HP && isFunctionOrMethodVariadic(D);
276 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
277 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000278
279 llvm::APSInt IdxInt;
280 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
281 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000282 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
283 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
284 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000285 return false;
286 }
287
288 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000289 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000290 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
291 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000292 return false;
293 }
294 Idx--; // Convert to zero-based.
295 if (HasImplicitThisParam) {
296 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000297 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000298 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000299 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000300 return false;
301 }
302 --Idx;
303 }
304
305 return true;
306}
307
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000308/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
309/// If not emit an error and return false. If the argument is an identifier it
310/// will emit an error with a fixit hint and treat it as if it was a string
311/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000312bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
313 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000314 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000315 // Look for identifiers. If we have one emit a hint to fix it to a literal.
316 if (Attr.isArgIdent(ArgNum)) {
317 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000318 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000319 << Attr.getName() << AANT_ArgumentString
320 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000321 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000322 Str = Loc->Ident->getName();
323 if (ArgLocation)
324 *ArgLocation = Loc->Loc;
325 return true;
326 }
327
328 // Now check for an actual string literal.
329 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
330 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
331 if (ArgLocation)
332 *ArgLocation = ArgExpr->getLocStart();
333
334 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000335 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000336 << Attr.getName() << AANT_ArgumentString;
337 return false;
338 }
339
340 Str = Literal->getString();
341 return true;
342}
343
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000344/// \brief Applies the given attribute to the Decl without performing any
345/// additional semantic checking.
346template <typename AttrType>
347static void handleSimpleAttribute(Sema &S, Decl *D,
348 const AttributeList &Attr) {
349 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
350 Attr.getAttributeSpellingListIndex()));
351}
352
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000353/// \brief Check if the passed-in expression is of type int or bool.
354static bool isIntOrBool(Expr *Exp) {
355 QualType QT = Exp->getType();
356 return QT->isBooleanType() || QT->isIntegerType();
357}
358
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000359
360// Check to see if the type is a smart pointer of some kind. We assume
361// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
363 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
364 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000365 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000366 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000367
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000368 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
369 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000370 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000371 return false;
372
373 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000374}
375
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000376/// \brief Check if passed in Decl is a pointer type.
377/// Note that this function may produce an error message.
378/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000379static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
380 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000381 const ValueDecl *vd = cast<ValueDecl>(D);
382 QualType QT = vd->getType();
383 if (QT->isAnyPointerType())
384 return true;
385
386 if (const RecordType *RT = QT->getAs<RecordType>()) {
387 // If it's an incomplete type, it could be a smart pointer; skip it.
388 // (We don't want to force template instantiation if we can avoid it,
389 // since that would alter the order in which templates are instantiated.)
390 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000391 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000392
Aaron Ballman553e6812013-12-26 14:54:11 +0000393 if (threadSafetyCheckIsSmartPointer(S, RT))
394 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000395 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000396
397 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000398 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000399 return false;
400}
401
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000402/// \brief Checks that the passed in QualType either is of RecordType or points
403/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000404static const RecordType *getRecordType(QualType QT) {
405 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000406 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000407
408 // Now check if we point to record type.
409 if (const PointerType *PT = QT->getAs<PointerType>())
410 return PT->getPointeeType()->getAs<RecordType>();
411
412 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000413}
414
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000415
Jordy Rose740b0c22012-05-08 03:27:22 +0000416static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
417 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000418 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000419 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000420}
421
422
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000423/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000424/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000425static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
426 QualType Ty) {
427 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000428
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000429 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000430 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000431 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000432 << Attr.getName() << Ty.getAsString();
433 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000434 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435
Michael Hana9171bc2012-08-03 17:40:43 +0000436 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000437 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000438 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000439
440 // Allow smart pointers to be used as lockable objects.
441 // FIXME -- Check the type that the smart pointer points to.
442 if (threadSafetyCheckIsSmartPointer(S, RT))
443 return;
444
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000445 // Check if the type is lockable.
446 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000447 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000448 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000449
450 // Else check if any base classes are lockable.
451 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
452 CXXBasePaths BPaths(false, false);
453 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
454 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000455 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000456
457 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
458 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459}
460
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000461/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000462/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463/// \param Sidx The attribute argument index to start checking with.
464/// \param ParamIdxOk Whether an argument can be indexing into a function
465/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000466static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 const AttributeList &Attr,
468 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469 int Sidx = 0,
470 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000471 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000472 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000473
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000474 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000475 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000476 Args.push_back(ArgExp);
477 continue;
478 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000479
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000480 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000481 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000482 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000483 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000484 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000485 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000486 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000487 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000488
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000489 // We allow constant strings to be used as a placeholder for expressions
490 // that are not valid C++ syntax, but warn that they are ignored.
491 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
492 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000493 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000494 continue;
495 }
496
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000497 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000498
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000499 // A pointer to member expression of the form &MyClass::mu is treated
500 // specially -- we need to look at the type of the member.
501 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
502 if (UOp->getOpcode() == UO_AddrOf)
503 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
504 if (DRE->getDecl()->isCXXInstanceMember())
505 ArgTy = DRE->getDecl()->getType();
506
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000507 // First see if we can just cast to record type, or point to record type.
508 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000510 // Now check if we index into a record type function param.
511 if(!RT && ParamIdxOk) {
512 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000513 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
514 if(FD && IL) {
515 unsigned int NumParams = FD->getNumParams();
516 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000517 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
518 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
519 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
521 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000522 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000523 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000524 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525 }
526 }
527
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000528 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000530 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000532}
533
Chris Lattner58418ff2008-06-29 00:16:31 +0000534//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000535// Attribute Implementations
536//===----------------------------------------------------------------------===//
537
Daniel Dunbar032db472008-07-31 22:40:48 +0000538// FIXME: All this manual attribute parsing code is gross. At the
539// least add some helper functions to check most argument patterns (#
540// and types of args).
541
Michael Hana9171bc2012-08-03 17:40:43 +0000542static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000543 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000544 if (!threadSafetyCheckIsPointer(S, D, Attr))
545 return;
546
Michael Han99315932013-01-24 16:46:58 +0000547 D->addAttr(::new (S.Context)
548 PtGuardedVarAttr(Attr.getRange(), S.Context,
549 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000550}
551
Michael Hana9171bc2012-08-03 17:40:43 +0000552static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
553 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000554 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000555 SmallVector<Expr*, 1> Args;
556 // check that all arguments are lockable objects
557 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
558 unsigned Size = Args.size();
559 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000560 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000561
Michael Han3be3b442012-07-23 18:48:41 +0000562 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000563
Michael Han3be3b442012-07-23 18:48:41 +0000564 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000565}
566
Michael Han3be3b442012-07-23 18:48:41 +0000567static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
568 Expr *Arg = 0;
569 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
570 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000571
Michael Han3be3b442012-07-23 18:48:41 +0000572 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
573}
574
Michael Hana9171bc2012-08-03 17:40:43 +0000575static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000576 const AttributeList &Attr) {
577 Expr *Arg = 0;
578 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
579 return;
580
581 if (!threadSafetyCheckIsPointer(S, D, Attr))
582 return;
583
584 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
585 S.Context, Arg));
586}
587
Michael Hana9171bc2012-08-03 17:40:43 +0000588static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
589 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000590 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000592 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000593
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000594 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000595 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596 if (!QT->isDependentType()) {
597 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000598 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000599 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000600 << Attr.getName();
601 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000602 }
603 }
604
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000605 // Check that all arguments are lockable objects.
606 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000607 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000608 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000609
Michael Han3be3b442012-07-23 18:48:41 +0000610 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000611}
612
Michael Hana9171bc2012-08-03 17:40:43 +0000613static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000614 const AttributeList &Attr) {
615 SmallVector<Expr*, 1> Args;
616 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
617 return;
618
619 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000620 D->addAttr(::new (S.Context)
621 AcquiredAfterAttr(Attr.getRange(), S.Context,
622 StartArg, Args.size(),
623 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000624}
625
Michael Hana9171bc2012-08-03 17:40:43 +0000626static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000627 const AttributeList &Attr) {
628 SmallVector<Expr*, 1> Args;
629 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
630 return;
631
632 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000633 D->addAttr(::new (S.Context)
634 AcquiredBeforeAttr(Attr.getRange(), S.Context,
635 StartArg, Args.size(),
636 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000637}
638
Michael Hana9171bc2012-08-03 17:40:43 +0000639static bool checkLockFunAttrCommon(Sema &S, Decl *D,
640 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000641 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000642 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000643 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000644 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000645
Michael Han3be3b442012-07-23 18:48:41 +0000646 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000647}
648
Michael Hana9171bc2012-08-03 17:40:43 +0000649static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000650 const AttributeList &Attr) {
651 SmallVector<Expr*, 1> Args;
652 if (!checkLockFunAttrCommon(S, D, Attr, Args))
653 return;
654
655 unsigned Size = Args.size();
656 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000657 D->addAttr(::new (S.Context)
658 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
659 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000660}
661
Michael Hana9171bc2012-08-03 17:40:43 +0000662static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000663 const AttributeList &Attr) {
664 SmallVector<Expr*, 1> Args;
665 if (!checkLockFunAttrCommon(S, D, Attr, Args))
666 return;
667
668 unsigned Size = Args.size();
669 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000670 D->addAttr(::new (S.Context)
671 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
672 StartArg, Size,
673 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000674}
675
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000676static void handleAssertSharedLockAttr(Sema &S, Decl *D,
677 const AttributeList &Attr) {
678 SmallVector<Expr*, 1> Args;
679 if (!checkLockFunAttrCommon(S, D, Attr, Args))
680 return;
681
682 unsigned Size = Args.size();
683 Expr **StartArg = Size == 0 ? 0 : &Args[0];
684 D->addAttr(::new (S.Context)
685 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
686 Attr.getAttributeSpellingListIndex()));
687}
688
689static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
690 const AttributeList &Attr) {
691 SmallVector<Expr*, 1> Args;
692 if (!checkLockFunAttrCommon(S, D, Attr, Args))
693 return;
694
695 unsigned Size = Args.size();
696 Expr **StartArg = Size == 0 ? 0 : &Args[0];
697 D->addAttr(::new (S.Context)
698 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
699 StartArg, Size,
700 Attr.getAttributeSpellingListIndex()));
701}
702
703
Michael Hana9171bc2012-08-03 17:40:43 +0000704static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
705 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000706 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000707 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000708 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000709
Aaron Ballman00e99962013-08-31 01:11:41 +0000710 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000711 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000712 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000713 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000714 }
715
716 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000717 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000718
Michael Han3be3b442012-07-23 18:48:41 +0000719 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000720}
721
Michael Hana9171bc2012-08-03 17:40:43 +0000722static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000723 const AttributeList &Attr) {
724 SmallVector<Expr*, 2> Args;
725 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
726 return;
727
Michael Han99315932013-01-24 16:46:58 +0000728 D->addAttr(::new (S.Context)
729 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000730 Attr.getArgAsExpr(0),
731 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000732 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000733}
734
Michael Hana9171bc2012-08-03 17:40:43 +0000735static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000736 const AttributeList &Attr) {
737 SmallVector<Expr*, 2> Args;
738 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
739 return;
740
Michael Han99315932013-01-24 16:46:58 +0000741 D->addAttr(::new (S.Context)
742 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000743 Attr.getArgAsExpr(0),
744 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000745 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000746}
747
Michael Hana9171bc2012-08-03 17:40:43 +0000748static bool checkLocksRequiredCommon(Sema &S, Decl *D,
749 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000750 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000751 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000752 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000753
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000754 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000755 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000756 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000757 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000758
Michael Han3be3b442012-07-23 18:48:41 +0000759 return true;
760}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000761
Michael Hana9171bc2012-08-03 17:40:43 +0000762static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000763 const AttributeList &Attr) {
764 SmallVector<Expr*, 1> Args;
765 if (!checkLocksRequiredCommon(S, D, Attr, Args))
766 return;
767
768 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000769 D->addAttr(::new (S.Context)
770 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
771 StartArg, Args.size(),
772 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000773}
774
Michael Hana9171bc2012-08-03 17:40:43 +0000775static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000776 const AttributeList &Attr) {
777 SmallVector<Expr*, 1> Args;
778 if (!checkLocksRequiredCommon(S, D, Attr, Args))
779 return;
780
781 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000782 D->addAttr(::new (S.Context)
783 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
784 StartArg, Args.size(),
785 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786}
787
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000788static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000789 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000790 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000791 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000792 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000793 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000794 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000795 Expr **StartArg = Size == 0 ? 0 : &Args[0];
796
Michael Han99315932013-01-24 16:46:58 +0000797 D->addAttr(::new (S.Context)
798 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
799 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000800}
801
802static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000803 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000804 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000805 SmallVector<Expr*, 1> Args;
806 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
807 unsigned Size = Args.size();
808 if (Size == 0)
809 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000810
Michael Han99315932013-01-24 16:46:58 +0000811 D->addAttr(::new (S.Context)
812 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
813 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000814}
815
816static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000817 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000818 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000819 return;
820
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000821 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000822 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000823 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000824 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000825 if (Size == 0)
826 return;
827 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000828
Michael Han99315932013-01-24 16:46:58 +0000829 D->addAttr(::new (S.Context)
830 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
831 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000832}
833
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000834static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000835 ConsumableAttr::ConsumedState DefaultState;
836
837 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000838 IdentifierLoc *IL = Attr.getArgAsIdent(0);
839 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
840 DefaultState)) {
841 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
842 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000843 return;
844 }
David Blaikie16f76d22013-09-06 01:28:43 +0000845 } else {
846 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
847 << Attr.getName() << AANT_ArgumentIdentifier;
848 return;
849 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000850
851 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000852 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000853 Attr.getAttributeSpellingListIndex()));
854}
855
856static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
857 const AttributeList &Attr) {
858 ASTContext &CurrContext = S.getASTContext();
859 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
860
861 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
862 if (!RD->hasAttr<ConsumableAttr>()) {
863 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
864 RD->getNameAsString();
865
866 return false;
867 }
868 }
869
870 return true;
871}
872
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000873
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000874static void handleCallableWhenAttr(Sema &S, Decl *D,
875 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000876 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
877 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000878
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000879 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
880 return;
881
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000882 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
883 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
884 CallableWhenAttr::ConsumedState CallableState;
885
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000886 StringRef StateString;
887 SourceLocation Loc;
888 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
889 return;
890
891 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000892 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000893 S.Diag(Loc, diag::warn_attribute_type_not_supported)
894 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000895 return;
896 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000897
898 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000899 }
900
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000901 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000902 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
903 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000904}
905
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000906
DeLesley Hutchins69391772013-10-17 23:23:53 +0000907static void handleParamTypestateAttr(Sema &S, Decl *D,
908 const AttributeList &Attr) {
909 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000910
DeLesley Hutchins69391772013-10-17 23:23:53 +0000911 ParamTypestateAttr::ConsumedState ParamState;
912
913 if (Attr.isArgIdent(0)) {
914 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
915 StringRef StateString = Ident->Ident->getName();
916
917 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
918 ParamState)) {
919 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
920 << Attr.getName() << StateString;
921 return;
922 }
923 } else {
924 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
925 Attr.getName() << AANT_ArgumentIdentifier;
926 return;
927 }
928
929 // FIXME: This check is currently being done in the analysis. It can be
930 // enabled here only after the parser propagates attributes at
931 // template specialization definition, not declaration.
932 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
933 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
934 //
935 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
936 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
937 // ReturnType.getAsString();
938 // return;
939 //}
940
941 D->addAttr(::new (S.Context)
942 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
943 Attr.getAttributeSpellingListIndex()));
944}
945
946
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000947static void handleReturnTypestateAttr(Sema &S, Decl *D,
948 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000949 if (!checkAttributeNumArgs(S, Attr, 1)) return;
950
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000951 ReturnTypestateAttr::ConsumedState ReturnState;
952
953 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000954 IdentifierLoc *IL = Attr.getArgAsIdent(0);
955 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
956 ReturnState)) {
957 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
958 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000959 return;
960 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000961 } else {
962 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
963 Attr.getName() << AANT_ArgumentIdentifier;
964 return;
965 }
966
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000967 // FIXME: This check is currently being done in the analysis. It can be
968 // enabled here only after the parser propagates attributes at
969 // template specialization definition, not declaration.
970 //QualType ReturnType;
971 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000972 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
973 // ReturnType = Param->getType();
974 //
975 //} else if (const CXXConstructorDecl *Constructor =
976 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000977 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
978 //
979 //} else {
980 //
981 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
982 //}
983 //
984 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
985 //
986 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
987 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
988 // ReturnType.getAsString();
989 // return;
990 //}
991
992 D->addAttr(::new (S.Context)
993 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
994 Attr.getAttributeSpellingListIndex()));
995}
996
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000997
998static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000999 if (!checkAttributeNumArgs(S, Attr, 1))
1000 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001001
1002 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1003 return;
1004
1005 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001006 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001007 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1008 StringRef Param = Ident->Ident->getName();
1009 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1010 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1011 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012 return;
1013 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001014 } else {
1015 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1016 Attr.getName() << AANT_ArgumentIdentifier;
1017 return;
1018 }
1019
1020 D->addAttr(::new (S.Context)
1021 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1022 Attr.getAttributeSpellingListIndex()));
1023}
1024
Chris Wailes9385f9f2013-10-29 20:28:41 +00001025static void handleTestTypestateAttr(Sema &S, Decl *D,
1026 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001027 if (!checkAttributeNumArgs(S, Attr, 1))
1028 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001029
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001030 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1031 return;
1032
Chris Wailes9385f9f2013-10-29 20:28:41 +00001033 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001034 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001035 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1036 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001037 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001038 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1039 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001040 return;
1041 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001042 } else {
1043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1044 Attr.getName() << AANT_ArgumentIdentifier;
1045 return;
1046 }
1047
1048 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001049 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001050 Attr.getAttributeSpellingListIndex()));
1051}
1052
Chandler Carruthedc2c642011-07-02 00:01:44 +00001053static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1054 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001055 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001056 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001057}
1058
Chandler Carruthedc2c642011-07-02 00:01:44 +00001059static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001060 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001061 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001062 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001063 // If the alignment is less than or equal to 8 bits, the packed attribute
1064 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001065 if (!FD->getType()->isDependentType() &&
1066 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001067 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001068 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001069 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001070 else
Michael Han99315932013-01-24 16:46:58 +00001071 FD->addAttr(::new (S.Context)
1072 PackedAttr(Attr.getRange(), S.Context,
1073 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001074 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001075 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001076}
1077
Ted Kremenek7fd17232011-09-29 07:02:25 +00001078static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1079 // The IBOutlet/IBOutletCollection attributes only apply to instance
1080 // variables or properties of Objective-C classes. The outlet must also
1081 // have an object reference type.
1082 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1083 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001084 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001085 << Attr.getName() << VD->getType() << 0;
1086 return false;
1087 }
1088 }
1089 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1090 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001091 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001092 << Attr.getName() << PD->getType() << 1;
1093 return false;
1094 }
1095 }
1096 else {
1097 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1098 return false;
1099 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001100
Ted Kremenek7fd17232011-09-29 07:02:25 +00001101 return true;
1102}
1103
Chandler Carruthedc2c642011-07-02 00:01:44 +00001104static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001105 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001106 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001107
Michael Han99315932013-01-24 16:46:58 +00001108 D->addAttr(::new (S.Context)
1109 IBOutletAttr(Attr.getRange(), S.Context,
1110 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001111}
1112
Chandler Carruthedc2c642011-07-02 00:01:44 +00001113static void handleIBOutletCollection(Sema &S, Decl *D,
1114 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001115
1116 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001117 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001118 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1119 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001120 return;
1121 }
1122
Ted Kremenek7fd17232011-09-29 07:02:25 +00001123 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001124 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001125
Richard Smithb1f9a282013-10-31 01:56:18 +00001126 ParsedType PT;
1127
1128 if (Attr.hasParsedType())
1129 PT = Attr.getTypeArg();
1130 else {
1131 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1132 S.getScopeForContext(D->getDeclContext()->getParent()));
1133 if (!PT) {
1134 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1135 return;
1136 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001137 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001138
Richard Smithb87c4652013-10-31 21:23:20 +00001139 TypeSourceInfo *QTLoc = 0;
1140 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1141 if (!QTLoc)
1142 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001143
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001144 // Diagnose use of non-object type in iboutletcollection attribute.
1145 // FIXME. Gnu attribute extension ignores use of builtin types in
1146 // attributes. So, __attribute__((iboutletcollection(char))) will be
1147 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001148 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001149 S.Diag(Attr.getLoc(),
1150 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1151 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001152 return;
1153 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001154
Michael Han99315932013-01-24 16:46:58 +00001155 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001156 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001157 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001158}
1159
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001160static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001161 if (const RecordType *UT = T->getAsUnionType())
1162 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1163 RecordDecl *UD = UT->getDecl();
1164 for (RecordDecl::field_iterator it = UD->field_begin(),
1165 itend = UD->field_end(); it != itend; ++it) {
1166 QualType QT = it->getType();
1167 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1168 T = QT;
1169 return;
1170 }
1171 }
1172 }
1173}
1174
Chandler Carruthedc2c642011-07-02 00:01:44 +00001175static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001176 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1177 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001178 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001179 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001180 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001181 return;
1182 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001183
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001184 SmallVector<unsigned, 8> NonNullArgs;
1185 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001186 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001187 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001188 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001189 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001190
1191 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001192 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001193 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001194
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001195 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001196 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001197 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1198 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001199 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001200 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001201
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001202 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001203 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001204
1205 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1206 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001207 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001208 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1209 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001210 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001211 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001212 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001213 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001214
Ted Kremenek22813f42010-10-21 18:49:36 +00001215 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001216 if (NonNullArgs.empty()) {
1217 // Warn the trivial case only if attribute is not coming from a
1218 // macro instantiation.
1219 if (Attr.getLoc().isFileID())
1220 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001221 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001222 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001223 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001224
Nick Lewyckye1121512013-01-24 01:12:16 +00001225 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001226 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001227 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001228 D->addAttr(::new (S.Context)
1229 NonNullAttr(Attr.getRange(), S.Context, start, size,
1230 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001231}
1232
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001233static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1234 switch (K) {
1235 case OwnershipAttr::Holds: return "'ownership_holds'";
1236 case OwnershipAttr::Takes: return "'ownership_takes'";
1237 case OwnershipAttr::Returns: return "'ownership_returns'";
1238 }
1239 llvm_unreachable("unknown ownership");
1240}
1241
Chandler Carruthedc2c642011-07-02 00:01:44 +00001242static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001243 // This attribute must be applied to a function declaration. The first
1244 // argument to the attribute must be an identifier, the name of the resource,
1245 // for example: malloc. The following arguments must be argument indexes, the
1246 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001247 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001248 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001249 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001250
Aaron Ballman00e99962013-08-31 01:11:41 +00001251 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001252 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001253 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001254 return;
1255 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001256
Richard Smith852e9ce2013-11-27 01:46:48 +00001257 // Figure out our Kind.
1258 OwnershipAttr::OwnershipKind K =
1259 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1260 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001261
Richard Smith852e9ce2013-11-27 01:46:48 +00001262 // Check arguments.
1263 switch (K) {
1264 case OwnershipAttr::Takes:
1265 case OwnershipAttr::Holds:
1266 if (AL.getNumArgs() < 2) {
1267 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1268 return;
1269 }
1270 break;
1271 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001272 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001273 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 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()) {
1369 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001370 nd->getNameAsString();
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 Ballmanfb763042013-12-02 18:05:46 +00001428 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr, "hot"))
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 Ballmanfb763042013-12-02 18:05:46 +00001436 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr, "cold"))
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) {
1642 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001643 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001644 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001645
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001646 uint32_t priority = ConstructorAttr::DefaultPriority;
1647 if (Attr.getNumArgs() > 0 &&
1648 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1649 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001650
Michael Han99315932013-01-24 16:46:58 +00001651 D->addAttr(::new (S.Context)
1652 ConstructorAttr(Attr.getRange(), S.Context, priority,
1653 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001654}
1655
Chandler Carruthedc2c642011-07-02 00:01:44 +00001656static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001657 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001658 if (Attr.getNumArgs() > 1) {
1659 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001660 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001661 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001662
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001663 uint32_t priority = ConstructorAttr::DefaultPriority;
1664 if (Attr.getNumArgs() > 0 &&
1665 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1666 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001667
Michael Han99315932013-01-24 16:46:58 +00001668 D->addAttr(::new (S.Context)
1669 DestructorAttr(Attr.getRange(), S.Context, priority,
1670 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001671}
1672
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001673template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001674static void handleAttrWithMessage(Sema &S, Decl *D,
1675 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001676 unsigned NumArgs = Attr.getNumArgs();
1677 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001678 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001679 return;
1680 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001681
1682 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001683 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001684 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001685 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001686
Michael Han99315932013-01-24 16:46:58 +00001687 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1688 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001689}
1690
Ted Kremenek28eace62013-11-23 01:01:34 +00001691static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1692 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001693 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001694 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1695 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001696}
1697
Jordy Rose740b0c22012-05-08 03:27:22 +00001698static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1699 IdentifierInfo *Platform,
1700 VersionTuple Introduced,
1701 VersionTuple Deprecated,
1702 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001703 StringRef PlatformName
1704 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1705 if (PlatformName.empty())
1706 PlatformName = Platform->getName();
1707
1708 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1709 // of these steps are needed).
1710 if (!Introduced.empty() && !Deprecated.empty() &&
1711 !(Introduced <= Deprecated)) {
1712 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1713 << 1 << PlatformName << Deprecated.getAsString()
1714 << 0 << Introduced.getAsString();
1715 return true;
1716 }
1717
1718 if (!Introduced.empty() && !Obsoleted.empty() &&
1719 !(Introduced <= Obsoleted)) {
1720 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1721 << 2 << PlatformName << Obsoleted.getAsString()
1722 << 0 << Introduced.getAsString();
1723 return true;
1724 }
1725
1726 if (!Deprecated.empty() && !Obsoleted.empty() &&
1727 !(Deprecated <= Obsoleted)) {
1728 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1729 << 2 << PlatformName << Obsoleted.getAsString()
1730 << 1 << Deprecated.getAsString();
1731 return true;
1732 }
1733
1734 return false;
1735}
1736
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001737/// \brief Check whether the two versions match.
1738///
1739/// If either version tuple is empty, then they are assumed to match. If
1740/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1741static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1742 bool BeforeIsOkay) {
1743 if (X.empty() || Y.empty())
1744 return true;
1745
1746 if (X == Y)
1747 return true;
1748
1749 if (BeforeIsOkay && X < Y)
1750 return true;
1751
1752 return false;
1753}
1754
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001755AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001756 IdentifierInfo *Platform,
1757 VersionTuple Introduced,
1758 VersionTuple Deprecated,
1759 VersionTuple Obsoleted,
1760 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001761 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001762 bool Override,
1763 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001764 VersionTuple MergedIntroduced = Introduced;
1765 VersionTuple MergedDeprecated = Deprecated;
1766 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001767 bool FoundAny = false;
1768
Rafael Espindolac67f2232012-05-10 02:50:16 +00001769 if (D->hasAttrs()) {
1770 AttrVec &Attrs = D->getAttrs();
1771 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1772 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1773 if (!OldAA) {
1774 ++i;
1775 continue;
1776 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001777
Rafael Espindolac67f2232012-05-10 02:50:16 +00001778 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1779 if (OldPlatform != Platform) {
1780 ++i;
1781 continue;
1782 }
1783
1784 FoundAny = true;
1785 VersionTuple OldIntroduced = OldAA->getIntroduced();
1786 VersionTuple OldDeprecated = OldAA->getDeprecated();
1787 VersionTuple OldObsoleted = OldAA->getObsoleted();
1788 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001789
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001790 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1791 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1792 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1793 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001794 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001795 if (Override) {
1796 int Which = -1;
1797 VersionTuple FirstVersion;
1798 VersionTuple SecondVersion;
1799 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1800 Which = 0;
1801 FirstVersion = OldIntroduced;
1802 SecondVersion = Introduced;
1803 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1804 Which = 1;
1805 FirstVersion = Deprecated;
1806 SecondVersion = OldDeprecated;
1807 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1808 Which = 2;
1809 FirstVersion = Obsoleted;
1810 SecondVersion = OldObsoleted;
1811 }
1812
1813 if (Which == -1) {
1814 Diag(OldAA->getLocation(),
1815 diag::warn_mismatched_availability_override_unavail)
1816 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1817 } else {
1818 Diag(OldAA->getLocation(),
1819 diag::warn_mismatched_availability_override)
1820 << Which
1821 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1822 << FirstVersion.getAsString() << SecondVersion.getAsString();
1823 }
1824 Diag(Range.getBegin(), diag::note_overridden_method);
1825 } else {
1826 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1827 Diag(Range.getBegin(), diag::note_previous_attribute);
1828 }
1829
Rafael Espindolac67f2232012-05-10 02:50:16 +00001830 Attrs.erase(Attrs.begin() + i);
1831 --e;
1832 continue;
1833 }
1834
1835 VersionTuple MergedIntroduced2 = MergedIntroduced;
1836 VersionTuple MergedDeprecated2 = MergedDeprecated;
1837 VersionTuple MergedObsoleted2 = MergedObsoleted;
1838
1839 if (MergedIntroduced2.empty())
1840 MergedIntroduced2 = OldIntroduced;
1841 if (MergedDeprecated2.empty())
1842 MergedDeprecated2 = OldDeprecated;
1843 if (MergedObsoleted2.empty())
1844 MergedObsoleted2 = OldObsoleted;
1845
1846 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1847 MergedIntroduced2, MergedDeprecated2,
1848 MergedObsoleted2)) {
1849 Attrs.erase(Attrs.begin() + i);
1850 --e;
1851 continue;
1852 }
1853
1854 MergedIntroduced = MergedIntroduced2;
1855 MergedDeprecated = MergedDeprecated2;
1856 MergedObsoleted = MergedObsoleted2;
1857 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001858 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001859 }
1860
1861 if (FoundAny &&
1862 MergedIntroduced == Introduced &&
1863 MergedDeprecated == Deprecated &&
1864 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001865 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001866
Ted Kremenekb5445722013-04-06 00:34:27 +00001867 // Only create a new attribute if !Override, but we want to do
1868 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001869 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001870 MergedDeprecated, MergedObsoleted) &&
1871 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001872 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1873 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001874 Obsoleted, IsUnavailable, Message,
1875 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001876 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001877 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001878}
1879
Chandler Carruthedc2c642011-07-02 00:01:44 +00001880static void handleAvailabilityAttr(Sema &S, Decl *D,
1881 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001882 if (!checkAttributeNumArgs(S, Attr, 1))
1883 return;
1884 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001885 unsigned Index = Attr.getAttributeSpellingListIndex();
1886
Aaron Ballman00e99962013-08-31 01:11:41 +00001887 IdentifierInfo *II = Platform->Ident;
1888 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1889 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1890 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001891
Rafael Espindolac231fab2013-01-08 21:30:32 +00001892 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1893 if (!ND) {
1894 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1895 return;
1896 }
1897
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001898 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1899 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1900 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001901 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001902 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001903 if (const StringLiteral *SE =
1904 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001905 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001906
Aaron Ballman00e99962013-08-31 01:11:41 +00001907 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001908 Introduced.Version,
1909 Deprecated.Version,
1910 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001911 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001912 /*Override=*/false,
1913 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001914 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001915 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001916}
1917
John McCalld041a9b2013-02-20 01:54:26 +00001918template <class T>
1919static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1920 typename T::VisibilityType value,
1921 unsigned attrSpellingListIndex) {
1922 T *existingAttr = D->getAttr<T>();
1923 if (existingAttr) {
1924 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1925 if (existingValue == value)
1926 return NULL;
1927 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1928 S.Diag(range.getBegin(), diag::note_previous_attribute);
1929 D->dropAttr<T>();
1930 }
1931 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1932}
1933
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001934VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001935 VisibilityAttr::VisibilityType Vis,
1936 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001937 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1938 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001939}
1940
John McCalld041a9b2013-02-20 01:54:26 +00001941TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1942 TypeVisibilityAttr::VisibilityType Vis,
1943 unsigned AttrSpellingListIndex) {
1944 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1945 AttrSpellingListIndex);
1946}
1947
1948static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1949 bool isTypeVisibility) {
1950 // Visibility attributes don't mean anything on a typedef.
1951 if (isa<TypedefNameDecl>(D)) {
1952 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1953 << Attr.getName();
1954 return;
1955 }
1956
1957 // 'type_visibility' can only go on a type or namespace.
1958 if (isTypeVisibility &&
1959 !(isa<TagDecl>(D) ||
1960 isa<ObjCInterfaceDecl>(D) ||
1961 isa<NamespaceDecl>(D))) {
1962 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1963 << Attr.getName() << ExpectedTypeOrNamespace;
1964 return;
1965 }
1966
Benjamin Kramer70370212013-09-09 15:08:57 +00001967 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001968 StringRef TypeStr;
1969 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001970 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001971 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001972
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001973 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001974 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001975 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001976 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001977 return;
1978 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001979
1980 // Complain about attempts to use protected visibility on targets
1981 // (like Darwin) that don't support it.
1982 if (type == VisibilityAttr::Protected &&
1983 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1984 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1985 type = VisibilityAttr::Default;
1986 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001987
Michael Han99315932013-01-24 16:46:58 +00001988 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001989 clang::Attr *newAttr;
1990 if (isTypeVisibility) {
1991 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1992 (TypeVisibilityAttr::VisibilityType) type,
1993 Index);
1994 } else {
1995 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1996 }
1997 if (newAttr)
1998 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001999}
2000
Chandler Carruthedc2c642011-07-02 00:01:44 +00002001static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2002 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002003 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002004 if (!Attr.isArgIdent(0)) {
2005 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2006 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002007 return;
2008 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002009
Aaron Ballman682ee422013-09-11 19:47:58 +00002010 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2011 ObjCMethodFamilyAttr::FamilyKind F;
2012 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2013 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2014 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002015 return;
2016 }
2017
Aaron Ballman682ee422013-09-11 19:47:58 +00002018 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002019 !method->getResultType()->isObjCObjectPointerType()) {
2020 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2021 << method->getResultType();
2022 // Ignore the attribute.
2023 return;
2024 }
2025
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002026 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002027 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002028}
2029
Chandler Carruthedc2c642011-07-02 00:01:44 +00002030static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002031 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002032 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002033 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002034 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2035 return;
2036 }
2037 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002038 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2039 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002040 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002041 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2042 return;
2043 }
2044 }
2045 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002046 // It is okay to include this attribute on properties, e.g.:
2047 //
2048 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2049 //
2050 // In this case it follows tradition and suppresses an error in the above
2051 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002052 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002053 }
Michael Han99315932013-01-24 16:46:58 +00002054 D->addAttr(::new (S.Context)
2055 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2056 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002057}
2058
Chandler Carruthedc2c642011-07-02 00:01:44 +00002059static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002060 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002061 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002062 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002063 return;
2064 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002065
Aaron Ballman00e99962013-08-31 01:11:41 +00002066 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002067 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002068 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2069 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2070 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002071 return;
2072 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002073
Michael Han99315932013-01-24 16:46:58 +00002074 D->addAttr(::new (S.Context)
2075 BlocksAttr(Attr.getRange(), S.Context, type,
2076 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002077}
2078
Chandler Carruthedc2c642011-07-02 00:01:44 +00002079static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002080 // check the attribute arguments.
2081 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002083 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002084 }
2085
Aaron Ballman18a78382013-11-21 00:28:23 +00002086 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002087 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002088 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002089 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002090 if (E->isTypeDependent() || E->isValueDependent() ||
2091 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002092 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002093 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002094 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002095 return;
2096 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002097
John McCallb46f2872011-09-09 07:56:05 +00002098 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002099 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2100 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002101 return;
2102 }
John McCallb46f2872011-09-09 07:56:05 +00002103
2104 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002105 }
2106
Aaron Ballman18a78382013-11-21 00:28:23 +00002107 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002108 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002109 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002110 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002111 if (E->isTypeDependent() || E->isValueDependent() ||
2112 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002113 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002114 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002115 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002116 return;
2117 }
2118 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002119
John McCallb46f2872011-09-09 07:56:05 +00002120 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002121 // FIXME: This error message could be improved, it would be nice
2122 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002123 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2124 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002125 return;
2126 }
2127 }
2128
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002129 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002130 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002131 if (isa<FunctionNoProtoType>(FT)) {
2132 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2133 return;
2134 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002135
Chris Lattner9363e312009-03-17 23:03:47 +00002136 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002137 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002138 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002139 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002140 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002141 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002142 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002143 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002144 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002145 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2146 if (!BD->isVariadic()) {
2147 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2148 return;
2149 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002150 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002151 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002152 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002153 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002154 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002155 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002156 int m = Ty->isFunctionPointerType() ? 0 : 1;
2157 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002158 return;
2159 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002160 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002162 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002163 return;
2164 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002165 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002166 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002167 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002168 return;
2169 }
Michael Han99315932013-01-24 16:46:58 +00002170 D->addAttr(::new (S.Context)
2171 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2172 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002173}
2174
Chandler Carruthedc2c642011-07-02 00:01:44 +00002175static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002176 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002178 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002179 return;
2180 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002181
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002182 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2183 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2184 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002185 return;
2186 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002187 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2188 if (MD->getResultType()->isVoidType()) {
2189 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2190 << Attr.getName() << 1;
2191 return;
2192 }
2193
Michael Han99315932013-01-24 16:46:58 +00002194 D->addAttr(::new (S.Context)
2195 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2196 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002197}
2198
Chandler Carruthedc2c642011-07-02 00:01:44 +00002199static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002200 // weak_import only applies to variable & function declarations.
2201 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002202 if (!D->canBeWeakImported(isDef)) {
2203 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002204 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2205 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002206 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002207 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002208 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002209 // Nothing to warn about here.
2210 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002212 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002213
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002214 return;
2215 }
2216
Michael Han99315932013-01-24 16:46:58 +00002217 D->addAttr(::new (S.Context)
2218 WeakImportAttr(Attr.getRange(), S.Context,
2219 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002220}
2221
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002222// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002223template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002224static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002225 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002226 uint32_t WGSize[3];
2227 for (unsigned i = 0; i < 3; ++i)
2228 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002229 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002230
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002231 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2232 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2233 Existing->getYDim() == WGSize[1] &&
2234 Existing->getZDim() == WGSize[2]))
2235 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002236
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002237 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2238 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002239 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002240}
2241
Joey Goulyaba589c2013-03-08 09:42:32 +00002242static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002243 if (!Attr.hasParsedType()) {
2244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2245 << Attr.getName() << 1;
2246 return;
2247 }
2248
Richard Smithb87c4652013-10-31 21:23:20 +00002249 TypeSourceInfo *ParmTSI = 0;
2250 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2251 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002252
2253 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2254 (ParmType->isBooleanType() ||
2255 !ParmType->isIntegralType(S.getASTContext()))) {
2256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2257 << ParmType;
2258 return;
2259 }
2260
Aaron Ballmana9e05402013-12-02 22:16:55 +00002261 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002262 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002263 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2264 return;
2265 }
2266 }
2267
2268 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002269 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002270}
2271
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002272SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002273 StringRef Name,
2274 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002275 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2276 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002277 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002278 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2279 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002280 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002281 }
Michael Han99315932013-01-24 16:46:58 +00002282 return ::new (Context) SectionAttr(Range, Context, Name,
2283 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002284}
2285
Chandler Carruthedc2c642011-07-02 00:01:44 +00002286static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002287 // Make sure that there is a string literal as the sections's single
2288 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002289 StringRef Str;
2290 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002291 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002292 return;
Mike Stump11289f42009-09-09 15:08:12 +00002293
Chris Lattner30ba6742009-08-10 19:03:04 +00002294 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002295 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002296 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002297 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002298 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002299 return;
2300 }
Mike Stump11289f42009-09-09 15:08:12 +00002301
Michael Han99315932013-01-24 16:46:58 +00002302 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002303 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002304 if (NewAttr)
2305 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002306}
2307
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002308
Chandler Carruthedc2c642011-07-02 00:01:44 +00002309static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002310 VarDecl *VD = cast<VarDecl>(D);
2311 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002312 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002313 return;
2314 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002315
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002316 Expr *E = Attr.getArgAsExpr(0);
2317 SourceLocation Loc = E->getExprLoc();
2318 FunctionDecl *FD = 0;
2319 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002320
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002321 // gcc only allows for simple identifiers. Since we support more than gcc, we
2322 // will warn the user.
2323 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2324 if (DRE->hasQualifier())
2325 S.Diag(Loc, diag::warn_cleanup_ext);
2326 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2327 NI = DRE->getNameInfo();
2328 if (!FD) {
2329 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2330 << NI.getName();
2331 return;
2332 }
2333 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2334 if (ULE->hasExplicitTemplateArgs())
2335 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002336 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2337 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002338 if (!FD) {
2339 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2340 << NI.getName();
2341 if (ULE->getType() == S.Context.OverloadTy)
2342 S.NoteAllOverloadCandidates(ULE);
2343 return;
2344 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002345 } else {
2346 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002347 return;
2348 }
2349
Anders Carlssond277d792009-01-31 01:16:18 +00002350 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002351 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2352 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002353 return;
2354 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002355
Anders Carlsson723f55d2009-02-07 23:16:50 +00002356 // We're currently more strict than GCC about what function types we accept.
2357 // If this ever proves to be a problem it should be easy to fix.
2358 QualType Ty = S.Context.getPointerType(VD->getType());
2359 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002360 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2361 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002362 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2363 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002364 return;
2365 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002366
Michael Han99315932013-01-24 16:46:58 +00002367 D->addAttr(::new (S.Context)
2368 CleanupAttr(Attr.getRange(), S.Context, FD,
2369 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002370}
2371
Mike Stumpd3bb5572009-07-24 19:02:52 +00002372/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002373/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002374static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002375 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002376 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002377 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002378 return;
2379 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002380
Aaron Ballman00e99962013-08-31 01:11:41 +00002381 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002382 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002383 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002384 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002385
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002386 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002387 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002389 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2390 if (not_nsstring_type &&
2391 !isCFStringType(Ty, S.Context) &&
2392 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002393 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002394 // FIXME: Should highlight the actual expression that has the wrong type.
2395 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002396 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002397 << IdxExpr->getSourceRange();
2398 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002399 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002400 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002401 if (!isNSStringType(Ty, S.Context) &&
2402 !isCFStringType(Ty, S.Context) &&
2403 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002404 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002405 // FIXME: Should highlight the actual expression that has the wrong type.
2406 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002407 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002408 << IdxExpr->getSourceRange();
2409 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002410 }
2411
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002412 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2413 // because that has corrected for the implicit this parameter, and is zero-
2414 // based. The attribute expects what the user wrote explicitly.
2415 llvm::APSInt Val;
2416 IdxExpr->EvaluateAsInt(Val, S.Context);
2417
Michael Han99315932013-01-24 16:46:58 +00002418 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002419 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002420 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002421}
2422
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002423enum FormatAttrKind {
2424 CFStringFormat,
2425 NSStringFormat,
2426 StrftimeFormat,
2427 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002428 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002429 InvalidFormat
2430};
2431
2432/// getFormatAttrKind - Map from format attribute names to supported format
2433/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002434static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002435 return llvm::StringSwitch<FormatAttrKind>(Format)
2436 // Check for formats that get handled specially.
2437 .Case("NSString", NSStringFormat)
2438 .Case("CFString", CFStringFormat)
2439 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002440
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002441 // Otherwise, check for supported formats.
2442 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2443 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2444 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002445
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002446 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2447 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002448}
2449
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002450/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002451/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002452static void handleInitPriorityAttr(Sema &S, Decl *D,
2453 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002454 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002455 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2456 return;
2457 }
2458
Aaron Ballman4a611152013-11-27 16:34:09 +00002459 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002460 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2461 Attr.setInvalid();
2462 return;
2463 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002464 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002465 if (S.Context.getAsArrayType(T))
2466 T = S.Context.getBaseElementType(T);
2467 if (!T->getAs<RecordType>()) {
2468 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2469 Attr.setInvalid();
2470 return;
2471 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002472
2473 Expr *E = Attr.getArgAsExpr(0);
2474 uint32_t prioritynum;
2475 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002476 Attr.setInvalid();
2477 return;
2478 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002479
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002480 if (prioritynum < 101 || prioritynum > 65535) {
2481 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002482 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002483 Attr.setInvalid();
2484 return;
2485 }
Michael Han99315932013-01-24 16:46:58 +00002486 D->addAttr(::new (S.Context)
2487 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2488 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002489}
2490
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002491FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2492 IdentifierInfo *Format, int FormatIdx,
2493 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002494 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002495 // Check whether we already have an equivalent format attribute.
2496 for (specific_attr_iterator<FormatAttr>
2497 i = D->specific_attr_begin<FormatAttr>(),
2498 e = D->specific_attr_end<FormatAttr>();
2499 i != e ; ++i) {
2500 FormatAttr *f = *i;
2501 if (f->getType() == Format &&
2502 f->getFormatIdx() == FormatIdx &&
2503 f->getFirstArg() == FirstArg) {
2504 // If we don't have a valid location for this attribute, adopt the
2505 // location.
2506 if (f->getLocation().isInvalid())
2507 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002508 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002509 }
2510 }
2511
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002512 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2513 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002514}
2515
Mike Stumpd3bb5572009-07-24 19:02:52 +00002516/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002517/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002518static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002519 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002521 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002522 return;
2523 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002524
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002525 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002526 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002527 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528 return;
2529 }
2530
Chandler Carruth743682b2010-11-16 08:35:43 +00002531 // In C++ the implicit 'this' function parameter also counts, and they are
2532 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002533 bool HasImplicitThisParam = isInstanceMethod(D);
2534 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002535
Aaron Ballman00e99962013-08-31 01:11:41 +00002536 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2537 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002538
2539 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002540 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002541 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002542 // If we've modified the string name, we need a new identifier for it.
2543 II = &S.Context.Idents.get(Format);
2544 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002545
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002546 // Check for supported formats.
2547 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002548
2549 if (Kind == IgnoredFormat)
2550 return;
2551
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002552 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002553 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002554 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002555 return;
2556 }
2557
2558 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002559 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002560 uint32_t Idx;
2561 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002563
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002564 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002566 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567 return;
2568 }
2569
2570 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002571 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002572
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002573 if (HasImplicitThisParam) {
2574 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002575 S.Diag(Attr.getLoc(),
2576 diag::err_format_attribute_implicit_this_format_string)
2577 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002578 return;
2579 }
2580 ArgIdx--;
2581 }
Mike Stump11289f42009-09-09 15:08:12 +00002582
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002583 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002584 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002586 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002587 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002588 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2589 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002590 return;
2591 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002592 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002593 // FIXME: do we need to check if the type is NSString*? What are the
2594 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002595 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002596 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002597 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2598 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002599 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002600 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002601 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002602 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002603 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002604 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2605 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606 return;
2607 }
2608
2609 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002610 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002611 uint32_t FirstArg;
2612 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002613 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614
2615 // check if the function is variadic if the 3rd argument non-zero
2616 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002617 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618 ++NumArgs; // +1 for ...
2619 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002620 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621 return;
2622 }
2623 }
2624
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002625 // strftime requires FirstArg to be 0 because it doesn't read from any
2626 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002627 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002629 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2630 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631 return;
2632 }
2633 // if 0 it disables parameter checking (to use with e.g. va_list)
2634 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002635 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002636 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637 return;
2638 }
2639
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002640 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002641 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002642 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002643 if (NewAttr)
2644 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002645}
2646
Chandler Carruthedc2c642011-07-02 00:01:44 +00002647static void handleTransparentUnionAttr(Sema &S, Decl *D,
2648 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002649 // Try to find the underlying union declaration.
2650 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002651 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002652 if (TD && TD->getUnderlyingType()->isUnionType())
2653 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2654 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002655 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002656
2657 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002659 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002660 return;
2661 }
2662
John McCallf937c022011-10-07 06:10:15 +00002663 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002664 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002665 diag::warn_transparent_union_attribute_not_definition);
2666 return;
2667 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002668
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002669 RecordDecl::field_iterator Field = RD->field_begin(),
2670 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002671 if (Field == FieldEnd) {
2672 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2673 return;
2674 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002675
David Blaikie40ed2972012-06-06 20:45:41 +00002676 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002677 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002678 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002679 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002680 diag::warn_transparent_union_attribute_floating)
2681 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002682 return;
2683 }
2684
2685 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2686 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2687 for (; Field != FieldEnd; ++Field) {
2688 QualType FieldType = Field->getType();
2689 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2690 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2691 // Warn if we drop the attribute.
2692 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002693 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002694 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002695 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002696 diag::warn_transparent_union_attribute_field_size_align)
2697 << isSize << Field->getDeclName() << FieldBits;
2698 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002699 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002700 diag::note_transparent_union_first_field_size_align)
2701 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002702 return;
2703 }
2704 }
2705
Michael Han99315932013-01-24 16:46:58 +00002706 RD->addAttr(::new (S.Context)
2707 TransparentUnionAttr(Attr.getRange(), S.Context,
2708 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002709}
2710
Chandler Carruthedc2c642011-07-02 00:01:44 +00002711static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002712 // Make sure that there is a string literal as the annotation's single
2713 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002714 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002715 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002717
2718 // Don't duplicate annotations that are already set.
2719 for (specific_attr_iterator<AnnotateAttr>
2720 i = D->specific_attr_begin<AnnotateAttr>(),
2721 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002722 if ((*i)->getAnnotation() == Str)
2723 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002724 }
Michael Han99315932013-01-24 16:46:58 +00002725
2726 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002727 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002728 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002729}
2730
Chandler Carruthedc2c642011-07-02 00:01:44 +00002731static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002732 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002733 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002734 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2735 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002736 return;
2737 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002738
Richard Smith848e1f12013-02-01 08:12:08 +00002739 if (Attr.getNumArgs() == 0) {
2740 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2741 true, 0, Attr.getAttributeSpellingListIndex()));
2742 return;
2743 }
2744
Aaron Ballman00e99962013-08-31 01:11:41 +00002745 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002746 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2747 S.Diag(Attr.getEllipsisLoc(),
2748 diag::err_pack_expansion_without_parameter_packs);
2749 return;
2750 }
2751
2752 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2753 return;
2754
2755 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2756 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002757}
2758
2759void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002760 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002761 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2762 SourceLocation AttrLoc = AttrRange.getBegin();
2763
Richard Smith1dba27c2013-01-29 09:02:09 +00002764 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002765 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002766 // C++11 [dcl.align]p1:
2767 // An alignment-specifier may be applied to a variable or to a class
2768 // data member, but it shall not be applied to a bit-field, a function
2769 // parameter, the formal parameter of a catch clause, or a variable
2770 // declared with the register storage class specifier. An
2771 // alignment-specifier may also be applied to the declaration of a class
2772 // or enumeration type.
2773 // C11 6.7.5/2:
2774 // An alignment attribute shall not be specified in a declaration of
2775 // a typedef, or a bit-field, or a function, or a parameter, or an
2776 // object declared with the register storage-class specifier.
2777 int DiagKind = -1;
2778 if (isa<ParmVarDecl>(D)) {
2779 DiagKind = 0;
2780 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2781 if (VD->getStorageClass() == SC_Register)
2782 DiagKind = 1;
2783 if (VD->isExceptionVariable())
2784 DiagKind = 2;
2785 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2786 if (FD->isBitField())
2787 DiagKind = 3;
2788 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002789 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2790 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002791 << (TmpAttr.isC11() ? ExpectedVariableOrField
2792 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002793 return;
2794 }
2795 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002796 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002797 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002798 return;
2799 }
2800 }
2801
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002802 if (E->isTypeDependent() || E->isValueDependent()) {
2803 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002804 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2805 AA->setPackExpansion(IsPackExpansion);
2806 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002807 return;
2808 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002809
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002810 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002811 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002812 ExprResult ICE
2813 = VerifyIntegerConstantExpression(E, &Alignment,
2814 diag::err_aligned_attribute_argument_not_int,
2815 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002816 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002817 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002818
2819 // C++11 [dcl.align]p2:
2820 // -- if the constant expression evaluates to zero, the alignment
2821 // specifier shall have no effect
2822 // C11 6.7.5p6:
2823 // An alignment specification of zero has no effect.
2824 if (!(TmpAttr.isAlignas() && !Alignment) &&
2825 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002826 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2827 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002828 return;
2829 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002830
Richard Smith848e1f12013-02-01 08:12:08 +00002831 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002832 // We've already verified it's a power of 2, now let's make sure it's
2833 // 8192 or less.
2834 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002835 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002836 << E->getSourceRange();
2837 return;
2838 }
2839 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002840
Richard Smith44c247f2013-02-22 08:32:16 +00002841 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2842 ICE.take(), SpellingListIndex);
2843 AA->setPackExpansion(IsPackExpansion);
2844 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002845}
2846
Michael Hanaf02bbe2013-02-01 01:19:17 +00002847void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002848 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002849 // FIXME: Cache the number on the Attr object if non-dependent?
2850 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002851 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2852 SpellingListIndex);
2853 AA->setPackExpansion(IsPackExpansion);
2854 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002856
Richard Smith848e1f12013-02-01 08:12:08 +00002857void Sema::CheckAlignasUnderalignment(Decl *D) {
2858 assert(D->hasAttrs() && "no attributes on decl");
2859
2860 QualType Ty;
2861 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2862 Ty = VD->getType();
2863 else
2864 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002865 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002866 return;
2867
2868 // C++11 [dcl.align]p5, C11 6.7.5/4:
2869 // The combined effect of all alignment attributes in a declaration shall
2870 // not specify an alignment that is less strict than the alignment that
2871 // would otherwise be required for the entity being declared.
2872 AlignedAttr *AlignasAttr = 0;
2873 unsigned Align = 0;
2874 for (specific_attr_iterator<AlignedAttr>
2875 I = D->specific_attr_begin<AlignedAttr>(),
2876 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2877 if (I->isAlignmentDependent())
2878 return;
2879 if (I->isAlignas())
2880 AlignasAttr = *I;
2881 Align = std::max(Align, I->getAlignment(Context));
2882 }
2883
2884 if (AlignasAttr && Align) {
2885 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2886 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2887 if (NaturalAlign > RequestedAlign)
2888 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2889 << Ty << (unsigned)NaturalAlign.getQuantity();
2890 }
2891}
2892
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002893/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002894/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002895///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002896/// Despite what would be logical, the mode attribute is a decl attribute, not a
2897/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2898/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002899static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002900 // This attribute isn't documented, but glibc uses it. It changes
2901 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002902 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002903 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2904 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002905 return;
2906 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002907
Aaron Ballman00e99962013-08-31 01:11:41 +00002908 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2909 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002910
2911 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002912 if (Str.startswith("__") && Str.endswith("__"))
2913 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002914
2915 unsigned DestWidth = 0;
2916 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002917 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002918 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002919 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002920 switch (Str[0]) {
2921 case 'Q': DestWidth = 8; break;
2922 case 'H': DestWidth = 16; break;
2923 case 'S': DestWidth = 32; break;
2924 case 'D': DestWidth = 64; break;
2925 case 'X': DestWidth = 96; break;
2926 case 'T': DestWidth = 128; break;
2927 }
2928 if (Str[1] == 'F') {
2929 IntegerMode = false;
2930 } else if (Str[1] == 'C') {
2931 IntegerMode = false;
2932 ComplexMode = true;
2933 } else if (Str[1] != 'I') {
2934 DestWidth = 0;
2935 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002936 break;
2937 case 4:
2938 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2939 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002940 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002941 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002942 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002943 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002944 break;
2945 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002946 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002947 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002948 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002949 case 11:
2950 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002951 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002952 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002953 }
2954
2955 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002956 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002957 OldTy = TD->getUnderlyingType();
2958 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2959 OldTy = VD->getType();
2960 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002961 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002962 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002963 return;
2964 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002965
John McCall9dd450b2009-09-21 23:43:11 +00002966 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002967 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2968 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002969 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002970 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2971 } else if (ComplexMode) {
2972 if (!OldTy->isComplexType())
2973 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2974 } else {
2975 if (!OldTy->isFloatingType())
2976 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2977 }
2978
Mike Stump87c57ac2009-05-16 07:39:55 +00002979 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2980 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002981 // FIXME: Make sure floating-point mappings are accurate
2982 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002983 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002984 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002985 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002986 }
2987
2988 QualType NewTy;
2989
2990 if (IntegerMode)
2991 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2992 OldTy->isSignedIntegerType());
2993 else
2994 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2995
2996 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002997 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002998 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002999 }
3000
Eli Friedman4735374e2009-03-03 06:41:03 +00003001 if (ComplexMode) {
3002 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003003 }
3004
3005 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003006 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3007 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3008 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003009 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003010
3011 D->addAttr(::new (S.Context)
3012 ModeAttr(Attr.getRange(), S.Context, Name,
3013 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003014}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003015
Chandler Carruthedc2c642011-07-02 00:01:44 +00003016static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003017 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3018 if (!VD->hasGlobalStorage())
3019 S.Diag(Attr.getLoc(),
3020 diag::warn_attribute_requires_functions_or_static_globals)
3021 << Attr.getName();
3022 } else if (!isFunctionOrMethod(D)) {
3023 S.Diag(Attr.getLoc(),
3024 diag::warn_attribute_requires_functions_or_static_globals)
3025 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003026 return;
3027 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003028
Michael Han99315932013-01-24 16:46:58 +00003029 D->addAttr(::new (S.Context)
3030 NoDebugAttr(Attr.getRange(), S.Context,
3031 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003032}
3033
Chandler Carruthedc2c642011-07-02 00:01:44 +00003034static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003035 FunctionDecl *FD = cast<FunctionDecl>(D);
3036 if (!FD->getResultType()->isVoidType()) {
3037 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3038 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3039 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3040 << FD->getType()
3041 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3042 "void");
3043 } else {
3044 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3045 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003046 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003047 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003048 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003049
Aaron Ballman3aff6332013-12-02 19:30:36 +00003050 D->addAttr(::new (S.Context)
3051 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003052 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003053}
3054
Chandler Carruthedc2c642011-07-02 00:01:44 +00003055static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003056 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003057 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003058 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003059 return;
3060 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003061
Michael Han99315932013-01-24 16:46:58 +00003062 D->addAttr(::new (S.Context)
3063 GNUInlineAttr(Attr.getRange(), S.Context,
3064 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003065}
3066
Chandler Carruthedc2c642011-07-02 00:01:44 +00003067static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003068 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003069
Aaron Ballman02df2e02012-12-09 17:45:41 +00003070 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003071 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003072 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3073 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003074 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003075 return;
3076
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003077 if (!isa<ObjCMethodDecl>(D)) {
3078 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3079 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003080 return;
3081 }
3082
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003083 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003084 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003085 D->addAttr(::new (S.Context)
3086 FastCallAttr(Attr.getRange(), S.Context,
3087 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003088 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003089 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003090 D->addAttr(::new (S.Context)
3091 StdCallAttr(Attr.getRange(), S.Context,
3092 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003093 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003094 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003095 D->addAttr(::new (S.Context)
3096 ThisCallAttr(Attr.getRange(), S.Context,
3097 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003098 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003099 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003100 D->addAttr(::new (S.Context)
3101 CDeclAttr(Attr.getRange(), S.Context,
3102 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003103 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003104 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003105 D->addAttr(::new (S.Context)
3106 PascalAttr(Attr.getRange(), S.Context,
3107 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003108 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003109 case AttributeList::AT_MSABI:
3110 D->addAttr(::new (S.Context)
3111 MSABIAttr(Attr.getRange(), S.Context,
3112 Attr.getAttributeSpellingListIndex()));
3113 return;
3114 case AttributeList::AT_SysVABI:
3115 D->addAttr(::new (S.Context)
3116 SysVABIAttr(Attr.getRange(), S.Context,
3117 Attr.getAttributeSpellingListIndex()));
3118 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003119 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003120 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003121 switch (CC) {
3122 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003123 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003124 break;
3125 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003126 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003127 break;
3128 default:
3129 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003130 }
3131
Michael Han99315932013-01-24 16:46:58 +00003132 D->addAttr(::new (S.Context)
3133 PcsAttr(Attr.getRange(), S.Context, PCS,
3134 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003135 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003136 }
Derek Schuffa2020962012-10-16 22:30:41 +00003137 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003138 D->addAttr(::new (S.Context)
3139 PnaclCallAttr(Attr.getRange(), S.Context,
3140 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003141 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003142 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003143 D->addAttr(::new (S.Context)
3144 IntelOclBiccAttr(Attr.getRange(), S.Context,
3145 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003146 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003147
Abramo Bagnara50099372010-04-30 13:10:51 +00003148 default:
3149 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003150 }
3151}
3152
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003153static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3154 const AttributeList &Attr) {
3155 uint32_t ArgNum;
3156 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003157 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003158
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003159 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3160 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003161}
3162
Aaron Ballman02df2e02012-12-09 17:45:41 +00003163bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3164 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003165 if (attr.isInvalid())
3166 return true;
3167
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003168 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003169 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003170 attr.setInvalid();
3171 return true;
3172 }
3173
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003174 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3175 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003176 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003177 case AttributeList::AT_CDecl: CC = CC_C; break;
3178 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3179 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3180 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3181 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003182 case AttributeList::AT_MSABI:
3183 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3184 CC_X86_64Win64;
3185 break;
3186 case AttributeList::AT_SysVABI:
3187 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3188 CC_C;
3189 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003190 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003191 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003192 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003193 attr.setInvalid();
3194 return true;
3195 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003196 if (StrRef == "aapcs") {
3197 CC = CC_AAPCS;
3198 break;
3199 } else if (StrRef == "aapcs-vfp") {
3200 CC = CC_AAPCS_VFP;
3201 break;
3202 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003203
3204 attr.setInvalid();
3205 Diag(attr.getLoc(), diag::err_invalid_pcs);
3206 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003207 }
Derek Schuffa2020962012-10-16 22:30:41 +00003208 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003209 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003210 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003211 }
3212
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003213 const TargetInfo &TI = Context.getTargetInfo();
3214 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3215 if (A == TargetInfo::CCCR_Warning) {
3216 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003217
3218 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3219 if (FD)
3220 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3221 TargetInfo::CCMT_NonMember;
3222 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003223 }
3224
John McCall3882ace2011-01-05 12:14:39 +00003225 return false;
3226}
3227
Chandler Carruthedc2c642011-07-02 00:01:44 +00003228static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003229 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003230
3231 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003232 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003233 return;
3234
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003235 if (!isa<ObjCMethodDecl>(D)) {
3236 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3237 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003238 return;
3239 }
Eli Friedman7044b762009-03-27 21:06:47 +00003240
Michael Han99315932013-01-24 16:46:58 +00003241 D->addAttr(::new (S.Context)
3242 RegparmAttr(Attr.getRange(), S.Context, numParams,
3243 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003244}
3245
3246/// Checks a regparm attribute, returning true if it is ill-formed and
3247/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003248bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3249 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003250 return true;
3251
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003252 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003253 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003254 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003255 }
Eli Friedman7044b762009-03-27 21:06:47 +00003256
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003257 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003258 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003259 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
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
Douglas Gregore8bbc122011-09-02 00:18:52 +00003264 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003265 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003266 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003267 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003268 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003269 }
3270
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003271 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003272 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003273 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003274 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003275 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003276 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003277 }
3278
John McCall3882ace2011-01-05 12:14:39 +00003279 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003280}
3281
Aaron Ballman66039932013-12-19 00:41:31 +00003282static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3283 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003284 // check the attribute arguments.
3285 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3286 // FIXME: 0 is not okay.
3287 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3288 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003289 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003290
3291 if (!isFunctionOrMethod(D)) {
3292 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3293 << Attr.getName() << ExpectedFunctionOrMethod;
3294 return;
3295 }
3296
Aaron Ballman66039932013-12-19 00:41:31 +00003297 uint32_t MaxThreads, MinBlocks = 0;
3298 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3299 return;
3300 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3301 Attr.getArgAsExpr(1),
3302 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003303 return;
3304
3305 D->addAttr(::new (S.Context)
3306 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3307 MaxThreads, MinBlocks,
3308 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003309}
3310
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003311static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3312 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003313 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003315 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003316 return;
3317 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003318
3319 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003320 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003321
Aaron Ballman00e99962013-08-31 01:11:41 +00003322 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323
3324 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3326 << Attr.getName() << ExpectedFunctionOrMethod;
3327 return;
3328 }
3329
3330 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003331 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3332 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003333 return;
3334
3335 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003336 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3337 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003338 return;
3339
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003340 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003341 if (IsPointer) {
3342 // Ensure that buffer has a pointer type.
3343 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3344 if (!BufferTy->isPointerType()) {
3345 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003346 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003347 }
3348 }
3349
Michael Han99315932013-01-24 16:46:58 +00003350 D->addAttr(::new (S.Context)
3351 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3352 ArgumentIdx, TypeTagIdx, IsPointer,
3353 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003354}
3355
3356static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3357 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003358 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003359 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003360 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003361 return;
3362 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003363
3364 if (!checkAttributeNumArgs(S, Attr, 1))
3365 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003366
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003367 if (!isa<VarDecl>(D)) {
3368 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3369 << Attr.getName() << ExpectedVariable;
3370 return;
3371 }
3372
Aaron Ballman00e99962013-08-31 01:11:41 +00003373 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003374 TypeSourceInfo *MatchingCTypeLoc = 0;
3375 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3376 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003377
Michael Han99315932013-01-24 16:46:58 +00003378 D->addAttr(::new (S.Context)
3379 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003380 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003381 Attr.getLayoutCompatible(),
3382 Attr.getMustBeNull(),
3383 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003384}
3385
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003386//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003387// Checker-specific attribute handlers.
3388//===----------------------------------------------------------------------===//
3389
John McCalled433932011-01-25 03:31:58 +00003390static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003391 return type->isDependentType() ||
3392 type->isObjCObjectPointerType() ||
3393 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003394}
3395static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003396 return type->isDependentType() ||
3397 type->isPointerType() ||
3398 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003399}
3400
Chandler Carruthedc2c642011-07-02 00:01:44 +00003401static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003402 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003403 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003404
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003405 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003406 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3407 cf = false;
3408 } else {
3409 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3410 cf = true;
3411 }
3412
3413 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003414 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003415 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003416 return;
3417 }
3418
3419 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003420 param->addAttr(::new (S.Context)
3421 CFConsumedAttr(Attr.getRange(), S.Context,
3422 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003423 else
Michael Han99315932013-01-24 16:46:58 +00003424 param->addAttr(::new (S.Context)
3425 NSConsumedAttr(Attr.getRange(), S.Context,
3426 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003427}
3428
Chandler Carruthedc2c642011-07-02 00:01:44 +00003429static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3430 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003431
John McCalled433932011-01-25 03:31:58 +00003432 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003433
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003434 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003435 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003436 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003437 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003438 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003439 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3440 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003441 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003442 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003443 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003444 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003445 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003446 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003447 return;
3448 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003449
John McCalled433932011-01-25 03:31:58 +00003450 bool typeOK;
3451 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003453 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003454 case AttributeList::AT_NSReturnsAutoreleased:
3455 case AttributeList::AT_NSReturnsRetained:
3456 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003457 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3458 cf = false;
3459 break;
3460
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003461 case AttributeList::AT_CFReturnsRetained:
3462 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003463 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3464 cf = true;
3465 break;
3466 }
3467
3468 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003469 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003470 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003471 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003472 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003473
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003474 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003475 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003476 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003477 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003478 D->addAttr(::new (S.Context)
3479 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3480 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003481 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003482 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003483 D->addAttr(::new (S.Context)
3484 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3485 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003486 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003487 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003488 D->addAttr(::new (S.Context)
3489 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3490 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003491 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003492 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003493 D->addAttr(::new (S.Context)
3494 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3495 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003496 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003497 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003498 D->addAttr(::new (S.Context)
3499 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3500 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003501 return;
3502 };
3503}
3504
John McCallcf166702011-07-22 08:53:00 +00003505static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3506 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003507 const int EP_ObjCMethod = 1;
3508 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003509
John McCallcf166702011-07-22 08:53:00 +00003510 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003511 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003512 if (isa<ObjCMethodDecl>(D))
3513 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003514 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003515 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003516
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003517 if (!resultType->isReferenceType() &&
3518 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003519 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003520 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003521 << attr.getName()
3522 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003523 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003524
3525 // Drop the attribute.
3526 return;
3527 }
3528
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003529 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003530 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3531 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003532}
3533
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003534static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3535 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003536 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003537
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003538 DeclContext *DC = method->getDeclContext();
3539 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3540 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3541 << attr.getName() << 0;
3542 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3543 return;
3544 }
3545 if (method->getMethodFamily() == OMF_dealloc) {
3546 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3547 << attr.getName() << 1;
3548 return;
3549 }
3550
Michael Han99315932013-01-24 16:46:58 +00003551 method->addAttr(::new (S.Context)
3552 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3553 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003554}
3555
Aaron Ballmanfb763042013-12-02 18:05:46 +00003556static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3557 const AttributeList &Attr) {
3558 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3559 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003560 return;
John McCall32f5fe12011-09-30 05:12:12 +00003561
Aaron Ballmanfb763042013-12-02 18:05:46 +00003562 D->addAttr(::new (S.Context)
3563 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3564 Attr.getAttributeSpellingListIndex()));
3565}
3566
3567static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3568 const AttributeList &Attr) {
3569 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3570 "cf_audited_transfer"))
3571 return;
3572
3573 D->addAttr(::new (S.Context)
3574 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3575 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003576}
3577
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003578static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3579 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003580 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003581
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003582 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003583 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003584 return;
3585 }
3586
3587 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003588 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003589 Attr.getAttributeSpellingListIndex()));
3590}
3591
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003592static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3593 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003594 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003595
3596 if (!Parm) {
3597 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3598 return;
3599 }
3600
3601 D->addAttr(::new (S.Context)
3602 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3603 Attr.getAttributeSpellingListIndex()));
3604}
3605
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003606static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3607 const AttributeList &Attr) {
3608 IdentifierInfo *RelatedClass =
3609 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3610 if (!RelatedClass) {
3611 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3612 return;
3613 }
3614 IdentifierInfo *ClassMethod =
3615 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3616 IdentifierInfo *InstanceMethod =
3617 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3618 D->addAttr(::new (S.Context)
3619 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3620 ClassMethod, InstanceMethod,
3621 Attr.getAttributeSpellingListIndex()));
3622}
3623
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003624static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3625 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003626 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003627 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003628 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003629 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3630 Attr.getAttributeSpellingListIndex()));
3631}
3632
Chandler Carruthedc2c642011-07-02 00:01:44 +00003633static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3634 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003635 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003636
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003637 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003638 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003639}
3640
Chandler Carruthedc2c642011-07-02 00:01:44 +00003641static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3642 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003643 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003644 QualType type = vd->getType();
3645
3646 if (!type->isDependentType() &&
3647 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003648 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003649 << type;
3650 return;
3651 }
3652
3653 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3654
3655 // If we have no lifetime yet, check the lifetime we're presumably
3656 // going to infer.
3657 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3658 lifetime = type->getObjCARCImplicitLifetime();
3659
3660 switch (lifetime) {
3661 case Qualifiers::OCL_None:
3662 assert(type->isDependentType() &&
3663 "didn't infer lifetime for non-dependent type?");
3664 break;
3665
3666 case Qualifiers::OCL_Weak: // meaningful
3667 case Qualifiers::OCL_Strong: // meaningful
3668 break;
3669
3670 case Qualifiers::OCL_ExplicitNone:
3671 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003672 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003673 << (lifetime == Qualifiers::OCL_Autoreleasing);
3674 break;
3675 }
3676
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003677 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003678 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3679 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003680}
3681
Francois Picheta83957a2010-12-19 06:50:37 +00003682//===----------------------------------------------------------------------===//
3683// Microsoft specific attribute handlers.
3684//===----------------------------------------------------------------------===//
3685
Chandler Carruthedc2c642011-07-02 00:01:44 +00003686static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003687 if (!S.LangOpts.CPlusPlus) {
3688 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3689 << Attr.getName() << AttributeLangSupport::C;
3690 return;
3691 }
3692
Aaron Ballman60e705e2013-11-24 20:58:02 +00003693 if (!isa<CXXRecordDecl>(D)) {
3694 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3695 << Attr.getName() << ExpectedClass;
3696 return;
3697 }
3698
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003699 StringRef StrRef;
3700 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003701 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003702 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003703
David Majnemer89085342013-08-09 08:56:20 +00003704 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3705 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003706 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3707 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003708
Reid Kleckner140c4a72013-05-17 14:04:52 +00003709 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003710 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003711 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003712 return;
3713 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003714
David Majnemer89085342013-08-09 08:56:20 +00003715 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003716 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003717 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003718 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003719 return;
3720 }
David Majnemer89085342013-08-09 08:56:20 +00003721 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003722 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003723 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003724 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003725 }
Francois Picheta83957a2010-12-19 06:50:37 +00003726
David Majnemer89085342013-08-09 08:56:20 +00003727 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3728 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003729}
3730
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003731/// Handles semantic checking for features that are common to all attributes,
3732/// such as checking whether a parameter was properly specified, or the correct
3733/// number of arguments were passed, etc.
3734static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3735 const AttributeList &Attr) {
3736 // Several attributes carry different semantics than the parsing requires, so
3737 // those are opted out of the common handling.
3738 //
3739 // We also bail on unknown and ignored attributes because those are handled
3740 // as part of the target-specific handling logic.
3741 if (Attr.hasCustomParsing() ||
3742 Attr.getKind() == AttributeList::UnknownAttribute ||
3743 Attr.getKind() == AttributeList::IgnoredAttribute)
3744 return false;
3745
Aaron Ballman3aff6332013-12-02 19:30:36 +00003746 // Check whether the attribute requires specific language extensions to be
3747 // enabled.
3748 if (!Attr.diagnoseLangOpts(S))
3749 return true;
3750
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003751 // If there are no optional arguments, then checking for the argument count
3752 // is trivial.
3753 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3754 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3755 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003756
3757 // Check whether the attribute appertains to the given subject.
3758 if (!Attr.diagnoseAppertainsTo(S, D))
3759 return true;
3760
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003761 return false;
3762}
3763
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003764//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003765// Top Level Sema Entry Points
3766//===----------------------------------------------------------------------===//
3767
Richard Smithf8a75c32013-08-29 00:47:48 +00003768/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3769/// the attribute applies to decls. If the attribute is a type attribute, just
3770/// silently ignore it if a GNU attribute.
3771static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3772 const AttributeList &Attr,
3773 bool IncludeCXX11Attributes) {
3774 if (Attr.isInvalid())
3775 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003776
Richard Smithf8a75c32013-08-29 00:47:48 +00003777 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3778 // instead.
3779 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3780 return;
3781
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003782 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3783 return;
3784
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003785 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003786 case AttributeList::AT_IBAction:
3787 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003788 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3789 case AttributeList::AT_IBOutletCollection:
3790 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003791 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003792 case AttributeList::AT_ObjCGC:
3793 case AttributeList::AT_VectorSize:
3794 case AttributeList::AT_NeonVectorType:
3795 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003796 case AttributeList::AT_Ptr32:
3797 case AttributeList::AT_Ptr64:
3798 case AttributeList::AT_SPtr:
3799 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003800 // Ignore these, these are type attributes, handled by
3801 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003802 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003803 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3804 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003805 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003806 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003807 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003808 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003809 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003810 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3811 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3812 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003813 handleDependencyAttr(S, scope, D, Attr);
3814 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003815 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003816 case AttributeList::AT_CUDAConstant:
3817 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003818 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003819 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003820 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003821 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003822 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003823 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003824 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3825 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003826 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003827 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003828 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003829 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003830 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003831 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3832 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3833 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003834 case AttributeList::AT_CUDADevice:
3835 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003836 case AttributeList::AT_CUDAHost:
3837 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003838 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3839 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003840 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003841 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003842 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003843 case AttributeList::AT_MayAlias:
3844 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003845 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003846 case AttributeList::AT_NoCommon:
3847 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003848 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003849 case AttributeList::AT_Overloadable:
3850 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003851 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003852 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3853 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003854 case AttributeList::AT_Naked:
3855 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003856 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003857 case AttributeList::AT_NoThrow:
3858 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003859 case AttributeList::AT_CUDAShared:
3860 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003861 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003862
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003863 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003864 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003865 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003866 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003867
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003868 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003869 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3870
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003871 case AttributeList::AT_ObjCRequiresSuper:
3872 handleObjCRequiresSuperAttr(S, D, Attr); break;
3873
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003874 case AttributeList::AT_ObjCBridge:
3875 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003876
3877 case AttributeList::AT_ObjCBridgeMutable:
3878 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003879
3880 case AttributeList::AT_ObjCBridgeRelated:
3881 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003882
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003883 case AttributeList::AT_ObjCDesignatedInitializer:
3884 handleObjCDesignatedInitializer(S, D, Attr); break;
3885
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003886 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003887 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003888 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003889 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003890
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003891 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003892 case AttributeList::AT_CFConsumed:
3893 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3894 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003895 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003896
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003897 case AttributeList::AT_NSReturnsAutoreleased:
3898 case AttributeList::AT_NSReturnsNotRetained:
3899 case AttributeList::AT_CFReturnsNotRetained:
3900 case AttributeList::AT_NSReturnsRetained:
3901 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003902 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00003903 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003904 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003905 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003906 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00003907 case AttributeList::AT_VecTypeHint:
3908 handleVecTypeHint(S, D, Attr); break;
3909
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003910 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003911 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003912
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003913 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
3914 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
3915 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003916 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003917 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003918 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003919 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003920 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003921 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00003922 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00003923 handleObjCSuppresProtocolAttr(S, D, Attr);
3924 break;
3925 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003926 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003927 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
3928 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003929 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003930 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00003931 case AttributeList::AT_Visibility:
3932 handleVisibilityAttr(S, D, Attr, false);
3933 break;
3934 case AttributeList::AT_TypeVisibility:
3935 handleVisibilityAttr(S, D, Attr, true);
3936 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00003937 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00003938 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003939 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003940 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00003941 case AttributeList::AT_Weak:
3942 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003943 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
3944 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
3945 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003946 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003947 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003948 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003949 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003951 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003952 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003953 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
3954 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
3955 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003956 case AttributeList::AT_Const:
3957 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003958 case AttributeList::AT_Pure:
3959 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
3961 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003962 case AttributeList::AT_NoInline:
3963 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003964 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003965 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003966 // Just ignore
3967 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003968 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003969 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003970 case AttributeList::AT_StdCall:
3971 case AttributeList::AT_CDecl:
3972 case AttributeList::AT_FastCall:
3973 case AttributeList::AT_ThisCall:
3974 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00003975 case AttributeList::AT_MSABI:
3976 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003977 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00003978 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00003979 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003980 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003981 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003982 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003983 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003984 case AttributeList::AT_OpenCLImageAccess:
3985 handleOpenCLImageAccessAttr(S, D, Attr);
3986 break;
John McCall8d32c052012-05-22 21:28:12 +00003987
3988 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003989 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003990 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00003991 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003993 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003994 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00003995 case AttributeList::AT_MSInheritance:
3996 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00003998 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00003999 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004000 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004001
4002 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004003 case AttributeList::AT_AssertExclusiveLock:
4004 handleAssertExclusiveLockAttr(S, D, Attr);
4005 break;
4006 case AttributeList::AT_AssertSharedLock:
4007 handleAssertSharedLockAttr(S, D, Attr);
4008 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004009 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004010 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004011 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004012 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004013 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004014 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004015 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004016 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004017 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004018 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004019 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004020 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004021 break;
4022 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004023 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004024 break;
4025 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004026 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004027 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004028 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004029 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004030 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004031 handleGuardedByAttr(S, D, Attr);
4032 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004033 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004034 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004035 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004036 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004037 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004038 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004039 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004040 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004041 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004042 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004043 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004044 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004046 handleLockReturnedAttr(S, D, Attr);
4047 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004048 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004049 handleLocksExcludedAttr(S, D, Attr);
4050 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004051 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004052 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004053 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004054 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004055 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004056 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004057 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004058 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004059 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004060 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004061 handleUnlockFunAttr(S, D, Attr);
4062 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004063 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004064 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004065 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004066 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004067 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004068 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004069
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004070 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004071 case AttributeList::AT_Consumable:
4072 handleConsumableAttr(S, D, Attr);
4073 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004074 case AttributeList::AT_CallableWhen:
4075 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004076 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004077 case AttributeList::AT_ParamTypestate:
4078 handleParamTypestateAttr(S, D, Attr);
4079 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004080 case AttributeList::AT_ReturnTypestate:
4081 handleReturnTypestateAttr(S, D, Attr);
4082 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004083 case AttributeList::AT_SetTypestate:
4084 handleSetTypestateAttr(S, D, Attr);
4085 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004086 case AttributeList::AT_TestTypestate:
4087 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004088 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004089
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004090 // Type safety attributes.
4091 case AttributeList::AT_ArgumentWithTypeTag:
4092 handleArgumentWithTypeTagAttr(S, D, Attr);
4093 break;
4094 case AttributeList::AT_TypeTagForDatatype:
4095 handleTypeTagForDatatypeAttr(S, D, Attr);
4096 break;
4097
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004098 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004099 // Ask target about the attribute.
4100 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4101 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004102 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4103 diag::warn_unhandled_ms_attribute_ignored :
4104 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004105 break;
4106 }
4107}
4108
4109/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4110/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004111void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004112 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004113 bool IncludeCXX11Attributes) {
4114 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004115 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004116
Joey Gouly2cd9db12013-12-13 16:15:28 +00004117 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004118 // GCC accepts
4119 // static int a9 __attribute__((weakref));
4120 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004121 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004122 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004123 cast<NamedDecl>(D)->getNameAsString();
4124 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004125 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004126 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004127
4128 if (!D->hasAttr<OpenCLKernelAttr>()) {
4129 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004130 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4131 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004132 D->setInvalidDecl();
4133 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004134 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4135 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004136 D->setInvalidDecl();
4137 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004138 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4139 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004140 D->setInvalidDecl();
4141 }
4142 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004143}
4144
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004145// Annotation attributes are the only attributes allowed after an access
4146// specifier.
4147bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4148 const AttributeList *AttrList) {
4149 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004150 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004151 handleAnnotateAttr(*this, ASDecl, *l);
4152 } else {
4153 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4154 return true;
4155 }
4156 }
4157
4158 return false;
4159}
4160
John McCall42856de2011-10-01 05:17:03 +00004161/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4162/// contains any decl attributes that we should warn about.
4163static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4164 for ( ; A; A = A->getNext()) {
4165 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004166 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004167 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4168
4169 if (A->getKind() == AttributeList::UnknownAttribute) {
4170 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4171 << A->getName() << A->getRange();
4172 } else {
4173 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4174 << A->getName() << A->getRange();
4175 }
4176 }
4177}
4178
4179/// checkUnusedDeclAttributes - Given a declarator which is not being
4180/// used to build a declaration, complain about any decl attributes
4181/// which might be lying around on it.
4182void Sema::checkUnusedDeclAttributes(Declarator &D) {
4183 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4184 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4185 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4186 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4187}
4188
Ryan Flynn7d470f32009-07-30 03:15:39 +00004189/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004190/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004191NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4192 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004193 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004194 NamedDecl *NewD = 0;
4195 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004196 FunctionDecl *NewFD;
4197 // FIXME: Missing call to CheckFunctionDeclaration().
4198 // FIXME: Mangling?
4199 // FIXME: Is the qualifier info correct?
4200 // FIXME: Is the DeclContext correct?
4201 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4202 Loc, Loc, DeclarationName(II),
4203 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004204 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004205 FD->hasPrototype(),
4206 false/*isConstexprSpecified*/);
4207 NewD = NewFD;
4208
4209 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004210 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004211
4212 // Fake up parameter variables; they are declared as if this were
4213 // a typedef.
4214 QualType FDTy = FD->getType();
4215 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4216 SmallVector<ParmVarDecl*, 16> Params;
4217 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4218 AE = FT->arg_type_end(); AI != AE; ++AI) {
4219 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4220 Param->setScopeInfo(0, Params.size());
4221 Params.push_back(Param);
4222 }
David Blaikie9c70e042011-09-21 18:16:56 +00004223 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004224 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004225 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4226 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004227 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004228 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004229 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004230 if (VD->getQualifier()) {
4231 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004232 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004233 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004234 }
4235 return NewD;
4236}
4237
James Dennett634962f2012-06-14 21:40:34 +00004238/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004239/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004240void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004241 if (W.getUsed()) return; // only do this once
4242 W.setUsed(true);
4243 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4244 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004245 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004246 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4247 NDId->getName()));
4248 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004249 WeakTopLevelDecl.push_back(NewD);
4250 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4251 // to insert Decl at TU scope, sorry.
4252 DeclContext *SavedContext = CurContext;
4253 CurContext = Context.getTranslationUnitDecl();
4254 PushOnScopeChains(NewD, S);
4255 CurContext = SavedContext;
4256 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004257 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004258 }
4259}
4260
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004261void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4262 // It's valid to "forward-declare" #pragma weak, in which case we
4263 // have to do this.
4264 LoadExternalWeakUndeclaredIdentifiers();
4265 if (!WeakUndeclaredIdentifiers.empty()) {
4266 NamedDecl *ND = NULL;
4267 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4268 if (VD->isExternC())
4269 ND = VD;
4270 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4271 if (FD->isExternC())
4272 ND = FD;
4273 if (ND) {
4274 if (IdentifierInfo *Id = ND->getIdentifier()) {
4275 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4276 = WeakUndeclaredIdentifiers.find(Id);
4277 if (I != WeakUndeclaredIdentifiers.end()) {
4278 WeakInfo W = I->second;
4279 DeclApplyPragmaWeak(S, ND, W);
4280 WeakUndeclaredIdentifiers[Id] = W;
4281 }
4282 }
4283 }
4284 }
4285}
4286
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004287/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4288/// it, apply them to D. This is a bit tricky because PD can have attributes
4289/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004290void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004291 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004292 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004293 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004294
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004295 // Walk the declarator structure, applying decl attributes that were in a type
4296 // position to the decl itself. This handles cases like:
4297 // int *__attr__(x)** D;
4298 // when X is a decl attribute.
4299 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4300 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004301 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004302
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004303 // Finally, apply any attributes on the decl itself.
4304 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004305 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004306}
John McCall28a6aea2009-11-04 02:18:39 +00004307
John McCall31168b02011-06-15 23:02:42 +00004308/// Is the given declaration allowed to use a forbidden type?
4309static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4310 // Private ivars are always okay. Unfortunately, people don't
4311 // always properly make their ivars private, even in system headers.
4312 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004313 // Function declarations in sys headers will be marked unavailable.
4314 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4315 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004316 return false;
4317
4318 // Require it to be declared in a system header.
4319 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4320}
4321
4322/// Handle a delayed forbidden-type diagnostic.
4323static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4324 Decl *decl) {
4325 if (decl && isForbiddenTypeAllowed(S, decl)) {
4326 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4327 "this system declaration uses an unsupported type"));
4328 return;
4329 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004330 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004331 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004332 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004333 // kind of forbidden type messages on unavailable functions.
4334 if (FD->hasAttr<UnavailableAttr>() &&
4335 diag.getForbiddenTypeDiagnostic() ==
4336 diag::err_arc_array_param_no_ownership) {
4337 diag.Triggered = true;
4338 return;
4339 }
4340 }
John McCall31168b02011-06-15 23:02:42 +00004341
4342 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4343 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4344 diag.Triggered = true;
4345}
4346
John McCall2ec85372012-05-07 06:16:41 +00004347void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4348 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004349 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004350 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004351
John McCall2ec85372012-05-07 06:16:41 +00004352 // When delaying diagnostics to run in the context of a parsed
4353 // declaration, we only want to actually emit anything if parsing
4354 // succeeds.
4355 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004356
John McCall2ec85372012-05-07 06:16:41 +00004357 // We emit all the active diagnostics in this pool or any of its
4358 // parents. In general, we'll get one pool for the decl spec
4359 // and a child pool for each declarator; in a decl group like:
4360 // deprecated_typedef foo, *bar, baz();
4361 // only the declarator pops will be passed decls. This is correct;
4362 // we really do need to consider delayed diagnostics from the decl spec
4363 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004364 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004365 do {
John McCall6347b682012-05-07 06:16:58 +00004366 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004367 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4368 // This const_cast is a bit lame. Really, Triggered should be mutable.
4369 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004370 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004371 continue;
4372
John McCallc1465822011-02-14 07:13:47 +00004373 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004374 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004375 case DelayedDiagnostic::Unavailable:
4376 // Don't bother giving deprecation/unavailable diagnostics if
4377 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004378 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004379 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004380 break;
4381
4382 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004383 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004384 break;
John McCall31168b02011-06-15 23:02:42 +00004385
4386 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004387 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004388 break;
John McCall86121512010-01-27 03:50:35 +00004389 }
4390 }
John McCall2ec85372012-05-07 06:16:41 +00004391 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004392}
4393
John McCall6347b682012-05-07 06:16:58 +00004394/// Given a set of delayed diagnostics, re-emit them as if they had
4395/// been delayed in the current context instead of in the given pool.
4396/// Essentially, this just moves them to the current pool.
4397void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4398 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4399 assert(curPool && "re-emitting in undelayed context not supported");
4400 curPool->steal(pool);
4401}
4402
John McCall28a6aea2009-11-04 02:18:39 +00004403static bool isDeclDeprecated(Decl *D) {
4404 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004405 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004406 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004407 // A category implicitly has the availability of the interface.
4408 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4409 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004410 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4411 return false;
4412}
4413
Ted Kremenekb79ee572013-12-18 23:30:06 +00004414static bool isDeclUnavailable(Decl *D) {
4415 do {
4416 if (D->isUnavailable())
4417 return true;
4418 // A category implicitly has the availability of the interface.
4419 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4420 return CatD->getClassInterface()->isUnavailable();
4421 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4422 return false;
4423}
4424
Eli Friedman971bfa12012-08-08 21:52:41 +00004425static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004426DoEmitAvailabilityWarning(Sema &S,
4427 DelayedDiagnostic::DDKind K,
4428 Decl *Ctx,
4429 const NamedDecl *D,
4430 StringRef Message,
4431 SourceLocation Loc,
4432 const ObjCInterfaceDecl *UnknownObjCClass,
4433 const ObjCPropertyDecl *ObjCProperty) {
4434
4435 // Diagnostics for deprecated or unavailable.
4436 unsigned diag, diag_message, diag_fwdclass_message;
4437
4438 // Matches 'diag::note_property_attribute' options.
4439 unsigned property_note_select;
4440
4441 // Matches diag::note_availability_specified_here.
4442 unsigned available_here_select_kind;
4443
4444 // Don't warn if our current context is deprecated or unavailable.
4445 switch (K) {
4446 case DelayedDiagnostic::Deprecation:
4447 if (isDeclDeprecated(Ctx))
4448 return;
4449 diag = diag::warn_deprecated;
4450 diag_message = diag::warn_deprecated_message;
4451 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4452 property_note_select = /* deprecated */ 0;
4453 available_here_select_kind = /* deprecated */ 2;
4454 break;
4455
4456 case DelayedDiagnostic::Unavailable:
4457 if (isDeclUnavailable(Ctx))
4458 return;
4459 diag = diag::err_unavailable;
4460 diag_message = diag::err_unavailable_message;
4461 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4462 property_note_select = /* unavailable */ 1;
4463 available_here_select_kind = /* unavailable */ 0;
4464 break;
4465
4466 default:
4467 llvm_unreachable("Neither a deprecation or unavailable kind");
4468 }
4469
Eli Friedman971bfa12012-08-08 21:52:41 +00004470 DeclarationName Name = D->getDeclName();
4471 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004472 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004473 if (ObjCProperty)
4474 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4475 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004476 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004477 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004478 if (ObjCProperty)
4479 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4480 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004481 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004482 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004483 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4484 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004485
4486 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4487 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004488}
4489
Ted Kremenekb79ee572013-12-18 23:30:06 +00004490void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4491 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004492 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004493 DoEmitAvailabilityWarning(*this,
4494 (DelayedDiagnostic::DDKind) DD.Kind,
4495 Ctx,
4496 DD.getDeprecationDecl(),
4497 DD.getDeprecationMessage(),
4498 DD.Loc,
4499 DD.getUnknownObjCClass(),
4500 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004501}
4502
Ted Kremenekb79ee572013-12-18 23:30:06 +00004503void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4504 NamedDecl *D, StringRef Message,
4505 SourceLocation Loc,
4506 const ObjCInterfaceDecl *UnknownObjCClass,
4507 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004508 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004509 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004510 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4511 UnknownObjCClass,
4512 ObjCProperty,
4513 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004514 return;
4515 }
4516
Ted Kremenekb79ee572013-12-18 23:30:06 +00004517 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4518 DelayedDiagnostic::DDKind K;
4519 switch (AD) {
4520 case AD_Deprecation:
4521 K = DelayedDiagnostic::Deprecation;
4522 break;
4523 case AD_Unavailable:
4524 K = DelayedDiagnostic::Unavailable;
4525 break;
4526 }
4527
4528 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4529 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004530}