blob: 87c38eef4150229064d17b64f3c6cbf87e0fca23 [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,
264 StringRef AttrName,
265 SourceLocation AttrLoc,
266 unsigned AttrArgNum,
267 const Expr *IdxExpr,
268 uint64_t &Idx)
269{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000270 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000271
272 // In C++ the implicit 'this' function parameter also counts.
273 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000274 bool HP = hasFunctionProto(D);
275 bool HasImplicitThisParam = isInstanceMethod(D);
276 bool IV = HP && isFunctionOrMethodVariadic(D);
277 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
278 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000279
280 llvm::APSInt IdxInt;
281 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
282 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000283 std::string Name = std::string("'") + AttrName.str() + std::string("'");
284 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000285 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286 return false;
287 }
288
289 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000290 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
292 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
293 return false;
294 }
295 Idx--; // Convert to zero-based.
296 if (HasImplicitThisParam) {
297 if (Idx == 0) {
298 S.Diag(AttrLoc,
299 diag::err_attribute_invalid_implicit_this_argument)
300 << AttrName << IdxExpr->getSourceRange();
301 return false;
302 }
303 --Idx;
304 }
305
306 return true;
307}
308
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000309/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
310/// If not emit an error and return false. If the argument is an identifier it
311/// will emit an error with a fixit hint and treat it as if it was a string
312/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000313bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
314 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000315 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000316 // Look for identifiers. If we have one emit a hint to fix it to a literal.
317 if (Attr.isArgIdent(ArgNum)) {
318 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000319 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000320 << Attr.getName() << AANT_ArgumentString
321 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000322 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000323 Str = Loc->Ident->getName();
324 if (ArgLocation)
325 *ArgLocation = Loc->Loc;
326 return true;
327 }
328
329 // Now check for an actual string literal.
330 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
331 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
332 if (ArgLocation)
333 *ArgLocation = ArgExpr->getLocStart();
334
335 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000336 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000337 << Attr.getName() << AANT_ArgumentString;
338 return false;
339 }
340
341 Str = Literal->getString();
342 return true;
343}
344
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000345/// \brief Applies the given attribute to the Decl without performing any
346/// additional semantic checking.
347template <typename AttrType>
348static void handleSimpleAttribute(Sema &S, Decl *D,
349 const AttributeList &Attr) {
350 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
351 Attr.getAttributeSpellingListIndex()));
352}
353
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000354/// \brief Check if the passed-in expression is of type int or bool.
355static bool isIntOrBool(Expr *Exp) {
356 QualType QT = Exp->getType();
357 return QT->isBooleanType() || QT->isIntegerType();
358}
359
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000360
361// Check to see if the type is a smart pointer of some kind. We assume
362// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000363static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
364 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
365 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000366 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000367 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000368
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000369 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
370 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000371 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000372 return false;
373
374 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000375}
376
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000377/// \brief Check if passed in Decl is a pointer type.
378/// Note that this function may produce an error message.
379/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000380static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
381 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000382 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000383 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000384 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000385 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000386
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000387 if (const RecordType *RT = QT->getAs<RecordType>()) {
388 // If it's an incomplete type, it could be a smart pointer; skip it.
389 // (We don't want to force template instantiation if we can avoid it,
390 // since that would alter the order in which templates are instantiated.)
391 if (RT->isIncompleteType())
392 return true;
393
394 if (threadSafetyCheckIsSmartPointer(S, RT))
395 return true;
396 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000397
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000398 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000399 << Attr.getName()->getName() << QT;
400 } else {
401 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
402 << Attr.getName();
403 }
404 return false;
405}
406
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000407/// \brief Checks that the passed in QualType either is of RecordType or points
408/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000409static const RecordType *getRecordType(QualType QT) {
410 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000411 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000412
413 // Now check if we point to record type.
414 if (const PointerType *PT = QT->getAs<PointerType>())
415 return PT->getPointeeType()->getAs<RecordType>();
416
417 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418}
419
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000420
Jordy Rose740b0c22012-05-08 03:27:22 +0000421static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
422 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000423 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000424 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000425}
426
427
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000428/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000429/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000430static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
431 QualType Ty) {
432 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000433
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000435 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000436 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000437 << Attr.getName() << Ty.getAsString();
438 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000439 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000440
Michael Hana9171bc2012-08-03 17:40:43 +0000441 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000442 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000443 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000444
445 // Allow smart pointers to be used as lockable objects.
446 // FIXME -- Check the type that the smart pointer points to.
447 if (threadSafetyCheckIsSmartPointer(S, RT))
448 return;
449
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000450 // Check if the type is lockable.
451 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000452 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000453 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000454
455 // Else check if any base classes are lockable.
456 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
457 CXXBasePaths BPaths(false, false);
458 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
459 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000461
462 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
463 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000464}
465
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000467/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000468/// \param Sidx The attribute argument index to start checking with.
469/// \param ParamIdxOk Whether an argument can be indexing into a function
470/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000471static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000472 const AttributeList &Attr,
473 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474 int Sidx = 0,
475 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000477 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000478
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000479 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000480 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000481 Args.push_back(ArgExp);
482 continue;
483 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000484
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000485 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000486 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000487 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000488 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000489 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000490 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000491 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000492 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000493
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000494 // We allow constant strings to be used as a placeholder for expressions
495 // that are not valid C++ syntax, but warn that they are ignored.
496 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
497 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000498 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000499 continue;
500 }
501
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000502 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000503
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000504 // A pointer to member expression of the form &MyClass::mu is treated
505 // specially -- we need to look at the type of the member.
506 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
507 if (UOp->getOpcode() == UO_AddrOf)
508 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
509 if (DRE->getDecl()->isCXXInstanceMember())
510 ArgTy = DRE->getDecl()->getType();
511
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000512 // First see if we can just cast to record type, or point to record type.
513 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 // Now check if we index into a record type function param.
516 if(!RT && ParamIdxOk) {
517 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000518 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
519 if(FD && IL) {
520 unsigned int NumParams = FD->getNumParams();
521 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000522 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
523 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
524 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
526 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000527 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000529 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530 }
531 }
532
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000533 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000534
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000535 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000537}
538
Chris Lattner58418ff2008-06-29 00:16:31 +0000539//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000540// Attribute Implementations
541//===----------------------------------------------------------------------===//
542
Daniel Dunbar032db472008-07-31 22:40:48 +0000543// FIXME: All this manual attribute parsing code is gross. At the
544// least add some helper functions to check most argument patterns (#
545// and types of args).
546
Michael Hana9171bc2012-08-03 17:40:43 +0000547static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000548 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000549 if (!threadSafetyCheckIsPointer(S, D, Attr))
550 return;
551
Michael Han99315932013-01-24 16:46:58 +0000552 D->addAttr(::new (S.Context)
553 PtGuardedVarAttr(Attr.getRange(), S.Context,
554 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000555}
556
Michael Hana9171bc2012-08-03 17:40:43 +0000557static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
558 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000559 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000560 SmallVector<Expr*, 1> Args;
561 // check that all arguments are lockable objects
562 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
563 unsigned Size = Args.size();
564 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000565 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000566
Michael Han3be3b442012-07-23 18:48:41 +0000567 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000568
Michael Han3be3b442012-07-23 18:48:41 +0000569 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000570}
571
Michael Han3be3b442012-07-23 18:48:41 +0000572static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
573 Expr *Arg = 0;
574 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
575 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000576
Michael Han3be3b442012-07-23 18:48:41 +0000577 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000581 const AttributeList &Attr) {
582 Expr *Arg = 0;
583 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
584 return;
585
586 if (!threadSafetyCheckIsPointer(S, D, Attr))
587 return;
588
589 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
590 S.Context, Arg));
591}
592
Michael Hana9171bc2012-08-03 17:40:43 +0000593static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
594 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000595 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000597 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000599 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000600 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000601 if (!QT->isDependentType()) {
602 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000603 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000604 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000605 << Attr.getName();
606 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000607 }
608 }
609
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000610 // Check that all arguments are lockable objects.
611 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000612 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000613 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000614
Michael Han3be3b442012-07-23 18:48:41 +0000615 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000616}
617
Michael Hana9171bc2012-08-03 17:40:43 +0000618static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000619 const AttributeList &Attr) {
620 SmallVector<Expr*, 1> Args;
621 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
622 return;
623
624 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000625 D->addAttr(::new (S.Context)
626 AcquiredAfterAttr(Attr.getRange(), S.Context,
627 StartArg, Args.size(),
628 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000629}
630
Michael Hana9171bc2012-08-03 17:40:43 +0000631static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000632 const AttributeList &Attr) {
633 SmallVector<Expr*, 1> Args;
634 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
635 return;
636
637 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000638 D->addAttr(::new (S.Context)
639 AcquiredBeforeAttr(Attr.getRange(), S.Context,
640 StartArg, Args.size(),
641 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000642}
643
Michael Hana9171bc2012-08-03 17:40:43 +0000644static bool checkLockFunAttrCommon(Sema &S, Decl *D,
645 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000646 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000647 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000648 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000649 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000650
Michael Han3be3b442012-07-23 18:48:41 +0000651 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000652}
653
Michael Hana9171bc2012-08-03 17:40:43 +0000654static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000655 const AttributeList &Attr) {
656 SmallVector<Expr*, 1> Args;
657 if (!checkLockFunAttrCommon(S, D, Attr, Args))
658 return;
659
660 unsigned Size = Args.size();
661 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000662 D->addAttr(::new (S.Context)
663 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
664 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000665}
666
Michael Hana9171bc2012-08-03 17:40:43 +0000667static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000668 const AttributeList &Attr) {
669 SmallVector<Expr*, 1> Args;
670 if (!checkLockFunAttrCommon(S, D, Attr, Args))
671 return;
672
673 unsigned Size = Args.size();
674 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000675 D->addAttr(::new (S.Context)
676 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
677 StartArg, Size,
678 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000679}
680
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000681static void handleAssertSharedLockAttr(Sema &S, Decl *D,
682 const AttributeList &Attr) {
683 SmallVector<Expr*, 1> Args;
684 if (!checkLockFunAttrCommon(S, D, Attr, Args))
685 return;
686
687 unsigned Size = Args.size();
688 Expr **StartArg = Size == 0 ? 0 : &Args[0];
689 D->addAttr(::new (S.Context)
690 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
691 Attr.getAttributeSpellingListIndex()));
692}
693
694static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
695 const AttributeList &Attr) {
696 SmallVector<Expr*, 1> Args;
697 if (!checkLockFunAttrCommon(S, D, Attr, Args))
698 return;
699
700 unsigned Size = Args.size();
701 Expr **StartArg = Size == 0 ? 0 : &Args[0];
702 D->addAttr(::new (S.Context)
703 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
704 StartArg, Size,
705 Attr.getAttributeSpellingListIndex()));
706}
707
708
Michael Hana9171bc2012-08-03 17:40:43 +0000709static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
710 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000711 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000712 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000713 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000714
Aaron Ballman00e99962013-08-31 01:11:41 +0000715 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000716 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000717 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000718 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000719 }
720
721 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000722 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000723
Michael Han3be3b442012-07-23 18:48:41 +0000724 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000725}
726
Michael Hana9171bc2012-08-03 17:40:43 +0000727static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000728 const AttributeList &Attr) {
729 SmallVector<Expr*, 2> Args;
730 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
731 return;
732
Michael Han99315932013-01-24 16:46:58 +0000733 D->addAttr(::new (S.Context)
734 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000735 Attr.getArgAsExpr(0),
736 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000737 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000738}
739
Michael Hana9171bc2012-08-03 17:40:43 +0000740static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000741 const AttributeList &Attr) {
742 SmallVector<Expr*, 2> Args;
743 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
744 return;
745
Michael Han99315932013-01-24 16:46:58 +0000746 D->addAttr(::new (S.Context)
747 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000748 Attr.getArgAsExpr(0),
749 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000750 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000751}
752
Michael Hana9171bc2012-08-03 17:40:43 +0000753static bool checkLocksRequiredCommon(Sema &S, Decl *D,
754 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000755 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000756 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000757 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000758
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000759 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000761 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000762 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000763
Michael Han3be3b442012-07-23 18:48:41 +0000764 return true;
765}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000766
Michael Hana9171bc2012-08-03 17:40:43 +0000767static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000768 const AttributeList &Attr) {
769 SmallVector<Expr*, 1> Args;
770 if (!checkLocksRequiredCommon(S, D, Attr, Args))
771 return;
772
773 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000774 D->addAttr(::new (S.Context)
775 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
776 StartArg, Args.size(),
777 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000778}
779
Michael Hana9171bc2012-08-03 17:40:43 +0000780static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000781 const AttributeList &Attr) {
782 SmallVector<Expr*, 1> Args;
783 if (!checkLocksRequiredCommon(S, D, Attr, Args))
784 return;
785
786 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000787 D->addAttr(::new (S.Context)
788 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
789 StartArg, Args.size(),
790 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000791}
792
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000793static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000794 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000795 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000796 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000797 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000798 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000799 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000800 Expr **StartArg = Size == 0 ? 0 : &Args[0];
801
Michael Han99315932013-01-24 16:46:58 +0000802 D->addAttr(::new (S.Context)
803 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
804 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000805}
806
807static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000808 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000809 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000810 SmallVector<Expr*, 1> Args;
811 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
812 unsigned Size = Args.size();
813 if (Size == 0)
814 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000815
Michael Han99315932013-01-24 16:46:58 +0000816 D->addAttr(::new (S.Context)
817 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
818 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000819}
820
821static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000822 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000823 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000824 return;
825
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000826 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000827 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000828 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000829 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000830 if (Size == 0)
831 return;
832 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000833
Michael Han99315932013-01-24 16:46:58 +0000834 D->addAttr(::new (S.Context)
835 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
836 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000837}
838
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000839static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000840 ConsumableAttr::ConsumedState DefaultState;
841
842 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000843 IdentifierLoc *IL = Attr.getArgAsIdent(0);
844 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
845 DefaultState)) {
846 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
847 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000848 return;
849 }
David Blaikie16f76d22013-09-06 01:28:43 +0000850 } else {
851 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
852 << Attr.getName() << AANT_ArgumentIdentifier;
853 return;
854 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855
856 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000857 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000858 Attr.getAttributeSpellingListIndex()));
859}
860
861static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
862 const AttributeList &Attr) {
863 ASTContext &CurrContext = S.getASTContext();
864 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
865
866 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
867 if (!RD->hasAttr<ConsumableAttr>()) {
868 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
869 RD->getNameAsString();
870
871 return false;
872 }
873 }
874
875 return true;
876}
877
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000878
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000879static void handleCallableWhenAttr(Sema &S, Decl *D,
880 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000881 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
882 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000883
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000884 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
885 return;
886
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000887 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
888 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
889 CallableWhenAttr::ConsumedState CallableState;
890
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000891 StringRef StateString;
892 SourceLocation Loc;
893 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
894 return;
895
896 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000897 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000898 S.Diag(Loc, diag::warn_attribute_type_not_supported)
899 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000900 return;
901 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000902
903 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000904 }
905
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000906 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000907 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
908 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000909}
910
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000911
DeLesley Hutchins69391772013-10-17 23:23:53 +0000912static void handleParamTypestateAttr(Sema &S, Decl *D,
913 const AttributeList &Attr) {
914 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000915
DeLesley Hutchins69391772013-10-17 23:23:53 +0000916 ParamTypestateAttr::ConsumedState ParamState;
917
918 if (Attr.isArgIdent(0)) {
919 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
920 StringRef StateString = Ident->Ident->getName();
921
922 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
923 ParamState)) {
924 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
925 << Attr.getName() << StateString;
926 return;
927 }
928 } else {
929 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
930 Attr.getName() << AANT_ArgumentIdentifier;
931 return;
932 }
933
934 // FIXME: This check is currently being done in the analysis. It can be
935 // enabled here only after the parser propagates attributes at
936 // template specialization definition, not declaration.
937 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
938 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
939 //
940 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
941 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
942 // ReturnType.getAsString();
943 // return;
944 //}
945
946 D->addAttr(::new (S.Context)
947 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
948 Attr.getAttributeSpellingListIndex()));
949}
950
951
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000952static void handleReturnTypestateAttr(Sema &S, Decl *D,
953 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000954 if (!checkAttributeNumArgs(S, Attr, 1)) return;
955
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000956 ReturnTypestateAttr::ConsumedState ReturnState;
957
958 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000959 IdentifierLoc *IL = Attr.getArgAsIdent(0);
960 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
961 ReturnState)) {
962 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
963 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000964 return;
965 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000966 } else {
967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
968 Attr.getName() << AANT_ArgumentIdentifier;
969 return;
970 }
971
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000972 // FIXME: This check is currently being done in the analysis. It can be
973 // enabled here only after the parser propagates attributes at
974 // template specialization definition, not declaration.
975 //QualType ReturnType;
976 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000977 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
978 // ReturnType = Param->getType();
979 //
980 //} else if (const CXXConstructorDecl *Constructor =
981 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000982 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
983 //
984 //} else {
985 //
986 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
987 //}
988 //
989 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
990 //
991 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
992 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
993 // ReturnType.getAsString();
994 // return;
995 //}
996
997 D->addAttr(::new (S.Context)
998 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
999 Attr.getAttributeSpellingListIndex()));
1000}
1001
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001002
1003static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001004 if (!checkAttributeNumArgs(S, Attr, 1))
1005 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001006
1007 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1008 return;
1009
1010 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001011 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001012 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1013 StringRef Param = Ident->Ident->getName();
1014 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1015 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1016 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 return;
1018 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001019 } else {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1021 Attr.getName() << AANT_ArgumentIdentifier;
1022 return;
1023 }
1024
1025 D->addAttr(::new (S.Context)
1026 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1027 Attr.getAttributeSpellingListIndex()));
1028}
1029
Chris Wailes9385f9f2013-10-29 20:28:41 +00001030static void handleTestTypestateAttr(Sema &S, Decl *D,
1031 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001032 if (!checkAttributeNumArgs(S, Attr, 1))
1033 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001034
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001035 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1036 return;
1037
Chris Wailes9385f9f2013-10-29 20:28:41 +00001038 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001039 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001040 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1041 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001042 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001043 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1044 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001045 return;
1046 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001047 } else {
1048 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1049 Attr.getName() << AANT_ArgumentIdentifier;
1050 return;
1051 }
1052
1053 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001054 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001055 Attr.getAttributeSpellingListIndex()));
1056}
1057
Chandler Carruthedc2c642011-07-02 00:01:44 +00001058static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1059 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001060 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001061 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001062}
1063
Chandler Carruthedc2c642011-07-02 00:01:44 +00001064static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001065 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001066 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001067 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001068 // If the alignment is less than or equal to 8 bits, the packed attribute
1069 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001070 if (!FD->getType()->isDependentType() &&
1071 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001072 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001073 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001074 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001075 else
Michael Han99315932013-01-24 16:46:58 +00001076 FD->addAttr(::new (S.Context)
1077 PackedAttr(Attr.getRange(), S.Context,
1078 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001079 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001080 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081}
1082
Ted Kremenek7fd17232011-09-29 07:02:25 +00001083static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1084 // The IBOutlet/IBOutletCollection attributes only apply to instance
1085 // variables or properties of Objective-C classes. The outlet must also
1086 // have an object reference type.
1087 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1088 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001089 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001090 << Attr.getName() << VD->getType() << 0;
1091 return false;
1092 }
1093 }
1094 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1095 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001096 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001097 << Attr.getName() << PD->getType() << 1;
1098 return false;
1099 }
1100 }
1101 else {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1103 return false;
1104 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001105
Ted Kremenek7fd17232011-09-29 07:02:25 +00001106 return true;
1107}
1108
Chandler Carruthedc2c642011-07-02 00:01:44 +00001109static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001110 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001111 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001112
Michael Han99315932013-01-24 16:46:58 +00001113 D->addAttr(::new (S.Context)
1114 IBOutletAttr(Attr.getRange(), S.Context,
1115 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001116}
1117
Chandler Carruthedc2c642011-07-02 00:01:44 +00001118static void handleIBOutletCollection(Sema &S, Decl *D,
1119 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001120
1121 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001122 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1124 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001125 return;
1126 }
1127
Ted Kremenek7fd17232011-09-29 07:02:25 +00001128 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001129 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001130
Richard Smithb1f9a282013-10-31 01:56:18 +00001131 ParsedType PT;
1132
1133 if (Attr.hasParsedType())
1134 PT = Attr.getTypeArg();
1135 else {
1136 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1137 S.getScopeForContext(D->getDeclContext()->getParent()));
1138 if (!PT) {
1139 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1140 return;
1141 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001142 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001143
Richard Smithb87c4652013-10-31 21:23:20 +00001144 TypeSourceInfo *QTLoc = 0;
1145 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1146 if (!QTLoc)
1147 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001148
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001149 // Diagnose use of non-object type in iboutletcollection attribute.
1150 // FIXME. Gnu attribute extension ignores use of builtin types in
1151 // attributes. So, __attribute__((iboutletcollection(char))) will be
1152 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001153 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001154 S.Diag(Attr.getLoc(),
1155 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1156 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001157 return;
1158 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001159
Michael Han99315932013-01-24 16:46:58 +00001160 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001161 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001162 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001163}
1164
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001165static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001166 if (const RecordType *UT = T->getAsUnionType())
1167 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1168 RecordDecl *UD = UT->getDecl();
1169 for (RecordDecl::field_iterator it = UD->field_begin(),
1170 itend = UD->field_end(); it != itend; ++it) {
1171 QualType QT = it->getType();
1172 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1173 T = QT;
1174 return;
1175 }
1176 }
1177 }
1178}
1179
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001180static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001181 if (!isFunctionOrMethod(D)) {
1182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001183 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001184 return;
1185 }
1186
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001187 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1188 return;
1189
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001190 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001191 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001192 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001193 uint64_t Idx;
1194 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1195 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001196 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001197
1198 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001199 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001200 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1202 << Attr.getName() << AANT_ArgumentIntegerConstant
1203 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001204 return;
1205 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001206 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001207 }
1208
1209 // check if the function returns a pointer
1210 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1211 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001212 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001213 }
1214
Michael Han99315932013-01-24 16:46:58 +00001215 D->addAttr(::new (S.Context)
1216 AllocSizeAttr(Attr.getRange(), S.Context,
1217 SizeArgs.data(), SizeArgs.size(),
1218 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001219}
1220
Chandler Carruthedc2c642011-07-02 00:01:44 +00001221static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001222 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1223 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001224 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001226 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001227 return;
1228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001229
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001230 SmallVector<unsigned, 8> NonNullArgs;
1231 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001232 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001233 uint64_t Idx;
1234 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1235 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237
1238 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001239 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001240 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001241
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001242 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001243 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001244 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001245 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001246 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001248
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001249 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001251
1252 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1253 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001254 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001255 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1256 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001257 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001258 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001259 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Ted Kremenek22813f42010-10-21 18:49:36 +00001262 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001263 if (NonNullArgs.empty()) {
1264 // Warn the trivial case only if attribute is not coming from a
1265 // macro instantiation.
1266 if (Attr.getLoc().isFileID())
1267 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001268 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001269 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001270 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001271
Nick Lewyckye1121512013-01-24 01:12:16 +00001272 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001273 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001274 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001275 D->addAttr(::new (S.Context)
1276 NonNullAttr(Attr.getRange(), S.Context, start, size,
1277 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001278}
1279
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001280static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1281 switch (K) {
1282 case OwnershipAttr::Holds: return "'ownership_holds'";
1283 case OwnershipAttr::Takes: return "'ownership_takes'";
1284 case OwnershipAttr::Returns: return "'ownership_returns'";
1285 }
1286 llvm_unreachable("unknown ownership");
1287}
1288
Chandler Carruthedc2c642011-07-02 00:01:44 +00001289static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001290 // This attribute must be applied to a function declaration. The first
1291 // argument to the attribute must be an identifier, the name of the resource,
1292 // for example: malloc. The following arguments must be argument indexes, the
1293 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001294 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001295 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001296 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001297
Aaron Ballman00e99962013-08-31 01:11:41 +00001298 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001299 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001300 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001301 return;
1302 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001303
Richard Smith852e9ce2013-11-27 01:46:48 +00001304 // Figure out our Kind.
1305 OwnershipAttr::OwnershipKind K =
1306 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1307 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001308
Richard Smith852e9ce2013-11-27 01:46:48 +00001309 // Check arguments.
1310 switch (K) {
1311 case OwnershipAttr::Takes:
1312 case OwnershipAttr::Holds:
1313 if (AL.getNumArgs() < 2) {
1314 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1315 return;
1316 }
1317 break;
1318 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001319 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001320 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321 return;
1322 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001323 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001324 }
1325
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001326 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001327 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1328 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001329 return;
1330 }
1331
Richard Smith852e9ce2013-11-27 01:46:48 +00001332 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001333
1334 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001335 StringRef ModuleName = Module->getName();
1336 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1337 ModuleName.size() > 4) {
1338 ModuleName = ModuleName.drop_front(2).drop_back(2);
1339 Module = &S.PP.getIdentifierTable().get(ModuleName);
1340 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001341
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001342 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001343 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1344 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001345 uint64_t Idx;
1346 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001347 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001348 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001349
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001350 // Is the function argument a pointer type?
1351 QualType T = getFunctionOrMethodArgType(D, Idx);
1352 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001353 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001354 case OwnershipAttr::Takes:
1355 case OwnershipAttr::Holds:
1356 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1357 Err = 0;
1358 break;
1359 case OwnershipAttr::Returns:
1360 if (!T->isIntegerType())
1361 Err = 1;
1362 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001364 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001365 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001366 << Ex->getSourceRange();
1367 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001368 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001369
1370 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001371 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001372 i = D->specific_attr_begin<OwnershipAttr>(),
1373 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001374 // FIXME: A returns attribute should conflict with any returns attribute
1375 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001376 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1377 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1378 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1379 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1380 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381 }
1382 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001383 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 }
1385
1386 unsigned* start = OwnershipArgs.data();
1387 unsigned size = OwnershipArgs.size();
1388 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001389
Michael Han99315932013-01-24 16:46:58 +00001390 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001391 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001392 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001393}
1394
Chandler Carruthedc2c642011-07-02 00:01:44 +00001395static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001396 // Check the attribute arguments.
1397 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1399 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001400 return;
1401 }
1402
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001403 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001404
Rafael Espindolac18086a2010-02-23 22:00:30 +00001405 // gcc rejects
1406 // class c {
1407 // static int a __attribute__((weakref ("v2")));
1408 // static int b() __attribute__((weakref ("f3")));
1409 // };
1410 // and ignores the attributes of
1411 // void f(void) {
1412 // static int a __attribute__((weakref ("v2")));
1413 // }
1414 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001415 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001416 if (!Ctx->isFileContext()) {
1417 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001418 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001419 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001420 }
1421
1422 // The GCC manual says
1423 //
1424 // At present, a declaration to which `weakref' is attached can only
1425 // be `static'.
1426 //
1427 // It also says
1428 //
1429 // Without a TARGET,
1430 // given as an argument to `weakref' or to `alias', `weakref' is
1431 // equivalent to `weak'.
1432 //
1433 // gcc 4.4.1 will accept
1434 // int a7 __attribute__((weakref));
1435 // as
1436 // int a7 __attribute__((weak));
1437 // This looks like a bug in gcc. We reject that for now. We should revisit
1438 // it if this behaviour is actually used.
1439
Rafael Espindolac18086a2010-02-23 22:00:30 +00001440 // GCC rejects
1441 // static ((alias ("y"), weakref)).
1442 // Should we? How to check that weakref is before or after alias?
1443
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001444 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1445 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1446 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001447 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001448 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001449 // GCC will accept anything as the argument of weakref. Should we
1450 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001451 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1452 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001453
Michael Han99315932013-01-24 16:46:58 +00001454 D->addAttr(::new (S.Context)
1455 WeakRefAttr(Attr.getRange(), S.Context,
1456 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001457}
1458
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001459static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1460 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001461 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001462 return;
1463
Douglas Gregore8bbc122011-09-02 00:18:52 +00001464 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001465 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1466 return;
1467 }
1468
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001469 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001470
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001471 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001472 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001473}
1474
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001475static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001476 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr, "hot"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001477 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001478
Michael Han99315932013-01-24 16:46:58 +00001479 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1480 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001481}
1482
1483static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001484 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr, "cold"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001485 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001486
Michael Han99315932013-01-24 16:46:58 +00001487 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1488 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001489}
1490
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001491static void handleTLSModelAttr(Sema &S, Decl *D,
1492 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001493 StringRef Model;
1494 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001495 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001496 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001497 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001498
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001499 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001500 if (Model != "global-dynamic" && Model != "local-dynamic"
1501 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001502 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001503 return;
1504 }
1505
Michael Han99315932013-01-24 16:46:58 +00001506 D->addAttr(::new (S.Context)
1507 TLSModelAttr(Attr.getRange(), S.Context, Model,
1508 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001509}
1510
Chandler Carruthedc2c642011-07-02 00:01:44 +00001511static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001512 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001513 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001514 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001515 D->addAttr(::new (S.Context)
1516 MallocAttr(Attr.getRange(), S.Context,
1517 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001518 return;
1519 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001520 }
1521
Ted Kremenek08479ae2009-08-15 00:51:46 +00001522 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001523}
1524
Chandler Carruthedc2c642011-07-02 00:01:44 +00001525static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001526 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001527 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1528 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001529 return;
1530 }
1531
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001532 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1533 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001534}
1535
Chandler Carruthedc2c642011-07-02 00:01:44 +00001536static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001537 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001538
1539 if (S.CheckNoReturnAttr(attr)) return;
1540
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001541 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001542 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001543 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001544 return;
1545 }
1546
Michael Han99315932013-01-24 16:46:58 +00001547 D->addAttr(::new (S.Context)
1548 NoReturnAttr(attr.getRange(), S.Context,
1549 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001550}
1551
1552bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001553 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001554 attr.setInvalid();
1555 return true;
1556 }
1557
1558 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001559}
1560
Chandler Carruthedc2c642011-07-02 00:01:44 +00001561static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1562 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001563
1564 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1565 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001566 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1567 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001568 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1569 && !VD->getType()->isFunctionPointerType())) {
1570 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001571 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001572 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001573 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001574 return;
1575 }
1576 }
1577
Michael Han99315932013-01-24 16:46:58 +00001578 D->addAttr(::new (S.Context)
1579 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1580 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001581}
1582
John Thompsoncdb847ba2010-08-09 21:53:52 +00001583// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001584static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001585/*
1586 Returning a Vector Class in Registers
1587
Eric Christopherbc638a82010-12-01 22:13:54 +00001588 According to the PPU ABI specifications, a class with a single member of
1589 vector type is returned in memory when used as the return value of a function.
1590 This results in inefficient code when implementing vector classes. To return
1591 the value in a single vector register, add the vecreturn attribute to the
1592 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001593
1594 Example:
1595
1596 struct Vector
1597 {
1598 __vector float xyzw;
1599 } __attribute__((vecreturn));
1600
1601 Vector Add(Vector lhs, Vector rhs)
1602 {
1603 Vector result;
1604 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1605 return result; // This will be returned in a register
1606 }
1607*/
Aaron Ballman9ead1242013-12-19 02:39:40 +00001608 if (D->hasAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001609 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1610 return;
1611 }
1612
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001613 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001614 int count = 0;
1615
1616 if (!isa<CXXRecordDecl>(record)) {
1617 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1618 return;
1619 }
1620
1621 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1622 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1623 return;
1624 }
1625
Eric Christopherbc638a82010-12-01 22:13:54 +00001626 for (RecordDecl::field_iterator iter = record->field_begin();
1627 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001628 if ((count == 1) || !iter->getType()->isVectorType()) {
1629 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1630 return;
1631 }
1632 count++;
1633 }
1634
Michael Han99315932013-01-24 16:46:58 +00001635 D->addAttr(::new (S.Context)
1636 VecReturnAttr(Attr.getRange(), S.Context,
1637 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001638}
1639
Richard Smithe233fbf2013-01-28 22:42:45 +00001640static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1641 const AttributeList &Attr) {
1642 if (isa<ParmVarDecl>(D)) {
1643 // [[carries_dependency]] can only be applied to a parameter if it is a
1644 // parameter of a function declaration or lambda.
1645 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1646 S.Diag(Attr.getLoc(),
1647 diag::err_carries_dependency_param_not_function_decl);
1648 return;
1649 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001650 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001651
1652 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1653 Attr.getRange(), S.Context,
1654 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001655}
1656
Chandler Carruthedc2c642011-07-02 00:01:44 +00001657static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001658 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001659 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001661 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001662 return;
1663 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001664
Michael Han99315932013-01-24 16:46:58 +00001665 D->addAttr(::new (S.Context)
1666 UnusedAttr(Attr.getRange(), S.Context,
1667 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001668}
1669
Chandler Carruthedc2c642011-07-02 00:01:44 +00001670static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001671 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001672 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001673 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1674 return;
1675 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001676 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001678 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001679 return;
1680 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001681
Michael Han99315932013-01-24 16:46:58 +00001682 D->addAttr(::new (S.Context)
1683 UsedAttr(Attr.getRange(), S.Context,
1684 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001685}
1686
Chandler Carruthedc2c642011-07-02 00:01:44 +00001687static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001688 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001689 if (Attr.getNumArgs() > 1) {
1690 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001691 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001692 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001693
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001694 uint32_t priority = ConstructorAttr::DefaultPriority;
1695 if (Attr.getNumArgs() > 0 &&
1696 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1697 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001698
Michael Han99315932013-01-24 16:46:58 +00001699 D->addAttr(::new (S.Context)
1700 ConstructorAttr(Attr.getRange(), S.Context, priority,
1701 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001702}
1703
Chandler Carruthedc2c642011-07-02 00:01:44 +00001704static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001705 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001706 if (Attr.getNumArgs() > 1) {
1707 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001708 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001709 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001710
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001711 uint32_t priority = ConstructorAttr::DefaultPriority;
1712 if (Attr.getNumArgs() > 0 &&
1713 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1714 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Michael Han99315932013-01-24 16:46:58 +00001716 D->addAttr(::new (S.Context)
1717 DestructorAttr(Attr.getRange(), S.Context, priority,
1718 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001719}
1720
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001721template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001722static void handleAttrWithMessage(Sema &S, Decl *D,
1723 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001724 unsigned NumArgs = Attr.getNumArgs();
1725 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001726 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001727 return;
1728 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001729
1730 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001731 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001732 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001733 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001734
Michael Han99315932013-01-24 16:46:58 +00001735 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1736 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001737}
1738
Ted Kremenek28eace62013-11-23 01:01:34 +00001739static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1740 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001741 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001742 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1743 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001744}
1745
Jordy Rose740b0c22012-05-08 03:27:22 +00001746static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1747 IdentifierInfo *Platform,
1748 VersionTuple Introduced,
1749 VersionTuple Deprecated,
1750 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001751 StringRef PlatformName
1752 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1753 if (PlatformName.empty())
1754 PlatformName = Platform->getName();
1755
1756 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1757 // of these steps are needed).
1758 if (!Introduced.empty() && !Deprecated.empty() &&
1759 !(Introduced <= Deprecated)) {
1760 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1761 << 1 << PlatformName << Deprecated.getAsString()
1762 << 0 << Introduced.getAsString();
1763 return true;
1764 }
1765
1766 if (!Introduced.empty() && !Obsoleted.empty() &&
1767 !(Introduced <= Obsoleted)) {
1768 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1769 << 2 << PlatformName << Obsoleted.getAsString()
1770 << 0 << Introduced.getAsString();
1771 return true;
1772 }
1773
1774 if (!Deprecated.empty() && !Obsoleted.empty() &&
1775 !(Deprecated <= Obsoleted)) {
1776 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1777 << 2 << PlatformName << Obsoleted.getAsString()
1778 << 1 << Deprecated.getAsString();
1779 return true;
1780 }
1781
1782 return false;
1783}
1784
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001785/// \brief Check whether the two versions match.
1786///
1787/// If either version tuple is empty, then they are assumed to match. If
1788/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1789static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1790 bool BeforeIsOkay) {
1791 if (X.empty() || Y.empty())
1792 return true;
1793
1794 if (X == Y)
1795 return true;
1796
1797 if (BeforeIsOkay && X < Y)
1798 return true;
1799
1800 return false;
1801}
1802
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001803AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001804 IdentifierInfo *Platform,
1805 VersionTuple Introduced,
1806 VersionTuple Deprecated,
1807 VersionTuple Obsoleted,
1808 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001809 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001810 bool Override,
1811 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001812 VersionTuple MergedIntroduced = Introduced;
1813 VersionTuple MergedDeprecated = Deprecated;
1814 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001815 bool FoundAny = false;
1816
Rafael Espindolac67f2232012-05-10 02:50:16 +00001817 if (D->hasAttrs()) {
1818 AttrVec &Attrs = D->getAttrs();
1819 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1820 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1821 if (!OldAA) {
1822 ++i;
1823 continue;
1824 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001825
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1827 if (OldPlatform != Platform) {
1828 ++i;
1829 continue;
1830 }
1831
1832 FoundAny = true;
1833 VersionTuple OldIntroduced = OldAA->getIntroduced();
1834 VersionTuple OldDeprecated = OldAA->getDeprecated();
1835 VersionTuple OldObsoleted = OldAA->getObsoleted();
1836 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001837
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001838 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1839 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1840 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1841 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001842 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001843 if (Override) {
1844 int Which = -1;
1845 VersionTuple FirstVersion;
1846 VersionTuple SecondVersion;
1847 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1848 Which = 0;
1849 FirstVersion = OldIntroduced;
1850 SecondVersion = Introduced;
1851 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1852 Which = 1;
1853 FirstVersion = Deprecated;
1854 SecondVersion = OldDeprecated;
1855 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1856 Which = 2;
1857 FirstVersion = Obsoleted;
1858 SecondVersion = OldObsoleted;
1859 }
1860
1861 if (Which == -1) {
1862 Diag(OldAA->getLocation(),
1863 diag::warn_mismatched_availability_override_unavail)
1864 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1865 } else {
1866 Diag(OldAA->getLocation(),
1867 diag::warn_mismatched_availability_override)
1868 << Which
1869 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1870 << FirstVersion.getAsString() << SecondVersion.getAsString();
1871 }
1872 Diag(Range.getBegin(), diag::note_overridden_method);
1873 } else {
1874 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1875 Diag(Range.getBegin(), diag::note_previous_attribute);
1876 }
1877
Rafael Espindolac67f2232012-05-10 02:50:16 +00001878 Attrs.erase(Attrs.begin() + i);
1879 --e;
1880 continue;
1881 }
1882
1883 VersionTuple MergedIntroduced2 = MergedIntroduced;
1884 VersionTuple MergedDeprecated2 = MergedDeprecated;
1885 VersionTuple MergedObsoleted2 = MergedObsoleted;
1886
1887 if (MergedIntroduced2.empty())
1888 MergedIntroduced2 = OldIntroduced;
1889 if (MergedDeprecated2.empty())
1890 MergedDeprecated2 = OldDeprecated;
1891 if (MergedObsoleted2.empty())
1892 MergedObsoleted2 = OldObsoleted;
1893
1894 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1895 MergedIntroduced2, MergedDeprecated2,
1896 MergedObsoleted2)) {
1897 Attrs.erase(Attrs.begin() + i);
1898 --e;
1899 continue;
1900 }
1901
1902 MergedIntroduced = MergedIntroduced2;
1903 MergedDeprecated = MergedDeprecated2;
1904 MergedObsoleted = MergedObsoleted2;
1905 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001906 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907 }
1908
1909 if (FoundAny &&
1910 MergedIntroduced == Introduced &&
1911 MergedDeprecated == Deprecated &&
1912 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001913 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001914
Ted Kremenekb5445722013-04-06 00:34:27 +00001915 // Only create a new attribute if !Override, but we want to do
1916 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001917 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001918 MergedDeprecated, MergedObsoleted) &&
1919 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001920 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1921 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001922 Obsoleted, IsUnavailable, Message,
1923 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001925 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001926}
1927
Chandler Carruthedc2c642011-07-02 00:01:44 +00001928static void handleAvailabilityAttr(Sema &S, Decl *D,
1929 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001930 if (!checkAttributeNumArgs(S, Attr, 1))
1931 return;
1932 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001933 unsigned Index = Attr.getAttributeSpellingListIndex();
1934
Aaron Ballman00e99962013-08-31 01:11:41 +00001935 IdentifierInfo *II = Platform->Ident;
1936 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1937 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1938 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001939
Rafael Espindolac231fab2013-01-08 21:30:32 +00001940 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1941 if (!ND) {
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1943 return;
1944 }
1945
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001946 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1947 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1948 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001949 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001950 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001951 if (const StringLiteral *SE =
1952 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001953 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001954
Aaron Ballman00e99962013-08-31 01:11:41 +00001955 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001956 Introduced.Version,
1957 Deprecated.Version,
1958 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001959 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001960 /*Override=*/false,
1961 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001962 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001963 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001964}
1965
John McCalld041a9b2013-02-20 01:54:26 +00001966template <class T>
1967static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1968 typename T::VisibilityType value,
1969 unsigned attrSpellingListIndex) {
1970 T *existingAttr = D->getAttr<T>();
1971 if (existingAttr) {
1972 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1973 if (existingValue == value)
1974 return NULL;
1975 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1976 S.Diag(range.getBegin(), diag::note_previous_attribute);
1977 D->dropAttr<T>();
1978 }
1979 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1980}
1981
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001982VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001983 VisibilityAttr::VisibilityType Vis,
1984 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001985 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1986 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001987}
1988
John McCalld041a9b2013-02-20 01:54:26 +00001989TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1990 TypeVisibilityAttr::VisibilityType Vis,
1991 unsigned AttrSpellingListIndex) {
1992 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1993 AttrSpellingListIndex);
1994}
1995
1996static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1997 bool isTypeVisibility) {
1998 // Visibility attributes don't mean anything on a typedef.
1999 if (isa<TypedefNameDecl>(D)) {
2000 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2001 << Attr.getName();
2002 return;
2003 }
2004
2005 // 'type_visibility' can only go on a type or namespace.
2006 if (isTypeVisibility &&
2007 !(isa<TagDecl>(D) ||
2008 isa<ObjCInterfaceDecl>(D) ||
2009 isa<NamespaceDecl>(D))) {
2010 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2011 << Attr.getName() << ExpectedTypeOrNamespace;
2012 return;
2013 }
2014
Benjamin Kramer70370212013-09-09 15:08:57 +00002015 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002016 StringRef TypeStr;
2017 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002018 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002019 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002020
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002021 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002022 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002023 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002025 return;
2026 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002027
2028 // Complain about attempts to use protected visibility on targets
2029 // (like Darwin) that don't support it.
2030 if (type == VisibilityAttr::Protected &&
2031 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2032 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2033 type = VisibilityAttr::Default;
2034 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002035
Michael Han99315932013-01-24 16:46:58 +00002036 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002037 clang::Attr *newAttr;
2038 if (isTypeVisibility) {
2039 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2040 (TypeVisibilityAttr::VisibilityType) type,
2041 Index);
2042 } else {
2043 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2044 }
2045 if (newAttr)
2046 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002047}
2048
Chandler Carruthedc2c642011-07-02 00:01:44 +00002049static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2050 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002051 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002052 if (!Attr.isArgIdent(0)) {
2053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2054 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002055 return;
2056 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002057
Aaron Ballman682ee422013-09-11 19:47:58 +00002058 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2059 ObjCMethodFamilyAttr::FamilyKind F;
2060 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2061 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2062 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002063 return;
2064 }
2065
Aaron Ballman682ee422013-09-11 19:47:58 +00002066 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002067 !method->getResultType()->isObjCObjectPointerType()) {
2068 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2069 << method->getResultType();
2070 // Ignore the attribute.
2071 return;
2072 }
2073
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002074 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002075 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002076}
2077
Chandler Carruthedc2c642011-07-02 00:01:44 +00002078static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002079 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002080 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002081 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002082 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2083 return;
2084 }
2085 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002086 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2087 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002088 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002089 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2090 return;
2091 }
2092 }
2093 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002094 // It is okay to include this attribute on properties, e.g.:
2095 //
2096 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2097 //
2098 // In this case it follows tradition and suppresses an error in the above
2099 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002100 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002101 }
Michael Han99315932013-01-24 16:46:58 +00002102 D->addAttr(::new (S.Context)
2103 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2104 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002105}
2106
Chandler Carruthedc2c642011-07-02 00:01:44 +00002107static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002108 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002110 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002111 return;
2112 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002113
Aaron Ballman00e99962013-08-31 01:11:41 +00002114 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002115 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002116 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2117 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2118 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002119 return;
2120 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002121
Michael Han99315932013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context)
2123 BlocksAttr(Attr.getRange(), S.Context, type,
2124 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002125}
2126
Chandler Carruthedc2c642011-07-02 00:01:44 +00002127static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002128 // check the attribute arguments.
2129 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002130 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002131 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002132 }
2133
Aaron Ballman18a78382013-11-21 00:28:23 +00002134 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002135 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002136 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002137 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002138 if (E->isTypeDependent() || E->isValueDependent() ||
2139 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002141 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002142 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002143 return;
2144 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002145
John McCallb46f2872011-09-09 07:56:05 +00002146 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002147 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2148 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002149 return;
2150 }
John McCallb46f2872011-09-09 07:56:05 +00002151
2152 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002153 }
2154
Aaron Ballman18a78382013-11-21 00:28:23 +00002155 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002156 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002157 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002158 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002159 if (E->isTypeDependent() || E->isValueDependent() ||
2160 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002162 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002163 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002164 return;
2165 }
2166 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002167
John McCallb46f2872011-09-09 07:56:05 +00002168 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002169 // FIXME: This error message could be improved, it would be nice
2170 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002171 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2172 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002173 return;
2174 }
2175 }
2176
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002177 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002178 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002179 if (isa<FunctionNoProtoType>(FT)) {
2180 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2181 return;
2182 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002183
Chris Lattner9363e312009-03-17 23:03:47 +00002184 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002185 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002186 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002187 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002188 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002189 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002190 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002191 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002192 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002193 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2194 if (!BD->isVariadic()) {
2195 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2196 return;
2197 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002198 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002199 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002200 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002201 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002202 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002203 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002204 int m = Ty->isFunctionPointerType() ? 0 : 1;
2205 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002206 return;
2207 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002208 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002210 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002211 return;
2212 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002213 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002214 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002215 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002216 return;
2217 }
Michael Han99315932013-01-24 16:46:58 +00002218 D->addAttr(::new (S.Context)
2219 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2220 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002221}
2222
Chandler Carruthedc2c642011-07-02 00:01:44 +00002223static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002224 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002226 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002227 return;
2228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002229
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002230 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2231 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2232 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002233 return;
2234 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002235 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2236 if (MD->getResultType()->isVoidType()) {
2237 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2238 << Attr.getName() << 1;
2239 return;
2240 }
2241
Michael Han99315932013-01-24 16:46:58 +00002242 D->addAttr(::new (S.Context)
2243 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2244 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002245}
2246
Chandler Carruthedc2c642011-07-02 00:01:44 +00002247static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002248 // weak_import only applies to variable & function declarations.
2249 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002250 if (!D->canBeWeakImported(isDef)) {
2251 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002252 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2253 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002254 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002255 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002256 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002257 // Nothing to warn about here.
2258 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002259 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002260 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002261
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002262 return;
2263 }
2264
Michael Han99315932013-01-24 16:46:58 +00002265 D->addAttr(::new (S.Context)
2266 WeakImportAttr(Attr.getRange(), S.Context,
2267 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002268}
2269
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002270// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002271template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002272static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002273 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002274 uint32_t WGSize[3];
2275 for (unsigned i = 0; i < 3; ++i)
2276 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002277 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002278
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002279 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2280 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2281 Existing->getYDim() == WGSize[1] &&
2282 Existing->getZDim() == WGSize[2]))
2283 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002284
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002285 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2286 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002287 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002288}
2289
Joey Goulyaba589c2013-03-08 09:42:32 +00002290static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002291 if (!Attr.hasParsedType()) {
2292 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2293 << Attr.getName() << 1;
2294 return;
2295 }
2296
Richard Smithb87c4652013-10-31 21:23:20 +00002297 TypeSourceInfo *ParmTSI = 0;
2298 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2299 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002300
2301 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2302 (ParmType->isBooleanType() ||
2303 !ParmType->isIntegralType(S.getASTContext()))) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2305 << ParmType;
2306 return;
2307 }
2308
Aaron Ballmana9e05402013-12-02 22:16:55 +00002309 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002310 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002311 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2312 return;
2313 }
2314 }
2315
2316 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002317 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002318}
2319
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002320SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002321 StringRef Name,
2322 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002323 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2324 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002325 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002326 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2327 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002328 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002329 }
Michael Han99315932013-01-24 16:46:58 +00002330 return ::new (Context) SectionAttr(Range, Context, Name,
2331 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002332}
2333
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002335 // Make sure that there is a string literal as the sections's single
2336 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002337 StringRef Str;
2338 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002339 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002340 return;
Mike Stump11289f42009-09-09 15:08:12 +00002341
Chris Lattner30ba6742009-08-10 19:03:04 +00002342 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002343 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002344 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002345 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002346 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002347 return;
2348 }
Mike Stump11289f42009-09-09 15:08:12 +00002349
Michael Han99315932013-01-24 16:46:58 +00002350 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002351 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002352 if (NewAttr)
2353 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002354}
2355
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002356
Chandler Carruthedc2c642011-07-02 00:01:44 +00002357static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002358 VarDecl *VD = cast<VarDecl>(D);
2359 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002360 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002361 return;
2362 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002363
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002364 Expr *E = Attr.getArgAsExpr(0);
2365 SourceLocation Loc = E->getExprLoc();
2366 FunctionDecl *FD = 0;
2367 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002368
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002369 // gcc only allows for simple identifiers. Since we support more than gcc, we
2370 // will warn the user.
2371 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2372 if (DRE->hasQualifier())
2373 S.Diag(Loc, diag::warn_cleanup_ext);
2374 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2375 NI = DRE->getNameInfo();
2376 if (!FD) {
2377 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2378 << NI.getName();
2379 return;
2380 }
2381 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2382 if (ULE->hasExplicitTemplateArgs())
2383 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002384 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2385 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002386 if (!FD) {
2387 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2388 << NI.getName();
2389 if (ULE->getType() == S.Context.OverloadTy)
2390 S.NoteAllOverloadCandidates(ULE);
2391 return;
2392 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002393 } else {
2394 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002395 return;
2396 }
2397
Anders Carlssond277d792009-01-31 01:16:18 +00002398 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002399 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2400 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002401 return;
2402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403
Anders Carlsson723f55d2009-02-07 23:16:50 +00002404 // We're currently more strict than GCC about what function types we accept.
2405 // If this ever proves to be a problem it should be easy to fix.
2406 QualType Ty = S.Context.getPointerType(VD->getType());
2407 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002408 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2409 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002410 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2411 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002412 return;
2413 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002414
Michael Han99315932013-01-24 16:46:58 +00002415 D->addAttr(::new (S.Context)
2416 CleanupAttr(Attr.getRange(), S.Context, FD,
2417 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002418}
2419
Mike Stumpd3bb5572009-07-24 19:02:52 +00002420/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002421/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002422static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002423 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002424 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002425 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002426 return;
2427 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002428
Aaron Ballman00e99962013-08-31 01:11:41 +00002429 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002430 uint64_t ArgIdx;
2431 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2432 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002433 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002434
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002435 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002436 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002437
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002438 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2439 if (not_nsstring_type &&
2440 !isCFStringType(Ty, S.Context) &&
2441 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002442 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002443 // FIXME: Should highlight the actual expression that has the wrong type.
2444 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002445 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002446 << IdxExpr->getSourceRange();
2447 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002448 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002449 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002450 if (!isNSStringType(Ty, S.Context) &&
2451 !isCFStringType(Ty, S.Context) &&
2452 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002453 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002454 // FIXME: Should highlight the actual expression that has the wrong type.
2455 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002456 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002457 << IdxExpr->getSourceRange();
2458 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459 }
2460
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002461 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2462 // because that has corrected for the implicit this parameter, and is zero-
2463 // based. The attribute expects what the user wrote explicitly.
2464 llvm::APSInt Val;
2465 IdxExpr->EvaluateAsInt(Val, S.Context);
2466
Michael Han99315932013-01-24 16:46:58 +00002467 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002468 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002469 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002470}
2471
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002472enum FormatAttrKind {
2473 CFStringFormat,
2474 NSStringFormat,
2475 StrftimeFormat,
2476 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002477 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002478 InvalidFormat
2479};
2480
2481/// getFormatAttrKind - Map from format attribute names to supported format
2482/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002483static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002484 return llvm::StringSwitch<FormatAttrKind>(Format)
2485 // Check for formats that get handled specially.
2486 .Case("NSString", NSStringFormat)
2487 .Case("CFString", CFStringFormat)
2488 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002489
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002490 // Otherwise, check for supported formats.
2491 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2492 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2493 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002494
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002495 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2496 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002497}
2498
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002499/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002500/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002501static void handleInitPriorityAttr(Sema &S, Decl *D,
2502 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002503 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002504 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2505 return;
2506 }
2507
Aaron Ballman4a611152013-11-27 16:34:09 +00002508 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002509 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2510 Attr.setInvalid();
2511 return;
2512 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002513 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002514 if (S.Context.getAsArrayType(T))
2515 T = S.Context.getBaseElementType(T);
2516 if (!T->getAs<RecordType>()) {
2517 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2518 Attr.setInvalid();
2519 return;
2520 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002521
2522 Expr *E = Attr.getArgAsExpr(0);
2523 uint32_t prioritynum;
2524 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002525 Attr.setInvalid();
2526 return;
2527 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002528
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002529 if (prioritynum < 101 || prioritynum > 65535) {
2530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002531 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002532 Attr.setInvalid();
2533 return;
2534 }
Michael Han99315932013-01-24 16:46:58 +00002535 D->addAttr(::new (S.Context)
2536 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2537 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002538}
2539
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002540FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2541 IdentifierInfo *Format, int FormatIdx,
2542 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002543 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002544 // Check whether we already have an equivalent format attribute.
2545 for (specific_attr_iterator<FormatAttr>
2546 i = D->specific_attr_begin<FormatAttr>(),
2547 e = D->specific_attr_end<FormatAttr>();
2548 i != e ; ++i) {
2549 FormatAttr *f = *i;
2550 if (f->getType() == Format &&
2551 f->getFormatIdx() == FormatIdx &&
2552 f->getFirstArg() == FirstArg) {
2553 // If we don't have a valid location for this attribute, adopt the
2554 // location.
2555 if (f->getLocation().isInvalid())
2556 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002557 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002558 }
2559 }
2560
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002561 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2562 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002563}
2564
Mike Stumpd3bb5572009-07-24 19:02:52 +00002565/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002566/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002567static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002568 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002570 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002571 return;
2572 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002573
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002574 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002575 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002576 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002577 return;
2578 }
2579
Chandler Carruth743682b2010-11-16 08:35:43 +00002580 // In C++ the implicit 'this' function parameter also counts, and they are
2581 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002582 bool HasImplicitThisParam = isInstanceMethod(D);
2583 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002584
Aaron Ballman00e99962013-08-31 01:11:41 +00002585 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2586 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587
2588 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002589 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002590 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002591 // If we've modified the string name, we need a new identifier for it.
2592 II = &S.Context.Idents.get(Format);
2593 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002594
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002595 // Check for supported formats.
2596 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002597
2598 if (Kind == IgnoredFormat)
2599 return;
2600
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002601 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002602 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002603 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002604 return;
2605 }
2606
2607 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002608 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002609 uint32_t Idx;
2610 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002611 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002612
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002613 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002614 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002615 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616 return;
2617 }
2618
2619 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002620 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002622 if (HasImplicitThisParam) {
2623 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002624 S.Diag(Attr.getLoc(),
2625 diag::err_format_attribute_implicit_this_format_string)
2626 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002627 return;
2628 }
2629 ArgIdx--;
2630 }
Mike Stump11289f42009-09-09 15:08:12 +00002631
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002632 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002633 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002634
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002635 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002636 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002637 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2638 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002639 return;
2640 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002641 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002642 // FIXME: do we need to check if the type is NSString*? What are the
2643 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002644 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002645 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002646 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2647 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002648 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002649 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002651 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002652 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002653 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2654 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002655 return;
2656 }
2657
2658 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002659 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002660 uint32_t FirstArg;
2661 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002663
2664 // check if the function is variadic if the 3rd argument non-zero
2665 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002666 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002667 ++NumArgs; // +1 for ...
2668 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002669 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002670 return;
2671 }
2672 }
2673
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002674 // strftime requires FirstArg to be 0 because it doesn't read from any
2675 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002676 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002677 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002678 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2679 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002680 return;
2681 }
2682 // if 0 it disables parameter checking (to use with e.g. va_list)
2683 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002685 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002686 return;
2687 }
2688
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002689 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002690 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002691 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002692 if (NewAttr)
2693 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002694}
2695
Chandler Carruthedc2c642011-07-02 00:01:44 +00002696static void handleTransparentUnionAttr(Sema &S, Decl *D,
2697 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002698 // Try to find the underlying union declaration.
2699 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002700 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002701 if (TD && TD->getUnderlyingType()->isUnionType())
2702 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2703 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002704 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002705
2706 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002708 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002709 return;
2710 }
2711
John McCallf937c022011-10-07 06:10:15 +00002712 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002713 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002714 diag::warn_transparent_union_attribute_not_definition);
2715 return;
2716 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002717
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002718 RecordDecl::field_iterator Field = RD->field_begin(),
2719 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002720 if (Field == FieldEnd) {
2721 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2722 return;
2723 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002724
David Blaikie40ed2972012-06-06 20:45:41 +00002725 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002726 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002727 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002728 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002729 diag::warn_transparent_union_attribute_floating)
2730 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 return;
2732 }
2733
2734 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2735 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2736 for (; Field != FieldEnd; ++Field) {
2737 QualType FieldType = Field->getType();
2738 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2739 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2740 // Warn if we drop the attribute.
2741 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002742 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002743 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002744 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002745 diag::warn_transparent_union_attribute_field_size_align)
2746 << isSize << Field->getDeclName() << FieldBits;
2747 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002748 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002749 diag::note_transparent_union_first_field_size_align)
2750 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002751 return;
2752 }
2753 }
2754
Michael Han99315932013-01-24 16:46:58 +00002755 RD->addAttr(::new (S.Context)
2756 TransparentUnionAttr(Attr.getRange(), S.Context,
2757 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002758}
2759
Chandler Carruthedc2c642011-07-02 00:01:44 +00002760static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002761 // Make sure that there is a string literal as the annotation's single
2762 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002763 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002764 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002766
2767 // Don't duplicate annotations that are already set.
2768 for (specific_attr_iterator<AnnotateAttr>
2769 i = D->specific_attr_begin<AnnotateAttr>(),
2770 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002771 if ((*i)->getAnnotation() == Str)
2772 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002773 }
Michael Han99315932013-01-24 16:46:58 +00002774
2775 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002776 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002777 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002778}
2779
Chandler Carruthedc2c642011-07-02 00:01:44 +00002780static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002781 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002782 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002783 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2784 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002785 return;
2786 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002787
Richard Smith848e1f12013-02-01 08:12:08 +00002788 if (Attr.getNumArgs() == 0) {
2789 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2790 true, 0, Attr.getAttributeSpellingListIndex()));
2791 return;
2792 }
2793
Aaron Ballman00e99962013-08-31 01:11:41 +00002794 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002795 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2796 S.Diag(Attr.getEllipsisLoc(),
2797 diag::err_pack_expansion_without_parameter_packs);
2798 return;
2799 }
2800
2801 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2802 return;
2803
2804 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2805 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002806}
2807
2808void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002809 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002810 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2811 SourceLocation AttrLoc = AttrRange.getBegin();
2812
Richard Smith1dba27c2013-01-29 09:02:09 +00002813 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002814 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002815 // C++11 [dcl.align]p1:
2816 // An alignment-specifier may be applied to a variable or to a class
2817 // data member, but it shall not be applied to a bit-field, a function
2818 // parameter, the formal parameter of a catch clause, or a variable
2819 // declared with the register storage class specifier. An
2820 // alignment-specifier may also be applied to the declaration of a class
2821 // or enumeration type.
2822 // C11 6.7.5/2:
2823 // An alignment attribute shall not be specified in a declaration of
2824 // a typedef, or a bit-field, or a function, or a parameter, or an
2825 // object declared with the register storage-class specifier.
2826 int DiagKind = -1;
2827 if (isa<ParmVarDecl>(D)) {
2828 DiagKind = 0;
2829 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2830 if (VD->getStorageClass() == SC_Register)
2831 DiagKind = 1;
2832 if (VD->isExceptionVariable())
2833 DiagKind = 2;
2834 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2835 if (FD->isBitField())
2836 DiagKind = 3;
2837 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002838 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2839 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002840 << (TmpAttr.isC11() ? ExpectedVariableOrField
2841 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002842 return;
2843 }
2844 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002845 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002846 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002847 return;
2848 }
2849 }
2850
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002851 if (E->isTypeDependent() || E->isValueDependent()) {
2852 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002853 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2854 AA->setPackExpansion(IsPackExpansion);
2855 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002856 return;
2857 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002858
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002859 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002860 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002861 ExprResult ICE
2862 = VerifyIntegerConstantExpression(E, &Alignment,
2863 diag::err_aligned_attribute_argument_not_int,
2864 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002865 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002866 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002867
2868 // C++11 [dcl.align]p2:
2869 // -- if the constant expression evaluates to zero, the alignment
2870 // specifier shall have no effect
2871 // C11 6.7.5p6:
2872 // An alignment specification of zero has no effect.
2873 if (!(TmpAttr.isAlignas() && !Alignment) &&
2874 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002875 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2876 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002877 return;
2878 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002879
Richard Smith848e1f12013-02-01 08:12:08 +00002880 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002881 // We've already verified it's a power of 2, now let's make sure it's
2882 // 8192 or less.
2883 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002884 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002885 << E->getSourceRange();
2886 return;
2887 }
2888 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002889
Richard Smith44c247f2013-02-22 08:32:16 +00002890 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2891 ICE.take(), SpellingListIndex);
2892 AA->setPackExpansion(IsPackExpansion);
2893 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002894}
2895
Michael Hanaf02bbe2013-02-01 01:19:17 +00002896void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002897 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002898 // FIXME: Cache the number on the Attr object if non-dependent?
2899 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002900 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2901 SpellingListIndex);
2902 AA->setPackExpansion(IsPackExpansion);
2903 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002904}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002905
Richard Smith848e1f12013-02-01 08:12:08 +00002906void Sema::CheckAlignasUnderalignment(Decl *D) {
2907 assert(D->hasAttrs() && "no attributes on decl");
2908
2909 QualType Ty;
2910 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2911 Ty = VD->getType();
2912 else
2913 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002914 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002915 return;
2916
2917 // C++11 [dcl.align]p5, C11 6.7.5/4:
2918 // The combined effect of all alignment attributes in a declaration shall
2919 // not specify an alignment that is less strict than the alignment that
2920 // would otherwise be required for the entity being declared.
2921 AlignedAttr *AlignasAttr = 0;
2922 unsigned Align = 0;
2923 for (specific_attr_iterator<AlignedAttr>
2924 I = D->specific_attr_begin<AlignedAttr>(),
2925 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2926 if (I->isAlignmentDependent())
2927 return;
2928 if (I->isAlignas())
2929 AlignasAttr = *I;
2930 Align = std::max(Align, I->getAlignment(Context));
2931 }
2932
2933 if (AlignasAttr && Align) {
2934 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2935 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2936 if (NaturalAlign > RequestedAlign)
2937 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2938 << Ty << (unsigned)NaturalAlign.getQuantity();
2939 }
2940}
2941
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002942/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002943/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002944///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002945/// Despite what would be logical, the mode attribute is a decl attribute, not a
2946/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2947/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002948static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002949 // This attribute isn't documented, but glibc uses it. It changes
2950 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002951 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002952 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2953 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002954 return;
2955 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002956
Aaron Ballman00e99962013-08-31 01:11:41 +00002957 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2958 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002959
2960 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002961 if (Str.startswith("__") && Str.endswith("__"))
2962 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002963
2964 unsigned DestWidth = 0;
2965 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002966 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002967 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002968 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002969 switch (Str[0]) {
2970 case 'Q': DestWidth = 8; break;
2971 case 'H': DestWidth = 16; break;
2972 case 'S': DestWidth = 32; break;
2973 case 'D': DestWidth = 64; break;
2974 case 'X': DestWidth = 96; break;
2975 case 'T': DestWidth = 128; break;
2976 }
2977 if (Str[1] == 'F') {
2978 IntegerMode = false;
2979 } else if (Str[1] == 'C') {
2980 IntegerMode = false;
2981 ComplexMode = true;
2982 } else if (Str[1] != 'I') {
2983 DestWidth = 0;
2984 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002985 break;
2986 case 4:
2987 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2988 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002989 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002990 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002991 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002992 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002993 break;
2994 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002995 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002996 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002997 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002998 case 11:
2999 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003000 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003001 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003002 }
3003
3004 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003005 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003006 OldTy = TD->getUnderlyingType();
3007 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3008 OldTy = VD->getType();
3009 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003010 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003011 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003012 return;
3013 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003014
John McCall9dd450b2009-09-21 23:43:11 +00003015 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003016 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3017 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003018 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003019 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3020 } else if (ComplexMode) {
3021 if (!OldTy->isComplexType())
3022 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3023 } else {
3024 if (!OldTy->isFloatingType())
3025 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3026 }
3027
Mike Stump87c57ac2009-05-16 07:39:55 +00003028 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3029 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003030 // FIXME: Make sure floating-point mappings are accurate
3031 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003032 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003033 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003034 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003035 }
3036
3037 QualType NewTy;
3038
3039 if (IntegerMode)
3040 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3041 OldTy->isSignedIntegerType());
3042 else
3043 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3044
3045 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003046 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003047 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003048 }
3049
Eli Friedman4735374e2009-03-03 06:41:03 +00003050 if (ComplexMode) {
3051 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003052 }
3053
3054 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003055 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3056 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3057 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003058 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003059
3060 D->addAttr(::new (S.Context)
3061 ModeAttr(Attr.getRange(), S.Context, Name,
3062 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003063}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003064
Chandler Carruthedc2c642011-07-02 00:01:44 +00003065static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003066 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3067 if (!VD->hasGlobalStorage())
3068 S.Diag(Attr.getLoc(),
3069 diag::warn_attribute_requires_functions_or_static_globals)
3070 << Attr.getName();
3071 } else if (!isFunctionOrMethod(D)) {
3072 S.Diag(Attr.getLoc(),
3073 diag::warn_attribute_requires_functions_or_static_globals)
3074 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003075 return;
3076 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003077
Michael Han99315932013-01-24 16:46:58 +00003078 D->addAttr(::new (S.Context)
3079 NoDebugAttr(Attr.getRange(), S.Context,
3080 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003081}
3082
Chandler Carruthedc2c642011-07-02 00:01:44 +00003083static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003084 FunctionDecl *FD = cast<FunctionDecl>(D);
3085 if (!FD->getResultType()->isVoidType()) {
3086 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3087 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3088 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3089 << FD->getType()
3090 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3091 "void");
3092 } else {
3093 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3094 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003095 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003096 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003097 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003098
Aaron Ballman3aff6332013-12-02 19:30:36 +00003099 D->addAttr(::new (S.Context)
3100 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003101 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003102}
3103
Chandler Carruthedc2c642011-07-02 00:01:44 +00003104static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003105 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003106 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003107 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003108 return;
3109 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003110
Michael Han99315932013-01-24 16:46:58 +00003111 D->addAttr(::new (S.Context)
3112 GNUInlineAttr(Attr.getRange(), S.Context,
3113 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003114}
3115
Chandler Carruthedc2c642011-07-02 00:01:44 +00003116static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003117 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003118
Aaron Ballman02df2e02012-12-09 17:45:41 +00003119 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003120 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003121 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3122 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003123 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003124 return;
3125
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003126 if (!isa<ObjCMethodDecl>(D)) {
3127 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3128 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003129 return;
3130 }
3131
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003132 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003133 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003134 D->addAttr(::new (S.Context)
3135 FastCallAttr(Attr.getRange(), S.Context,
3136 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003137 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003138 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003139 D->addAttr(::new (S.Context)
3140 StdCallAttr(Attr.getRange(), S.Context,
3141 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003142 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003143 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003144 D->addAttr(::new (S.Context)
3145 ThisCallAttr(Attr.getRange(), S.Context,
3146 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003147 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003148 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003149 D->addAttr(::new (S.Context)
3150 CDeclAttr(Attr.getRange(), S.Context,
3151 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003152 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003153 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003154 D->addAttr(::new (S.Context)
3155 PascalAttr(Attr.getRange(), S.Context,
3156 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003157 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003158 case AttributeList::AT_MSABI:
3159 D->addAttr(::new (S.Context)
3160 MSABIAttr(Attr.getRange(), S.Context,
3161 Attr.getAttributeSpellingListIndex()));
3162 return;
3163 case AttributeList::AT_SysVABI:
3164 D->addAttr(::new (S.Context)
3165 SysVABIAttr(Attr.getRange(), S.Context,
3166 Attr.getAttributeSpellingListIndex()));
3167 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003168 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003169 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003170 switch (CC) {
3171 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003172 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003173 break;
3174 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003175 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003176 break;
3177 default:
3178 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003179 }
3180
Michael Han99315932013-01-24 16:46:58 +00003181 D->addAttr(::new (S.Context)
3182 PcsAttr(Attr.getRange(), S.Context, PCS,
3183 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003184 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003185 }
Derek Schuffa2020962012-10-16 22:30:41 +00003186 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003187 D->addAttr(::new (S.Context)
3188 PnaclCallAttr(Attr.getRange(), S.Context,
3189 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003190 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003191 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003192 D->addAttr(::new (S.Context)
3193 IntelOclBiccAttr(Attr.getRange(), S.Context,
3194 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003195 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003196
Abramo Bagnara50099372010-04-30 13:10:51 +00003197 default:
3198 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003199 }
3200}
3201
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003202static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3203 const AttributeList &Attr) {
3204 uint32_t ArgNum;
3205 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003206 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003207
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003208 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3209 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003210}
3211
Aaron Ballman02df2e02012-12-09 17:45:41 +00003212bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3213 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003214 if (attr.isInvalid())
3215 return true;
3216
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003217 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003218 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003219 attr.setInvalid();
3220 return true;
3221 }
3222
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003223 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3224 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003225 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003226 case AttributeList::AT_CDecl: CC = CC_C; break;
3227 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3228 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3229 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3230 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003231 case AttributeList::AT_MSABI:
3232 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3233 CC_X86_64Win64;
3234 break;
3235 case AttributeList::AT_SysVABI:
3236 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3237 CC_C;
3238 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003239 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003240 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003241 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003242 attr.setInvalid();
3243 return true;
3244 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003245 if (StrRef == "aapcs") {
3246 CC = CC_AAPCS;
3247 break;
3248 } else if (StrRef == "aapcs-vfp") {
3249 CC = CC_AAPCS_VFP;
3250 break;
3251 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003252
3253 attr.setInvalid();
3254 Diag(attr.getLoc(), diag::err_invalid_pcs);
3255 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003256 }
Derek Schuffa2020962012-10-16 22:30:41 +00003257 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003258 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003259 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003260 }
3261
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003262 const TargetInfo &TI = Context.getTargetInfo();
3263 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3264 if (A == TargetInfo::CCCR_Warning) {
3265 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003266
3267 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3268 if (FD)
3269 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3270 TargetInfo::CCMT_NonMember;
3271 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003272 }
3273
John McCall3882ace2011-01-05 12:14:39 +00003274 return false;
3275}
3276
Chandler Carruthedc2c642011-07-02 00:01:44 +00003277static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003278 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003279
3280 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003281 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003282 return;
3283
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003284 if (!isa<ObjCMethodDecl>(D)) {
3285 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3286 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003287 return;
3288 }
Eli Friedman7044b762009-03-27 21:06:47 +00003289
Michael Han99315932013-01-24 16:46:58 +00003290 D->addAttr(::new (S.Context)
3291 RegparmAttr(Attr.getRange(), S.Context, numParams,
3292 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003293}
3294
3295/// Checks a regparm attribute, returning true if it is ill-formed and
3296/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003297bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3298 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003299 return true;
3300
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003301 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003302 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003303 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003304 }
Eli Friedman7044b762009-03-27 21:06:47 +00003305
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003306 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003307 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003308 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003309 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003310 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003311 }
3312
Douglas Gregore8bbc122011-09-02 00:18:52 +00003313 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003314 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003315 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003316 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003317 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003318 }
3319
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003320 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003321 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003322 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003323 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003324 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003325 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003326 }
3327
John McCall3882ace2011-01-05 12:14:39 +00003328 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003329}
3330
Aaron Ballman66039932013-12-19 00:41:31 +00003331static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3332 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003333 // check the attribute arguments.
3334 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3335 // FIXME: 0 is not okay.
3336 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3337 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003338 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003339
3340 if (!isFunctionOrMethod(D)) {
3341 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3342 << Attr.getName() << ExpectedFunctionOrMethod;
3343 return;
3344 }
3345
Aaron Ballman66039932013-12-19 00:41:31 +00003346 uint32_t MaxThreads, MinBlocks = 0;
3347 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3348 return;
3349 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3350 Attr.getArgAsExpr(1),
3351 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003352 return;
3353
3354 D->addAttr(::new (S.Context)
3355 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3356 MaxThreads, MinBlocks,
3357 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003358}
3359
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003360static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3361 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003362 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003363 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003364 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003365 return;
3366 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003367
3368 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003369 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003370
Aaron Ballman00e99962013-08-31 01:11:41 +00003371 StringRef AttrName = Attr.getName()->getName();
3372 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003373
3374 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3376 << Attr.getName() << ExpectedFunctionOrMethod;
3377 return;
3378 }
3379
3380 uint64_t ArgumentIdx;
3381 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3382 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003383 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003384 return;
3385
3386 uint64_t TypeTagIdx;
3387 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3388 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003389 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003390 return;
3391
3392 bool IsPointer = (AttrName == "pointer_with_type_tag");
3393 if (IsPointer) {
3394 // Ensure that buffer has a pointer type.
3395 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3396 if (!BufferTy->isPointerType()) {
3397 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003398 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003399 }
3400 }
3401
Michael Han99315932013-01-24 16:46:58 +00003402 D->addAttr(::new (S.Context)
3403 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3404 ArgumentIdx, TypeTagIdx, IsPointer,
3405 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003406}
3407
3408static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3409 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003410 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003411 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003412 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003413 return;
3414 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003415
3416 if (!checkAttributeNumArgs(S, Attr, 1))
3417 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003418
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003419 if (!isa<VarDecl>(D)) {
3420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3421 << Attr.getName() << ExpectedVariable;
3422 return;
3423 }
3424
Aaron Ballman00e99962013-08-31 01:11:41 +00003425 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003426 TypeSourceInfo *MatchingCTypeLoc = 0;
3427 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3428 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003429
Michael Han99315932013-01-24 16:46:58 +00003430 D->addAttr(::new (S.Context)
3431 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003432 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003433 Attr.getLayoutCompatible(),
3434 Attr.getMustBeNull(),
3435 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003436}
3437
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003438//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003439// Checker-specific attribute handlers.
3440//===----------------------------------------------------------------------===//
3441
John McCalled433932011-01-25 03:31:58 +00003442static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003443 return type->isDependentType() ||
3444 type->isObjCObjectPointerType() ||
3445 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003446}
3447static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003448 return type->isDependentType() ||
3449 type->isPointerType() ||
3450 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003451}
3452
Chandler Carruthedc2c642011-07-02 00:01:44 +00003453static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003454 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003455 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003456
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003457 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003458 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3459 cf = false;
3460 } else {
3461 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3462 cf = true;
3463 }
3464
3465 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003466 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003467 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003468 return;
3469 }
3470
3471 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003472 param->addAttr(::new (S.Context)
3473 CFConsumedAttr(Attr.getRange(), S.Context,
3474 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003475 else
Michael Han99315932013-01-24 16:46:58 +00003476 param->addAttr(::new (S.Context)
3477 NSConsumedAttr(Attr.getRange(), S.Context,
3478 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003479}
3480
Chandler Carruthedc2c642011-07-02 00:01:44 +00003481static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3482 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003483
John McCalled433932011-01-25 03:31:58 +00003484 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003485
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003486 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003487 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003488 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003489 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003490 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003491 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3492 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003493 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003494 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003495 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003496 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003497 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003498 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003499 return;
3500 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003501
John McCalled433932011-01-25 03:31:58 +00003502 bool typeOK;
3503 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003504 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003505 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003506 case AttributeList::AT_NSReturnsAutoreleased:
3507 case AttributeList::AT_NSReturnsRetained:
3508 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003509 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3510 cf = false;
3511 break;
3512
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003513 case AttributeList::AT_CFReturnsRetained:
3514 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003515 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3516 cf = true;
3517 break;
3518 }
3519
3520 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003521 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003522 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003523 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003524 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003525
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003526 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003527 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003528 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003529 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003530 D->addAttr(::new (S.Context)
3531 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3532 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003533 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003534 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003535 D->addAttr(::new (S.Context)
3536 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3537 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003538 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003539 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003540 D->addAttr(::new (S.Context)
3541 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3542 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003543 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003544 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003545 D->addAttr(::new (S.Context)
3546 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3547 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003548 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003549 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003550 D->addAttr(::new (S.Context)
3551 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3552 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003553 return;
3554 };
3555}
3556
John McCallcf166702011-07-22 08:53:00 +00003557static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3558 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003559 const int EP_ObjCMethod = 1;
3560 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003561
John McCallcf166702011-07-22 08:53:00 +00003562 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003563 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003564 if (isa<ObjCMethodDecl>(D))
3565 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003566 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003567 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003568
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003569 if (!resultType->isReferenceType() &&
3570 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003571 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003572 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003573 << attr.getName()
3574 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003575 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003576
3577 // Drop the attribute.
3578 return;
3579 }
3580
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003581 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003582 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3583 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003584}
3585
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003586static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3587 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003588 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003589
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003590 DeclContext *DC = method->getDeclContext();
3591 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3592 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3593 << attr.getName() << 0;
3594 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3595 return;
3596 }
3597 if (method->getMethodFamily() == OMF_dealloc) {
3598 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3599 << attr.getName() << 1;
3600 return;
3601 }
3602
Michael Han99315932013-01-24 16:46:58 +00003603 method->addAttr(::new (S.Context)
3604 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3605 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003606}
3607
Aaron Ballmanfb763042013-12-02 18:05:46 +00003608static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3609 const AttributeList &Attr) {
3610 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3611 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003612 return;
John McCall32f5fe12011-09-30 05:12:12 +00003613
Aaron Ballmanfb763042013-12-02 18:05:46 +00003614 D->addAttr(::new (S.Context)
3615 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3616 Attr.getAttributeSpellingListIndex()));
3617}
3618
3619static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3620 const AttributeList &Attr) {
3621 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3622 "cf_audited_transfer"))
3623 return;
3624
3625 D->addAttr(::new (S.Context)
3626 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3627 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003628}
3629
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003630static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3631 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003632 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003633
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003634 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003635 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003636 return;
3637 }
3638
3639 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003640 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003641 Attr.getAttributeSpellingListIndex()));
3642}
3643
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003644static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3645 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003646 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003647
3648 if (!Parm) {
3649 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3650 return;
3651 }
3652
3653 D->addAttr(::new (S.Context)
3654 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3655 Attr.getAttributeSpellingListIndex()));
3656}
3657
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003658static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3659 const AttributeList &Attr) {
3660 IdentifierInfo *RelatedClass =
3661 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3662 if (!RelatedClass) {
3663 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3664 return;
3665 }
3666 IdentifierInfo *ClassMethod =
3667 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3668 IdentifierInfo *InstanceMethod =
3669 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3670 D->addAttr(::new (S.Context)
3671 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3672 ClassMethod, InstanceMethod,
3673 Attr.getAttributeSpellingListIndex()));
3674}
3675
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003676static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3677 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003678 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003679 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003680 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003681 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3682 Attr.getAttributeSpellingListIndex()));
3683}
3684
Chandler Carruthedc2c642011-07-02 00:01:44 +00003685static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3686 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003687 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003688
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003689 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003690 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003691}
3692
Chandler Carruthedc2c642011-07-02 00:01:44 +00003693static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3694 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003695 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003696 QualType type = vd->getType();
3697
3698 if (!type->isDependentType() &&
3699 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003700 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003701 << type;
3702 return;
3703 }
3704
3705 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3706
3707 // If we have no lifetime yet, check the lifetime we're presumably
3708 // going to infer.
3709 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3710 lifetime = type->getObjCARCImplicitLifetime();
3711
3712 switch (lifetime) {
3713 case Qualifiers::OCL_None:
3714 assert(type->isDependentType() &&
3715 "didn't infer lifetime for non-dependent type?");
3716 break;
3717
3718 case Qualifiers::OCL_Weak: // meaningful
3719 case Qualifiers::OCL_Strong: // meaningful
3720 break;
3721
3722 case Qualifiers::OCL_ExplicitNone:
3723 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003724 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003725 << (lifetime == Qualifiers::OCL_Autoreleasing);
3726 break;
3727 }
3728
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003729 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003730 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3731 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003732}
3733
Francois Picheta83957a2010-12-19 06:50:37 +00003734//===----------------------------------------------------------------------===//
3735// Microsoft specific attribute handlers.
3736//===----------------------------------------------------------------------===//
3737
Chandler Carruthedc2c642011-07-02 00:01:44 +00003738static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003739 if (!S.LangOpts.CPlusPlus) {
3740 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3741 << Attr.getName() << AttributeLangSupport::C;
3742 return;
3743 }
3744
Aaron Ballman60e705e2013-11-24 20:58:02 +00003745 if (!isa<CXXRecordDecl>(D)) {
3746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3747 << Attr.getName() << ExpectedClass;
3748 return;
3749 }
3750
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003751 StringRef StrRef;
3752 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003753 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003754 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003755
David Majnemer89085342013-08-09 08:56:20 +00003756 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3757 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003758 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3759 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003760
Reid Kleckner140c4a72013-05-17 14:04:52 +00003761 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003762 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003763 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003764 return;
3765 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003766
David Majnemer89085342013-08-09 08:56:20 +00003767 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003768 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003769 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003770 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003771 return;
3772 }
David Majnemer89085342013-08-09 08:56:20 +00003773 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003774 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003775 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003776 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003777 }
Francois Picheta83957a2010-12-19 06:50:37 +00003778
David Majnemer89085342013-08-09 08:56:20 +00003779 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3780 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003781}
3782
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003783/// Handles semantic checking for features that are common to all attributes,
3784/// such as checking whether a parameter was properly specified, or the correct
3785/// number of arguments were passed, etc.
3786static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3787 const AttributeList &Attr) {
3788 // Several attributes carry different semantics than the parsing requires, so
3789 // those are opted out of the common handling.
3790 //
3791 // We also bail on unknown and ignored attributes because those are handled
3792 // as part of the target-specific handling logic.
3793 if (Attr.hasCustomParsing() ||
3794 Attr.getKind() == AttributeList::UnknownAttribute ||
3795 Attr.getKind() == AttributeList::IgnoredAttribute)
3796 return false;
3797
Aaron Ballman3aff6332013-12-02 19:30:36 +00003798 // Check whether the attribute requires specific language extensions to be
3799 // enabled.
3800 if (!Attr.diagnoseLangOpts(S))
3801 return true;
3802
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003803 // If there are no optional arguments, then checking for the argument count
3804 // is trivial.
3805 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3806 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3807 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003808
3809 // Check whether the attribute appertains to the given subject.
3810 if (!Attr.diagnoseAppertainsTo(S, D))
3811 return true;
3812
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003813 return false;
3814}
3815
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003816//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003817// Top Level Sema Entry Points
3818//===----------------------------------------------------------------------===//
3819
Richard Smithf8a75c32013-08-29 00:47:48 +00003820/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3821/// the attribute applies to decls. If the attribute is a type attribute, just
3822/// silently ignore it if a GNU attribute.
3823static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3824 const AttributeList &Attr,
3825 bool IncludeCXX11Attributes) {
3826 if (Attr.isInvalid())
3827 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003828
Richard Smithf8a75c32013-08-29 00:47:48 +00003829 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3830 // instead.
3831 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3832 return;
3833
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003834 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3835 return;
3836
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003837 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003838 case AttributeList::AT_IBAction:
3839 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003840 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3841 case AttributeList::AT_IBOutletCollection:
3842 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003843 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003844 case AttributeList::AT_ObjCGC:
3845 case AttributeList::AT_VectorSize:
3846 case AttributeList::AT_NeonVectorType:
3847 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003848 case AttributeList::AT_Ptr32:
3849 case AttributeList::AT_Ptr64:
3850 case AttributeList::AT_SPtr:
3851 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003852 // Ignore these, these are type attributes, handled by
3853 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003854 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003855 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3856 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3857 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3858 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003859 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003860 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003861 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003862 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003863 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3864 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3865 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003866 handleDependencyAttr(S, scope, D, Attr);
3867 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003868 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003869 case AttributeList::AT_CUDAConstant:
3870 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003871 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003872 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003873 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003874 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003875 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003876 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003877 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3878 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003879 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003880 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003881 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003882 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003883 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003884 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3885 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3886 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003887 case AttributeList::AT_CUDADevice:
3888 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003889 case AttributeList::AT_CUDAHost:
3890 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003891 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3892 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003893 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003894 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003895 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003896 case AttributeList::AT_MayAlias:
3897 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003898 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003899 case AttributeList::AT_NoCommon:
3900 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003901 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003902 case AttributeList::AT_Overloadable:
3903 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003904 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003905 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3906 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003907 case AttributeList::AT_Naked:
3908 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003909 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003910 case AttributeList::AT_NoThrow:
3911 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003912 case AttributeList::AT_CUDAShared:
3913 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003914 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003915
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003916 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003917 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003918 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003919 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003920
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003921 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003922 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3923
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003924 case AttributeList::AT_ObjCRequiresSuper:
3925 handleObjCRequiresSuperAttr(S, D, Attr); break;
3926
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003927 case AttributeList::AT_ObjCBridge:
3928 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003929
3930 case AttributeList::AT_ObjCBridgeMutable:
3931 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003932
3933 case AttributeList::AT_ObjCBridgeRelated:
3934 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003935
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003936 case AttributeList::AT_ObjCDesignatedInitializer:
3937 handleObjCDesignatedInitializer(S, D, Attr); break;
3938
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003939 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003940 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003941 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003942 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003943
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003944 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003945 case AttributeList::AT_CFConsumed:
3946 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3947 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003948 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003949
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_NSReturnsAutoreleased:
3951 case AttributeList::AT_NSReturnsNotRetained:
3952 case AttributeList::AT_CFReturnsNotRetained:
3953 case AttributeList::AT_NSReturnsRetained:
3954 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003955 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00003956 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003957 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003958 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003959 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00003960 case AttributeList::AT_VecTypeHint:
3961 handleVecTypeHint(S, D, Attr); break;
3962
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003963 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003964 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003965
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003966 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
3967 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
3968 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003969 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003970 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003972 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003973 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003974 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00003975 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00003976 handleObjCSuppresProtocolAttr(S, D, Attr);
3977 break;
3978 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003979 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003980 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
3981 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003982 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003983 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00003984 case AttributeList::AT_Visibility:
3985 handleVisibilityAttr(S, D, Attr, false);
3986 break;
3987 case AttributeList::AT_TypeVisibility:
3988 handleVisibilityAttr(S, D, Attr, true);
3989 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00003990 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00003991 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003993 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00003994 case AttributeList::AT_Weak:
3995 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003996 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
3997 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
3998 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003999 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004000 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004001 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004002 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004003 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004004 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004005 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004006 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4007 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4008 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004009 case AttributeList::AT_Const:
4010 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004011 case AttributeList::AT_Pure:
4012 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004013 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4014 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004015 case AttributeList::AT_NoInline:
4016 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004017 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004018 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004019 // Just ignore
4020 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004021 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004022 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004023 case AttributeList::AT_StdCall:
4024 case AttributeList::AT_CDecl:
4025 case AttributeList::AT_FastCall:
4026 case AttributeList::AT_ThisCall:
4027 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004028 case AttributeList::AT_MSABI:
4029 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004030 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004031 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004032 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004033 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004035 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004036 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004037 case AttributeList::AT_OpenCLImageAccess:
4038 handleOpenCLImageAccessAttr(S, D, Attr);
4039 break;
John McCall8d32c052012-05-22 21:28:12 +00004040
4041 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004042 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004043 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004044 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004046 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004047 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004048 case AttributeList::AT_MSInheritance:
4049 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004051 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004052 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004053 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004054
4055 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004056 case AttributeList::AT_AssertExclusiveLock:
4057 handleAssertExclusiveLockAttr(S, D, Attr);
4058 break;
4059 case AttributeList::AT_AssertSharedLock:
4060 handleAssertSharedLockAttr(S, D, Attr);
4061 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004062 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004063 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004064 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004065 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004066 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004068 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004069 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004070 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004071 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004072 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004073 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004074 break;
4075 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004076 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004077 break;
4078 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004079 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004080 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004081 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004082 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004083 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004084 handleGuardedByAttr(S, D, Attr);
4085 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004086 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004087 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004088 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004089 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004090 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004091 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004092 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004093 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004094 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004095 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004096 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004097 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004099 handleLockReturnedAttr(S, D, Attr);
4100 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004101 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004102 handleLocksExcludedAttr(S, D, Attr);
4103 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004105 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004106 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004108 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004109 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004111 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004112 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004113 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004114 handleUnlockFunAttr(S, D, Attr);
4115 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004116 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004117 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004118 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004119 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004120 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004121 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004122
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004123 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004124 case AttributeList::AT_Consumable:
4125 handleConsumableAttr(S, D, Attr);
4126 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004127 case AttributeList::AT_CallableWhen:
4128 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004129 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004130 case AttributeList::AT_ParamTypestate:
4131 handleParamTypestateAttr(S, D, Attr);
4132 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004133 case AttributeList::AT_ReturnTypestate:
4134 handleReturnTypestateAttr(S, D, Attr);
4135 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004136 case AttributeList::AT_SetTypestate:
4137 handleSetTypestateAttr(S, D, Attr);
4138 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004139 case AttributeList::AT_TestTypestate:
4140 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004141 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004142
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004143 // Type safety attributes.
4144 case AttributeList::AT_ArgumentWithTypeTag:
4145 handleArgumentWithTypeTagAttr(S, D, Attr);
4146 break;
4147 case AttributeList::AT_TypeTagForDatatype:
4148 handleTypeTagForDatatypeAttr(S, D, Attr);
4149 break;
4150
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004151 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004152 // Ask target about the attribute.
4153 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4154 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004155 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4156 diag::warn_unhandled_ms_attribute_ignored :
4157 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004158 break;
4159 }
4160}
4161
4162/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4163/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004164void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004165 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004166 bool IncludeCXX11Attributes) {
4167 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004168 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004169
Joey Gouly2cd9db12013-12-13 16:15:28 +00004170 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004171 // GCC accepts
4172 // static int a9 __attribute__((weakref));
4173 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004174 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004175 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004176 cast<NamedDecl>(D)->getNameAsString();
4177 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004178 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004179 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004180
4181 if (!D->hasAttr<OpenCLKernelAttr>()) {
4182 // These attributes cannot be applied to a non-kernel function.
4183 if (D->hasAttr<ReqdWorkGroupSizeAttr>()) {
4184 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4185 << "reqd_work_group_size";
4186 D->setInvalidDecl();
4187 }
4188 if (D->hasAttr<WorkGroupSizeHintAttr>()) {
4189 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4190 << "work_group_size_hint";
4191 D->setInvalidDecl();
4192 }
4193 if (D->hasAttr<VecTypeHintAttr>()) {
4194 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << "vec_type_hint";
4195 D->setInvalidDecl();
4196 }
4197 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004198}
4199
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004200// Annotation attributes are the only attributes allowed after an access
4201// specifier.
4202bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4203 const AttributeList *AttrList) {
4204 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004206 handleAnnotateAttr(*this, ASDecl, *l);
4207 } else {
4208 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4209 return true;
4210 }
4211 }
4212
4213 return false;
4214}
4215
John McCall42856de2011-10-01 05:17:03 +00004216/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4217/// contains any decl attributes that we should warn about.
4218static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4219 for ( ; A; A = A->getNext()) {
4220 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004221 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004222 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4223
4224 if (A->getKind() == AttributeList::UnknownAttribute) {
4225 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4226 << A->getName() << A->getRange();
4227 } else {
4228 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4229 << A->getName() << A->getRange();
4230 }
4231 }
4232}
4233
4234/// checkUnusedDeclAttributes - Given a declarator which is not being
4235/// used to build a declaration, complain about any decl attributes
4236/// which might be lying around on it.
4237void Sema::checkUnusedDeclAttributes(Declarator &D) {
4238 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4239 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4240 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4241 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4242}
4243
Ryan Flynn7d470f32009-07-30 03:15:39 +00004244/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004245/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004246NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4247 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004248 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004249 NamedDecl *NewD = 0;
4250 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004251 FunctionDecl *NewFD;
4252 // FIXME: Missing call to CheckFunctionDeclaration().
4253 // FIXME: Mangling?
4254 // FIXME: Is the qualifier info correct?
4255 // FIXME: Is the DeclContext correct?
4256 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4257 Loc, Loc, DeclarationName(II),
4258 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004259 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004260 FD->hasPrototype(),
4261 false/*isConstexprSpecified*/);
4262 NewD = NewFD;
4263
4264 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004265 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004266
4267 // Fake up parameter variables; they are declared as if this were
4268 // a typedef.
4269 QualType FDTy = FD->getType();
4270 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4271 SmallVector<ParmVarDecl*, 16> Params;
4272 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4273 AE = FT->arg_type_end(); AI != AE; ++AI) {
4274 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4275 Param->setScopeInfo(0, Params.size());
4276 Params.push_back(Param);
4277 }
David Blaikie9c70e042011-09-21 18:16:56 +00004278 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004279 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004280 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4281 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004282 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004283 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004284 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004285 if (VD->getQualifier()) {
4286 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004287 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004288 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004289 }
4290 return NewD;
4291}
4292
James Dennett634962f2012-06-14 21:40:34 +00004293/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004294/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004295void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004296 if (W.getUsed()) return; // only do this once
4297 W.setUsed(true);
4298 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4299 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004300 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004301 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4302 NDId->getName()));
4303 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004304 WeakTopLevelDecl.push_back(NewD);
4305 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4306 // to insert Decl at TU scope, sorry.
4307 DeclContext *SavedContext = CurContext;
4308 CurContext = Context.getTranslationUnitDecl();
4309 PushOnScopeChains(NewD, S);
4310 CurContext = SavedContext;
4311 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004312 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004313 }
4314}
4315
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004316void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4317 // It's valid to "forward-declare" #pragma weak, in which case we
4318 // have to do this.
4319 LoadExternalWeakUndeclaredIdentifiers();
4320 if (!WeakUndeclaredIdentifiers.empty()) {
4321 NamedDecl *ND = NULL;
4322 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4323 if (VD->isExternC())
4324 ND = VD;
4325 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4326 if (FD->isExternC())
4327 ND = FD;
4328 if (ND) {
4329 if (IdentifierInfo *Id = ND->getIdentifier()) {
4330 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4331 = WeakUndeclaredIdentifiers.find(Id);
4332 if (I != WeakUndeclaredIdentifiers.end()) {
4333 WeakInfo W = I->second;
4334 DeclApplyPragmaWeak(S, ND, W);
4335 WeakUndeclaredIdentifiers[Id] = W;
4336 }
4337 }
4338 }
4339 }
4340}
4341
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004342/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4343/// it, apply them to D. This is a bit tricky because PD can have attributes
4344/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004345void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004346 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004347 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004348 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004349
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004350 // Walk the declarator structure, applying decl attributes that were in a type
4351 // position to the decl itself. This handles cases like:
4352 // int *__attr__(x)** D;
4353 // when X is a decl attribute.
4354 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4355 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004356 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004357
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004358 // Finally, apply any attributes on the decl itself.
4359 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004360 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004361}
John McCall28a6aea2009-11-04 02:18:39 +00004362
John McCall31168b02011-06-15 23:02:42 +00004363/// Is the given declaration allowed to use a forbidden type?
4364static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4365 // Private ivars are always okay. Unfortunately, people don't
4366 // always properly make their ivars private, even in system headers.
4367 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004368 // Function declarations in sys headers will be marked unavailable.
4369 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4370 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004371 return false;
4372
4373 // Require it to be declared in a system header.
4374 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4375}
4376
4377/// Handle a delayed forbidden-type diagnostic.
4378static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4379 Decl *decl) {
4380 if (decl && isForbiddenTypeAllowed(S, decl)) {
4381 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4382 "this system declaration uses an unsupported type"));
4383 return;
4384 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004385 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004386 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004387 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004388 // kind of forbidden type messages on unavailable functions.
4389 if (FD->hasAttr<UnavailableAttr>() &&
4390 diag.getForbiddenTypeDiagnostic() ==
4391 diag::err_arc_array_param_no_ownership) {
4392 diag.Triggered = true;
4393 return;
4394 }
4395 }
John McCall31168b02011-06-15 23:02:42 +00004396
4397 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4398 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4399 diag.Triggered = true;
4400}
4401
John McCall2ec85372012-05-07 06:16:41 +00004402void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4403 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004404 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004405 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004406
John McCall2ec85372012-05-07 06:16:41 +00004407 // When delaying diagnostics to run in the context of a parsed
4408 // declaration, we only want to actually emit anything if parsing
4409 // succeeds.
4410 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004411
John McCall2ec85372012-05-07 06:16:41 +00004412 // We emit all the active diagnostics in this pool or any of its
4413 // parents. In general, we'll get one pool for the decl spec
4414 // and a child pool for each declarator; in a decl group like:
4415 // deprecated_typedef foo, *bar, baz();
4416 // only the declarator pops will be passed decls. This is correct;
4417 // we really do need to consider delayed diagnostics from the decl spec
4418 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004419 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004420 do {
John McCall6347b682012-05-07 06:16:58 +00004421 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004422 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4423 // This const_cast is a bit lame. Really, Triggered should be mutable.
4424 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004425 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004426 continue;
4427
John McCallc1465822011-02-14 07:13:47 +00004428 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004429 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004430 case DelayedDiagnostic::Unavailable:
4431 // Don't bother giving deprecation/unavailable diagnostics if
4432 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004433 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004434 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004435 break;
4436
4437 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004438 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004439 break;
John McCall31168b02011-06-15 23:02:42 +00004440
4441 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004442 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004443 break;
John McCall86121512010-01-27 03:50:35 +00004444 }
4445 }
John McCall2ec85372012-05-07 06:16:41 +00004446 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004447}
4448
John McCall6347b682012-05-07 06:16:58 +00004449/// Given a set of delayed diagnostics, re-emit them as if they had
4450/// been delayed in the current context instead of in the given pool.
4451/// Essentially, this just moves them to the current pool.
4452void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4453 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4454 assert(curPool && "re-emitting in undelayed context not supported");
4455 curPool->steal(pool);
4456}
4457
John McCall28a6aea2009-11-04 02:18:39 +00004458static bool isDeclDeprecated(Decl *D) {
4459 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004460 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004461 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004462 // A category implicitly has the availability of the interface.
4463 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4464 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004465 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4466 return false;
4467}
4468
Ted Kremenekb79ee572013-12-18 23:30:06 +00004469static bool isDeclUnavailable(Decl *D) {
4470 do {
4471 if (D->isUnavailable())
4472 return true;
4473 // A category implicitly has the availability of the interface.
4474 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4475 return CatD->getClassInterface()->isUnavailable();
4476 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4477 return false;
4478}
4479
Eli Friedman971bfa12012-08-08 21:52:41 +00004480static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004481DoEmitAvailabilityWarning(Sema &S,
4482 DelayedDiagnostic::DDKind K,
4483 Decl *Ctx,
4484 const NamedDecl *D,
4485 StringRef Message,
4486 SourceLocation Loc,
4487 const ObjCInterfaceDecl *UnknownObjCClass,
4488 const ObjCPropertyDecl *ObjCProperty) {
4489
4490 // Diagnostics for deprecated or unavailable.
4491 unsigned diag, diag_message, diag_fwdclass_message;
4492
4493 // Matches 'diag::note_property_attribute' options.
4494 unsigned property_note_select;
4495
4496 // Matches diag::note_availability_specified_here.
4497 unsigned available_here_select_kind;
4498
4499 // Don't warn if our current context is deprecated or unavailable.
4500 switch (K) {
4501 case DelayedDiagnostic::Deprecation:
4502 if (isDeclDeprecated(Ctx))
4503 return;
4504 diag = diag::warn_deprecated;
4505 diag_message = diag::warn_deprecated_message;
4506 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4507 property_note_select = /* deprecated */ 0;
4508 available_here_select_kind = /* deprecated */ 2;
4509 break;
4510
4511 case DelayedDiagnostic::Unavailable:
4512 if (isDeclUnavailable(Ctx))
4513 return;
4514 diag = diag::err_unavailable;
4515 diag_message = diag::err_unavailable_message;
4516 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4517 property_note_select = /* unavailable */ 1;
4518 available_here_select_kind = /* unavailable */ 0;
4519 break;
4520
4521 default:
4522 llvm_unreachable("Neither a deprecation or unavailable kind");
4523 }
4524
Eli Friedman971bfa12012-08-08 21:52:41 +00004525 DeclarationName Name = D->getDeclName();
4526 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004527 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004528 if (ObjCProperty)
4529 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4530 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004531 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004532 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004533 if (ObjCProperty)
4534 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4535 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004536 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004537 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004538 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4539 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004540
4541 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4542 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004543}
4544
Ted Kremenekb79ee572013-12-18 23:30:06 +00004545void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4546 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004547 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004548 DoEmitAvailabilityWarning(*this,
4549 (DelayedDiagnostic::DDKind) DD.Kind,
4550 Ctx,
4551 DD.getDeprecationDecl(),
4552 DD.getDeprecationMessage(),
4553 DD.Loc,
4554 DD.getUnknownObjCClass(),
4555 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004556}
4557
Ted Kremenekb79ee572013-12-18 23:30:06 +00004558void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4559 NamedDecl *D, StringRef Message,
4560 SourceLocation Loc,
4561 const ObjCInterfaceDecl *UnknownObjCClass,
4562 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004563 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004564 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004565 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4566 UnknownObjCClass,
4567 ObjCProperty,
4568 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004569 return;
4570 }
4571
Ted Kremenekb79ee572013-12-18 23:30:06 +00004572 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4573 DelayedDiagnostic::DDKind K;
4574 switch (AD) {
4575 case AD_Deprecation:
4576 K = DelayedDiagnostic::Deprecation;
4577 break;
4578 case AD_Unavailable:
4579 K = DelayedDiagnostic::Unavailable;
4580 break;
4581 }
4582
4583 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4584 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004585}