blob: 23395cacfec1223d75b3ac419511b96a3f03c278 [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
John McCall5fca7ea2011-03-02 12:29:23 +000035/// These constants match the enumerated choices of
36/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith9eaab4b2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld041a9b2013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballmanf90ccb02013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +000057 ExpectedObjectiveCInterface,
Fariborz Jahanianf720f862013-11-19 17:42:25 +000058 ExpectedMethodOrProperty,
59 ExpectedStructOrUnion,
Aaron Ballmandbb634f2013-11-20 01:35:23 +000060 ExpectedStructOrUnionOrClass,
61 ExpectedType
John McCall5fca7ea2011-03-02 12:29:23 +000062};
63
Chris Lattner58418ff2008-06-29 00:16:31 +000064//===----------------------------------------------------------------------===//
65// Helper functions
66//===----------------------------------------------------------------------===//
67
Chandler Carruthff4c4f02011-07-01 23:49:12 +000068static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000069 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000071 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000072 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000073 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000074 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000075 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000076 Ty = decl->getUnderlyingType();
77 else
78 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000079
Chris Lattner2c6fcf52008-06-26 18:38:35 +000080 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000081 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000082 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000083 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000084
John McCall9dd450b2009-09-21 23:43:11 +000085 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000086}
87
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088// FIXME: We should provide an abstraction around a method or function
89// to provide the following bits of information.
90
Nuno Lopes518e3702009-12-20 23:11:08 +000091/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000092/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093static bool isFunction(const Decl *D) {
94 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000095}
96
97/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000098/// type (function or function-typed variable) or an Objective-C
99/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000101 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000102}
103
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104/// isFunctionOrMethodOrBlock - Return true if the given decl has function
105/// type (function or function-typed variable) or an Objective-C
106/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static bool isFunctionOrMethodOrBlock(const Decl *D) {
108 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000109 return true;
110 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000111 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112 QualType Ty = V->getType();
113 return Ty->isBlockPointerType();
114 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116}
117
John McCall3882ace2011-01-05 12:14:39 +0000118/// Return true if the given decl has a declarator that should have
119/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000120static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000121 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
123 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000124}
125
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000126/// hasFunctionProto - Return true if the given decl has a argument
127/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000128/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129static bool hasFunctionProto(const Decl *D) {
130 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000131 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000132 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000134 return true;
135 }
136}
137
138/// getFunctionOrMethodNumArgs - Return number of function or method
139/// arguments. It is an error to call this on a K&R function (use
140/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
142 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000143 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000145 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000146 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000147}
148
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
150 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000151 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000153 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000156}
157
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000158static QualType getFunctionOrMethodResultType(const Decl *D) {
159 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000160 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000161 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000162}
163
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000164static bool isFunctionOrMethodVariadic(const Decl *D) {
165 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000166 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000167 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000168 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000169 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000170 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000171 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000172 }
173}
174
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000175static bool isInstanceMethod(const Decl *D) {
176 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000177 return MethodDecl->isInstance();
178 return false;
179}
180
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000181static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000182 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000183 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000184 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000185
John McCall96fa4842010-05-17 21:00:27 +0000186 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
187 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
John McCall96fa4842010-05-17 21:00:27 +0000190 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000191
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 // FIXME: Should we walk the chain of classes?
193 return ClsName == &Ctx.Idents.get("NSString") ||
194 ClsName == &Ctx.Idents.get("NSMutableString");
195}
196
Daniel Dunbar980c6692008-09-26 03:32:58 +0000197static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000198 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000199 if (!PT)
200 return false;
201
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000202 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000203 if (!RT)
204 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000205
Daniel Dunbar980c6692008-09-26 03:32:58 +0000206 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000207 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000208 return false;
209
210 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
211}
212
Richard Smithb87c4652013-10-31 21:23:20 +0000213static unsigned getNumAttributeArgs(const AttributeList &Attr) {
214 // FIXME: Include the type in the argument list.
215 return Attr.getNumArgs() + Attr.hasParsedType();
216}
217
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000218/// \brief Check if the attribute has exactly as many args as Num. May
219/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000220static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000221 unsigned Num) {
222 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000223 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
224 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000225 return false;
226 }
227
228 return true;
229}
230
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000231
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000232/// \brief Check if the attribute has at least as many args as Num. May
233/// output an error.
234static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000235 unsigned Num) {
236 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000237 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
238 return false;
239 }
240
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000241 return true;
242}
243
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000244/// \brief Check if IdxExpr is a valid argument index for a function or
245/// instance method D. May output an error.
246///
247/// \returns true if IdxExpr is a valid index.
248static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
249 StringRef AttrName,
250 SourceLocation AttrLoc,
251 unsigned AttrArgNum,
252 const Expr *IdxExpr,
253 uint64_t &Idx)
254{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000255 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000256
257 // In C++ the implicit 'this' function parameter also counts.
258 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000259 bool HP = hasFunctionProto(D);
260 bool HasImplicitThisParam = isInstanceMethod(D);
261 bool IV = HP && isFunctionOrMethodVariadic(D);
262 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
263 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000264
265 llvm::APSInt IdxInt;
266 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
267 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000268 std::string Name = std::string("'") + AttrName.str() + std::string("'");
269 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000270 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000271 return false;
272 }
273
274 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000275 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000276 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
277 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
278 return false;
279 }
280 Idx--; // Convert to zero-based.
281 if (HasImplicitThisParam) {
282 if (Idx == 0) {
283 S.Diag(AttrLoc,
284 diag::err_attribute_invalid_implicit_this_argument)
285 << AttrName << IdxExpr->getSourceRange();
286 return false;
287 }
288 --Idx;
289 }
290
291 return true;
292}
293
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000294/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
295/// If not emit an error and return false. If the argument is an identifier it
296/// will emit an error with a fixit hint and treat it as if it was a string
297/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000298bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
299 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000300 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000301 // Look for identifiers. If we have one emit a hint to fix it to a literal.
302 if (Attr.isArgIdent(ArgNum)) {
303 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000304 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000305 << Attr.getName() << AANT_ArgumentString
306 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000307 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000308 Str = Loc->Ident->getName();
309 if (ArgLocation)
310 *ArgLocation = Loc->Loc;
311 return true;
312 }
313
314 // Now check for an actual string literal.
315 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
316 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
317 if (ArgLocation)
318 *ArgLocation = ArgExpr->getLocStart();
319
320 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000321 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000322 << Attr.getName() << AANT_ArgumentString;
323 return false;
324 }
325
326 Str = Literal->getString();
327 return true;
328}
329
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000330///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000331/// \brief Check if passed in Decl is a field or potentially shared global var
332/// \return true if the Decl is a field or potentially shared global variable
333///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000334static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000335 if (isa<FieldDecl>(D))
336 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000337 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000338 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000339
340 return false;
341}
342
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000343/// \brief Check if the passed-in expression is of type int or bool.
344static bool isIntOrBool(Expr *Exp) {
345 QualType QT = Exp->getType();
346 return QT->isBooleanType() || QT->isIntegerType();
347}
348
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000349
350// Check to see if the type is a smart pointer of some kind. We assume
351// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000352static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
353 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
354 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000355 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000356 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000357
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000358 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
359 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000360 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000361 return false;
362
363 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000364}
365
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000366/// \brief Check if passed in Decl is a pointer type.
367/// Note that this function may produce an error message.
368/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000369static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
370 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000371 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000372 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000373 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000374 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000375
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000376 if (const RecordType *RT = QT->getAs<RecordType>()) {
377 // If it's an incomplete type, it could be a smart pointer; skip it.
378 // (We don't want to force template instantiation if we can avoid it,
379 // since that would alter the order in which templates are instantiated.)
380 if (RT->isIncompleteType())
381 return true;
382
383 if (threadSafetyCheckIsSmartPointer(S, RT))
384 return true;
385 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000386
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000387 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000388 << Attr.getName()->getName() << QT;
389 } else {
390 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
391 << Attr.getName();
392 }
393 return false;
394}
395
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000396/// \brief Checks that the passed in QualType either is of RecordType or points
397/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000398static const RecordType *getRecordType(QualType QT) {
399 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000400 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000401
402 // Now check if we point to record type.
403 if (const PointerType *PT = QT->getAs<PointerType>())
404 return PT->getPointeeType()->getAs<RecordType>();
405
406 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000407}
408
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000409
Jordy Rose740b0c22012-05-08 03:27:22 +0000410static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
411 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000412 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
413 if (RT->getDecl()->getAttr<LockableAttr>())
414 return true;
415 return false;
416}
417
418
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000419/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000420/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000421static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
422 QualType Ty) {
423 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000424
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000425 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000426 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000427 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000428 << Attr.getName() << Ty.getAsString();
429 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000430 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000431
Michael Hana9171bc2012-08-03 17:40:43 +0000432 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000433 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435
436 // Allow smart pointers to be used as lockable objects.
437 // FIXME -- Check the type that the smart pointer points to.
438 if (threadSafetyCheckIsSmartPointer(S, RT))
439 return;
440
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000441 // Check if the type is lockable.
442 RecordDecl *RD = RT->getDecl();
443 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000444 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000445
446 // Else check if any base classes are lockable.
447 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
448 CXXBasePaths BPaths(false, false);
449 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
450 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000451 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452
453 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
454 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000455}
456
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000458/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459/// \param Sidx The attribute argument index to start checking with.
460/// \param ParamIdxOk Whether an argument can be indexing into a function
461/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000462static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 const AttributeList &Attr,
464 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465 int Sidx = 0,
466 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000468 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000469
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000470 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000471 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000472 Args.push_back(ArgExp);
473 continue;
474 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000476 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000477 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000478 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000479 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000480 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000481 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000482 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000483 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000484
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000485 // We allow constant strings to be used as a placeholder for expressions
486 // that are not valid C++ syntax, but warn that they are ignored.
487 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
488 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000489 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000490 continue;
491 }
492
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000493 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000494
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000495 // A pointer to member expression of the form &MyClass::mu is treated
496 // specially -- we need to look at the type of the member.
497 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
498 if (UOp->getOpcode() == UO_AddrOf)
499 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
500 if (DRE->getDecl()->isCXXInstanceMember())
501 ArgTy = DRE->getDecl()->getType();
502
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000503 // First see if we can just cast to record type, or point to record type.
504 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000506 // Now check if we index into a record type function param.
507 if(!RT && ParamIdxOk) {
508 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
510 if(FD && IL) {
511 unsigned int NumParams = FD->getNumParams();
512 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000513 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
514 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
515 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
517 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000518 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000519 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000520 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 }
522 }
523
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000524 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000526 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528}
529
Chris Lattner58418ff2008-06-29 00:16:31 +0000530//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000531// Attribute Implementations
532//===----------------------------------------------------------------------===//
533
Daniel Dunbar032db472008-07-31 22:40:48 +0000534// FIXME: All this manual attribute parsing code is gross. At the
535// least add some helper functions to check most argument patterns (#
536// and types of args).
537
Michael Hana9171bc2012-08-03 17:40:43 +0000538static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000539 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000540 // D must be either a member field or global (potentially shared) variable.
541 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000542 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
543 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000544 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000545 }
546
Michael Han3be3b442012-07-23 18:48:41 +0000547 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000548}
549
Michael Han3be3b442012-07-23 18:48:41 +0000550static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
551 if (!checkGuardedVarAttrCommon(S, D, Attr))
552 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000553
Michael Han99315932013-01-24 16:46:58 +0000554 D->addAttr(::new (S.Context)
555 GuardedVarAttr(Attr.getRange(), S.Context,
556 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000557}
558
Michael Hana9171bc2012-08-03 17:40:43 +0000559static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000560 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000561 if (!checkGuardedVarAttrCommon(S, D, Attr))
562 return;
563
564 if (!threadSafetyCheckIsPointer(S, D, Attr))
565 return;
566
Michael Han99315932013-01-24 16:46:58 +0000567 D->addAttr(::new (S.Context)
568 PtGuardedVarAttr(Attr.getRange(), S.Context,
569 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000570}
571
Michael Hana9171bc2012-08-03 17:40:43 +0000572static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
573 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000574 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000575 // D must be either a member field or global (potentially shared) variable.
576 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000577 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
578 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000579 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000580 }
581
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000582 SmallVector<Expr*, 1> Args;
583 // check that all arguments are lockable objects
584 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
585 unsigned Size = Args.size();
586 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000587 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000588
Michael Han3be3b442012-07-23 18:48:41 +0000589 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000590
Michael Han3be3b442012-07-23 18:48:41 +0000591 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000592}
593
Michael Han3be3b442012-07-23 18:48:41 +0000594static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
595 Expr *Arg = 0;
596 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
597 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598
Michael Han3be3b442012-07-23 18:48:41 +0000599 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
600}
601
Michael Hana9171bc2012-08-03 17:40:43 +0000602static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000603 const AttributeList &Attr) {
604 Expr *Arg = 0;
605 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
606 return;
607
608 if (!threadSafetyCheckIsPointer(S, D, Attr))
609 return;
610
611 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
612 S.Context, Arg));
613}
614
Michael Hana9171bc2012-08-03 17:40:43 +0000615static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000616 const AttributeList &Attr) {
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000617 // FIXME: Lockable structs for C code.
David Blaikie021221d2013-07-29 18:24:03 +0000618 if (!isa<RecordDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
620 << Attr.getName() << ExpectedStructOrUnionOrClass;
Michael Han3be3b442012-07-23 18:48:41 +0000621 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000622 }
623
Michael Han3be3b442012-07-23 18:48:41 +0000624 return true;
625}
626
627static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
628 if (!checkLockableAttrCommon(S, D, Attr))
629 return;
630
631 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
632}
633
Michael Hana9171bc2012-08-03 17:40:43 +0000634static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000635 const AttributeList &Attr) {
636 if (!checkLockableAttrCommon(S, D, Attr))
637 return;
638
Michael Han99315932013-01-24 16:46:58 +0000639 D->addAttr(::new (S.Context)
640 ScopedLockableAttr(Attr.getRange(), S.Context,
641 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000642}
643
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000644static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
645 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000646 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
648 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000649 return;
650 }
651
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000652 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000653 S.Context));
654}
655
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000656static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000657 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000658 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000660 << Attr.getName() << ExpectedFunctionOrMethod;
661 return;
662 }
663
Michael Han99315932013-01-24 16:46:58 +0000664 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000665 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
666 Attr.getAttributeSpellingListIndex()));
667}
668
669static void handleNoSanitizeMemory(Sema &S, Decl *D,
670 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000671 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
672 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
673 << Attr.getName() << ExpectedFunctionOrMethod;
674 return;
675 }
676
677 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
678 S.Context));
679}
680
681static void handleNoSanitizeThread(Sema &S, Decl *D,
682 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000683 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
685 << Attr.getName() << ExpectedFunctionOrMethod;
686 return;
687 }
688
689 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
690 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000691}
692
Michael Hana9171bc2012-08-03 17:40:43 +0000693static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
694 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000695 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000696 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000697 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000698
699 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000700 ValueDecl *VD = dyn_cast<ValueDecl>(D);
701 if (!VD || !mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000702 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
703 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000705 }
706
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000707 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 QualType QT = VD->getType();
709 if (!QT->isDependentType()) {
710 const RecordType *RT = getRecordType(QT);
711 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000712 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000713 << Attr.getName();
714 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000715 }
716 }
717
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000718 // Check that all arguments are lockable objects.
719 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000720 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000721 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000722
Michael Han3be3b442012-07-23 18:48:41 +0000723 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000724}
725
Michael Hana9171bc2012-08-03 17:40:43 +0000726static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000727 const AttributeList &Attr) {
728 SmallVector<Expr*, 1> Args;
729 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
730 return;
731
732 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000733 D->addAttr(::new (S.Context)
734 AcquiredAfterAttr(Attr.getRange(), S.Context,
735 StartArg, Args.size(),
736 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000737}
738
Michael Hana9171bc2012-08-03 17:40:43 +0000739static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000740 const AttributeList &Attr) {
741 SmallVector<Expr*, 1> Args;
742 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
743 return;
744
745 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000746 D->addAttr(::new (S.Context)
747 AcquiredBeforeAttr(Attr.getRange(), S.Context,
748 StartArg, Args.size(),
749 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000750}
751
Michael Hana9171bc2012-08-03 17:40:43 +0000752static bool checkLockFunAttrCommon(Sema &S, Decl *D,
753 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000754 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000755 // zero or more arguments ok
756
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000757 // check that the attribute is applied to a function
758 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
760 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000761 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000762 }
763
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000764 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000765 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000766
Michael Han3be3b442012-07-23 18:48:41 +0000767 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000768}
769
Michael Hana9171bc2012-08-03 17:40:43 +0000770static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000771 const AttributeList &Attr) {
772 SmallVector<Expr*, 1> Args;
773 if (!checkLockFunAttrCommon(S, D, Attr, Args))
774 return;
775
776 unsigned Size = Args.size();
777 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000778 D->addAttr(::new (S.Context)
779 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
780 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000781}
782
Michael Hana9171bc2012-08-03 17:40:43 +0000783static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000784 const AttributeList &Attr) {
785 SmallVector<Expr*, 1> Args;
786 if (!checkLockFunAttrCommon(S, D, Attr, Args))
787 return;
788
789 unsigned Size = Args.size();
790 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000791 D->addAttr(::new (S.Context)
792 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
793 StartArg, Size,
794 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000795}
796
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000797static void handleAssertSharedLockAttr(Sema &S, Decl *D,
798 const AttributeList &Attr) {
799 SmallVector<Expr*, 1> Args;
800 if (!checkLockFunAttrCommon(S, D, Attr, Args))
801 return;
802
803 unsigned Size = Args.size();
804 Expr **StartArg = Size == 0 ? 0 : &Args[0];
805 D->addAttr(::new (S.Context)
806 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
807 Attr.getAttributeSpellingListIndex()));
808}
809
810static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
811 const AttributeList &Attr) {
812 SmallVector<Expr*, 1> Args;
813 if (!checkLockFunAttrCommon(S, D, Attr, Args))
814 return;
815
816 unsigned Size = Args.size();
817 Expr **StartArg = Size == 0 ? 0 : &Args[0];
818 D->addAttr(::new (S.Context)
819 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
820 StartArg, Size,
821 Attr.getAttributeSpellingListIndex()));
822}
823
824
Michael Hana9171bc2012-08-03 17:40:43 +0000825static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
826 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000827 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000828 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000829 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000830
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000831 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000832 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
833 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000834 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000835 }
836
Aaron Ballman00e99962013-08-31 01:11:41 +0000837 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000838 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000839 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000840 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000841 }
842
843 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000844 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000845
Michael Han3be3b442012-07-23 18:48:41 +0000846 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000847}
848
Michael Hana9171bc2012-08-03 17:40:43 +0000849static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000850 const AttributeList &Attr) {
851 SmallVector<Expr*, 2> Args;
852 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
853 return;
854
Michael Han99315932013-01-24 16:46:58 +0000855 D->addAttr(::new (S.Context)
856 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000857 Attr.getArgAsExpr(0),
858 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000859 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000860}
861
Michael Hana9171bc2012-08-03 17:40:43 +0000862static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000863 const AttributeList &Attr) {
864 SmallVector<Expr*, 2> Args;
865 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
866 return;
867
Michael Han99315932013-01-24 16:46:58 +0000868 D->addAttr(::new (S.Context)
869 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000870 Attr.getArgAsExpr(0),
871 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000872 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000873}
874
Michael Hana9171bc2012-08-03 17:40:43 +0000875static bool checkLocksRequiredCommon(Sema &S, Decl *D,
876 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000877 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000878 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000879 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000880
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000881 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000882 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
883 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000884 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000885 }
886
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000887 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000888 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000889 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000890 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000891
Michael Han3be3b442012-07-23 18:48:41 +0000892 return true;
893}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000894
Michael Hana9171bc2012-08-03 17:40:43 +0000895static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000896 const AttributeList &Attr) {
897 SmallVector<Expr*, 1> Args;
898 if (!checkLocksRequiredCommon(S, D, Attr, Args))
899 return;
900
901 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000902 D->addAttr(::new (S.Context)
903 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
904 StartArg, Args.size(),
905 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000906}
907
Michael Hana9171bc2012-08-03 17:40:43 +0000908static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000909 const AttributeList &Attr) {
910 SmallVector<Expr*, 1> Args;
911 if (!checkLocksRequiredCommon(S, D, Attr, Args))
912 return;
913
914 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000915 D->addAttr(::new (S.Context)
916 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
917 StartArg, Args.size(),
918 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000919}
920
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000921static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000922 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000923 // zero or more arguments ok
924
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000925 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000926 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
927 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000928 return;
929 }
930
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000931 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000932 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000933 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000934 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000935 Expr **StartArg = Size == 0 ? 0 : &Args[0];
936
Michael Han99315932013-01-24 16:46:58 +0000937 D->addAttr(::new (S.Context)
938 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
939 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000940}
941
942static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000943 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000944 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
946 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000947 return;
948 }
949
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000950 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000951 SmallVector<Expr*, 1> Args;
952 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
953 unsigned Size = Args.size();
954 if (Size == 0)
955 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000956
Michael Han99315932013-01-24 16:46:58 +0000957 D->addAttr(::new (S.Context)
958 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
959 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000960}
961
962static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000963 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000964 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000965 return;
966
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000967 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000968 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
969 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000970 return;
971 }
972
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000973 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000974 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000975 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000976 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000977 if (Size == 0)
978 return;
979 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000980
Michael Han99315932013-01-24 16:46:58 +0000981 D->addAttr(::new (S.Context)
982 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
983 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000984}
985
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000986static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000987 ConsumableAttr::ConsumedState DefaultState;
988
989 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000990 IdentifierLoc *IL = Attr.getArgAsIdent(0);
991 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
992 DefaultState)) {
993 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
994 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000995 return;
996 }
David Blaikie16f76d22013-09-06 01:28:43 +0000997 } else {
998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
999 << Attr.getName() << AANT_ArgumentIdentifier;
1000 return;
1001 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001002
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001003 if (!isa<CXXRecordDecl>(D)) {
1004 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1005 Attr.getName() << ExpectedClass;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001006 return;
1007 }
1008
1009 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +00001010 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001011 Attr.getAttributeSpellingListIndex()));
1012}
1013
1014static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1015 const AttributeList &Attr) {
1016 ASTContext &CurrContext = S.getASTContext();
1017 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1018
1019 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1020 if (!RD->hasAttr<ConsumableAttr>()) {
1021 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1022 RD->getNameAsString();
1023
1024 return false;
1025 }
1026 }
1027
1028 return true;
1029}
1030
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001031
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001032static void handleCallableWhenAttr(Sema &S, Decl *D,
1033 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001034 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1035 return;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001036
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001037 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001038 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1039 Attr.getName() << ExpectedMethod;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001040 return;
1041 }
1042
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001043 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1044 return;
1045
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001046 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1047 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1048 CallableWhenAttr::ConsumedState CallableState;
1049
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001050 StringRef StateString;
1051 SourceLocation Loc;
1052 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1053 return;
1054
1055 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001056 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001057 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1058 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001059 return;
1060 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001061
1062 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001063 }
1064
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001065 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001066 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1067 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001068}
1069
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001070
DeLesley Hutchins69391772013-10-17 23:23:53 +00001071static void handleParamTypestateAttr(Sema &S, Decl *D,
1072 const AttributeList &Attr) {
1073 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1074
1075 if (!isa<ParmVarDecl>(D)) {
1076 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1077 Attr.getName() << ExpectedParameter;
1078 return;
1079 }
1080
1081 ParamTypestateAttr::ConsumedState ParamState;
1082
1083 if (Attr.isArgIdent(0)) {
1084 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1085 StringRef StateString = Ident->Ident->getName();
1086
1087 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1088 ParamState)) {
1089 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1090 << Attr.getName() << StateString;
1091 return;
1092 }
1093 } else {
1094 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1095 Attr.getName() << AANT_ArgumentIdentifier;
1096 return;
1097 }
1098
1099 // FIXME: This check is currently being done in the analysis. It can be
1100 // enabled here only after the parser propagates attributes at
1101 // template specialization definition, not declaration.
1102 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1103 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1104 //
1105 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1106 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1107 // ReturnType.getAsString();
1108 // return;
1109 //}
1110
1111 D->addAttr(::new (S.Context)
1112 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1113 Attr.getAttributeSpellingListIndex()));
1114}
1115
1116
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001117static void handleReturnTypestateAttr(Sema &S, Decl *D,
1118 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001119 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1120
1121 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1122 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1123 Attr.getName() << ExpectedFunctionMethodOrParameter;
1124 return;
1125 }
1126
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001127 ReturnTypestateAttr::ConsumedState ReturnState;
1128
1129 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001130 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1131 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1132 ReturnState)) {
1133 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1134 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001135 return;
1136 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001137 } else {
1138 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1139 Attr.getName() << AANT_ArgumentIdentifier;
1140 return;
1141 }
1142
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001143 // FIXME: This check is currently being done in the analysis. It can be
1144 // enabled here only after the parser propagates attributes at
1145 // template specialization definition, not declaration.
1146 //QualType ReturnType;
1147 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001148 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1149 // ReturnType = Param->getType();
1150 //
1151 //} else if (const CXXConstructorDecl *Constructor =
1152 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001153 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1154 //
1155 //} else {
1156 //
1157 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1158 //}
1159 //
1160 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1161 //
1162 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1163 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1164 // ReturnType.getAsString();
1165 // return;
1166 //}
1167
1168 D->addAttr(::new (S.Context)
1169 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1170 Attr.getAttributeSpellingListIndex()));
1171}
1172
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001173
1174static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001175 if (!checkAttributeNumArgs(S, Attr, 1))
1176 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001177
1178 if (!isa<CXXMethodDecl>(D)) {
1179 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1180 Attr.getName() << ExpectedMethod;
1181 return;
1182 }
1183
1184 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1185 return;
1186
1187 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001188 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001189 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1190 StringRef Param = Ident->Ident->getName();
1191 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1192 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1193 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001194 return;
1195 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001196 } else {
1197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1198 Attr.getName() << AANT_ArgumentIdentifier;
1199 return;
1200 }
1201
1202 D->addAttr(::new (S.Context)
1203 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1204 Attr.getAttributeSpellingListIndex()));
1205}
1206
Chris Wailes9385f9f2013-10-29 20:28:41 +00001207static void handleTestTypestateAttr(Sema &S, Decl *D,
1208 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001209 if (!checkAttributeNumArgs(S, Attr, 1))
1210 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001211
1212 if (!isa<CXXMethodDecl>(D)) {
1213 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1214 Attr.getName() << ExpectedMethod;
1215 return;
1216 }
1217
1218 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1219 return;
1220
Chris Wailes9385f9f2013-10-29 20:28:41 +00001221 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001222 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001223 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1224 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001225 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001226 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1227 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001228 return;
1229 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001230 } else {
1231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1232 Attr.getName() << AANT_ArgumentIdentifier;
1233 return;
1234 }
1235
1236 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001237 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001238 Attr.getAttributeSpellingListIndex()));
1239}
1240
Chandler Carruthedc2c642011-07-02 00:01:44 +00001241static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1242 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001243 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1244 if (TD == 0) {
1245 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1246 // and type-ids.
Aaron Ballmandbb634f2013-11-20 01:35:23 +00001247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) <<
1248 Attr.getName() << ExpectedType;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001249 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001251
Richard Smith1f5a4322013-01-13 02:11:23 +00001252 // Remember this typedef decl, we will need it later for diagnostics.
1253 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001254}
1255
Chandler Carruthedc2c642011-07-02 00:01:44 +00001256static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001257 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001258 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001259 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001260 // If the alignment is less than or equal to 8 bits, the packed attribute
1261 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001262 if (!FD->getType()->isDependentType() &&
1263 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001264 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001265 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001266 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001267 else
Michael Han99315932013-01-24 16:46:58 +00001268 FD->addAttr(::new (S.Context)
1269 PackedAttr(Attr.getRange(), S.Context,
1270 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001271 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001272 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001273}
1274
Chandler Carruthedc2c642011-07-02 00:01:44 +00001275static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001276 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001277 RD->addAttr(::new (S.Context)
1278 MsStructAttr(Attr.getRange(), S.Context,
1279 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001280 else
Aaron Ballmanb80f94b2013-11-20 22:22:04 +00001281 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1282 << Attr.getName() << ExpectedStructOrUnionOrClass;
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001283}
1284
Chandler Carruthedc2c642011-07-02 00:01:44 +00001285static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001286 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001287 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001288 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001289 D->addAttr(::new (S.Context)
1290 IBActionAttr(Attr.getRange(), S.Context,
1291 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001292 return;
1293 }
1294
Ted Kremenekd68ec812011-02-04 06:54:16 +00001295 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001296}
1297
Ted Kremenek7fd17232011-09-29 07:02:25 +00001298static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1299 // The IBOutlet/IBOutletCollection attributes only apply to instance
1300 // variables or properties of Objective-C classes. The outlet must also
1301 // have an object reference type.
1302 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1303 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001304 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001305 << Attr.getName() << VD->getType() << 0;
1306 return false;
1307 }
1308 }
1309 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1310 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001311 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001312 << Attr.getName() << PD->getType() << 1;
1313 return false;
1314 }
1315 }
1316 else {
1317 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1318 return false;
1319 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001320
Ted Kremenek7fd17232011-09-29 07:02:25 +00001321 return true;
1322}
1323
Chandler Carruthedc2c642011-07-02 00:01:44 +00001324static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001325 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001326 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001327
Michael Han99315932013-01-24 16:46:58 +00001328 D->addAttr(::new (S.Context)
1329 IBOutletAttr(Attr.getRange(), S.Context,
1330 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001331}
1332
Chandler Carruthedc2c642011-07-02 00:01:44 +00001333static void handleIBOutletCollection(Sema &S, Decl *D,
1334 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001335
1336 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001337 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1339 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001340 return;
1341 }
1342
Ted Kremenek7fd17232011-09-29 07:02:25 +00001343 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001344 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001345
Richard Smithb1f9a282013-10-31 01:56:18 +00001346 ParsedType PT;
1347
1348 if (Attr.hasParsedType())
1349 PT = Attr.getTypeArg();
1350 else {
1351 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1352 S.getScopeForContext(D->getDeclContext()->getParent()));
1353 if (!PT) {
1354 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1355 return;
1356 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001357 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001358
Richard Smithb87c4652013-10-31 21:23:20 +00001359 TypeSourceInfo *QTLoc = 0;
1360 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1361 if (!QTLoc)
1362 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001363
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001364 // Diagnose use of non-object type in iboutletcollection attribute.
1365 // FIXME. Gnu attribute extension ignores use of builtin types in
1366 // attributes. So, __attribute__((iboutletcollection(char))) will be
1367 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001368 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001369 S.Diag(Attr.getLoc(),
1370 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1371 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001372 return;
1373 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001374
Michael Han99315932013-01-24 16:46:58 +00001375 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001376 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001377 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001378}
1379
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001380static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001381 if (const RecordType *UT = T->getAsUnionType())
1382 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1383 RecordDecl *UD = UT->getDecl();
1384 for (RecordDecl::field_iterator it = UD->field_begin(),
1385 itend = UD->field_end(); it != itend; ++it) {
1386 QualType QT = it->getType();
1387 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1388 T = QT;
1389 return;
1390 }
1391 }
1392 }
1393}
1394
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001395static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001396 if (!isFunctionOrMethod(D)) {
1397 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001398 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001399 return;
1400 }
1401
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001402 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1403 return;
1404
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001405 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001406 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001407 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001408 uint64_t Idx;
1409 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1410 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001411 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001412
1413 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001414 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001415 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001416 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1417 << Attr.getName() << AANT_ArgumentIntegerConstant
1418 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001419 return;
1420 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001421 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001422 }
1423
1424 // check if the function returns a pointer
1425 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1426 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001427 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001428 }
1429
Michael Han99315932013-01-24 16:46:58 +00001430 D->addAttr(::new (S.Context)
1431 AllocSizeAttr(Attr.getRange(), S.Context,
1432 SizeArgs.data(), SizeArgs.size(),
1433 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001434}
1435
Chandler Carruthedc2c642011-07-02 00:01:44 +00001436static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001437 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1438 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001439 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001440 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001441 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001442 return;
1443 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001444
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001445 SmallVector<unsigned, 8> NonNullArgs;
1446 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001447 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001448 uint64_t Idx;
1449 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1450 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001451 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001452
1453 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001454 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001455 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001456
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001457 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001458 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001459 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001460 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001461 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001462 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001463
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001464 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001465 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001466
1467 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1468 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001469 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001470 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1471 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001472 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001473 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001474 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001475 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001476
Ted Kremenek22813f42010-10-21 18:49:36 +00001477 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001478 if (NonNullArgs.empty()) {
1479 // Warn the trivial case only if attribute is not coming from a
1480 // macro instantiation.
1481 if (Attr.getLoc().isFileID())
1482 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001483 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001484 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001485 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001486
Nick Lewyckye1121512013-01-24 01:12:16 +00001487 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001488 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001489 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001490 D->addAttr(::new (S.Context)
1491 NonNullAttr(Attr.getRange(), S.Context, start, size,
1492 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001493}
1494
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001495static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1496 switch (K) {
1497 case OwnershipAttr::Holds: return "'ownership_holds'";
1498 case OwnershipAttr::Takes: return "'ownership_takes'";
1499 case OwnershipAttr::Returns: return "'ownership_returns'";
1500 }
1501 llvm_unreachable("unknown ownership");
1502}
1503
Chandler Carruthedc2c642011-07-02 00:01:44 +00001504static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001505 // This attribute must be applied to a function declaration. The first
1506 // argument to the attribute must be an identifier, the name of the resource,
1507 // for example: malloc. The following arguments must be argument indexes, the
1508 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001509 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001510 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001511 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001512
Aaron Ballman00e99962013-08-31 01:11:41 +00001513 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001514 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001515 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001516 return;
1517 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001518
Ted Kremenekd21139a2010-07-31 01:52:11 +00001519 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001520 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001521 switch (AL.getKind()) {
1522 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001523 K = OwnershipAttr::Takes;
Aaron Ballman00e99962013-08-31 01:11:41 +00001524 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001525 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001526 return;
1527 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001528 break;
1529 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001530 K = OwnershipAttr::Holds;
Aaron Ballman00e99962013-08-31 01:11:41 +00001531 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001532 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001533 return;
1534 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001535 break;
1536 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001537 K = OwnershipAttr::Returns;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001538
Aaron Ballman00e99962013-08-31 01:11:41 +00001539 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001540 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001541 return;
1542 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001543 break;
1544 default:
1545 // This should never happen given how we are called.
1546 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001547 }
1548
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001549 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001550 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1551 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001552 return;
1553 }
1554
Aaron Ballman00e99962013-08-31 01:11:41 +00001555 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001556
1557 // Normalize the argument, __foo__ becomes foo.
1558 if (Module.startswith("__") && Module.endswith("__"))
1559 Module = Module.substr(2, Module.size() - 4);
1560
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001561 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001562 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1563 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001564 uint64_t Idx;
1565 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001566 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001567 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001568
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001569 // Is the function argument a pointer type?
1570 QualType T = getFunctionOrMethodArgType(D, Idx);
1571 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001572 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001573 case OwnershipAttr::Takes:
1574 case OwnershipAttr::Holds:
1575 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1576 Err = 0;
1577 break;
1578 case OwnershipAttr::Returns:
1579 if (!T->isIntegerType())
1580 Err = 1;
1581 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001582 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001583 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001584 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001585 << Ex->getSourceRange();
1586 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001587 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001588
1589 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001590 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001591 i = D->specific_attr_begin<OwnershipAttr>(),
1592 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1593 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1594 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1595 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1596 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1597 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001598 }
1599 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001600 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001601 }
1602
1603 unsigned* start = OwnershipArgs.data();
1604 unsigned size = OwnershipArgs.size();
1605 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001606
Michael Han99315932013-01-24 16:46:58 +00001607 D->addAttr(::new (S.Context)
1608 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1609 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001610}
1611
Chandler Carruthedc2c642011-07-02 00:01:44 +00001612static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001613 // Check the attribute arguments.
1614 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1616 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001617 return;
1618 }
1619
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001620 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001622 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001623 return;
1624 }
1625
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001626 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001627
Rafael Espindolac18086a2010-02-23 22:00:30 +00001628 // gcc rejects
1629 // class c {
1630 // static int a __attribute__((weakref ("v2")));
1631 // static int b() __attribute__((weakref ("f3")));
1632 // };
1633 // and ignores the attributes of
1634 // void f(void) {
1635 // static int a __attribute__((weakref ("v2")));
1636 // }
1637 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001638 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001639 if (!Ctx->isFileContext()) {
1640 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001641 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001642 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001643 }
1644
1645 // The GCC manual says
1646 //
1647 // At present, a declaration to which `weakref' is attached can only
1648 // be `static'.
1649 //
1650 // It also says
1651 //
1652 // Without a TARGET,
1653 // given as an argument to `weakref' or to `alias', `weakref' is
1654 // equivalent to `weak'.
1655 //
1656 // gcc 4.4.1 will accept
1657 // int a7 __attribute__((weakref));
1658 // as
1659 // int a7 __attribute__((weak));
1660 // This looks like a bug in gcc. We reject that for now. We should revisit
1661 // it if this behaviour is actually used.
1662
Rafael Espindolac18086a2010-02-23 22:00:30 +00001663 // GCC rejects
1664 // static ((alias ("y"), weakref)).
1665 // Should we? How to check that weakref is before or after alias?
1666
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001667 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1668 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1669 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001670 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001671 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001672 // GCC will accept anything as the argument of weakref. Should we
1673 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001674 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1675 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001676
Michael Han99315932013-01-24 16:46:58 +00001677 D->addAttr(::new (S.Context)
1678 WeakRefAttr(Attr.getRange(), S.Context,
1679 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001680}
1681
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001682static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1683 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001684 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001685 return;
1686
Douglas Gregore8bbc122011-09-02 00:18:52 +00001687 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001688 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1689 return;
1690 }
1691
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001692 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001693
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001694 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001695 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001696}
1697
Quentin Colombet4e172062012-11-01 23:55:47 +00001698static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombet4e172062012-11-01 23:55:47 +00001699 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1701 << Attr.getName() << ExpectedFunctionOrMethod;
1702 return;
1703 }
1704
Michael Han99315932013-01-24 16:46:58 +00001705 D->addAttr(::new (S.Context)
1706 MinSizeAttr(Attr.getRange(), S.Context,
1707 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001708}
1709
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001710static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001711 if (!isa<FunctionDecl>(D)) {
1712 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1713 << Attr.getName() << ExpectedFunction;
1714 return;
1715 }
1716
1717 if (D->hasAttr<HotAttr>()) {
1718 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1719 << Attr.getName() << "hot";
1720 return;
1721 }
1722
Michael Han99315932013-01-24 16:46:58 +00001723 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1724 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001725}
1726
1727static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001728 if (!isa<FunctionDecl>(D)) {
1729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1730 << Attr.getName() << ExpectedFunction;
1731 return;
1732 }
1733
1734 if (D->hasAttr<ColdAttr>()) {
1735 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1736 << Attr.getName() << "cold";
1737 return;
1738 }
1739
Michael Han99315932013-01-24 16:46:58 +00001740 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1741 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001742}
1743
Chandler Carruthedc2c642011-07-02 00:01:44 +00001744static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001745 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001747 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001748 return;
1749 }
1750
Michael Han99315932013-01-24 16:46:58 +00001751 D->addAttr(::new (S.Context)
1752 NakedAttr(Attr.getRange(), S.Context,
1753 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001754}
1755
Chandler Carruthedc2c642011-07-02 00:01:44 +00001756static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1757 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001758 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001760 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001761 return;
1762 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001763
Michael Han99315932013-01-24 16:46:58 +00001764 D->addAttr(::new (S.Context)
1765 AlwaysInlineAttr(Attr.getRange(), S.Context,
1766 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001767}
1768
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001769static void handleTLSModelAttr(Sema &S, Decl *D,
1770 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001771 StringRef Model;
1772 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001773 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001774 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001775 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001776
Richard Smithfd3834f2013-04-13 02:43:54 +00001777 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001778 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1779 << Attr.getName() << ExpectedTLSVar;
1780 return;
1781 }
1782
1783 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001784 if (Model != "global-dynamic" && Model != "local-dynamic"
1785 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001786 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001787 return;
1788 }
1789
Michael Han99315932013-01-24 16:46:58 +00001790 D->addAttr(::new (S.Context)
1791 TLSModelAttr(Attr.getRange(), S.Context, Model,
1792 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001793}
1794
Chandler Carruthedc2c642011-07-02 00:01:44 +00001795static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001796 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001797 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001798 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001799 D->addAttr(::new (S.Context)
1800 MallocAttr(Attr.getRange(), S.Context,
1801 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001802 return;
1803 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001804 }
1805
Ted Kremenek08479ae2009-08-15 00:51:46 +00001806 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001807}
1808
Chandler Carruthedc2c642011-07-02 00:01:44 +00001809static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00001810 D->addAttr(::new (S.Context)
1811 MayAliasAttr(Attr.getRange(), S.Context,
1812 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001813}
1814
Chandler Carruthedc2c642011-07-02 00:01:44 +00001815static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001816 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001817 D->addAttr(::new (S.Context)
1818 NoCommonAttr(Attr.getRange(), S.Context,
1819 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001820 else
1821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001822 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001823}
1824
Chandler Carruthedc2c642011-07-02 00:01:44 +00001825static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001826 if (S.LangOpts.CPlusPlus) {
1827 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1828 return;
1829 }
1830
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001831 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001832 D->addAttr(::new (S.Context)
1833 CommonAttr(Attr.getRange(), S.Context,
1834 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001835 else
1836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001837 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001838}
1839
Chandler Carruthedc2c642011-07-02 00:01:44 +00001840static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001841 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001842
1843 if (S.CheckNoReturnAttr(attr)) return;
1844
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001845 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001846 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001847 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001848 return;
1849 }
1850
Michael Han99315932013-01-24 16:46:58 +00001851 D->addAttr(::new (S.Context)
1852 NoReturnAttr(attr.getRange(), S.Context,
1853 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001854}
1855
1856bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001857 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001858 attr.setInvalid();
1859 return true;
1860 }
1861
1862 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001863}
1864
Chandler Carruthedc2c642011-07-02 00:01:44 +00001865static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1866 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001867
1868 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1869 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001870 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1871 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001872 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1873 && !VD->getType()->isFunctionPointerType())) {
1874 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001875 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001876 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001877 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001878 return;
1879 }
1880 }
1881
Michael Han99315932013-01-24 16:46:58 +00001882 D->addAttr(::new (S.Context)
1883 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1884 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001885}
1886
Richard Smith10876ef2013-01-17 01:30:42 +00001887static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1888 const AttributeList &Attr) {
1889 // C++11 [dcl.attr.noreturn]p1:
1890 // The attribute may be applied to the declarator-id in a function
1891 // declaration.
1892 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1893 if (!FD) {
1894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1895 << Attr.getName() << ExpectedFunctionOrMethod;
1896 return;
1897 }
1898
Michael Han99315932013-01-24 16:46:58 +00001899 D->addAttr(::new (S.Context)
1900 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1901 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001902}
1903
John Thompsoncdb847ba2010-08-09 21:53:52 +00001904// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001905static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001906/*
1907 Returning a Vector Class in Registers
1908
Eric Christopherbc638a82010-12-01 22:13:54 +00001909 According to the PPU ABI specifications, a class with a single member of
1910 vector type is returned in memory when used as the return value of a function.
1911 This results in inefficient code when implementing vector classes. To return
1912 the value in a single vector register, add the vecreturn attribute to the
1913 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001914
1915 Example:
1916
1917 struct Vector
1918 {
1919 __vector float xyzw;
1920 } __attribute__((vecreturn));
1921
1922 Vector Add(Vector lhs, Vector rhs)
1923 {
1924 Vector result;
1925 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1926 return result; // This will be returned in a register
1927 }
1928*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001929 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001930 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001931 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001932 return;
1933 }
1934
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001935 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001936 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1937 return;
1938 }
1939
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001940 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001941 int count = 0;
1942
1943 if (!isa<CXXRecordDecl>(record)) {
1944 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1945 return;
1946 }
1947
1948 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1949 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1950 return;
1951 }
1952
Eric Christopherbc638a82010-12-01 22:13:54 +00001953 for (RecordDecl::field_iterator iter = record->field_begin();
1954 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001955 if ((count == 1) || !iter->getType()->isVectorType()) {
1956 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1957 return;
1958 }
1959 count++;
1960 }
1961
Michael Han99315932013-01-24 16:46:58 +00001962 D->addAttr(::new (S.Context)
1963 VecReturnAttr(Attr.getRange(), S.Context,
1964 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001965}
1966
Richard Smithe233fbf2013-01-28 22:42:45 +00001967static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1968 const AttributeList &Attr) {
1969 if (isa<ParmVarDecl>(D)) {
1970 // [[carries_dependency]] can only be applied to a parameter if it is a
1971 // parameter of a function declaration or lambda.
1972 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1973 S.Diag(Attr.getLoc(),
1974 diag::err_carries_dependency_param_not_function_decl);
1975 return;
1976 }
1977 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001979 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001980 return;
1981 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001982
1983 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1984 Attr.getRange(), S.Context,
1985 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001986}
1987
Chandler Carruthedc2c642011-07-02 00:01:44 +00001988static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001989 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001990 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001991 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001992 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001993 return;
1994 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001995
Michael Han99315932013-01-24 16:46:58 +00001996 D->addAttr(::new (S.Context)
1997 UnusedAttr(Attr.getRange(), S.Context,
1998 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001999}
2000
Rafael Espindola70107f92011-10-03 14:59:42 +00002001static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2002 const AttributeList &Attr) {
Rafael Espindola70107f92011-10-03 14:59:42 +00002003 if (!isa<FunctionDecl>(D)) {
2004 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2005 << Attr.getName() << ExpectedFunction;
2006 return;
2007 }
2008
Michael Han99315932013-01-24 16:46:58 +00002009 D->addAttr(::new (S.Context)
2010 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2011 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00002012}
2013
Chandler Carruthedc2c642011-07-02 00:01:44 +00002014static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002015 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002016 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002017 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2018 return;
2019 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002020 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002021 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002022 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002023 return;
2024 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002025
Michael Han99315932013-01-24 16:46:58 +00002026 D->addAttr(::new (S.Context)
2027 UsedAttr(Attr.getRange(), S.Context,
2028 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002029}
2030
Chandler Carruthedc2c642011-07-02 00:01:44 +00002031static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002032 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002033 if (Attr.getNumArgs() > 1) {
2034 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002035 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002036 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002037
Aaron Ballman18a78382013-11-21 00:28:23 +00002038 int priority = ConstructorAttr::DefaultPriority;
Daniel Dunbar032db472008-07-31 22:40:48 +00002039 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002040 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00002041 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002042 if (E->isTypeDependent() || E->isValueDependent() ||
2043 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002044 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002045 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002046 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002047 return;
2048 }
2049 priority = Idx.getZExtValue();
2050 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002051
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002052 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002053 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002054 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002055 return;
2056 }
2057
Michael Han99315932013-01-24 16:46:58 +00002058 D->addAttr(::new (S.Context)
2059 ConstructorAttr(Attr.getRange(), S.Context, priority,
2060 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002061}
2062
Chandler Carruthedc2c642011-07-02 00:01:44 +00002063static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002064 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002065 if (Attr.getNumArgs() > 1) {
2066 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002067 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002068 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002069
Aaron Ballman18a78382013-11-21 00:28:23 +00002070 int priority = DestructorAttr::DefaultPriority;
Daniel Dunbar032db472008-07-31 22:40:48 +00002071 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002072 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00002073 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002074 if (E->isTypeDependent() || E->isValueDependent() ||
2075 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002076 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002077 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002078 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00002079 return;
2080 }
2081 priority = Idx.getZExtValue();
2082 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002083
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002084 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002085 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002086 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002087 return;
2088 }
2089
Michael Han99315932013-01-24 16:46:58 +00002090 D->addAttr(::new (S.Context)
2091 DestructorAttr(Attr.getRange(), S.Context, priority,
2092 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002093}
2094
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002095template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002096static void handleAttrWithMessage(Sema &S, Decl *D,
2097 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002098 unsigned NumArgs = Attr.getNumArgs();
2099 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002100 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002101 return;
2102 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002103
2104 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002105 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002106 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002107 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002108
Michael Han99315932013-01-24 16:46:58 +00002109 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2110 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002111}
2112
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002113static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2114 const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002115 D->addAttr(::new (S.Context)
2116 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2117 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002118}
2119
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002120static void handleObjCRootClassAttr(Sema &S, Decl *D,
2121 const AttributeList &Attr) {
2122 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2124 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002125 return;
2126 }
2127
Michael Han99315932013-01-24 16:46:58 +00002128 D->addAttr(::new (S.Context)
2129 ObjCRootClassAttr(Attr.getRange(), S.Context,
2130 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002131}
2132
Michael Han99315932013-01-24 16:46:58 +00002133static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2134 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002135 if (!isa<ObjCInterfaceDecl>(D)) {
2136 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2137 return;
2138 }
2139
Michael Han99315932013-01-24 16:46:58 +00002140 D->addAttr(::new (S.Context)
2141 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2142 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002143}
2144
Jordy Rose740b0c22012-05-08 03:27:22 +00002145static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2146 IdentifierInfo *Platform,
2147 VersionTuple Introduced,
2148 VersionTuple Deprecated,
2149 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002150 StringRef PlatformName
2151 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2152 if (PlatformName.empty())
2153 PlatformName = Platform->getName();
2154
2155 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2156 // of these steps are needed).
2157 if (!Introduced.empty() && !Deprecated.empty() &&
2158 !(Introduced <= Deprecated)) {
2159 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2160 << 1 << PlatformName << Deprecated.getAsString()
2161 << 0 << Introduced.getAsString();
2162 return true;
2163 }
2164
2165 if (!Introduced.empty() && !Obsoleted.empty() &&
2166 !(Introduced <= Obsoleted)) {
2167 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2168 << 2 << PlatformName << Obsoleted.getAsString()
2169 << 0 << Introduced.getAsString();
2170 return true;
2171 }
2172
2173 if (!Deprecated.empty() && !Obsoleted.empty() &&
2174 !(Deprecated <= Obsoleted)) {
2175 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2176 << 2 << PlatformName << Obsoleted.getAsString()
2177 << 1 << Deprecated.getAsString();
2178 return true;
2179 }
2180
2181 return false;
2182}
2183
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002184/// \brief Check whether the two versions match.
2185///
2186/// If either version tuple is empty, then they are assumed to match. If
2187/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2188static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2189 bool BeforeIsOkay) {
2190 if (X.empty() || Y.empty())
2191 return true;
2192
2193 if (X == Y)
2194 return true;
2195
2196 if (BeforeIsOkay && X < Y)
2197 return true;
2198
2199 return false;
2200}
2201
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002202AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002203 IdentifierInfo *Platform,
2204 VersionTuple Introduced,
2205 VersionTuple Deprecated,
2206 VersionTuple Obsoleted,
2207 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002208 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002209 bool Override,
2210 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002211 VersionTuple MergedIntroduced = Introduced;
2212 VersionTuple MergedDeprecated = Deprecated;
2213 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002214 bool FoundAny = false;
2215
Rafael Espindolac67f2232012-05-10 02:50:16 +00002216 if (D->hasAttrs()) {
2217 AttrVec &Attrs = D->getAttrs();
2218 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2219 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2220 if (!OldAA) {
2221 ++i;
2222 continue;
2223 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002224
Rafael Espindolac67f2232012-05-10 02:50:16 +00002225 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2226 if (OldPlatform != Platform) {
2227 ++i;
2228 continue;
2229 }
2230
2231 FoundAny = true;
2232 VersionTuple OldIntroduced = OldAA->getIntroduced();
2233 VersionTuple OldDeprecated = OldAA->getDeprecated();
2234 VersionTuple OldObsoleted = OldAA->getObsoleted();
2235 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002236
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002237 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2238 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2239 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2240 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002241 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002242 if (Override) {
2243 int Which = -1;
2244 VersionTuple FirstVersion;
2245 VersionTuple SecondVersion;
2246 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2247 Which = 0;
2248 FirstVersion = OldIntroduced;
2249 SecondVersion = Introduced;
2250 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2251 Which = 1;
2252 FirstVersion = Deprecated;
2253 SecondVersion = OldDeprecated;
2254 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2255 Which = 2;
2256 FirstVersion = Obsoleted;
2257 SecondVersion = OldObsoleted;
2258 }
2259
2260 if (Which == -1) {
2261 Diag(OldAA->getLocation(),
2262 diag::warn_mismatched_availability_override_unavail)
2263 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2264 } else {
2265 Diag(OldAA->getLocation(),
2266 diag::warn_mismatched_availability_override)
2267 << Which
2268 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2269 << FirstVersion.getAsString() << SecondVersion.getAsString();
2270 }
2271 Diag(Range.getBegin(), diag::note_overridden_method);
2272 } else {
2273 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2274 Diag(Range.getBegin(), diag::note_previous_attribute);
2275 }
2276
Rafael Espindolac67f2232012-05-10 02:50:16 +00002277 Attrs.erase(Attrs.begin() + i);
2278 --e;
2279 continue;
2280 }
2281
2282 VersionTuple MergedIntroduced2 = MergedIntroduced;
2283 VersionTuple MergedDeprecated2 = MergedDeprecated;
2284 VersionTuple MergedObsoleted2 = MergedObsoleted;
2285
2286 if (MergedIntroduced2.empty())
2287 MergedIntroduced2 = OldIntroduced;
2288 if (MergedDeprecated2.empty())
2289 MergedDeprecated2 = OldDeprecated;
2290 if (MergedObsoleted2.empty())
2291 MergedObsoleted2 = OldObsoleted;
2292
2293 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2294 MergedIntroduced2, MergedDeprecated2,
2295 MergedObsoleted2)) {
2296 Attrs.erase(Attrs.begin() + i);
2297 --e;
2298 continue;
2299 }
2300
2301 MergedIntroduced = MergedIntroduced2;
2302 MergedDeprecated = MergedDeprecated2;
2303 MergedObsoleted = MergedObsoleted2;
2304 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002305 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002306 }
2307
2308 if (FoundAny &&
2309 MergedIntroduced == Introduced &&
2310 MergedDeprecated == Deprecated &&
2311 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002312 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002313
Ted Kremenekb5445722013-04-06 00:34:27 +00002314 // Only create a new attribute if !Override, but we want to do
2315 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002316 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002317 MergedDeprecated, MergedObsoleted) &&
2318 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002319 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2320 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002321 Obsoleted, IsUnavailable, Message,
2322 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002323 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002324 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002325}
2326
Chandler Carruthedc2c642011-07-02 00:01:44 +00002327static void handleAvailabilityAttr(Sema &S, Decl *D,
2328 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002329 if (!checkAttributeNumArgs(S, Attr, 1))
2330 return;
2331 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002332 unsigned Index = Attr.getAttributeSpellingListIndex();
2333
Aaron Ballman00e99962013-08-31 01:11:41 +00002334 IdentifierInfo *II = Platform->Ident;
2335 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2336 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2337 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002338
Rafael Espindolac231fab2013-01-08 21:30:32 +00002339 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2340 if (!ND) {
2341 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2342 return;
2343 }
2344
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002345 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2346 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2347 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002348 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002349 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002350 if (const StringLiteral *SE =
2351 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002352 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002353
Aaron Ballman00e99962013-08-31 01:11:41 +00002354 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002355 Introduced.Version,
2356 Deprecated.Version,
2357 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002358 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002359 /*Override=*/false,
2360 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002361 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002362 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002363}
2364
John McCalld041a9b2013-02-20 01:54:26 +00002365template <class T>
2366static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2367 typename T::VisibilityType value,
2368 unsigned attrSpellingListIndex) {
2369 T *existingAttr = D->getAttr<T>();
2370 if (existingAttr) {
2371 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2372 if (existingValue == value)
2373 return NULL;
2374 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2375 S.Diag(range.getBegin(), diag::note_previous_attribute);
2376 D->dropAttr<T>();
2377 }
2378 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2379}
2380
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002381VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002382 VisibilityAttr::VisibilityType Vis,
2383 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002384 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2385 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002386}
2387
John McCalld041a9b2013-02-20 01:54:26 +00002388TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2389 TypeVisibilityAttr::VisibilityType Vis,
2390 unsigned AttrSpellingListIndex) {
2391 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2392 AttrSpellingListIndex);
2393}
2394
2395static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2396 bool isTypeVisibility) {
2397 // Visibility attributes don't mean anything on a typedef.
2398 if (isa<TypedefNameDecl>(D)) {
2399 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2400 << Attr.getName();
2401 return;
2402 }
2403
2404 // 'type_visibility' can only go on a type or namespace.
2405 if (isTypeVisibility &&
2406 !(isa<TagDecl>(D) ||
2407 isa<ObjCInterfaceDecl>(D) ||
2408 isa<NamespaceDecl>(D))) {
2409 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2410 << Attr.getName() << ExpectedTypeOrNamespace;
2411 return;
2412 }
2413
Benjamin Kramer70370212013-09-09 15:08:57 +00002414 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002415 StringRef TypeStr;
2416 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002417 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002418 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002419
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002420 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002421 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002422 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002423 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002424 return;
2425 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002426
2427 // Complain about attempts to use protected visibility on targets
2428 // (like Darwin) that don't support it.
2429 if (type == VisibilityAttr::Protected &&
2430 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2431 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2432 type = VisibilityAttr::Default;
2433 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002434
Michael Han99315932013-01-24 16:46:58 +00002435 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002436 clang::Attr *newAttr;
2437 if (isTypeVisibility) {
2438 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2439 (TypeVisibilityAttr::VisibilityType) type,
2440 Index);
2441 } else {
2442 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2443 }
2444 if (newAttr)
2445 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002446}
2447
Chandler Carruthedc2c642011-07-02 00:01:44 +00002448static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2449 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002450 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2451 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002452 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002453 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002454 return;
2455 }
2456
Aaron Ballman00e99962013-08-31 01:11:41 +00002457 if (!Attr.isArgIdent(0)) {
2458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2459 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002460 return;
2461 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002462
Aaron Ballman682ee422013-09-11 19:47:58 +00002463 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2464 ObjCMethodFamilyAttr::FamilyKind F;
2465 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2466 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2467 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002468 return;
2469 }
2470
Aaron Ballman682ee422013-09-11 19:47:58 +00002471 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002472 !method->getResultType()->isObjCObjectPointerType()) {
2473 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2474 << method->getResultType();
2475 // Ignore the attribute.
2476 return;
2477 }
2478
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002479 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002480 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002481}
2482
Chandler Carruthedc2c642011-07-02 00:01:44 +00002483static void handleObjCExceptionAttr(Sema &S, Decl *D,
2484 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002485 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2486 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002487 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2488 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002489 return;
2490 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002491
Michael Han99315932013-01-24 16:46:58 +00002492 D->addAttr(::new (S.Context)
2493 ObjCExceptionAttr(Attr.getRange(), S.Context,
2494 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002495}
2496
Chandler Carruthedc2c642011-07-02 00:01:44 +00002497static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002498 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002499 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002500 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002501 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2502 return;
2503 }
2504 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002505 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2506 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002507 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002508 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2509 return;
2510 }
2511 }
2512 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002513 // It is okay to include this attribute on properties, e.g.:
2514 //
2515 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2516 //
2517 // In this case it follows tradition and suppresses an error in the above
2518 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002519 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002520 }
Michael Han99315932013-01-24 16:46:58 +00002521 D->addAttr(::new (S.Context)
2522 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2523 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002524}
2525
Mike Stumpd3bb5572009-07-24 19:02:52 +00002526static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002527handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002528 if (!isa<FunctionDecl>(D)) {
2529 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2530 return;
2531 }
2532
Michael Han99315932013-01-24 16:46:58 +00002533 D->addAttr(::new (S.Context)
2534 OverloadableAttr(Attr.getRange(), S.Context,
2535 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002536}
2537
Chandler Carruthedc2c642011-07-02 00:01:44 +00002538static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002539 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002541 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002542 return;
2543 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002544
Aaron Ballman00e99962013-08-31 01:11:41 +00002545 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002546 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002547 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2548 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2549 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002550 return;
2551 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002552
Michael Han99315932013-01-24 16:46:58 +00002553 D->addAttr(::new (S.Context)
2554 BlocksAttr(Attr.getRange(), S.Context, type,
2555 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002556}
2557
Chandler Carruthedc2c642011-07-02 00:01:44 +00002558static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002559 // check the attribute arguments.
2560 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002561 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002562 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002563 }
2564
Aaron Ballman18a78382013-11-21 00:28:23 +00002565 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002566 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002567 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002568 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002569 if (E->isTypeDependent() || E->isValueDependent() ||
2570 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002572 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002573 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002574 return;
2575 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002576
John McCallb46f2872011-09-09 07:56:05 +00002577 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002578 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2579 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002580 return;
2581 }
John McCallb46f2872011-09-09 07:56:05 +00002582
2583 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002584 }
2585
Aaron Ballman18a78382013-11-21 00:28:23 +00002586 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002587 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002588 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002589 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002590 if (E->isTypeDependent() || E->isValueDependent() ||
2591 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002592 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002593 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002594 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002595 return;
2596 }
2597 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002598
John McCallb46f2872011-09-09 07:56:05 +00002599 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002600 // FIXME: This error message could be improved, it would be nice
2601 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002602 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2603 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002604 return;
2605 }
2606 }
2607
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002608 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002609 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002610 if (isa<FunctionNoProtoType>(FT)) {
2611 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2612 return;
2613 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002614
Chris Lattner9363e312009-03-17 23:03:47 +00002615 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002616 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002617 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002618 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002619 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002620 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002621 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002622 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002623 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002624 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2625 if (!BD->isVariadic()) {
2626 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2627 return;
2628 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002629 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002630 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002631 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002632 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002633 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002634 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002635 int m = Ty->isFunctionPointerType() ? 0 : 1;
2636 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002637 return;
2638 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002639 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002641 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002642 return;
2643 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002644 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002645 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002646 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002647 return;
2648 }
Michael Han99315932013-01-24 16:46:58 +00002649 D->addAttr(::new (S.Context)
2650 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2651 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002652}
2653
Lubos Lunakedc13882013-07-20 15:05:36 +00002654static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002655 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2656 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2657 else
2658 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2659}
2660
Chandler Carruthedc2c642011-07-02 00:01:44 +00002661static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002662 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002664 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002665 return;
2666 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002667
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002668 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2669 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2670 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002671 return;
2672 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002673 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2674 if (MD->getResultType()->isVoidType()) {
2675 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2676 << Attr.getName() << 1;
2677 return;
2678 }
2679
Michael Han99315932013-01-24 16:46:58 +00002680 D->addAttr(::new (S.Context)
2681 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2682 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002683}
2684
Chandler Carruthedc2c642011-07-02 00:01:44 +00002685static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002686 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002687 if (isa<CXXRecordDecl>(D)) {
2688 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2689 return;
2690 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2692 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002693 return;
2694 }
2695
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002696 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002697
Michael Han99315932013-01-24 16:46:58 +00002698 nd->addAttr(::new (S.Context)
2699 WeakAttr(Attr.getRange(), S.Context,
2700 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002701}
2702
Chandler Carruthedc2c642011-07-02 00:01:44 +00002703static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002704 // weak_import only applies to variable & function declarations.
2705 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002706 if (!D->canBeWeakImported(isDef)) {
2707 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002708 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2709 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002710 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002711 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002712 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002713 // Nothing to warn about here.
2714 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002716 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002717
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002718 return;
2719 }
2720
Michael Han99315932013-01-24 16:46:58 +00002721 D->addAttr(::new (S.Context)
2722 WeakImportAttr(Attr.getRange(), S.Context,
2723 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002724}
2725
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002726// Handles reqd_work_group_size and work_group_size_hint.
2727static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002728 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002729 unsigned WGSize[3];
2730 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002731 Expr *E = Attr.getArgAsExpr(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002732 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002733 if (E->isTypeDependent() || E->isValueDependent() ||
2734 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9d695092013-07-30 14:10:17 +00002735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2736 << Attr.getName() << AANT_ArgumentIntegerConstant
2737 << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002738 return;
2739 }
2740 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2741 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002742
2743 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2744 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2745 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2746 if (!(A->getXDim() == WGSize[0] &&
2747 A->getYDim() == WGSize[1] &&
2748 A->getZDim() == WGSize[2])) {
2749 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2750 Attr.getName();
2751 }
2752 }
2753
2754 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2755 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2756 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2757 if (!(A->getXDim() == WGSize[0] &&
2758 A->getYDim() == WGSize[1] &&
2759 A->getZDim() == WGSize[2])) {
2760 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2761 Attr.getName();
2762 }
2763 }
2764
2765 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2766 D->addAttr(::new (S.Context)
2767 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002768 WGSize[0], WGSize[1], WGSize[2],
2769 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002770 else
2771 D->addAttr(::new (S.Context)
2772 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002773 WGSize[0], WGSize[1], WGSize[2],
2774 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002775}
2776
Joey Goulyaba589c2013-03-08 09:42:32 +00002777static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2778 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2779
Aaron Ballman00e99962013-08-31 01:11:41 +00002780 if (!Attr.hasParsedType()) {
2781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2782 << Attr.getName() << 1;
2783 return;
2784 }
2785
Richard Smithb87c4652013-10-31 21:23:20 +00002786 TypeSourceInfo *ParmTSI = 0;
2787 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2788 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002789
2790 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2791 (ParmType->isBooleanType() ||
2792 !ParmType->isIntegralType(S.getASTContext()))) {
2793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2794 << ParmType;
2795 return;
2796 }
2797
2798 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2799 D->hasAttr<VecTypeHintAttr>()) {
2800 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002801 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002802 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2803 return;
2804 }
2805 }
2806
2807 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002808 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002809}
2810
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002811SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002812 StringRef Name,
2813 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002814 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2815 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002816 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002817 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2818 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002819 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002820 }
Michael Han99315932013-01-24 16:46:58 +00002821 return ::new (Context) SectionAttr(Range, Context, Name,
2822 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002823}
2824
Chandler Carruthedc2c642011-07-02 00:01:44 +00002825static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002826 // Make sure that there is a string literal as the sections's single
2827 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002828 StringRef Str;
2829 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002830 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002831 return;
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattner30ba6742009-08-10 19:03:04 +00002833 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002834 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002835 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002836 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002837 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002838 return;
2839 }
Mike Stump11289f42009-09-09 15:08:12 +00002840
Chris Lattner20aee9b2010-01-12 20:58:53 +00002841 // This attribute cannot be applied to local variables.
2842 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002843 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002844 return;
2845 }
Michael Han99315932013-01-24 16:46:58 +00002846
2847 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002848 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002849 if (NewAttr)
2850 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002851}
2852
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002853
Chandler Carruthedc2c642011-07-02 00:01:44 +00002854static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002855 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002856 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002857 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002858 } else {
Michael Han99315932013-01-24 16:46:58 +00002859 D->addAttr(::new (S.Context)
2860 NoThrowAttr(Attr.getRange(), S.Context,
2861 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002862 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002863}
2864
Chandler Carruthedc2c642011-07-02 00:01:44 +00002865static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002866 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002867 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002868 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002869 } else {
Michael Han99315932013-01-24 16:46:58 +00002870 D->addAttr(::new (S.Context)
2871 ConstAttr(Attr.getRange(), S.Context,
2872 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002873 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002874}
2875
Chandler Carruthedc2c642011-07-02 00:01:44 +00002876static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002877 D->addAttr(::new (S.Context)
2878 PureAttr(Attr.getRange(), S.Context,
2879 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002880}
2881
Chandler Carruthedc2c642011-07-02 00:01:44 +00002882static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002883 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002884 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002886 return;
2887 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002888
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002889 Expr *E = Attr.getArgAsExpr(0);
2890 SourceLocation Loc = E->getExprLoc();
2891 FunctionDecl *FD = 0;
2892 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002893
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002894 // gcc only allows for simple identifiers. Since we support more than gcc, we
2895 // will warn the user.
2896 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2897 if (DRE->hasQualifier())
2898 S.Diag(Loc, diag::warn_cleanup_ext);
2899 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2900 NI = DRE->getNameInfo();
2901 if (!FD) {
2902 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2903 << NI.getName();
2904 return;
2905 }
2906 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2907 if (ULE->hasExplicitTemplateArgs())
2908 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002909 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2910 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002911 if (!FD) {
2912 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2913 << NI.getName();
2914 if (ULE->getType() == S.Context.OverloadTy)
2915 S.NoteAllOverloadCandidates(ULE);
2916 return;
2917 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002918 } else {
2919 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002920 return;
2921 }
2922
Anders Carlssond277d792009-01-31 01:16:18 +00002923 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002924 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2925 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002926 return;
2927 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002928
Anders Carlsson723f55d2009-02-07 23:16:50 +00002929 // We're currently more strict than GCC about what function types we accept.
2930 // If this ever proves to be a problem it should be easy to fix.
2931 QualType Ty = S.Context.getPointerType(VD->getType());
2932 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002933 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2934 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002935 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2936 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002937 return;
2938 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002939
Michael Han99315932013-01-24 16:46:58 +00002940 D->addAttr(::new (S.Context)
2941 CleanupAttr(Attr.getRange(), S.Context, FD,
2942 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002943}
2944
Mike Stumpd3bb5572009-07-24 19:02:52 +00002945/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002946/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002947static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002948 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002949 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002950 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002951 return;
2952 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002953
Aaron Ballman00e99962013-08-31 01:11:41 +00002954 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002955 uint64_t ArgIdx;
2956 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2957 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002958 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002959
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002960 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002961 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002962
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002963 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2964 if (not_nsstring_type &&
2965 !isCFStringType(Ty, S.Context) &&
2966 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002967 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002968 // FIXME: Should highlight the actual expression that has the wrong type.
2969 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002970 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002971 << IdxExpr->getSourceRange();
2972 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002973 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002974 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002975 if (!isNSStringType(Ty, S.Context) &&
2976 !isCFStringType(Ty, S.Context) &&
2977 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002978 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002979 // FIXME: Should highlight the actual expression that has the wrong type.
2980 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002981 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002982 << IdxExpr->getSourceRange();
2983 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002984 }
2985
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002986 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2987 // because that has corrected for the implicit this parameter, and is zero-
2988 // based. The attribute expects what the user wrote explicitly.
2989 llvm::APSInt Val;
2990 IdxExpr->EvaluateAsInt(Val, S.Context);
2991
Michael Han99315932013-01-24 16:46:58 +00002992 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002993 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002994 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002995}
2996
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002997enum FormatAttrKind {
2998 CFStringFormat,
2999 NSStringFormat,
3000 StrftimeFormat,
3001 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003002 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003003 InvalidFormat
3004};
3005
3006/// getFormatAttrKind - Map from format attribute names to supported format
3007/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003008static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003009 return llvm::StringSwitch<FormatAttrKind>(Format)
3010 // Check for formats that get handled specially.
3011 .Case("NSString", NSStringFormat)
3012 .Case("CFString", CFStringFormat)
3013 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003014
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003015 // Otherwise, check for supported formats.
3016 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3017 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3018 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003019
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003020 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3021 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003022}
3023
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003024/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003025/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003026static void handleInitPriorityAttr(Sema &S, Decl *D,
3027 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003028 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003029 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3030 return;
3031 }
3032
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003033 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003034 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3035 Attr.setInvalid();
3036 return;
3037 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003038 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003039 if (S.Context.getAsArrayType(T))
3040 T = S.Context.getBaseElementType(T);
3041 if (!T->getAs<RecordType>()) {
3042 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3043 Attr.setInvalid();
3044 return;
3045 }
3046
Aaron Ballman00e99962013-08-31 01:11:41 +00003047 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003048
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003049 llvm::APSInt priority(32);
3050 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3051 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9d695092013-07-30 14:10:17 +00003052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3053 << Attr.getName() << AANT_ArgumentIntegerConstant
3054 << priorityExpr->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003055 Attr.setInvalid();
3056 return;
3057 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00003058 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003059 if (prioritynum < 101 || prioritynum > 65535) {
3060 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3061 << priorityExpr->getSourceRange();
3062 Attr.setInvalid();
3063 return;
3064 }
Michael Han99315932013-01-24 16:46:58 +00003065 D->addAttr(::new (S.Context)
3066 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3067 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003068}
3069
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003070FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3071 IdentifierInfo *Format, int FormatIdx,
3072 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003073 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003074 // Check whether we already have an equivalent format attribute.
3075 for (specific_attr_iterator<FormatAttr>
3076 i = D->specific_attr_begin<FormatAttr>(),
3077 e = D->specific_attr_end<FormatAttr>();
3078 i != e ; ++i) {
3079 FormatAttr *f = *i;
3080 if (f->getType() == Format &&
3081 f->getFormatIdx() == FormatIdx &&
3082 f->getFirstArg() == FirstArg) {
3083 // If we don't have a valid location for this attribute, adopt the
3084 // location.
3085 if (f->getLocation().isInvalid())
3086 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003087 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003088 }
3089 }
3090
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003091 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3092 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003093}
3094
Mike Stumpd3bb5572009-07-24 19:02:52 +00003095/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003096/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003097static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003098 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003100 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003101 return;
3102 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003103
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003104 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003105 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003106 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003107 return;
3108 }
3109
Chandler Carruth743682b2010-11-16 08:35:43 +00003110 // In C++ the implicit 'this' function parameter also counts, and they are
3111 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003112 bool HasImplicitThisParam = isInstanceMethod(D);
3113 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003114 unsigned FirstIdx = 1;
3115
Aaron Ballman00e99962013-08-31 01:11:41 +00003116 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3117 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003118
3119 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003120 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003121 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003122 // If we've modified the string name, we need a new identifier for it.
3123 II = &S.Context.Idents.get(Format);
3124 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003125
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003126 // Check for supported formats.
3127 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003128
3129 if (Kind == IgnoredFormat)
3130 return;
3131
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003132 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003133 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003134 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003135 return;
3136 }
3137
3138 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003139 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003140 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003141 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3142 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003144 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00003145 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003146 return;
3147 }
3148
3149 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003151 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003152 return;
3153 }
3154
3155 // FIXME: Do we need to bounds check?
3156 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003157
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003158 if (HasImplicitThisParam) {
3159 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003160 S.Diag(Attr.getLoc(),
3161 diag::err_format_attribute_implicit_this_format_string)
3162 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003163 return;
3164 }
3165 ArgIdx--;
3166 }
Mike Stump11289f42009-09-09 15:08:12 +00003167
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003168 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003169 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003170
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003171 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003172 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003173 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3174 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003175 return;
3176 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003177 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003178 // FIXME: do we need to check if the type is NSString*? What are the
3179 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003180 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003181 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003182 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3183 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003184 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003185 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003186 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003187 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003188 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003189 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3190 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003191 return;
3192 }
3193
3194 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003195 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003196 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003197 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3198 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003199 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003200 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00003201 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003202 return;
3203 }
3204
3205 // check if the function is variadic if the 3rd argument non-zero
3206 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003207 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003208 ++NumArgs; // +1 for ...
3209 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003210 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003211 return;
3212 }
3213 }
3214
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003215 // strftime requires FirstArg to be 0 because it doesn't read from any
3216 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003217 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003219 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3220 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003221 return;
3222 }
3223 // if 0 it disables parameter checking (to use with e.g. va_list)
3224 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003226 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003227 return;
3228 }
3229
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003230 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003231 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003232 FirstArg.getZExtValue(),
3233 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003234 if (NewAttr)
3235 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003236}
3237
Chandler Carruthedc2c642011-07-02 00:01:44 +00003238static void handleTransparentUnionAttr(Sema &S, Decl *D,
3239 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003240 // Try to find the underlying union declaration.
3241 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003242 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003243 if (TD && TD->getUnderlyingType()->isUnionType())
3244 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3245 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003246 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003247
3248 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003249 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003250 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003251 return;
3252 }
3253
John McCallf937c022011-10-07 06:10:15 +00003254 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003255 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003256 diag::warn_transparent_union_attribute_not_definition);
3257 return;
3258 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003259
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003260 RecordDecl::field_iterator Field = RD->field_begin(),
3261 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003262 if (Field == FieldEnd) {
3263 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3264 return;
3265 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003266
David Blaikie40ed2972012-06-06 20:45:41 +00003267 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003268 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003269 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003270 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003271 diag::warn_transparent_union_attribute_floating)
3272 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003273 return;
3274 }
3275
3276 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3277 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3278 for (; Field != FieldEnd; ++Field) {
3279 QualType FieldType = Field->getType();
3280 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3281 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3282 // Warn if we drop the attribute.
3283 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003284 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003285 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003286 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003287 diag::warn_transparent_union_attribute_field_size_align)
3288 << isSize << Field->getDeclName() << FieldBits;
3289 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003290 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003291 diag::note_transparent_union_first_field_size_align)
3292 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003293 return;
3294 }
3295 }
3296
Michael Han99315932013-01-24 16:46:58 +00003297 RD->addAttr(::new (S.Context)
3298 TransparentUnionAttr(Attr.getRange(), S.Context,
3299 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003300}
3301
Chandler Carruthedc2c642011-07-02 00:01:44 +00003302static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003303 // Make sure that there is a string literal as the annotation's single
3304 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003305 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003306 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003307 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003308
3309 // Don't duplicate annotations that are already set.
3310 for (specific_attr_iterator<AnnotateAttr>
3311 i = D->specific_attr_begin<AnnotateAttr>(),
3312 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003313 if ((*i)->getAnnotation() == Str)
3314 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003315 }
Michael Han99315932013-01-24 16:46:58 +00003316
3317 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003318 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003319 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003320}
3321
Chandler Carruthedc2c642011-07-02 00:01:44 +00003322static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003323 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003324 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3326 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003327 return;
3328 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003329
Richard Smith848e1f12013-02-01 08:12:08 +00003330 if (Attr.getNumArgs() == 0) {
3331 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3332 true, 0, Attr.getAttributeSpellingListIndex()));
3333 return;
3334 }
3335
Aaron Ballman00e99962013-08-31 01:11:41 +00003336 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003337 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3338 S.Diag(Attr.getEllipsisLoc(),
3339 diag::err_pack_expansion_without_parameter_packs);
3340 return;
3341 }
3342
3343 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3344 return;
3345
3346 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3347 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003348}
3349
3350void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003351 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003352 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3353 SourceLocation AttrLoc = AttrRange.getBegin();
3354
Richard Smith1dba27c2013-01-29 09:02:09 +00003355 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003356 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003357 // C++11 [dcl.align]p1:
3358 // An alignment-specifier may be applied to a variable or to a class
3359 // data member, but it shall not be applied to a bit-field, a function
3360 // parameter, the formal parameter of a catch clause, or a variable
3361 // declared with the register storage class specifier. An
3362 // alignment-specifier may also be applied to the declaration of a class
3363 // or enumeration type.
3364 // C11 6.7.5/2:
3365 // An alignment attribute shall not be specified in a declaration of
3366 // a typedef, or a bit-field, or a function, or a parameter, or an
3367 // object declared with the register storage-class specifier.
3368 int DiagKind = -1;
3369 if (isa<ParmVarDecl>(D)) {
3370 DiagKind = 0;
3371 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3372 if (VD->getStorageClass() == SC_Register)
3373 DiagKind = 1;
3374 if (VD->isExceptionVariable())
3375 DiagKind = 2;
3376 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3377 if (FD->isBitField())
3378 DiagKind = 3;
3379 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003380 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3381 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003382 << (TmpAttr.isC11() ? ExpectedVariableOrField
3383 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003384 return;
3385 }
3386 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003387 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003388 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003389 return;
3390 }
3391 }
3392
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003393 if (E->isTypeDependent() || E->isValueDependent()) {
3394 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003395 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3396 AA->setPackExpansion(IsPackExpansion);
3397 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003398 return;
3399 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003400
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003401 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003402 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003403 ExprResult ICE
3404 = VerifyIntegerConstantExpression(E, &Alignment,
3405 diag::err_aligned_attribute_argument_not_int,
3406 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003407 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003408 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003409
3410 // C++11 [dcl.align]p2:
3411 // -- if the constant expression evaluates to zero, the alignment
3412 // specifier shall have no effect
3413 // C11 6.7.5p6:
3414 // An alignment specification of zero has no effect.
3415 if (!(TmpAttr.isAlignas() && !Alignment) &&
3416 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003417 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3418 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003419 return;
3420 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003421
Richard Smith848e1f12013-02-01 08:12:08 +00003422 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003423 // We've already verified it's a power of 2, now let's make sure it's
3424 // 8192 or less.
3425 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003426 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003427 << E->getSourceRange();
3428 return;
3429 }
3430 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003431
Richard Smith44c247f2013-02-22 08:32:16 +00003432 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3433 ICE.take(), SpellingListIndex);
3434 AA->setPackExpansion(IsPackExpansion);
3435 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003436}
3437
Michael Hanaf02bbe2013-02-01 01:19:17 +00003438void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003439 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003440 // FIXME: Cache the number on the Attr object if non-dependent?
3441 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003442 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3443 SpellingListIndex);
3444 AA->setPackExpansion(IsPackExpansion);
3445 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003446}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003447
Richard Smith848e1f12013-02-01 08:12:08 +00003448void Sema::CheckAlignasUnderalignment(Decl *D) {
3449 assert(D->hasAttrs() && "no attributes on decl");
3450
3451 QualType Ty;
3452 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3453 Ty = VD->getType();
3454 else
3455 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003456 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003457 return;
3458
3459 // C++11 [dcl.align]p5, C11 6.7.5/4:
3460 // The combined effect of all alignment attributes in a declaration shall
3461 // not specify an alignment that is less strict than the alignment that
3462 // would otherwise be required for the entity being declared.
3463 AlignedAttr *AlignasAttr = 0;
3464 unsigned Align = 0;
3465 for (specific_attr_iterator<AlignedAttr>
3466 I = D->specific_attr_begin<AlignedAttr>(),
3467 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3468 if (I->isAlignmentDependent())
3469 return;
3470 if (I->isAlignas())
3471 AlignasAttr = *I;
3472 Align = std::max(Align, I->getAlignment(Context));
3473 }
3474
3475 if (AlignasAttr && Align) {
3476 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3477 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3478 if (NaturalAlign > RequestedAlign)
3479 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3480 << Ty << (unsigned)NaturalAlign.getQuantity();
3481 }
3482}
3483
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003484/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003485/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003486///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003487/// Despite what would be logical, the mode attribute is a decl attribute, not a
3488/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3489/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003490static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003491 // This attribute isn't documented, but glibc uses it. It changes
3492 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003493 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003494 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3495 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003496 return;
3497 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003498
Aaron Ballman00e99962013-08-31 01:11:41 +00003499 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3500 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003501
3502 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003503 if (Str.startswith("__") && Str.endswith("__"))
3504 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003505
3506 unsigned DestWidth = 0;
3507 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003508 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003509 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003510 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003511 switch (Str[0]) {
3512 case 'Q': DestWidth = 8; break;
3513 case 'H': DestWidth = 16; break;
3514 case 'S': DestWidth = 32; break;
3515 case 'D': DestWidth = 64; break;
3516 case 'X': DestWidth = 96; break;
3517 case 'T': DestWidth = 128; break;
3518 }
3519 if (Str[1] == 'F') {
3520 IntegerMode = false;
3521 } else if (Str[1] == 'C') {
3522 IntegerMode = false;
3523 ComplexMode = true;
3524 } else if (Str[1] != 'I') {
3525 DestWidth = 0;
3526 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003527 break;
3528 case 4:
3529 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3530 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003531 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003532 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003533 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003534 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003535 break;
3536 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003537 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003538 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003539 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003540 case 11:
3541 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003542 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003543 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003544 }
3545
3546 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003547 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003548 OldTy = TD->getUnderlyingType();
3549 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3550 OldTy = VD->getType();
3551 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003552 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003553 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003554 return;
3555 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003556
John McCall9dd450b2009-09-21 23:43:11 +00003557 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003558 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3559 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003560 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003561 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3562 } else if (ComplexMode) {
3563 if (!OldTy->isComplexType())
3564 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3565 } else {
3566 if (!OldTy->isFloatingType())
3567 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3568 }
3569
Mike Stump87c57ac2009-05-16 07:39:55 +00003570 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3571 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003572 // FIXME: Make sure floating-point mappings are accurate
3573 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003574 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003575 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003576 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003577 }
3578
3579 QualType NewTy;
3580
3581 if (IntegerMode)
3582 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3583 OldTy->isSignedIntegerType());
3584 else
3585 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3586
3587 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003588 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003589 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003590 }
3591
Eli Friedman4735374e2009-03-03 06:41:03 +00003592 if (ComplexMode) {
3593 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003594 }
3595
3596 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003597 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3598 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3599 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003600 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003601
3602 D->addAttr(::new (S.Context)
3603 ModeAttr(Attr.getRange(), S.Context, Name,
3604 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003605}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003606
Chandler Carruthedc2c642011-07-02 00:01:44 +00003607static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003608 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3609 if (!VD->hasGlobalStorage())
3610 S.Diag(Attr.getLoc(),
3611 diag::warn_attribute_requires_functions_or_static_globals)
3612 << Attr.getName();
3613 } else if (!isFunctionOrMethod(D)) {
3614 S.Diag(Attr.getLoc(),
3615 diag::warn_attribute_requires_functions_or_static_globals)
3616 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003617 return;
3618 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003619
Michael Han99315932013-01-24 16:46:58 +00003620 D->addAttr(::new (S.Context)
3621 NoDebugAttr(Attr.getRange(), S.Context,
3622 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003623}
3624
Chandler Carruthedc2c642011-07-02 00:01:44 +00003625static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003626 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003627 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003628 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003629 return;
3630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003631
Michael Han99315932013-01-24 16:46:58 +00003632 D->addAttr(::new (S.Context)
3633 NoInlineAttr(Attr.getRange(), S.Context,
3634 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003635}
3636
Chandler Carruthedc2c642011-07-02 00:01:44 +00003637static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3638 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003639 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003641 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003642 return;
3643 }
3644
Michael Han99315932013-01-24 16:46:58 +00003645 D->addAttr(::new (S.Context)
3646 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3647 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003648}
3649
Chandler Carruthedc2c642011-07-02 00:01:44 +00003650static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003651 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003652 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003654 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003655 return;
3656 }
3657
Michael Han99315932013-01-24 16:46:58 +00003658 D->addAttr(::new (S.Context)
3659 CUDAConstantAttr(Attr.getRange(), S.Context,
3660 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003661 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003662 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003663 }
3664}
3665
Chandler Carruthedc2c642011-07-02 00:01:44 +00003666static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003667 if (S.LangOpts.CUDA) {
3668 // check the attribute arguments.
3669 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3671 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003672 return;
3673 }
3674
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003675 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003676 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003677 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003678 return;
3679 }
3680
Michael Han99315932013-01-24 16:46:58 +00003681 D->addAttr(::new (S.Context)
3682 CUDADeviceAttr(Attr.getRange(), S.Context,
3683 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003684 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003685 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003686 }
3687}
3688
Chandler Carruthedc2c642011-07-02 00:01:44 +00003689static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003690 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003691 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003692 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003693 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003694 return;
3695 }
3696
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003697 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003698 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003699 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003700 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003701 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3702 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003703 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003704 "void");
3705 } else {
3706 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3707 << FD->getType();
3708 }
3709 return;
3710 }
3711
Michael Han99315932013-01-24 16:46:58 +00003712 D->addAttr(::new (S.Context)
3713 CUDAGlobalAttr(Attr.getRange(), S.Context,
3714 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003715 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003716 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003717 }
3718}
3719
Chandler Carruthedc2c642011-07-02 00:01:44 +00003720static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003721 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003722 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003723 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003724 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003725 return;
3726 }
3727
Michael Han99315932013-01-24 16:46:58 +00003728 D->addAttr(::new (S.Context)
3729 CUDAHostAttr(Attr.getRange(), S.Context,
3730 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003731 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003732 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003733 }
3734}
3735
Chandler Carruthedc2c642011-07-02 00:01:44 +00003736static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003737 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003738 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003740 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003741 return;
3742 }
3743
Michael Han99315932013-01-24 16:46:58 +00003744 D->addAttr(::new (S.Context)
3745 CUDASharedAttr(Attr.getRange(), S.Context,
3746 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003747 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003748 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003749 }
3750}
3751
Chandler Carruthedc2c642011-07-02 00:01:44 +00003752static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003753 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003754 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003755 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003756 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003757 return;
3758 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003759
Douglas Gregor35b57532009-10-27 21:01:01 +00003760 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003761 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003762 return;
3763 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003764
Michael Han99315932013-01-24 16:46:58 +00003765 D->addAttr(::new (S.Context)
3766 GNUInlineAttr(Attr.getRange(), S.Context,
3767 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003768}
3769
Chandler Carruthedc2c642011-07-02 00:01:44 +00003770static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003771 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003772
Aaron Ballman02df2e02012-12-09 17:45:41 +00003773 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003774 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003775 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3776 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003777 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003778 return;
3779
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003780 if (!isa<ObjCMethodDecl>(D)) {
3781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3782 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003783 return;
3784 }
3785
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003786 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003787 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003788 D->addAttr(::new (S.Context)
3789 FastCallAttr(Attr.getRange(), S.Context,
3790 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003791 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003792 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003793 D->addAttr(::new (S.Context)
3794 StdCallAttr(Attr.getRange(), S.Context,
3795 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003796 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003797 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003798 D->addAttr(::new (S.Context)
3799 ThisCallAttr(Attr.getRange(), S.Context,
3800 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003801 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003802 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003803 D->addAttr(::new (S.Context)
3804 CDeclAttr(Attr.getRange(), S.Context,
3805 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003806 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003807 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003808 D->addAttr(::new (S.Context)
3809 PascalAttr(Attr.getRange(), S.Context,
3810 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003811 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003812 case AttributeList::AT_MSABI:
3813 D->addAttr(::new (S.Context)
3814 MSABIAttr(Attr.getRange(), S.Context,
3815 Attr.getAttributeSpellingListIndex()));
3816 return;
3817 case AttributeList::AT_SysVABI:
3818 D->addAttr(::new (S.Context)
3819 SysVABIAttr(Attr.getRange(), S.Context,
3820 Attr.getAttributeSpellingListIndex()));
3821 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003822 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003823 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003824 switch (CC) {
3825 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003826 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003827 break;
3828 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003829 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003830 break;
3831 default:
3832 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003833 }
3834
Michael Han99315932013-01-24 16:46:58 +00003835 D->addAttr(::new (S.Context)
3836 PcsAttr(Attr.getRange(), S.Context, PCS,
3837 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003838 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003839 }
Derek Schuffa2020962012-10-16 22:30:41 +00003840 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003841 D->addAttr(::new (S.Context)
3842 PnaclCallAttr(Attr.getRange(), S.Context,
3843 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003844 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003845 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003846 D->addAttr(::new (S.Context)
3847 IntelOclBiccAttr(Attr.getRange(), S.Context,
3848 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003849 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003850
Abramo Bagnara50099372010-04-30 13:10:51 +00003851 default:
3852 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003853 }
3854}
3855
Chandler Carruthedc2c642011-07-02 00:01:44 +00003856static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003857 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003858}
3859
Guy Benyeifb36ede2013-03-24 13:58:12 +00003860static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman00e99962013-08-31 01:11:41 +00003861 Expr *E = Attr.getArgAsExpr(0);
Guy Benyeifb36ede2013-03-24 13:58:12 +00003862 llvm::APSInt ArgNum(32);
3863 if (E->isTypeDependent() || E->isValueDependent() ||
3864 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9d695092013-07-30 14:10:17 +00003865 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3866 << Attr.getName() << AANT_ArgumentIntegerConstant
3867 << E->getSourceRange();
Guy Benyeifb36ede2013-03-24 13:58:12 +00003868 return;
3869 }
3870
3871 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3872 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3873}
3874
Aaron Ballman02df2e02012-12-09 17:45:41 +00003875bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3876 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003877 if (attr.isInvalid())
3878 return true;
3879
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003880 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003881 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003882 attr.setInvalid();
3883 return true;
3884 }
3885
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003886 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3887 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003888 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003889 case AttributeList::AT_CDecl: CC = CC_C; break;
3890 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3891 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3892 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3893 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003894 case AttributeList::AT_MSABI:
3895 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3896 CC_X86_64Win64;
3897 break;
3898 case AttributeList::AT_SysVABI:
3899 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3900 CC_C;
3901 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003902 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003903 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003904 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003905 attr.setInvalid();
3906 return true;
3907 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003908 if (StrRef == "aapcs") {
3909 CC = CC_AAPCS;
3910 break;
3911 } else if (StrRef == "aapcs-vfp") {
3912 CC = CC_AAPCS_VFP;
3913 break;
3914 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003915
3916 attr.setInvalid();
3917 Diag(attr.getLoc(), diag::err_invalid_pcs);
3918 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003919 }
Derek Schuffa2020962012-10-16 22:30:41 +00003920 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003921 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003922 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003923 }
3924
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003925 const TargetInfo &TI = Context.getTargetInfo();
3926 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3927 if (A == TargetInfo::CCCR_Warning) {
3928 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003929
3930 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3931 if (FD)
3932 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3933 TargetInfo::CCMT_NonMember;
3934 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003935 }
3936
John McCall3882ace2011-01-05 12:14:39 +00003937 return false;
3938}
3939
Chandler Carruthedc2c642011-07-02 00:01:44 +00003940static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003941 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003942
3943 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003944 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003945 return;
3946
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003947 if (!isa<ObjCMethodDecl>(D)) {
3948 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3949 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003950 return;
3951 }
Eli Friedman7044b762009-03-27 21:06:47 +00003952
Michael Han99315932013-01-24 16:46:58 +00003953 D->addAttr(::new (S.Context)
3954 RegparmAttr(Attr.getRange(), S.Context, numParams,
3955 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003956}
3957
3958/// Checks a regparm attribute, returning true if it is ill-formed and
3959/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003960bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3961 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003962 return true;
3963
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003964 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003965 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003966 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003967 }
Eli Friedman7044b762009-03-27 21:06:47 +00003968
Aaron Ballman00e99962013-08-31 01:11:41 +00003969 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003970 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003971 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003972 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9d695092013-07-30 14:10:17 +00003973 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3974 << Attr.getName() << AANT_ArgumentIntegerConstant
3975 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003976 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003977 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003978 }
3979
Douglas Gregore8bbc122011-09-02 00:18:52 +00003980 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003981 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003982 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003983 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003984 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003985 }
3986
John McCall3882ace2011-01-05 12:14:39 +00003987 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003988 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003989 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003990 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003991 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003992 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003993 }
3994
John McCall3882ace2011-01-05 12:14:39 +00003995 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003996}
3997
Chandler Carruthedc2c642011-07-02 00:01:44 +00003998static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003999 if (S.LangOpts.CUDA) {
4000 // check the attribute arguments.
4001 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004002 // FIXME: 0 is not okay.
4003 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004004 return;
4005 }
4006
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004007 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004008 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004009 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004010 return;
4011 }
4012
Aaron Ballman00e99962013-08-31 01:11:41 +00004013 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004014 llvm::APSInt MaxThreads(32);
4015 if (MaxThreadsExpr->isTypeDependent() ||
4016 MaxThreadsExpr->isValueDependent() ||
4017 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004018 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004019 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00004020 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne827301e2010-12-12 23:03:07 +00004021 return;
4022 }
4023
4024 llvm::APSInt MinBlocks(32);
4025 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004026 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004027 if (MinBlocksExpr->isTypeDependent() ||
4028 MinBlocksExpr->isValueDependent() ||
4029 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004031 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00004032 << MinBlocksExpr->getSourceRange();
Peter Collingbourne827301e2010-12-12 23:03:07 +00004033 return;
4034 }
4035 }
4036
Michael Han99315932013-01-24 16:46:58 +00004037 D->addAttr(::new (S.Context)
4038 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4039 MaxThreads.getZExtValue(),
4040 MinBlocks.getZExtValue(),
4041 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004042 } else {
4043 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4044 }
4045}
4046
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004047static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4048 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004049 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004051 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004052 return;
4053 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004054
4055 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004056 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004057
Aaron Ballman00e99962013-08-31 01:11:41 +00004058 StringRef AttrName = Attr.getName()->getName();
4059 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004060
4061 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4063 << Attr.getName() << ExpectedFunctionOrMethod;
4064 return;
4065 }
4066
4067 uint64_t ArgumentIdx;
4068 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4069 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004070 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004071 return;
4072
4073 uint64_t TypeTagIdx;
4074 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4075 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004076 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004077 return;
4078
4079 bool IsPointer = (AttrName == "pointer_with_type_tag");
4080 if (IsPointer) {
4081 // Ensure that buffer has a pointer type.
4082 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4083 if (!BufferTy->isPointerType()) {
4084 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004085 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004086 }
4087 }
4088
Michael Han99315932013-01-24 16:46:58 +00004089 D->addAttr(::new (S.Context)
4090 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4091 ArgumentIdx, TypeTagIdx, IsPointer,
4092 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004093}
4094
4095static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4096 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004097 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004098 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004099 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004100 return;
4101 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004102
4103 if (!checkAttributeNumArgs(S, Attr, 1))
4104 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004105
Aaron Ballman00e99962013-08-31 01:11:41 +00004106 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004107 TypeSourceInfo *MatchingCTypeLoc = 0;
4108 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4109 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004110
Michael Han99315932013-01-24 16:46:58 +00004111 D->addAttr(::new (S.Context)
4112 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004113 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004114 Attr.getLayoutCompatible(),
4115 Attr.getMustBeNull(),
4116 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004117}
4118
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004119//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004120// Checker-specific attribute handlers.
4121//===----------------------------------------------------------------------===//
4122
John McCalled433932011-01-25 03:31:58 +00004123static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004124 return type->isDependentType() ||
4125 type->isObjCObjectPointerType() ||
4126 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004127}
4128static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004129 return type->isDependentType() ||
4130 type->isPointerType() ||
4131 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004132}
4133
Chandler Carruthedc2c642011-07-02 00:01:44 +00004134static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004135 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004136 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004137 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004138 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004139 return;
4140 }
4141
4142 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004143 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004144 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4145 cf = false;
4146 } else {
4147 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4148 cf = true;
4149 }
4150
4151 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004152 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004153 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004154 return;
4155 }
4156
4157 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004158 param->addAttr(::new (S.Context)
4159 CFConsumedAttr(Attr.getRange(), S.Context,
4160 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004161 else
Michael Han99315932013-01-24 16:46:58 +00004162 param->addAttr(::new (S.Context)
4163 NSConsumedAttr(Attr.getRange(), S.Context,
4164 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004165}
4166
Chandler Carruthedc2c642011-07-02 00:01:44 +00004167static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4168 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004169 if (!isa<ObjCMethodDecl>(D)) {
4170 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004171 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004172 return;
4173 }
4174
Michael Han99315932013-01-24 16:46:58 +00004175 D->addAttr(::new (S.Context)
4176 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4177 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004178}
4179
Chandler Carruthedc2c642011-07-02 00:01:44 +00004180static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4181 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004182
John McCalled433932011-01-25 03:31:58 +00004183 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004184
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004185 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004186 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004187 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004189 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004190 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4191 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004192 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004193 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004194 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004195 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004196 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004197 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004198 return;
4199 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004200
John McCalled433932011-01-25 03:31:58 +00004201 bool typeOK;
4202 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004203 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004204 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 case AttributeList::AT_NSReturnsAutoreleased:
4206 case AttributeList::AT_NSReturnsRetained:
4207 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004208 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4209 cf = false;
4210 break;
4211
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004212 case AttributeList::AT_CFReturnsRetained:
4213 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004214 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4215 cf = true;
4216 break;
4217 }
4218
4219 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004220 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004221 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004222 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004223 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004224
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004225 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004226 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004227 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004229 D->addAttr(::new (S.Context)
4230 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4231 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004232 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004233 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004234 D->addAttr(::new (S.Context)
4235 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4236 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004237 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004239 D->addAttr(::new (S.Context)
4240 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4241 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004242 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004243 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004244 D->addAttr(::new (S.Context)
4245 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4246 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004247 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004248 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004249 D->addAttr(::new (S.Context)
4250 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4251 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004252 return;
4253 };
4254}
4255
John McCallcf166702011-07-22 08:53:00 +00004256static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4257 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004258 const int EP_ObjCMethod = 1;
4259 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004260
John McCallcf166702011-07-22 08:53:00 +00004261 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004262 QualType resultType;
4263
John McCallcf166702011-07-22 08:53:00 +00004264 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4265
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004266 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004267 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4268 if (!property) {
4269 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4270 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4271 return;
4272 }
4273 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004274 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004275 else
4276 // Check that the method returns a normal pointer.
4277 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004278
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004279 if (!resultType->isReferenceType() &&
4280 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004281 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004282 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004283 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4284 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004285
4286 // Drop the attribute.
4287 return;
4288 }
4289
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004290 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004291 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4292 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004293}
4294
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004295static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4296 const AttributeList &attr) {
4297 SourceLocation loc = attr.getLoc();
4298 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4299
4300 if (!method) {
4301 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4302 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4303 return;
4304 }
4305 DeclContext *DC = method->getDeclContext();
4306 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4307 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4308 << attr.getName() << 0;
4309 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4310 return;
4311 }
4312 if (method->getMethodFamily() == OMF_dealloc) {
4313 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4314 << attr.getName() << 1;
4315 return;
4316 }
4317
Michael Han99315932013-01-24 16:46:58 +00004318 method->addAttr(::new (S.Context)
4319 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4320 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004321}
4322
John McCall32f5fe12011-09-30 05:12:12 +00004323/// Handle cf_audited_transfer and cf_unknown_transfer.
4324static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4325 if (!isa<FunctionDecl>(D)) {
4326 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004327 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004328 return;
4329 }
4330
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004331 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004332
4333 // Check whether there's a conflicting attribute already present.
4334 Attr *Existing;
4335 if (IsAudited) {
4336 Existing = D->getAttr<CFUnknownTransferAttr>();
4337 } else {
4338 Existing = D->getAttr<CFAuditedTransferAttr>();
4339 }
4340 if (Existing) {
4341 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4342 << A.getName()
4343 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4344 << A.getRange() << Existing->getRange();
4345 return;
4346 }
4347
4348 // All clear; add the attribute.
4349 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004350 D->addAttr(::new (S.Context)
4351 CFAuditedTransferAttr(A.getRange(), S.Context,
4352 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004353 } else {
Michael Han99315932013-01-24 16:46:58 +00004354 D->addAttr(::new (S.Context)
4355 CFUnknownTransferAttr(A.getRange(), S.Context,
4356 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004357 }
4358}
4359
John McCallf1e8b342011-09-29 07:17:38 +00004360static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4361 const AttributeList &Attr) {
4362 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4363 if (!RD || RD->isUnion()) {
4364 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004365 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004366 }
4367
Aaron Ballman00e99962013-08-31 01:11:41 +00004368 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004369
4370 // In Objective-C, verify that the type names an Objective-C type.
4371 // We don't want to check this outside of ObjC because people sometimes
4372 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004373 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004374 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004375 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004376 Sema::LookupOrdinaryName);
4377 if (S.LookupName(R, Sc)) {
4378 NamedDecl *Target = R.getFoundDecl();
4379 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4380 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4381 S.Diag(Target->getLocStart(), diag::note_declared_at);
4382 }
4383 }
4384 }
4385
Michael Han99315932013-01-24 16:46:58 +00004386 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004387 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004388 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004389}
4390
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004391static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4392 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004393 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004394 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4395 << Attr.getName()
4396 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4397 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004398 return;
4399 }
4400
4401 if (Attr.getNumArgs() != 1) {
4402 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
4403 return;
4404 }
4405 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
4406 if (!Parm) {
4407 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
4408 return;
4409 }
4410
4411 D->addAttr(::new (S.Context)
4412 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
4413 Attr.getAttributeSpellingListIndex()));
4414}
4415
Chandler Carruthedc2c642011-07-02 00:01:44 +00004416static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4417 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004418 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004419
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004420 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004421 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004422}
4423
Chandler Carruthedc2c642011-07-02 00:01:44 +00004424static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4425 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004426 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004427 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004428 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004429 return;
4430 }
4431
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004432 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004433 QualType type = vd->getType();
4434
4435 if (!type->isDependentType() &&
4436 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004437 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004438 << type;
4439 return;
4440 }
4441
4442 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4443
4444 // If we have no lifetime yet, check the lifetime we're presumably
4445 // going to infer.
4446 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4447 lifetime = type->getObjCARCImplicitLifetime();
4448
4449 switch (lifetime) {
4450 case Qualifiers::OCL_None:
4451 assert(type->isDependentType() &&
4452 "didn't infer lifetime for non-dependent type?");
4453 break;
4454
4455 case Qualifiers::OCL_Weak: // meaningful
4456 case Qualifiers::OCL_Strong: // meaningful
4457 break;
4458
4459 case Qualifiers::OCL_ExplicitNone:
4460 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004461 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004462 << (lifetime == Qualifiers::OCL_Autoreleasing);
4463 break;
4464 }
4465
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004466 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004467 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4468 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004469}
4470
Francois Picheta83957a2010-12-19 06:50:37 +00004471//===----------------------------------------------------------------------===//
4472// Microsoft specific attribute handlers.
4473//===----------------------------------------------------------------------===//
4474
Reid Kleckner140c4a72013-05-17 14:04:52 +00004475// Check if MS extensions or some other language extensions are enabled. If
4476// not, issue a diagnostic that the given attribute is unused.
4477static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4478 bool OtherExtension = false) {
4479 if (S.LangOpts.MicrosoftExt || OtherExtension)
4480 return true;
4481 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4482 return false;
4483}
4484
Chandler Carruthedc2c642011-07-02 00:01:44 +00004485static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004486 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4487 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004488
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004489 StringRef StrRef;
4490 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004491 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004492 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004493
David Majnemer89085342013-08-09 08:56:20 +00004494 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4495 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004496 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4497 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004498
Reid Kleckner140c4a72013-05-17 14:04:52 +00004499 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004500 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004501 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004502 return;
4503 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004504
David Majnemer89085342013-08-09 08:56:20 +00004505 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004506 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004507 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004508 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004509 return;
4510 }
David Majnemer89085342013-08-09 08:56:20 +00004511 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004512 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004513 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004514 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004515 }
Francois Picheta83957a2010-12-19 06:50:37 +00004516
David Majnemer89085342013-08-09 08:56:20 +00004517 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4518 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004519}
4520
John McCall8d32c052012-05-22 21:28:12 +00004521static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004522 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004523 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004524
4525 AttributeList::Kind Kind = Attr.getKind();
4526 if (Kind == AttributeList::AT_SingleInheritance)
4527 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004528 ::new (S.Context)
4529 SingleInheritanceAttr(Attr.getRange(), S.Context,
4530 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004531 else if (Kind == AttributeList::AT_MultipleInheritance)
4532 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004533 ::new (S.Context)
4534 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4535 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004536 else if (Kind == AttributeList::AT_VirtualInheritance)
4537 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004538 ::new (S.Context)
4539 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4540 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004541}
4542
4543static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004544 if (!checkMicrosoftExt(S, Attr))
4545 return;
4546
4547 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballman317a77f2013-05-22 23:25:32 +00004548 if (Kind == AttributeList::AT_Win64)
Reid Kleckner140c4a72013-05-17 14:04:52 +00004549 D->addAttr(
4550 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4551 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004552}
4553
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004554static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004555 if (!checkMicrosoftExt(S, Attr))
4556 return;
4557 D->addAttr(::new (S.Context)
4558 ForceInlineAttr(Attr.getRange(), S.Context,
4559 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004560}
4561
Reid Klecknerb144d362013-05-20 14:02:37 +00004562static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4563 if (!checkMicrosoftExt(S, Attr))
4564 return;
4565 // Check linkage after possibly merging declaratinos. See
4566 // checkAttributesAfterMerging().
4567 D->addAttr(::new (S.Context)
4568 SelectAnyAttr(Attr.getRange(), S.Context,
4569 Attr.getAttributeSpellingListIndex()));
4570}
4571
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004572/// Handles semantic checking for features that are common to all attributes,
4573/// such as checking whether a parameter was properly specified, or the correct
4574/// number of arguments were passed, etc.
4575static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4576 const AttributeList &Attr) {
4577 // Several attributes carry different semantics than the parsing requires, so
4578 // those are opted out of the common handling.
4579 //
4580 // We also bail on unknown and ignored attributes because those are handled
4581 // as part of the target-specific handling logic.
4582 if (Attr.hasCustomParsing() ||
4583 Attr.getKind() == AttributeList::UnknownAttribute ||
4584 Attr.getKind() == AttributeList::IgnoredAttribute)
4585 return false;
4586
4587 // If there are no optional arguments, then checking for the argument count
4588 // is trivial.
4589 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4590 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4591 return true;
4592 return false;
4593}
4594
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004595//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004596// Top Level Sema Entry Points
4597//===----------------------------------------------------------------------===//
4598
Richard Smithf8a75c32013-08-29 00:47:48 +00004599/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4600/// the attribute applies to decls. If the attribute is a type attribute, just
4601/// silently ignore it if a GNU attribute.
4602static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4603 const AttributeList &Attr,
4604 bool IncludeCXX11Attributes) {
4605 if (Attr.isInvalid())
4606 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004607
Richard Smithf8a75c32013-08-29 00:47:48 +00004608 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4609 // instead.
4610 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4611 return;
4612
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004613 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4614 return;
4615
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004616 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004617 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4618 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4619 case AttributeList::AT_IBOutletCollection:
4620 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004621 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004622 case AttributeList::AT_ObjCGC:
4623 case AttributeList::AT_VectorSize:
4624 case AttributeList::AT_NeonVectorType:
4625 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004626 case AttributeList::AT_Ptr32:
4627 case AttributeList::AT_Ptr64:
4628 case AttributeList::AT_SPtr:
4629 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004630 // Ignore these, these are type attributes, handled by
4631 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004632 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004633 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4634 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4635 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4636 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004637 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004638 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004639 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004640 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004641 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4642 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4643 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004644 handleDependencyAttr(S, scope, D, Attr);
4645 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004646 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4647 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4648 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004649 case AttributeList::AT_CXX11NoReturn:
4650 handleCXX11NoReturnAttr(S, D, Attr);
4651 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004652 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004653 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004654 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004655 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4656 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004657 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004658 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004659 case AttributeList::AT_MinSize:
4660 handleMinSizeAttr(S, D, Attr);
4661 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004662 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4663 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4664 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004665 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4666 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004667 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4668 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004669 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004670 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004671 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4672 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004673 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004674 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4675 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004676 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004677 case AttributeList::AT_ownership_returns:
4678 case AttributeList::AT_ownership_takes:
4679 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004680 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004681 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4682 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4683 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4684 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4685 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4686 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4687 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004688
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004689 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004690 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004692 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004693
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004694 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004695 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4696
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004697 case AttributeList::AT_ObjCRequiresSuper:
4698 handleObjCRequiresSuperAttr(S, D, Attr); break;
4699
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004700 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004701 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004702
4703 case AttributeList::AT_ObjCBridge:
4704 handleObjCBridgeAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004705
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004706 case AttributeList::AT_CFAuditedTransfer:
4707 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004708 handleCFTransferAttr(S, D, Attr); break;
4709
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004710 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004711 case AttributeList::AT_CFConsumed:
4712 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4713 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004714 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004715
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004716 case AttributeList::AT_NSReturnsAutoreleased:
4717 case AttributeList::AT_NSReturnsNotRetained:
4718 case AttributeList::AT_CFReturnsNotRetained:
4719 case AttributeList::AT_NSReturnsRetained:
4720 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004721 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004722
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004723 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004724 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004725 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004726
Joey Goulyaba589c2013-03-08 09:42:32 +00004727 case AttributeList::AT_VecTypeHint:
4728 handleVecTypeHint(S, D, Attr); break;
4729
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004730 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004731 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004732
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004733 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4734 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4735 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004736 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004737 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004738 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004739 handleArcWeakrefUnavailableAttr (S, D, Attr);
4740 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004741 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004742 handleObjCRootClassAttr(S, D, Attr);
4743 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004744 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004745 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004746 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004747 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4748 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004749 handleReturnsTwiceAttr(S, D, Attr);
4750 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004752 case AttributeList::AT_Visibility:
4753 handleVisibilityAttr(S, D, Attr, false);
4754 break;
4755 case AttributeList::AT_TypeVisibility:
4756 handleVisibilityAttr(S, D, Attr, true);
4757 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004758 case AttributeList::AT_WarnUnused:
4759 handleWarnUnusedAttr(S, D, Attr);
4760 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004761 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004762 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004763 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4764 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4765 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4766 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004767 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004768 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004769 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004770 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004771 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004772 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004773 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004774 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004775 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4776 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4777 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4778 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4779 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4780 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4781 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4782 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4783 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004784 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004785 // Just ignore
4786 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004787 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004788 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004789 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004790 case AttributeList::AT_StdCall:
4791 case AttributeList::AT_CDecl:
4792 case AttributeList::AT_FastCall:
4793 case AttributeList::AT_ThisCall:
4794 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004795 case AttributeList::AT_MSABI:
4796 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004797 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004798 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004799 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004800 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004801 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004802 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004803 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004804 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004805 case AttributeList::AT_OpenCLImageAccess:
4806 handleOpenCLImageAccessAttr(S, D, Attr);
4807 break;
John McCall8d32c052012-05-22 21:28:12 +00004808
4809 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004810 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004811 handleMsStructAttr(S, D, Attr);
4812 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004813 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004814 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004815 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004816 case AttributeList::AT_SingleInheritance:
4817 case AttributeList::AT_MultipleInheritance:
4818 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004819 handleInheritanceAttr(S, D, Attr);
4820 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004821 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004822 handlePortabilityAttr(S, D, Attr);
4823 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004824 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004825 handleForceInlineAttr(S, D, Attr);
4826 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004827 case AttributeList::AT_SelectAny:
4828 handleSelectAnyAttr(S, D, Attr);
4829 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004830
4831 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004832 case AttributeList::AT_AssertExclusiveLock:
4833 handleAssertExclusiveLockAttr(S, D, Attr);
4834 break;
4835 case AttributeList::AT_AssertSharedLock:
4836 handleAssertSharedLockAttr(S, D, Attr);
4837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004838 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004839 handleGuardedVarAttr(S, D, Attr);
4840 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004841 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004842 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004843 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004844 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004845 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004846 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004847 case AttributeList::AT_NoSanitizeAddress:
4848 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004849 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004850 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004851 handleNoThreadSafetyAnalysis(S, D, Attr);
4852 break;
4853 case AttributeList::AT_NoSanitizeThread:
4854 handleNoSanitizeThread(S, D, Attr);
4855 break;
4856 case AttributeList::AT_NoSanitizeMemory:
4857 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004858 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004859 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004860 handleLockableAttr(S, D, Attr);
4861 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004862 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004863 handleGuardedByAttr(S, D, Attr);
4864 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004865 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004866 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004867 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004868 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004869 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004870 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004871 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004872 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004873 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004874 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004875 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004876 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004877 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004878 handleLockReturnedAttr(S, D, Attr);
4879 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004880 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004881 handleLocksExcludedAttr(S, D, Attr);
4882 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004883 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004884 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004885 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004886 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004887 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004888 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004889 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004890 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004891 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004892 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004893 handleUnlockFunAttr(S, D, Attr);
4894 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004895 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004896 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004897 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004898 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004899 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004900 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004901
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004902 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004903 case AttributeList::AT_Consumable:
4904 handleConsumableAttr(S, D, Attr);
4905 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004906 case AttributeList::AT_CallableWhen:
4907 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004908 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004909 case AttributeList::AT_ParamTypestate:
4910 handleParamTypestateAttr(S, D, Attr);
4911 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004912 case AttributeList::AT_ReturnTypestate:
4913 handleReturnTypestateAttr(S, D, Attr);
4914 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004915 case AttributeList::AT_SetTypestate:
4916 handleSetTypestateAttr(S, D, Attr);
4917 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004918 case AttributeList::AT_TestTypestate:
4919 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004920 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004921
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004922 // Type safety attributes.
4923 case AttributeList::AT_ArgumentWithTypeTag:
4924 handleArgumentWithTypeTagAttr(S, D, Attr);
4925 break;
4926 case AttributeList::AT_TypeTagForDatatype:
4927 handleTypeTagForDatatypeAttr(S, D, Attr);
4928 break;
4929
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004930 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004931 // Ask target about the attribute.
4932 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4933 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004934 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4935 diag::warn_unhandled_ms_attribute_ignored :
4936 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004937 break;
4938 }
4939}
4940
4941/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4942/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004943void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004944 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004945 bool IncludeCXX11Attributes) {
4946 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004947 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004948
4949 // GCC accepts
4950 // static int a9 __attribute__((weakref));
4951 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004952 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004953 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004954 cast<NamedDecl>(D)->getNameAsString();
4955 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004956 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004957 }
4958}
4959
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004960// Annotation attributes are the only attributes allowed after an access
4961// specifier.
4962bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4963 const AttributeList *AttrList) {
4964 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004965 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004966 handleAnnotateAttr(*this, ASDecl, *l);
4967 } else {
4968 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4969 return true;
4970 }
4971 }
4972
4973 return false;
4974}
4975
John McCall42856de2011-10-01 05:17:03 +00004976/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4977/// contains any decl attributes that we should warn about.
4978static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4979 for ( ; A; A = A->getNext()) {
4980 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004981 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004982 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4983
4984 if (A->getKind() == AttributeList::UnknownAttribute) {
4985 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4986 << A->getName() << A->getRange();
4987 } else {
4988 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4989 << A->getName() << A->getRange();
4990 }
4991 }
4992}
4993
4994/// checkUnusedDeclAttributes - Given a declarator which is not being
4995/// used to build a declaration, complain about any decl attributes
4996/// which might be lying around on it.
4997void Sema::checkUnusedDeclAttributes(Declarator &D) {
4998 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4999 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5000 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5001 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5002}
5003
Ryan Flynn7d470f32009-07-30 03:15:39 +00005004/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005005/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005006NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5007 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005008 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005009 NamedDecl *NewD = 0;
5010 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005011 FunctionDecl *NewFD;
5012 // FIXME: Missing call to CheckFunctionDeclaration().
5013 // FIXME: Mangling?
5014 // FIXME: Is the qualifier info correct?
5015 // FIXME: Is the DeclContext correct?
5016 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5017 Loc, Loc, DeclarationName(II),
5018 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005019 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005020 FD->hasPrototype(),
5021 false/*isConstexprSpecified*/);
5022 NewD = NewFD;
5023
5024 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005025 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005026
5027 // Fake up parameter variables; they are declared as if this were
5028 // a typedef.
5029 QualType FDTy = FD->getType();
5030 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5031 SmallVector<ParmVarDecl*, 16> Params;
5032 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5033 AE = FT->arg_type_end(); AI != AE; ++AI) {
5034 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5035 Param->setScopeInfo(0, Params.size());
5036 Params.push_back(Param);
5037 }
David Blaikie9c70e042011-09-21 18:16:56 +00005038 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005039 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005040 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5041 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005042 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005043 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005044 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005045 if (VD->getQualifier()) {
5046 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005047 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005048 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005049 }
5050 return NewD;
5051}
5052
James Dennett634962f2012-06-14 21:40:34 +00005053/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005054/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005055void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005056 if (W.getUsed()) return; // only do this once
5057 W.setUsed(true);
5058 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5059 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005060 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005061 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5062 NDId->getName()));
5063 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005064 WeakTopLevelDecl.push_back(NewD);
5065 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5066 // to insert Decl at TU scope, sorry.
5067 DeclContext *SavedContext = CurContext;
5068 CurContext = Context.getTranslationUnitDecl();
5069 PushOnScopeChains(NewD, S);
5070 CurContext = SavedContext;
5071 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005072 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005073 }
5074}
5075
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005076void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5077 // It's valid to "forward-declare" #pragma weak, in which case we
5078 // have to do this.
5079 LoadExternalWeakUndeclaredIdentifiers();
5080 if (!WeakUndeclaredIdentifiers.empty()) {
5081 NamedDecl *ND = NULL;
5082 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5083 if (VD->isExternC())
5084 ND = VD;
5085 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5086 if (FD->isExternC())
5087 ND = FD;
5088 if (ND) {
5089 if (IdentifierInfo *Id = ND->getIdentifier()) {
5090 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5091 = WeakUndeclaredIdentifiers.find(Id);
5092 if (I != WeakUndeclaredIdentifiers.end()) {
5093 WeakInfo W = I->second;
5094 DeclApplyPragmaWeak(S, ND, W);
5095 WeakUndeclaredIdentifiers[Id] = W;
5096 }
5097 }
5098 }
5099 }
5100}
5101
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005102/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5103/// it, apply them to D. This is a bit tricky because PD can have attributes
5104/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005105void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005106 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005107 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005108 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005109
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005110 // Walk the declarator structure, applying decl attributes that were in a type
5111 // position to the decl itself. This handles cases like:
5112 // int *__attr__(x)** D;
5113 // when X is a decl attribute.
5114 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5115 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005116 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005117
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005118 // Finally, apply any attributes on the decl itself.
5119 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005120 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005121}
John McCall28a6aea2009-11-04 02:18:39 +00005122
John McCall31168b02011-06-15 23:02:42 +00005123/// Is the given declaration allowed to use a forbidden type?
5124static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5125 // Private ivars are always okay. Unfortunately, people don't
5126 // always properly make their ivars private, even in system headers.
5127 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005128 // Function declarations in sys headers will be marked unavailable.
5129 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5130 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005131 return false;
5132
5133 // Require it to be declared in a system header.
5134 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5135}
5136
5137/// Handle a delayed forbidden-type diagnostic.
5138static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5139 Decl *decl) {
5140 if (decl && isForbiddenTypeAllowed(S, decl)) {
5141 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5142 "this system declaration uses an unsupported type"));
5143 return;
5144 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005145 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005146 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005147 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005148 // kind of forbidden type messages on unavailable functions.
5149 if (FD->hasAttr<UnavailableAttr>() &&
5150 diag.getForbiddenTypeDiagnostic() ==
5151 diag::err_arc_array_param_no_ownership) {
5152 diag.Triggered = true;
5153 return;
5154 }
5155 }
John McCall31168b02011-06-15 23:02:42 +00005156
5157 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5158 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5159 diag.Triggered = true;
5160}
5161
John McCall2ec85372012-05-07 06:16:41 +00005162void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5163 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005164 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005165 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005166
John McCall2ec85372012-05-07 06:16:41 +00005167 // When delaying diagnostics to run in the context of a parsed
5168 // declaration, we only want to actually emit anything if parsing
5169 // succeeds.
5170 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005171
John McCall2ec85372012-05-07 06:16:41 +00005172 // We emit all the active diagnostics in this pool or any of its
5173 // parents. In general, we'll get one pool for the decl spec
5174 // and a child pool for each declarator; in a decl group like:
5175 // deprecated_typedef foo, *bar, baz();
5176 // only the declarator pops will be passed decls. This is correct;
5177 // we really do need to consider delayed diagnostics from the decl spec
5178 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005179 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005180 do {
John McCall6347b682012-05-07 06:16:58 +00005181 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005182 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5183 // This const_cast is a bit lame. Really, Triggered should be mutable.
5184 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005185 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005186 continue;
5187
John McCallc1465822011-02-14 07:13:47 +00005188 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005189 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005190 // Don't bother giving deprecation diagnostics if the decl is invalid.
5191 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005192 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005193 break;
5194
5195 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005196 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005197 break;
John McCall31168b02011-06-15 23:02:42 +00005198
5199 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005200 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005201 break;
John McCall86121512010-01-27 03:50:35 +00005202 }
5203 }
John McCall2ec85372012-05-07 06:16:41 +00005204 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005205}
5206
John McCall6347b682012-05-07 06:16:58 +00005207/// Given a set of delayed diagnostics, re-emit them as if they had
5208/// been delayed in the current context instead of in the given pool.
5209/// Essentially, this just moves them to the current pool.
5210void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5211 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5212 assert(curPool && "re-emitting in undelayed context not supported");
5213 curPool->steal(pool);
5214}
5215
John McCall28a6aea2009-11-04 02:18:39 +00005216static bool isDeclDeprecated(Decl *D) {
5217 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005218 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005219 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005220 // A category implicitly has the availability of the interface.
5221 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5222 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005223 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5224 return false;
5225}
5226
Eli Friedman971bfa12012-08-08 21:52:41 +00005227static void
5228DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5229 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005230 const ObjCInterfaceDecl *UnknownObjCClass,
5231 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005232 DeclarationName Name = D->getDeclName();
5233 if (!Message.empty()) {
5234 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5235 S.Diag(D->getLocation(),
5236 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5237 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005238 if (ObjCPropery)
5239 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5240 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005241 } else if (!UnknownObjCClass) {
5242 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5243 S.Diag(D->getLocation(),
5244 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5245 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005246 if (ObjCPropery)
5247 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5248 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005249 } else {
5250 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5251 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5252 }
5253}
5254
John McCallb45a1e72010-08-26 02:13:20 +00005255void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005256 Decl *Ctx) {
5257 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005258 return;
5259
John McCall86121512010-01-27 03:50:35 +00005260 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005261 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5262 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005263 DD.getUnknownObjCClass(),
5264 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005265}
5266
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005267void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005268 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005269 const ObjCInterfaceDecl *UnknownObjCClass,
5270 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005271 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005272 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005273 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5274 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005275 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005276 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005277 return;
5278 }
5279
5280 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005281 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005282 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005283 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005284}