blob: 38bffa2f99e46b89a9f3e6acf5ffa52338b4b592 [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
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244/// \brief If Expr is a valid integer constant, get the value of the integer
245/// expression and return success or failure. May output an error.
246static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
247 const Expr *Expr, uint32_t &Val,
248 unsigned Idx = UINT_MAX) {
249 llvm::APSInt I(32);
250 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
251 !Expr->isIntegerConstantExpr(I, S.Context)) {
252 if (Idx != UINT_MAX)
253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
254 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
255 << Expr->getSourceRange();
256 else
257 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
258 << Attr.getName() << AANT_ArgumentIntegerConstant
259 << Expr->getSourceRange();
260 return false;
261 }
262 Val = (uint32_t)I.getZExtValue();
263 return true;
264}
265
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266/// \brief Check if IdxExpr is a valid argument index for a function or
267/// instance method D. May output an error.
268///
269/// \returns true if IdxExpr is a valid index.
270static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
271 StringRef AttrName,
272 SourceLocation AttrLoc,
273 unsigned AttrArgNum,
274 const Expr *IdxExpr,
275 uint64_t &Idx)
276{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000277 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000278
279 // In C++ the implicit 'this' function parameter also counts.
280 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000281 bool HP = hasFunctionProto(D);
282 bool HasImplicitThisParam = isInstanceMethod(D);
283 bool IV = HP && isFunctionOrMethodVariadic(D);
284 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
285 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286
287 llvm::APSInt IdxInt;
288 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
289 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000290 std::string Name = std::string("'") + AttrName.str() + std::string("'");
291 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000292 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000293 return false;
294 }
295
296 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000297 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000298 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
299 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
300 return false;
301 }
302 Idx--; // Convert to zero-based.
303 if (HasImplicitThisParam) {
304 if (Idx == 0) {
305 S.Diag(AttrLoc,
306 diag::err_attribute_invalid_implicit_this_argument)
307 << AttrName << IdxExpr->getSourceRange();
308 return false;
309 }
310 --Idx;
311 }
312
313 return true;
314}
315
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000316/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
317/// If not emit an error and return false. If the argument is an identifier it
318/// will emit an error with a fixit hint and treat it as if it was a string
319/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000320bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
321 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000322 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000323 // Look for identifiers. If we have one emit a hint to fix it to a literal.
324 if (Attr.isArgIdent(ArgNum)) {
325 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000326 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000327 << Attr.getName() << AANT_ArgumentString
328 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000329 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000330 Str = Loc->Ident->getName();
331 if (ArgLocation)
332 *ArgLocation = Loc->Loc;
333 return true;
334 }
335
336 // Now check for an actual string literal.
337 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
338 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
339 if (ArgLocation)
340 *ArgLocation = ArgExpr->getLocStart();
341
342 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000343 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000344 << Attr.getName() << AANT_ArgumentString;
345 return false;
346 }
347
348 Str = Literal->getString();
349 return true;
350}
351
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000352///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000353/// \brief Check if passed in Decl is a field or potentially shared global var
354/// \return true if the Decl is a field or potentially shared global variable
355///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000356static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000357 if (isa<FieldDecl>(D))
358 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000359 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000360 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000361
362 return false;
363}
364
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000365/// \brief Check if the passed-in expression is of type int or bool.
366static bool isIntOrBool(Expr *Exp) {
367 QualType QT = Exp->getType();
368 return QT->isBooleanType() || QT->isIntegerType();
369}
370
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000371
372// Check to see if the type is a smart pointer of some kind. We assume
373// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000374static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
375 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
376 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000377 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000379
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000380 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
381 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000382 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000383 return false;
384
385 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000386}
387
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000388/// \brief Check if passed in Decl is a pointer type.
389/// Note that this function may produce an error message.
390/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000391static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
392 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000393 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000394 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000395 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000397
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000398 if (const RecordType *RT = QT->getAs<RecordType>()) {
399 // If it's an incomplete type, it could be a smart pointer; skip it.
400 // (We don't want to force template instantiation if we can avoid it,
401 // since that would alter the order in which templates are instantiated.)
402 if (RT->isIncompleteType())
403 return true;
404
405 if (threadSafetyCheckIsSmartPointer(S, RT))
406 return true;
407 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000408
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000409 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000410 << Attr.getName()->getName() << QT;
411 } else {
412 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
413 << Attr.getName();
414 }
415 return false;
416}
417
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418/// \brief Checks that the passed in QualType either is of RecordType or points
419/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000420static const RecordType *getRecordType(QualType QT) {
421 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000422 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000423
424 // Now check if we point to record type.
425 if (const PointerType *PT = QT->getAs<PointerType>())
426 return PT->getPointeeType()->getAs<RecordType>();
427
428 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000429}
430
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000431
Jordy Rose740b0c22012-05-08 03:27:22 +0000432static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
433 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000434 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
435 if (RT->getDecl()->getAttr<LockableAttr>())
436 return true;
437 return false;
438}
439
440
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000441/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000442/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000443static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
444 QualType Ty) {
445 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000446
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000447 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000448 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000449 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000450 << Attr.getName() << Ty.getAsString();
451 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000452 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000453
Michael Hana9171bc2012-08-03 17:40:43 +0000454 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000455 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000456 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000457
458 // Allow smart pointers to be used as lockable objects.
459 // FIXME -- Check the type that the smart pointer points to.
460 if (threadSafetyCheckIsSmartPointer(S, RT))
461 return;
462
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000463 // Check if the type is lockable.
464 RecordDecl *RD = RT->getDecl();
465 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000466 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000467
468 // Else check if any base classes are lockable.
469 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
470 CXXBasePaths BPaths(false, false);
471 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
472 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000473 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000474
475 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
476 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000477}
478
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000479/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000480/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000481/// \param Sidx The attribute argument index to start checking with.
482/// \param ParamIdxOk Whether an argument can be indexing into a function
483/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000484static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000485 const AttributeList &Attr,
486 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000487 int Sidx = 0,
488 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000490 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000491
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000492 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000493 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000494 Args.push_back(ArgExp);
495 continue;
496 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000497
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000498 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000499 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000500 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000501 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000502 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000503 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000504 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000505 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000507 // We allow constant strings to be used as a placeholder for expressions
508 // that are not valid C++ syntax, but warn that they are ignored.
509 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
510 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000511 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000512 continue;
513 }
514
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000517 // A pointer to member expression of the form &MyClass::mu is treated
518 // specially -- we need to look at the type of the member.
519 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
520 if (UOp->getOpcode() == UO_AddrOf)
521 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
522 if (DRE->getDecl()->isCXXInstanceMember())
523 ArgTy = DRE->getDecl()->getType();
524
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000525 // First see if we can just cast to record type, or point to record type.
526 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 // Now check if we index into a record type function param.
529 if(!RT && ParamIdxOk) {
530 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
532 if(FD && IL) {
533 unsigned int NumParams = FD->getNumParams();
534 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000535 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
536 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
537 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
539 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000540 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000541 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000542 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000543 }
544 }
545
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000546 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000547
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000548 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550}
551
Chris Lattner58418ff2008-06-29 00:16:31 +0000552//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000553// Attribute Implementations
554//===----------------------------------------------------------------------===//
555
Daniel Dunbar032db472008-07-31 22:40:48 +0000556// FIXME: All this manual attribute parsing code is gross. At the
557// least add some helper functions to check most argument patterns (#
558// and types of args).
559
Michael Hana9171bc2012-08-03 17:40:43 +0000560static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000561 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000562 // D must be either a member field or global (potentially shared) variable.
563 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000564 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
565 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000566 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000567 }
568
Michael Han3be3b442012-07-23 18:48:41 +0000569 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000570}
571
Michael Han3be3b442012-07-23 18:48:41 +0000572static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
573 if (!checkGuardedVarAttrCommon(S, D, Attr))
574 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000575
Michael Han99315932013-01-24 16:46:58 +0000576 D->addAttr(::new (S.Context)
577 GuardedVarAttr(Attr.getRange(), S.Context,
578 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000579}
580
Michael Hana9171bc2012-08-03 17:40:43 +0000581static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000582 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000583 if (!checkGuardedVarAttrCommon(S, D, Attr))
584 return;
585
586 if (!threadSafetyCheckIsPointer(S, D, Attr))
587 return;
588
Michael Han99315932013-01-24 16:46:58 +0000589 D->addAttr(::new (S.Context)
590 PtGuardedVarAttr(Attr.getRange(), S.Context,
591 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000592}
593
Michael Hana9171bc2012-08-03 17:40:43 +0000594static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
595 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000596 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000597 // D must be either a member field or global (potentially shared) variable.
598 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
600 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000601 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000602 }
603
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000604 SmallVector<Expr*, 1> Args;
605 // check that all arguments are lockable objects
606 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
607 unsigned Size = Args.size();
608 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000609 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000610
Michael Han3be3b442012-07-23 18:48:41 +0000611 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000612
Michael Han3be3b442012-07-23 18:48:41 +0000613 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000614}
615
Michael Han3be3b442012-07-23 18:48:41 +0000616static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
617 Expr *Arg = 0;
618 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
619 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000620
Michael Han3be3b442012-07-23 18:48:41 +0000621 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000625 const AttributeList &Attr) {
626 Expr *Arg = 0;
627 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
628 return;
629
630 if (!threadSafetyCheckIsPointer(S, D, Attr))
631 return;
632
633 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
634 S.Context, Arg));
635}
636
Michael Hana9171bc2012-08-03 17:40:43 +0000637static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000638 const AttributeList &Attr) {
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000639 // FIXME: Lockable structs for C code.
David Blaikie021221d2013-07-29 18:24:03 +0000640 if (!isa<RecordDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
642 << Attr.getName() << ExpectedStructOrUnionOrClass;
Michael Han3be3b442012-07-23 18:48:41 +0000643 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000644 }
645
Michael Han3be3b442012-07-23 18:48:41 +0000646 return true;
647}
648
649static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
650 if (!checkLockableAttrCommon(S, D, Attr))
651 return;
652
653 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
654}
655
Michael Hana9171bc2012-08-03 17:40:43 +0000656static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000657 const AttributeList &Attr) {
658 if (!checkLockableAttrCommon(S, D, Attr))
659 return;
660
Michael Han99315932013-01-24 16:46:58 +0000661 D->addAttr(::new (S.Context)
662 ScopedLockableAttr(Attr.getRange(), S.Context,
663 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000664}
665
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000666static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
667 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000668 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
670 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000671 return;
672 }
673
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000674 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000675 S.Context));
676}
677
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000678static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000679 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000680 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000682 << Attr.getName() << ExpectedFunctionOrMethod;
683 return;
684 }
685
Michael Han99315932013-01-24 16:46:58 +0000686 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000687 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
688 Attr.getAttributeSpellingListIndex()));
689}
690
691static void handleNoSanitizeMemory(Sema &S, Decl *D,
692 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000693 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
694 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
695 << Attr.getName() << ExpectedFunctionOrMethod;
696 return;
697 }
698
699 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
700 S.Context));
701}
702
703static void handleNoSanitizeThread(Sema &S, Decl *D,
704 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000705 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
707 << Attr.getName() << ExpectedFunctionOrMethod;
708 return;
709 }
710
711 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
712 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000713}
714
Michael Hana9171bc2012-08-03 17:40:43 +0000715static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
716 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000717 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000718 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000719 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000720
721 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000722 ValueDecl *VD = dyn_cast<ValueDecl>(D);
723 if (!VD || !mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
725 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000726 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727 }
728
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000729 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000730 QualType QT = VD->getType();
731 if (!QT->isDependentType()) {
732 const RecordType *RT = getRecordType(QT);
733 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000734 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000735 << Attr.getName();
736 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000737 }
738 }
739
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000740 // Check that all arguments are lockable objects.
741 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000742 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000743 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000744
Michael Han3be3b442012-07-23 18:48:41 +0000745 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000746}
747
Michael Hana9171bc2012-08-03 17:40:43 +0000748static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000749 const AttributeList &Attr) {
750 SmallVector<Expr*, 1> Args;
751 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
752 return;
753
754 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000755 D->addAttr(::new (S.Context)
756 AcquiredAfterAttr(Attr.getRange(), S.Context,
757 StartArg, Args.size(),
758 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000759}
760
Michael Hana9171bc2012-08-03 17:40:43 +0000761static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000762 const AttributeList &Attr) {
763 SmallVector<Expr*, 1> Args;
764 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
765 return;
766
767 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000768 D->addAttr(::new (S.Context)
769 AcquiredBeforeAttr(Attr.getRange(), S.Context,
770 StartArg, Args.size(),
771 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000772}
773
Michael Hana9171bc2012-08-03 17:40:43 +0000774static bool checkLockFunAttrCommon(Sema &S, Decl *D,
775 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000776 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000777 // zero or more arguments ok
778
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000779 // check that the attribute is applied to a function
780 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
782 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000783 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784 }
785
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000786 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000787 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000788
Michael Han3be3b442012-07-23 18:48:41 +0000789 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000790}
791
Michael Hana9171bc2012-08-03 17:40:43 +0000792static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000793 const AttributeList &Attr) {
794 SmallVector<Expr*, 1> Args;
795 if (!checkLockFunAttrCommon(S, D, Attr, Args))
796 return;
797
798 unsigned Size = Args.size();
799 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000800 D->addAttr(::new (S.Context)
801 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000803}
804
Michael Hana9171bc2012-08-03 17:40:43 +0000805static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000806 const AttributeList &Attr) {
807 SmallVector<Expr*, 1> Args;
808 if (!checkLockFunAttrCommon(S, D, Attr, Args))
809 return;
810
811 unsigned Size = Args.size();
812 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000813 D->addAttr(::new (S.Context)
814 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
815 StartArg, Size,
816 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000817}
818
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000819static void handleAssertSharedLockAttr(Sema &S, Decl *D,
820 const AttributeList &Attr) {
821 SmallVector<Expr*, 1> Args;
822 if (!checkLockFunAttrCommon(S, D, Attr, Args))
823 return;
824
825 unsigned Size = Args.size();
826 Expr **StartArg = Size == 0 ? 0 : &Args[0];
827 D->addAttr(::new (S.Context)
828 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
829 Attr.getAttributeSpellingListIndex()));
830}
831
832static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
833 const AttributeList &Attr) {
834 SmallVector<Expr*, 1> Args;
835 if (!checkLockFunAttrCommon(S, D, Attr, Args))
836 return;
837
838 unsigned Size = Args.size();
839 Expr **StartArg = Size == 0 ? 0 : &Args[0];
840 D->addAttr(::new (S.Context)
841 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
842 StartArg, Size,
843 Attr.getAttributeSpellingListIndex()));
844}
845
846
Michael Hana9171bc2012-08-03 17:40:43 +0000847static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
848 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000849 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000850 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000851 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000852
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000853 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000854 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
855 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000856 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000857 }
858
Aaron Ballman00e99962013-08-31 01:11:41 +0000859 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000860 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000861 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000862 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000863 }
864
865 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000866 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000867
Michael Han3be3b442012-07-23 18:48:41 +0000868 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000869}
870
Michael Hana9171bc2012-08-03 17:40:43 +0000871static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000872 const AttributeList &Attr) {
873 SmallVector<Expr*, 2> Args;
874 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
875 return;
876
Michael Han99315932013-01-24 16:46:58 +0000877 D->addAttr(::new (S.Context)
878 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000879 Attr.getArgAsExpr(0),
880 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000881 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000882}
883
Michael Hana9171bc2012-08-03 17:40:43 +0000884static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000885 const AttributeList &Attr) {
886 SmallVector<Expr*, 2> Args;
887 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
888 return;
889
Michael Han99315932013-01-24 16:46:58 +0000890 D->addAttr(::new (S.Context)
891 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000892 Attr.getArgAsExpr(0),
893 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000894 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000895}
896
Michael Hana9171bc2012-08-03 17:40:43 +0000897static bool checkLocksRequiredCommon(Sema &S, Decl *D,
898 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000899 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000900 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000901 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000902
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000903 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
905 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000906 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000907 }
908
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000909 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000910 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000911 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000912 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000913
Michael Han3be3b442012-07-23 18:48:41 +0000914 return true;
915}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000916
Michael Hana9171bc2012-08-03 17:40:43 +0000917static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000918 const AttributeList &Attr) {
919 SmallVector<Expr*, 1> Args;
920 if (!checkLocksRequiredCommon(S, D, Attr, Args))
921 return;
922
923 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000924 D->addAttr(::new (S.Context)
925 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
926 StartArg, Args.size(),
927 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000928}
929
Michael Hana9171bc2012-08-03 17:40:43 +0000930static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000931 const AttributeList &Attr) {
932 SmallVector<Expr*, 1> Args;
933 if (!checkLocksRequiredCommon(S, D, Attr, Args))
934 return;
935
936 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000937 D->addAttr(::new (S.Context)
938 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
939 StartArg, Args.size(),
940 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000941}
942
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000943static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000944 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000945 // zero or more arguments ok
946
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000947 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000948 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
949 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000950 return;
951 }
952
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000953 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000954 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000955 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000956 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000957 Expr **StartArg = Size == 0 ? 0 : &Args[0];
958
Michael Han99315932013-01-24 16:46:58 +0000959 D->addAttr(::new (S.Context)
960 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
961 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000962}
963
964static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000965 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000966 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000967 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
968 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000969 return;
970 }
971
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000972 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000973 SmallVector<Expr*, 1> Args;
974 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
975 unsigned Size = Args.size();
976 if (Size == 0)
977 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000978
Michael Han99315932013-01-24 16:46:58 +0000979 D->addAttr(::new (S.Context)
980 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
981 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000982}
983
984static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000985 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000986 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000987 return;
988
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000989 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000990 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
991 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000992 return;
993 }
994
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000995 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000996 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000997 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000998 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000999 if (Size == 0)
1000 return;
1001 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001002
Michael Han99315932013-01-24 16:46:58 +00001003 D->addAttr(::new (S.Context)
1004 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1005 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001006}
1007
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001008static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +00001009 ConsumableAttr::ConsumedState DefaultState;
1010
1011 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001012 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1013 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1014 DefaultState)) {
1015 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1016 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001017 return;
1018 }
David Blaikie16f76d22013-09-06 01:28:43 +00001019 } else {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1021 << Attr.getName() << AANT_ArgumentIdentifier;
1022 return;
1023 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001024
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001025 if (!isa<CXXRecordDecl>(D)) {
1026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1027 Attr.getName() << ExpectedClass;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001028 return;
1029 }
1030
1031 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +00001032 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001033 Attr.getAttributeSpellingListIndex()));
1034}
1035
1036static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1037 const AttributeList &Attr) {
1038 ASTContext &CurrContext = S.getASTContext();
1039 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1040
1041 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1042 if (!RD->hasAttr<ConsumableAttr>()) {
1043 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1044 RD->getNameAsString();
1045
1046 return false;
1047 }
1048 }
1049
1050 return true;
1051}
1052
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001053
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001054static void handleCallableWhenAttr(Sema &S, Decl *D,
1055 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001056 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1057 return;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001058
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001059 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1061 Attr.getName() << ExpectedMethod;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001062 return;
1063 }
1064
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001065 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1066 return;
1067
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001068 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1069 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1070 CallableWhenAttr::ConsumedState CallableState;
1071
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001072 StringRef StateString;
1073 SourceLocation Loc;
1074 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1075 return;
1076
1077 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001078 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001079 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1080 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001081 return;
1082 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001083
1084 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001085 }
1086
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001087 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001088 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1089 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001090}
1091
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001092
DeLesley Hutchins69391772013-10-17 23:23:53 +00001093static void handleParamTypestateAttr(Sema &S, Decl *D,
1094 const AttributeList &Attr) {
1095 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1096
1097 if (!isa<ParmVarDecl>(D)) {
1098 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1099 Attr.getName() << ExpectedParameter;
1100 return;
1101 }
1102
1103 ParamTypestateAttr::ConsumedState ParamState;
1104
1105 if (Attr.isArgIdent(0)) {
1106 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1107 StringRef StateString = Ident->Ident->getName();
1108
1109 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1110 ParamState)) {
1111 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1112 << Attr.getName() << StateString;
1113 return;
1114 }
1115 } else {
1116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1117 Attr.getName() << AANT_ArgumentIdentifier;
1118 return;
1119 }
1120
1121 // FIXME: This check is currently being done in the analysis. It can be
1122 // enabled here only after the parser propagates attributes at
1123 // template specialization definition, not declaration.
1124 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1125 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1126 //
1127 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1128 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1129 // ReturnType.getAsString();
1130 // return;
1131 //}
1132
1133 D->addAttr(::new (S.Context)
1134 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1135 Attr.getAttributeSpellingListIndex()));
1136}
1137
1138
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001139static void handleReturnTypestateAttr(Sema &S, Decl *D,
1140 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001141 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1142
1143 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1144 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1145 Attr.getName() << ExpectedFunctionMethodOrParameter;
1146 return;
1147 }
1148
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001149 ReturnTypestateAttr::ConsumedState ReturnState;
1150
1151 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001152 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1153 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1154 ReturnState)) {
1155 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1156 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001157 return;
1158 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001159 } else {
1160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1161 Attr.getName() << AANT_ArgumentIdentifier;
1162 return;
1163 }
1164
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001165 // FIXME: This check is currently being done in the analysis. It can be
1166 // enabled here only after the parser propagates attributes at
1167 // template specialization definition, not declaration.
1168 //QualType ReturnType;
1169 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001170 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1171 // ReturnType = Param->getType();
1172 //
1173 //} else if (const CXXConstructorDecl *Constructor =
1174 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001175 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1176 //
1177 //} else {
1178 //
1179 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1180 //}
1181 //
1182 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1183 //
1184 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1185 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1186 // ReturnType.getAsString();
1187 // return;
1188 //}
1189
1190 D->addAttr(::new (S.Context)
1191 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1192 Attr.getAttributeSpellingListIndex()));
1193}
1194
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001195
1196static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001197 if (!checkAttributeNumArgs(S, Attr, 1))
1198 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001199
1200 if (!isa<CXXMethodDecl>(D)) {
1201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1202 Attr.getName() << ExpectedMethod;
1203 return;
1204 }
1205
1206 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1207 return;
1208
1209 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001210 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001211 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1212 StringRef Param = Ident->Ident->getName();
1213 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1214 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1215 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001216 return;
1217 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001218 } else {
1219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1220 Attr.getName() << AANT_ArgumentIdentifier;
1221 return;
1222 }
1223
1224 D->addAttr(::new (S.Context)
1225 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1226 Attr.getAttributeSpellingListIndex()));
1227}
1228
Chris Wailes9385f9f2013-10-29 20:28:41 +00001229static void handleTestTypestateAttr(Sema &S, Decl *D,
1230 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001231 if (!checkAttributeNumArgs(S, Attr, 1))
1232 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001233
1234 if (!isa<CXXMethodDecl>(D)) {
1235 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1236 Attr.getName() << ExpectedMethod;
1237 return;
1238 }
1239
1240 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1241 return;
1242
Chris Wailes9385f9f2013-10-29 20:28:41 +00001243 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001244 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001245 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1246 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001247 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001248 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1249 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001250 return;
1251 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001252 } else {
1253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1254 Attr.getName() << AANT_ArgumentIdentifier;
1255 return;
1256 }
1257
1258 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001259 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001260 Attr.getAttributeSpellingListIndex()));
1261}
1262
Chandler Carruthedc2c642011-07-02 00:01:44 +00001263static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1264 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001265 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1266 if (TD == 0) {
1267 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1268 // and type-ids.
Aaron Ballmandbb634f2013-11-20 01:35:23 +00001269 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) <<
1270 Attr.getName() << ExpectedType;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001271 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001272 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001273
Richard Smith1f5a4322013-01-13 02:11:23 +00001274 // Remember this typedef decl, we will need it later for diagnostics.
1275 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001276}
1277
Chandler Carruthedc2c642011-07-02 00:01:44 +00001278static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001279 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001280 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001281 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001282 // If the alignment is less than or equal to 8 bits, the packed attribute
1283 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001284 if (!FD->getType()->isDependentType() &&
1285 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001286 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001287 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001288 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001289 else
Michael Han99315932013-01-24 16:46:58 +00001290 FD->addAttr(::new (S.Context)
1291 PackedAttr(Attr.getRange(), S.Context,
1292 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001293 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001294 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001295}
1296
Chandler Carruthedc2c642011-07-02 00:01:44 +00001297static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001298 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001299 RD->addAttr(::new (S.Context)
1300 MsStructAttr(Attr.getRange(), S.Context,
1301 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001302 else
Aaron Ballmanb80f94b2013-11-20 22:22:04 +00001303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1304 << Attr.getName() << ExpectedStructOrUnionOrClass;
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001305}
1306
Chandler Carruthedc2c642011-07-02 00:01:44 +00001307static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001308 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001309 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001310 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001311 D->addAttr(::new (S.Context)
1312 IBActionAttr(Attr.getRange(), S.Context,
1313 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001314 return;
1315 }
1316
Ted Kremenekd68ec812011-02-04 06:54:16 +00001317 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001318}
1319
Ted Kremenek7fd17232011-09-29 07:02:25 +00001320static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1321 // The IBOutlet/IBOutletCollection attributes only apply to instance
1322 // variables or properties of Objective-C classes. The outlet must also
1323 // have an object reference type.
1324 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1325 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001326 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001327 << Attr.getName() << VD->getType() << 0;
1328 return false;
1329 }
1330 }
1331 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1332 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001333 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001334 << Attr.getName() << PD->getType() << 1;
1335 return false;
1336 }
1337 }
1338 else {
1339 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1340 return false;
1341 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001342
Ted Kremenek7fd17232011-09-29 07:02:25 +00001343 return true;
1344}
1345
Chandler Carruthedc2c642011-07-02 00:01:44 +00001346static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001347 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001348 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001349
Michael Han99315932013-01-24 16:46:58 +00001350 D->addAttr(::new (S.Context)
1351 IBOutletAttr(Attr.getRange(), S.Context,
1352 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001353}
1354
Chandler Carruthedc2c642011-07-02 00:01:44 +00001355static void handleIBOutletCollection(Sema &S, Decl *D,
1356 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001357
1358 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001359 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1361 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001362 return;
1363 }
1364
Ted Kremenek7fd17232011-09-29 07:02:25 +00001365 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001366 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001367
Richard Smithb1f9a282013-10-31 01:56:18 +00001368 ParsedType PT;
1369
1370 if (Attr.hasParsedType())
1371 PT = Attr.getTypeArg();
1372 else {
1373 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1374 S.getScopeForContext(D->getDeclContext()->getParent()));
1375 if (!PT) {
1376 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1377 return;
1378 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001379 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001380
Richard Smithb87c4652013-10-31 21:23:20 +00001381 TypeSourceInfo *QTLoc = 0;
1382 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1383 if (!QTLoc)
1384 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001385
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001386 // Diagnose use of non-object type in iboutletcollection attribute.
1387 // FIXME. Gnu attribute extension ignores use of builtin types in
1388 // attributes. So, __attribute__((iboutletcollection(char))) will be
1389 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001390 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001391 S.Diag(Attr.getLoc(),
1392 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1393 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001394 return;
1395 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001396
Michael Han99315932013-01-24 16:46:58 +00001397 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001398 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001399 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001400}
1401
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001402static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001403 if (const RecordType *UT = T->getAsUnionType())
1404 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1405 RecordDecl *UD = UT->getDecl();
1406 for (RecordDecl::field_iterator it = UD->field_begin(),
1407 itend = UD->field_end(); it != itend; ++it) {
1408 QualType QT = it->getType();
1409 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1410 T = QT;
1411 return;
1412 }
1413 }
1414 }
1415}
1416
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001417static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001418 if (!isFunctionOrMethod(D)) {
1419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001420 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001421 return;
1422 }
1423
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001424 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1425 return;
1426
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001427 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001429 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001430 uint64_t Idx;
1431 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1432 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001433 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001434
1435 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001436 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001437 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1439 << Attr.getName() << AANT_ArgumentIntegerConstant
1440 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001441 return;
1442 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001443 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001444 }
1445
1446 // check if the function returns a pointer
1447 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1448 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001449 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001450 }
1451
Michael Han99315932013-01-24 16:46:58 +00001452 D->addAttr(::new (S.Context)
1453 AllocSizeAttr(Attr.getRange(), S.Context,
1454 SizeArgs.data(), SizeArgs.size(),
1455 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001456}
1457
Chandler Carruthedc2c642011-07-02 00:01:44 +00001458static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001459 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1460 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001461 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001462 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001463 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001464 return;
1465 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001466
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001467 SmallVector<unsigned, 8> NonNullArgs;
1468 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001469 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001470 uint64_t Idx;
1471 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1472 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001473 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001474
1475 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001476 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001477 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001478
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001479 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001480 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001481 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001482 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001483 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001484 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001485
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001486 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001487 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001488
1489 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1490 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001491 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001492 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1493 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001494 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001495 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001496 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001497 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001498
Ted Kremenek22813f42010-10-21 18:49:36 +00001499 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001500 if (NonNullArgs.empty()) {
1501 // Warn the trivial case only if attribute is not coming from a
1502 // macro instantiation.
1503 if (Attr.getLoc().isFileID())
1504 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001505 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001506 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001507 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001508
Nick Lewyckye1121512013-01-24 01:12:16 +00001509 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001510 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001511 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001512 D->addAttr(::new (S.Context)
1513 NonNullAttr(Attr.getRange(), S.Context, start, size,
1514 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001515}
1516
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001517static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1518 switch (K) {
1519 case OwnershipAttr::Holds: return "'ownership_holds'";
1520 case OwnershipAttr::Takes: return "'ownership_takes'";
1521 case OwnershipAttr::Returns: return "'ownership_returns'";
1522 }
1523 llvm_unreachable("unknown ownership");
1524}
1525
Chandler Carruthedc2c642011-07-02 00:01:44 +00001526static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001527 // This attribute must be applied to a function declaration. The first
1528 // argument to the attribute must be an identifier, the name of the resource,
1529 // for example: malloc. The following arguments must be argument indexes, the
1530 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001531 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001532 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001533 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001534
Aaron Ballman00e99962013-08-31 01:11:41 +00001535 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001536 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001537 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001538 return;
1539 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001540
Ted Kremenekd21139a2010-07-31 01:52:11 +00001541 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001542 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001543 switch (AL.getKind()) {
1544 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001545 K = OwnershipAttr::Takes;
Aaron Ballman00e99962013-08-31 01:11:41 +00001546 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001547 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001548 return;
1549 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001550 break;
1551 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001552 K = OwnershipAttr::Holds;
Aaron Ballman00e99962013-08-31 01:11:41 +00001553 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001554 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001555 return;
1556 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001557 break;
1558 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001559 K = OwnershipAttr::Returns;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001560
Aaron Ballman00e99962013-08-31 01:11:41 +00001561 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001562 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001563 return;
1564 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001565 break;
1566 default:
1567 // This should never happen given how we are called.
1568 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001569 }
1570
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001571 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001572 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1573 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001574 return;
1575 }
1576
Aaron Ballman00e99962013-08-31 01:11:41 +00001577 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001578
1579 // Normalize the argument, __foo__ becomes foo.
1580 if (Module.startswith("__") && Module.endswith("__"))
1581 Module = Module.substr(2, Module.size() - 4);
1582
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001583 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001584 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1585 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001586 uint64_t Idx;
1587 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001588 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001589 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001590
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001591 // Is the function argument a pointer type?
1592 QualType T = getFunctionOrMethodArgType(D, Idx);
1593 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001594 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001595 case OwnershipAttr::Takes:
1596 case OwnershipAttr::Holds:
1597 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1598 Err = 0;
1599 break;
1600 case OwnershipAttr::Returns:
1601 if (!T->isIntegerType())
1602 Err = 1;
1603 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001604 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001605 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001606 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001607 << Ex->getSourceRange();
1608 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001609 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001610
1611 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001612 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001613 i = D->specific_attr_begin<OwnershipAttr>(),
1614 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1615 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1616 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1617 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1618 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1619 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001620 }
1621 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001622 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001623 }
1624
1625 unsigned* start = OwnershipArgs.data();
1626 unsigned size = OwnershipArgs.size();
1627 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001628
Michael Han99315932013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context)
1630 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1631 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001632}
1633
Chandler Carruthedc2c642011-07-02 00:01:44 +00001634static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001635 // Check the attribute arguments.
1636 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1638 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001639 return;
1640 }
1641
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001642 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001644 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001645 return;
1646 }
1647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001648 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001649
Rafael Espindolac18086a2010-02-23 22:00:30 +00001650 // gcc rejects
1651 // class c {
1652 // static int a __attribute__((weakref ("v2")));
1653 // static int b() __attribute__((weakref ("f3")));
1654 // };
1655 // and ignores the attributes of
1656 // void f(void) {
1657 // static int a __attribute__((weakref ("v2")));
1658 // }
1659 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001660 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001661 if (!Ctx->isFileContext()) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001663 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001664 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001665 }
1666
1667 // The GCC manual says
1668 //
1669 // At present, a declaration to which `weakref' is attached can only
1670 // be `static'.
1671 //
1672 // It also says
1673 //
1674 // Without a TARGET,
1675 // given as an argument to `weakref' or to `alias', `weakref' is
1676 // equivalent to `weak'.
1677 //
1678 // gcc 4.4.1 will accept
1679 // int a7 __attribute__((weakref));
1680 // as
1681 // int a7 __attribute__((weak));
1682 // This looks like a bug in gcc. We reject that for now. We should revisit
1683 // it if this behaviour is actually used.
1684
Rafael Espindolac18086a2010-02-23 22:00:30 +00001685 // GCC rejects
1686 // static ((alias ("y"), weakref)).
1687 // Should we? How to check that weakref is before or after alias?
1688
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001689 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1690 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1691 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001692 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001693 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001694 // GCC will accept anything as the argument of weakref. Should we
1695 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001696 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1697 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001698
Michael Han99315932013-01-24 16:46:58 +00001699 D->addAttr(::new (S.Context)
1700 WeakRefAttr(Attr.getRange(), S.Context,
1701 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001702}
1703
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001704static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1705 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001706 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001707 return;
1708
Douglas Gregore8bbc122011-09-02 00:18:52 +00001709 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001710 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1711 return;
1712 }
1713
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001714 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001716 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001717 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001718}
1719
Quentin Colombet4e172062012-11-01 23:55:47 +00001720static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombet4e172062012-11-01 23:55:47 +00001721 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1723 << Attr.getName() << ExpectedFunctionOrMethod;
1724 return;
1725 }
1726
Michael Han99315932013-01-24 16:46:58 +00001727 D->addAttr(::new (S.Context)
1728 MinSizeAttr(Attr.getRange(), S.Context,
1729 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001730}
1731
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001732static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001733 if (!isa<FunctionDecl>(D)) {
1734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1735 << Attr.getName() << ExpectedFunction;
1736 return;
1737 }
1738
1739 if (D->hasAttr<HotAttr>()) {
1740 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1741 << Attr.getName() << "hot";
1742 return;
1743 }
1744
Michael Han99315932013-01-24 16:46:58 +00001745 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1746 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001747}
1748
1749static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001750 if (!isa<FunctionDecl>(D)) {
1751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1752 << Attr.getName() << ExpectedFunction;
1753 return;
1754 }
1755
1756 if (D->hasAttr<ColdAttr>()) {
1757 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1758 << Attr.getName() << "cold";
1759 return;
1760 }
1761
Michael Han99315932013-01-24 16:46:58 +00001762 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1763 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001764}
1765
Chandler Carruthedc2c642011-07-02 00:01:44 +00001766static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001767 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001769 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001770 return;
1771 }
1772
Michael Han99315932013-01-24 16:46:58 +00001773 D->addAttr(::new (S.Context)
1774 NakedAttr(Attr.getRange(), S.Context,
1775 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001776}
1777
Chandler Carruthedc2c642011-07-02 00:01:44 +00001778static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1779 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001780 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001782 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001783 return;
1784 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001785
Michael Han99315932013-01-24 16:46:58 +00001786 D->addAttr(::new (S.Context)
1787 AlwaysInlineAttr(Attr.getRange(), S.Context,
1788 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001789}
1790
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001791static void handleTLSModelAttr(Sema &S, Decl *D,
1792 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001793 StringRef Model;
1794 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001795 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001796 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001797 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001798
Richard Smithfd3834f2013-04-13 02:43:54 +00001799 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1801 << Attr.getName() << ExpectedTLSVar;
1802 return;
1803 }
1804
1805 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001806 if (Model != "global-dynamic" && Model != "local-dynamic"
1807 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001808 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001809 return;
1810 }
1811
Michael Han99315932013-01-24 16:46:58 +00001812 D->addAttr(::new (S.Context)
1813 TLSModelAttr(Attr.getRange(), S.Context, Model,
1814 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001815}
1816
Chandler Carruthedc2c642011-07-02 00:01:44 +00001817static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001818 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001819 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001820 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001821 D->addAttr(::new (S.Context)
1822 MallocAttr(Attr.getRange(), S.Context,
1823 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001824 return;
1825 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001826 }
1827
Ted Kremenek08479ae2009-08-15 00:51:46 +00001828 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001829}
1830
Chandler Carruthedc2c642011-07-02 00:01:44 +00001831static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00001832 D->addAttr(::new (S.Context)
1833 MayAliasAttr(Attr.getRange(), S.Context,
1834 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001835}
1836
Chandler Carruthedc2c642011-07-02 00:01:44 +00001837static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001838 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001839 D->addAttr(::new (S.Context)
1840 NoCommonAttr(Attr.getRange(), S.Context,
1841 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001842 else
1843 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001844 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001845}
1846
Chandler Carruthedc2c642011-07-02 00:01:44 +00001847static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001848 if (S.LangOpts.CPlusPlus) {
1849 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1850 return;
1851 }
1852
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001853 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001854 D->addAttr(::new (S.Context)
1855 CommonAttr(Attr.getRange(), S.Context,
1856 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001857 else
1858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001859 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001860}
1861
Chandler Carruthedc2c642011-07-02 00:01:44 +00001862static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001863 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001864
1865 if (S.CheckNoReturnAttr(attr)) return;
1866
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001867 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001868 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001869 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001870 return;
1871 }
1872
Michael Han99315932013-01-24 16:46:58 +00001873 D->addAttr(::new (S.Context)
1874 NoReturnAttr(attr.getRange(), S.Context,
1875 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001876}
1877
1878bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001879 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001880 attr.setInvalid();
1881 return true;
1882 }
1883
1884 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001885}
1886
Chandler Carruthedc2c642011-07-02 00:01:44 +00001887static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1888 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001889
1890 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1891 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001892 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1893 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001894 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1895 && !VD->getType()->isFunctionPointerType())) {
1896 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001897 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001898 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001899 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001900 return;
1901 }
1902 }
1903
Michael Han99315932013-01-24 16:46:58 +00001904 D->addAttr(::new (S.Context)
1905 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1906 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001907}
1908
Richard Smith10876ef2013-01-17 01:30:42 +00001909static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1910 const AttributeList &Attr) {
1911 // C++11 [dcl.attr.noreturn]p1:
1912 // The attribute may be applied to the declarator-id in a function
1913 // declaration.
1914 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1915 if (!FD) {
1916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1917 << Attr.getName() << ExpectedFunctionOrMethod;
1918 return;
1919 }
1920
Michael Han99315932013-01-24 16:46:58 +00001921 D->addAttr(::new (S.Context)
1922 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1923 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001924}
1925
John Thompsoncdb847ba2010-08-09 21:53:52 +00001926// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001927static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001928/*
1929 Returning a Vector Class in Registers
1930
Eric Christopherbc638a82010-12-01 22:13:54 +00001931 According to the PPU ABI specifications, a class with a single member of
1932 vector type is returned in memory when used as the return value of a function.
1933 This results in inefficient code when implementing vector classes. To return
1934 the value in a single vector register, add the vecreturn attribute to the
1935 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001936
1937 Example:
1938
1939 struct Vector
1940 {
1941 __vector float xyzw;
1942 } __attribute__((vecreturn));
1943
1944 Vector Add(Vector lhs, Vector rhs)
1945 {
1946 Vector result;
1947 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1948 return result; // This will be returned in a register
1949 }
1950*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001951 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001952 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001953 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001954 return;
1955 }
1956
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001957 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001958 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1959 return;
1960 }
1961
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001962 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001963 int count = 0;
1964
1965 if (!isa<CXXRecordDecl>(record)) {
1966 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1967 return;
1968 }
1969
1970 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1971 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1972 return;
1973 }
1974
Eric Christopherbc638a82010-12-01 22:13:54 +00001975 for (RecordDecl::field_iterator iter = record->field_begin();
1976 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001977 if ((count == 1) || !iter->getType()->isVectorType()) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1979 return;
1980 }
1981 count++;
1982 }
1983
Michael Han99315932013-01-24 16:46:58 +00001984 D->addAttr(::new (S.Context)
1985 VecReturnAttr(Attr.getRange(), S.Context,
1986 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001987}
1988
Richard Smithe233fbf2013-01-28 22:42:45 +00001989static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1990 const AttributeList &Attr) {
1991 if (isa<ParmVarDecl>(D)) {
1992 // [[carries_dependency]] can only be applied to a parameter if it is a
1993 // parameter of a function declaration or lambda.
1994 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1995 S.Diag(Attr.getLoc(),
1996 diag::err_carries_dependency_param_not_function_decl);
1997 return;
1998 }
1999 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002000 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002001 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002002 return;
2003 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002004
2005 D->addAttr(::new (S.Context) CarriesDependencyAttr(
2006 Attr.getRange(), S.Context,
2007 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002008}
2009
Chandler Carruthedc2c642011-07-02 00:01:44 +00002010static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002011 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00002012 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002013 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002014 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00002015 return;
2016 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002017
Michael Han99315932013-01-24 16:46:58 +00002018 D->addAttr(::new (S.Context)
2019 UnusedAttr(Attr.getRange(), S.Context,
2020 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00002021}
2022
Rafael Espindola70107f92011-10-03 14:59:42 +00002023static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2024 const AttributeList &Attr) {
Rafael Espindola70107f92011-10-03 14:59:42 +00002025 if (!isa<FunctionDecl>(D)) {
2026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2027 << Attr.getName() << ExpectedFunction;
2028 return;
2029 }
2030
Michael Han99315932013-01-24 16:46:58 +00002031 D->addAttr(::new (S.Context)
2032 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2033 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00002034}
2035
Chandler Carruthedc2c642011-07-02 00:01:44 +00002036static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002037 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002038 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002039 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2040 return;
2041 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002042 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002044 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002045 return;
2046 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002047
Michael Han99315932013-01-24 16:46:58 +00002048 D->addAttr(::new (S.Context)
2049 UsedAttr(Attr.getRange(), S.Context,
2050 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002051}
2052
Chandler Carruthedc2c642011-07-02 00:01:44 +00002053static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002054 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002055 if (Attr.getNumArgs() > 1) {
2056 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002057 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002058 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002059
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002060 uint32_t priority = ConstructorAttr::DefaultPriority;
2061 if (Attr.getNumArgs() > 0 &&
2062 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2063 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002064
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002065 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002066 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002067 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002068 return;
2069 }
2070
Michael Han99315932013-01-24 16:46:58 +00002071 D->addAttr(::new (S.Context)
2072 ConstructorAttr(Attr.getRange(), S.Context, priority,
2073 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002074}
2075
Chandler Carruthedc2c642011-07-02 00:01:44 +00002076static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002077 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002078 if (Attr.getNumArgs() > 1) {
2079 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002080 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002081 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002082
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002083 uint32_t priority = ConstructorAttr::DefaultPriority;
2084 if (Attr.getNumArgs() > 0 &&
2085 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2086 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002087
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002088 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002089 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002090 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002091 return;
2092 }
2093
Michael Han99315932013-01-24 16:46:58 +00002094 D->addAttr(::new (S.Context)
2095 DestructorAttr(Attr.getRange(), S.Context, priority,
2096 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002097}
2098
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002099template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002100static void handleAttrWithMessage(Sema &S, Decl *D,
2101 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002102 unsigned NumArgs = Attr.getNumArgs();
2103 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002104 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002105 return;
2106 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002107
2108 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002109 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002110 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002111 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002112
Michael Han99315932013-01-24 16:46:58 +00002113 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2114 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002115}
2116
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002117static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2118 const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002119 D->addAttr(::new (S.Context)
2120 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2121 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002122}
2123
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002124static void handleObjCRootClassAttr(Sema &S, Decl *D,
2125 const AttributeList &Attr) {
2126 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2128 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002129 return;
2130 }
2131
Michael Han99315932013-01-24 16:46:58 +00002132 D->addAttr(::new (S.Context)
2133 ObjCRootClassAttr(Attr.getRange(), S.Context,
2134 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002135}
2136
Michael Han99315932013-01-24 16:46:58 +00002137static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2138 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002139 if (!isa<ObjCInterfaceDecl>(D)) {
2140 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2141 return;
2142 }
2143
Michael Han99315932013-01-24 16:46:58 +00002144 D->addAttr(::new (S.Context)
2145 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2146 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002147}
2148
Jordy Rose740b0c22012-05-08 03:27:22 +00002149static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2150 IdentifierInfo *Platform,
2151 VersionTuple Introduced,
2152 VersionTuple Deprecated,
2153 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002154 StringRef PlatformName
2155 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2156 if (PlatformName.empty())
2157 PlatformName = Platform->getName();
2158
2159 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2160 // of these steps are needed).
2161 if (!Introduced.empty() && !Deprecated.empty() &&
2162 !(Introduced <= Deprecated)) {
2163 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2164 << 1 << PlatformName << Deprecated.getAsString()
2165 << 0 << Introduced.getAsString();
2166 return true;
2167 }
2168
2169 if (!Introduced.empty() && !Obsoleted.empty() &&
2170 !(Introduced <= Obsoleted)) {
2171 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2172 << 2 << PlatformName << Obsoleted.getAsString()
2173 << 0 << Introduced.getAsString();
2174 return true;
2175 }
2176
2177 if (!Deprecated.empty() && !Obsoleted.empty() &&
2178 !(Deprecated <= Obsoleted)) {
2179 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2180 << 2 << PlatformName << Obsoleted.getAsString()
2181 << 1 << Deprecated.getAsString();
2182 return true;
2183 }
2184
2185 return false;
2186}
2187
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002188/// \brief Check whether the two versions match.
2189///
2190/// If either version tuple is empty, then they are assumed to match. If
2191/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2192static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2193 bool BeforeIsOkay) {
2194 if (X.empty() || Y.empty())
2195 return true;
2196
2197 if (X == Y)
2198 return true;
2199
2200 if (BeforeIsOkay && X < Y)
2201 return true;
2202
2203 return false;
2204}
2205
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002206AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002207 IdentifierInfo *Platform,
2208 VersionTuple Introduced,
2209 VersionTuple Deprecated,
2210 VersionTuple Obsoleted,
2211 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002212 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002213 bool Override,
2214 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002215 VersionTuple MergedIntroduced = Introduced;
2216 VersionTuple MergedDeprecated = Deprecated;
2217 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002218 bool FoundAny = false;
2219
Rafael Espindolac67f2232012-05-10 02:50:16 +00002220 if (D->hasAttrs()) {
2221 AttrVec &Attrs = D->getAttrs();
2222 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2223 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2224 if (!OldAA) {
2225 ++i;
2226 continue;
2227 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002228
Rafael Espindolac67f2232012-05-10 02:50:16 +00002229 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2230 if (OldPlatform != Platform) {
2231 ++i;
2232 continue;
2233 }
2234
2235 FoundAny = true;
2236 VersionTuple OldIntroduced = OldAA->getIntroduced();
2237 VersionTuple OldDeprecated = OldAA->getDeprecated();
2238 VersionTuple OldObsoleted = OldAA->getObsoleted();
2239 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002240
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002241 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2242 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2243 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2244 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002245 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002246 if (Override) {
2247 int Which = -1;
2248 VersionTuple FirstVersion;
2249 VersionTuple SecondVersion;
2250 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2251 Which = 0;
2252 FirstVersion = OldIntroduced;
2253 SecondVersion = Introduced;
2254 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2255 Which = 1;
2256 FirstVersion = Deprecated;
2257 SecondVersion = OldDeprecated;
2258 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2259 Which = 2;
2260 FirstVersion = Obsoleted;
2261 SecondVersion = OldObsoleted;
2262 }
2263
2264 if (Which == -1) {
2265 Diag(OldAA->getLocation(),
2266 diag::warn_mismatched_availability_override_unavail)
2267 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2268 } else {
2269 Diag(OldAA->getLocation(),
2270 diag::warn_mismatched_availability_override)
2271 << Which
2272 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2273 << FirstVersion.getAsString() << SecondVersion.getAsString();
2274 }
2275 Diag(Range.getBegin(), diag::note_overridden_method);
2276 } else {
2277 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2278 Diag(Range.getBegin(), diag::note_previous_attribute);
2279 }
2280
Rafael Espindolac67f2232012-05-10 02:50:16 +00002281 Attrs.erase(Attrs.begin() + i);
2282 --e;
2283 continue;
2284 }
2285
2286 VersionTuple MergedIntroduced2 = MergedIntroduced;
2287 VersionTuple MergedDeprecated2 = MergedDeprecated;
2288 VersionTuple MergedObsoleted2 = MergedObsoleted;
2289
2290 if (MergedIntroduced2.empty())
2291 MergedIntroduced2 = OldIntroduced;
2292 if (MergedDeprecated2.empty())
2293 MergedDeprecated2 = OldDeprecated;
2294 if (MergedObsoleted2.empty())
2295 MergedObsoleted2 = OldObsoleted;
2296
2297 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2298 MergedIntroduced2, MergedDeprecated2,
2299 MergedObsoleted2)) {
2300 Attrs.erase(Attrs.begin() + i);
2301 --e;
2302 continue;
2303 }
2304
2305 MergedIntroduced = MergedIntroduced2;
2306 MergedDeprecated = MergedDeprecated2;
2307 MergedObsoleted = MergedObsoleted2;
2308 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002309 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002310 }
2311
2312 if (FoundAny &&
2313 MergedIntroduced == Introduced &&
2314 MergedDeprecated == Deprecated &&
2315 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002316 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002317
Ted Kremenekb5445722013-04-06 00:34:27 +00002318 // Only create a new attribute if !Override, but we want to do
2319 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002320 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002321 MergedDeprecated, MergedObsoleted) &&
2322 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002323 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2324 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002325 Obsoleted, IsUnavailable, Message,
2326 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002327 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002328 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002329}
2330
Chandler Carruthedc2c642011-07-02 00:01:44 +00002331static void handleAvailabilityAttr(Sema &S, Decl *D,
2332 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002333 if (!checkAttributeNumArgs(S, Attr, 1))
2334 return;
2335 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002336 unsigned Index = Attr.getAttributeSpellingListIndex();
2337
Aaron Ballman00e99962013-08-31 01:11:41 +00002338 IdentifierInfo *II = Platform->Ident;
2339 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2340 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2341 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002342
Rafael Espindolac231fab2013-01-08 21:30:32 +00002343 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2344 if (!ND) {
2345 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2346 return;
2347 }
2348
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002349 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2350 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2351 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002352 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002353 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002354 if (const StringLiteral *SE =
2355 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002356 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002357
Aaron Ballman00e99962013-08-31 01:11:41 +00002358 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002359 Introduced.Version,
2360 Deprecated.Version,
2361 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002362 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002363 /*Override=*/false,
2364 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002365 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002366 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002367}
2368
John McCalld041a9b2013-02-20 01:54:26 +00002369template <class T>
2370static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2371 typename T::VisibilityType value,
2372 unsigned attrSpellingListIndex) {
2373 T *existingAttr = D->getAttr<T>();
2374 if (existingAttr) {
2375 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2376 if (existingValue == value)
2377 return NULL;
2378 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2379 S.Diag(range.getBegin(), diag::note_previous_attribute);
2380 D->dropAttr<T>();
2381 }
2382 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2383}
2384
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002385VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002386 VisibilityAttr::VisibilityType Vis,
2387 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002388 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2389 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002390}
2391
John McCalld041a9b2013-02-20 01:54:26 +00002392TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2393 TypeVisibilityAttr::VisibilityType Vis,
2394 unsigned AttrSpellingListIndex) {
2395 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2396 AttrSpellingListIndex);
2397}
2398
2399static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2400 bool isTypeVisibility) {
2401 // Visibility attributes don't mean anything on a typedef.
2402 if (isa<TypedefNameDecl>(D)) {
2403 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2404 << Attr.getName();
2405 return;
2406 }
2407
2408 // 'type_visibility' can only go on a type or namespace.
2409 if (isTypeVisibility &&
2410 !(isa<TagDecl>(D) ||
2411 isa<ObjCInterfaceDecl>(D) ||
2412 isa<NamespaceDecl>(D))) {
2413 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2414 << Attr.getName() << ExpectedTypeOrNamespace;
2415 return;
2416 }
2417
Benjamin Kramer70370212013-09-09 15:08:57 +00002418 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002419 StringRef TypeStr;
2420 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002421 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002422 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002423
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002424 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002425 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002426 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002427 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002428 return;
2429 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002430
2431 // Complain about attempts to use protected visibility on targets
2432 // (like Darwin) that don't support it.
2433 if (type == VisibilityAttr::Protected &&
2434 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2435 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2436 type = VisibilityAttr::Default;
2437 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002438
Michael Han99315932013-01-24 16:46:58 +00002439 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002440 clang::Attr *newAttr;
2441 if (isTypeVisibility) {
2442 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2443 (TypeVisibilityAttr::VisibilityType) type,
2444 Index);
2445 } else {
2446 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2447 }
2448 if (newAttr)
2449 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002450}
2451
Chandler Carruthedc2c642011-07-02 00:01:44 +00002452static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2453 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002454 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2455 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002457 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002458 return;
2459 }
2460
Aaron Ballman00e99962013-08-31 01:11:41 +00002461 if (!Attr.isArgIdent(0)) {
2462 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2463 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002464 return;
2465 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002466
Aaron Ballman682ee422013-09-11 19:47:58 +00002467 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2468 ObjCMethodFamilyAttr::FamilyKind F;
2469 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2470 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2471 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002472 return;
2473 }
2474
Aaron Ballman682ee422013-09-11 19:47:58 +00002475 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002476 !method->getResultType()->isObjCObjectPointerType()) {
2477 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2478 << method->getResultType();
2479 // Ignore the attribute.
2480 return;
2481 }
2482
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002483 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002484 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002485}
2486
Chandler Carruthedc2c642011-07-02 00:01:44 +00002487static void handleObjCExceptionAttr(Sema &S, Decl *D,
2488 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002489 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2490 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2492 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002493 return;
2494 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002495
Michael Han99315932013-01-24 16:46:58 +00002496 D->addAttr(::new (S.Context)
2497 ObjCExceptionAttr(Attr.getRange(), S.Context,
2498 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002499}
2500
Chandler Carruthedc2c642011-07-02 00:01:44 +00002501static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002502 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002503 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002504 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002505 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2506 return;
2507 }
2508 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002509 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2510 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002511 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002512 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2513 return;
2514 }
2515 }
2516 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002517 // It is okay to include this attribute on properties, e.g.:
2518 //
2519 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2520 //
2521 // In this case it follows tradition and suppresses an error in the above
2522 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002523 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002524 }
Michael Han99315932013-01-24 16:46:58 +00002525 D->addAttr(::new (S.Context)
2526 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2527 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002528}
2529
Mike Stumpd3bb5572009-07-24 19:02:52 +00002530static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002531handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002532 if (!isa<FunctionDecl>(D)) {
2533 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2534 return;
2535 }
2536
Michael Han99315932013-01-24 16:46:58 +00002537 D->addAttr(::new (S.Context)
2538 OverloadableAttr(Attr.getRange(), S.Context,
2539 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002540}
2541
Chandler Carruthedc2c642011-07-02 00:01:44 +00002542static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002543 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002544 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002545 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002546 return;
2547 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002548
Aaron Ballman00e99962013-08-31 01:11:41 +00002549 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002550 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002551 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2552 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2553 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002554 return;
2555 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002556
Michael Han99315932013-01-24 16:46:58 +00002557 D->addAttr(::new (S.Context)
2558 BlocksAttr(Attr.getRange(), S.Context, type,
2559 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002560}
2561
Chandler Carruthedc2c642011-07-02 00:01:44 +00002562static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002563 // check the attribute arguments.
2564 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002565 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002566 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002567 }
2568
Aaron Ballman18a78382013-11-21 00:28:23 +00002569 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002570 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002571 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002572 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002573 if (E->isTypeDependent() || E->isValueDependent() ||
2574 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002576 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002577 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002578 return;
2579 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002580
John McCallb46f2872011-09-09 07:56:05 +00002581 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002582 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2583 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002584 return;
2585 }
John McCallb46f2872011-09-09 07:56:05 +00002586
2587 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002588 }
2589
Aaron Ballman18a78382013-11-21 00:28:23 +00002590 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002591 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002592 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002593 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002594 if (E->isTypeDependent() || E->isValueDependent() ||
2595 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002597 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002598 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002599 return;
2600 }
2601 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602
John McCallb46f2872011-09-09 07:56:05 +00002603 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002604 // FIXME: This error message could be improved, it would be nice
2605 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2607 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002608 return;
2609 }
2610 }
2611
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002612 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002613 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002614 if (isa<FunctionNoProtoType>(FT)) {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2616 return;
2617 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002618
Chris Lattner9363e312009-03-17 23:03:47 +00002619 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002620 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002621 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002622 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002623 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002624 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002625 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002626 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002627 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002628 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2629 if (!BD->isVariadic()) {
2630 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2631 return;
2632 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002633 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002634 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002635 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002636 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002637 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002638 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002639 int m = Ty->isFunctionPointerType() ? 0 : 1;
2640 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002641 return;
2642 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002643 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002644 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002645 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002646 return;
2647 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002648 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002650 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002651 return;
2652 }
Michael Han99315932013-01-24 16:46:58 +00002653 D->addAttr(::new (S.Context)
2654 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2655 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002656}
2657
Lubos Lunakedc13882013-07-20 15:05:36 +00002658static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002659 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2660 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2661 else
2662 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2663}
2664
Chandler Carruthedc2c642011-07-02 00:01:44 +00002665static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002666 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002667 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002668 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002669 return;
2670 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002671
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002672 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2673 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2674 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002675 return;
2676 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002677 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2678 if (MD->getResultType()->isVoidType()) {
2679 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2680 << Attr.getName() << 1;
2681 return;
2682 }
2683
Michael Han99315932013-01-24 16:46:58 +00002684 D->addAttr(::new (S.Context)
2685 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2686 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002687}
2688
Chandler Carruthedc2c642011-07-02 00:01:44 +00002689static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002690 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002691 if (isa<CXXRecordDecl>(D)) {
2692 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2693 return;
2694 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002695 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2696 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002697 return;
2698 }
2699
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002700 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002701
Michael Han99315932013-01-24 16:46:58 +00002702 nd->addAttr(::new (S.Context)
2703 WeakAttr(Attr.getRange(), S.Context,
2704 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002705}
2706
Chandler Carruthedc2c642011-07-02 00:01:44 +00002707static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002708 // weak_import only applies to variable & function declarations.
2709 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002710 if (!D->canBeWeakImported(isDef)) {
2711 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002712 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2713 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002714 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002715 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002716 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002717 // Nothing to warn about here.
2718 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002720 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002721
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002722 return;
2723 }
2724
Michael Han99315932013-01-24 16:46:58 +00002725 D->addAttr(::new (S.Context)
2726 WeakImportAttr(Attr.getRange(), S.Context,
2727 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002728}
2729
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002730// Handles reqd_work_group_size and work_group_size_hint.
2731static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002732 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002733 uint32_t WGSize[3];
2734 for (unsigned i = 0; i < 3; ++i)
2735 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002736 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002737
2738 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2739 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2740 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2741 if (!(A->getXDim() == WGSize[0] &&
2742 A->getYDim() == WGSize[1] &&
2743 A->getZDim() == WGSize[2])) {
2744 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2745 Attr.getName();
2746 }
2747 }
2748
2749 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2750 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2751 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2752 if (!(A->getXDim() == WGSize[0] &&
2753 A->getYDim() == WGSize[1] &&
2754 A->getZDim() == WGSize[2])) {
2755 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2756 Attr.getName();
2757 }
2758 }
2759
2760 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2761 D->addAttr(::new (S.Context)
2762 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002763 WGSize[0], WGSize[1], WGSize[2],
2764 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002765 else
2766 D->addAttr(::new (S.Context)
2767 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002768 WGSize[0], WGSize[1], WGSize[2],
2769 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002770}
2771
Joey Goulyaba589c2013-03-08 09:42:32 +00002772static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2773 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2774
Aaron Ballman00e99962013-08-31 01:11:41 +00002775 if (!Attr.hasParsedType()) {
2776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2777 << Attr.getName() << 1;
2778 return;
2779 }
2780
Richard Smithb87c4652013-10-31 21:23:20 +00002781 TypeSourceInfo *ParmTSI = 0;
2782 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2783 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002784
2785 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2786 (ParmType->isBooleanType() ||
2787 !ParmType->isIntegralType(S.getASTContext()))) {
2788 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2789 << ParmType;
2790 return;
2791 }
2792
2793 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2794 D->hasAttr<VecTypeHintAttr>()) {
2795 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002796 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002797 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2798 return;
2799 }
2800 }
2801
2802 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002803 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002804}
2805
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002806SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002807 StringRef Name,
2808 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002809 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2810 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002811 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002812 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2813 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002814 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002815 }
Michael Han99315932013-01-24 16:46:58 +00002816 return ::new (Context) SectionAttr(Range, Context, Name,
2817 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002818}
2819
Chandler Carruthedc2c642011-07-02 00:01:44 +00002820static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002821 // Make sure that there is a string literal as the sections's single
2822 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002823 StringRef Str;
2824 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002825 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002826 return;
Mike Stump11289f42009-09-09 15:08:12 +00002827
Chris Lattner30ba6742009-08-10 19:03:04 +00002828 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002829 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002830 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002831 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002832 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002833 return;
2834 }
Mike Stump11289f42009-09-09 15:08:12 +00002835
Chris Lattner20aee9b2010-01-12 20:58:53 +00002836 // This attribute cannot be applied to local variables.
2837 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002838 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002839 return;
2840 }
Michael Han99315932013-01-24 16:46:58 +00002841
2842 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002843 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002844 if (NewAttr)
2845 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002846}
2847
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002848
Chandler Carruthedc2c642011-07-02 00:01:44 +00002849static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002850 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002851 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002852 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002853 } else {
Michael Han99315932013-01-24 16:46:58 +00002854 D->addAttr(::new (S.Context)
2855 NoThrowAttr(Attr.getRange(), S.Context,
2856 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002857 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002858}
2859
Chandler Carruthedc2c642011-07-02 00:01:44 +00002860static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002861 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002862 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002863 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002864 } else {
Michael Han99315932013-01-24 16:46:58 +00002865 D->addAttr(::new (S.Context)
2866 ConstAttr(Attr.getRange(), S.Context,
2867 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002868 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002869}
2870
Chandler Carruthedc2c642011-07-02 00:01:44 +00002871static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002872 D->addAttr(::new (S.Context)
2873 PureAttr(Attr.getRange(), S.Context,
2874 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002875}
2876
Chandler Carruthedc2c642011-07-02 00:01:44 +00002877static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002878 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002879 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002881 return;
2882 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002883
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002884 Expr *E = Attr.getArgAsExpr(0);
2885 SourceLocation Loc = E->getExprLoc();
2886 FunctionDecl *FD = 0;
2887 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002888
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002889 // gcc only allows for simple identifiers. Since we support more than gcc, we
2890 // will warn the user.
2891 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2892 if (DRE->hasQualifier())
2893 S.Diag(Loc, diag::warn_cleanup_ext);
2894 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2895 NI = DRE->getNameInfo();
2896 if (!FD) {
2897 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2898 << NI.getName();
2899 return;
2900 }
2901 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2902 if (ULE->hasExplicitTemplateArgs())
2903 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002904 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2905 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002906 if (!FD) {
2907 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2908 << NI.getName();
2909 if (ULE->getType() == S.Context.OverloadTy)
2910 S.NoteAllOverloadCandidates(ULE);
2911 return;
2912 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002913 } else {
2914 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002915 return;
2916 }
2917
Anders Carlssond277d792009-01-31 01:16:18 +00002918 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002919 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2920 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002921 return;
2922 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002923
Anders Carlsson723f55d2009-02-07 23:16:50 +00002924 // We're currently more strict than GCC about what function types we accept.
2925 // If this ever proves to be a problem it should be easy to fix.
2926 QualType Ty = S.Context.getPointerType(VD->getType());
2927 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002928 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2929 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002930 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2931 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002932 return;
2933 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002934
Michael Han99315932013-01-24 16:46:58 +00002935 D->addAttr(::new (S.Context)
2936 CleanupAttr(Attr.getRange(), S.Context, FD,
2937 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002938}
2939
Mike Stumpd3bb5572009-07-24 19:02:52 +00002940/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002941/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002942static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002943 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002944 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002945 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002946 return;
2947 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002948
Aaron Ballman00e99962013-08-31 01:11:41 +00002949 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002950 uint64_t ArgIdx;
2951 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2952 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002953 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002954
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002955 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002956 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002957
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002958 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2959 if (not_nsstring_type &&
2960 !isCFStringType(Ty, S.Context) &&
2961 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002962 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002963 // FIXME: Should highlight the actual expression that has the wrong type.
2964 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002965 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002966 << IdxExpr->getSourceRange();
2967 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002968 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002969 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002970 if (!isNSStringType(Ty, S.Context) &&
2971 !isCFStringType(Ty, S.Context) &&
2972 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002973 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002974 // FIXME: Should highlight the actual expression that has the wrong type.
2975 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002976 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002977 << IdxExpr->getSourceRange();
2978 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002979 }
2980
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002981 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2982 // because that has corrected for the implicit this parameter, and is zero-
2983 // based. The attribute expects what the user wrote explicitly.
2984 llvm::APSInt Val;
2985 IdxExpr->EvaluateAsInt(Val, S.Context);
2986
Michael Han99315932013-01-24 16:46:58 +00002987 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002988 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002989 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002990}
2991
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002992enum FormatAttrKind {
2993 CFStringFormat,
2994 NSStringFormat,
2995 StrftimeFormat,
2996 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002997 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002998 InvalidFormat
2999};
3000
3001/// getFormatAttrKind - Map from format attribute names to supported format
3002/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003003static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003004 return llvm::StringSwitch<FormatAttrKind>(Format)
3005 // Check for formats that get handled specially.
3006 .Case("NSString", NSStringFormat)
3007 .Case("CFString", CFStringFormat)
3008 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003009
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003010 // Otherwise, check for supported formats.
3011 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3012 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3013 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003014
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003015 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3016 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003017}
3018
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003019/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003020/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003021static void handleInitPriorityAttr(Sema &S, Decl *D,
3022 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003023 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003024 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3025 return;
3026 }
3027
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003028 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003029 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3030 Attr.setInvalid();
3031 return;
3032 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003033 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003034 if (S.Context.getAsArrayType(T))
3035 T = S.Context.getBaseElementType(T);
3036 if (!T->getAs<RecordType>()) {
3037 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3038 Attr.setInvalid();
3039 return;
3040 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003041
3042 Expr *E = Attr.getArgAsExpr(0);
3043 uint32_t prioritynum;
3044 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003045 Attr.setInvalid();
3046 return;
3047 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003048
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003049 if (prioritynum < 101 || prioritynum > 65535) {
3050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003051 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003052 Attr.setInvalid();
3053 return;
3054 }
Michael Han99315932013-01-24 16:46:58 +00003055 D->addAttr(::new (S.Context)
3056 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3057 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003058}
3059
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003060FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3061 IdentifierInfo *Format, int FormatIdx,
3062 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003063 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003064 // Check whether we already have an equivalent format attribute.
3065 for (specific_attr_iterator<FormatAttr>
3066 i = D->specific_attr_begin<FormatAttr>(),
3067 e = D->specific_attr_end<FormatAttr>();
3068 i != e ; ++i) {
3069 FormatAttr *f = *i;
3070 if (f->getType() == Format &&
3071 f->getFormatIdx() == FormatIdx &&
3072 f->getFirstArg() == FirstArg) {
3073 // If we don't have a valid location for this attribute, adopt the
3074 // location.
3075 if (f->getLocation().isInvalid())
3076 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003077 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003078 }
3079 }
3080
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003081 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3082 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003083}
3084
Mike Stumpd3bb5572009-07-24 19:02:52 +00003085/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003086/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003087static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003088 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003089 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003090 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003091 return;
3092 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003093
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003094 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003095 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003096 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003097 return;
3098 }
3099
Chandler Carruth743682b2010-11-16 08:35:43 +00003100 // In C++ the implicit 'this' function parameter also counts, and they are
3101 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003102 bool HasImplicitThisParam = isInstanceMethod(D);
3103 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003104
Aaron Ballman00e99962013-08-31 01:11:41 +00003105 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3106 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003107
3108 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003109 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003110 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003111 // If we've modified the string name, we need a new identifier for it.
3112 II = &S.Context.Idents.get(Format);
3113 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003114
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003115 // Check for supported formats.
3116 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003117
3118 if (Kind == IgnoredFormat)
3119 return;
3120
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003121 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003122 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003123 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003124 return;
3125 }
3126
3127 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003128 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003129 uint32_t Idx;
3130 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003131 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003132
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003133 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003134 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003135 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003136 return;
3137 }
3138
3139 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003140 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003141
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003142 if (HasImplicitThisParam) {
3143 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003144 S.Diag(Attr.getLoc(),
3145 diag::err_format_attribute_implicit_this_format_string)
3146 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003147 return;
3148 }
3149 ArgIdx--;
3150 }
Mike Stump11289f42009-09-09 15:08:12 +00003151
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003152 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003153 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003154
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003155 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003156 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003157 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3158 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003159 return;
3160 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003161 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003162 // FIXME: do we need to check if the type is NSString*? What are the
3163 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003164 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003165 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003166 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3167 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003168 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003169 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003170 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003171 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003172 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003173 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3174 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003175 return;
3176 }
3177
3178 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003179 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003180 uint32_t FirstArg;
3181 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003182 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003183
3184 // check if the function is variadic if the 3rd argument non-zero
3185 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003186 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003187 ++NumArgs; // +1 for ...
3188 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003189 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003190 return;
3191 }
3192 }
3193
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003194 // strftime requires FirstArg to be 0 because it doesn't read from any
3195 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003196 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003197 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003198 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3199 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003200 return;
3201 }
3202 // if 0 it disables parameter checking (to use with e.g. va_list)
3203 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003204 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003205 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003206 return;
3207 }
3208
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003209 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003210 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003211 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003212 if (NewAttr)
3213 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003214}
3215
Chandler Carruthedc2c642011-07-02 00:01:44 +00003216static void handleTransparentUnionAttr(Sema &S, Decl *D,
3217 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003218 // Try to find the underlying union declaration.
3219 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003220 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003221 if (TD && TD->getUnderlyingType()->isUnionType())
3222 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3223 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003224 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003225
3226 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003228 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003229 return;
3230 }
3231
John McCallf937c022011-10-07 06:10:15 +00003232 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003233 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003234 diag::warn_transparent_union_attribute_not_definition);
3235 return;
3236 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003237
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003238 RecordDecl::field_iterator Field = RD->field_begin(),
3239 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003240 if (Field == FieldEnd) {
3241 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3242 return;
3243 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003244
David Blaikie40ed2972012-06-06 20:45:41 +00003245 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003246 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003247 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003248 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003249 diag::warn_transparent_union_attribute_floating)
3250 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003251 return;
3252 }
3253
3254 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3255 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3256 for (; Field != FieldEnd; ++Field) {
3257 QualType FieldType = Field->getType();
3258 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3259 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3260 // Warn if we drop the attribute.
3261 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003262 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003263 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003264 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003265 diag::warn_transparent_union_attribute_field_size_align)
3266 << isSize << Field->getDeclName() << FieldBits;
3267 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003268 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003269 diag::note_transparent_union_first_field_size_align)
3270 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003271 return;
3272 }
3273 }
3274
Michael Han99315932013-01-24 16:46:58 +00003275 RD->addAttr(::new (S.Context)
3276 TransparentUnionAttr(Attr.getRange(), S.Context,
3277 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003278}
3279
Chandler Carruthedc2c642011-07-02 00:01:44 +00003280static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003281 // Make sure that there is a string literal as the annotation's single
3282 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003283 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003284 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003285 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003286
3287 // Don't duplicate annotations that are already set.
3288 for (specific_attr_iterator<AnnotateAttr>
3289 i = D->specific_attr_begin<AnnotateAttr>(),
3290 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003291 if ((*i)->getAnnotation() == Str)
3292 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003293 }
Michael Han99315932013-01-24 16:46:58 +00003294
3295 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003296 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003297 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003298}
3299
Chandler Carruthedc2c642011-07-02 00:01:44 +00003300static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003301 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003302 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3304 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003305 return;
3306 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003307
Richard Smith848e1f12013-02-01 08:12:08 +00003308 if (Attr.getNumArgs() == 0) {
3309 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3310 true, 0, Attr.getAttributeSpellingListIndex()));
3311 return;
3312 }
3313
Aaron Ballman00e99962013-08-31 01:11:41 +00003314 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003315 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3316 S.Diag(Attr.getEllipsisLoc(),
3317 diag::err_pack_expansion_without_parameter_packs);
3318 return;
3319 }
3320
3321 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3322 return;
3323
3324 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3325 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003326}
3327
3328void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003329 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003330 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3331 SourceLocation AttrLoc = AttrRange.getBegin();
3332
Richard Smith1dba27c2013-01-29 09:02:09 +00003333 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003334 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003335 // C++11 [dcl.align]p1:
3336 // An alignment-specifier may be applied to a variable or to a class
3337 // data member, but it shall not be applied to a bit-field, a function
3338 // parameter, the formal parameter of a catch clause, or a variable
3339 // declared with the register storage class specifier. An
3340 // alignment-specifier may also be applied to the declaration of a class
3341 // or enumeration type.
3342 // C11 6.7.5/2:
3343 // An alignment attribute shall not be specified in a declaration of
3344 // a typedef, or a bit-field, or a function, or a parameter, or an
3345 // object declared with the register storage-class specifier.
3346 int DiagKind = -1;
3347 if (isa<ParmVarDecl>(D)) {
3348 DiagKind = 0;
3349 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3350 if (VD->getStorageClass() == SC_Register)
3351 DiagKind = 1;
3352 if (VD->isExceptionVariable())
3353 DiagKind = 2;
3354 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3355 if (FD->isBitField())
3356 DiagKind = 3;
3357 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003358 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3359 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003360 << (TmpAttr.isC11() ? ExpectedVariableOrField
3361 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003362 return;
3363 }
3364 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003365 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003366 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003367 return;
3368 }
3369 }
3370
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003371 if (E->isTypeDependent() || E->isValueDependent()) {
3372 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003373 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3374 AA->setPackExpansion(IsPackExpansion);
3375 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003376 return;
3377 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003378
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003379 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003380 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003381 ExprResult ICE
3382 = VerifyIntegerConstantExpression(E, &Alignment,
3383 diag::err_aligned_attribute_argument_not_int,
3384 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003385 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003386 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003387
3388 // C++11 [dcl.align]p2:
3389 // -- if the constant expression evaluates to zero, the alignment
3390 // specifier shall have no effect
3391 // C11 6.7.5p6:
3392 // An alignment specification of zero has no effect.
3393 if (!(TmpAttr.isAlignas() && !Alignment) &&
3394 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003395 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3396 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003397 return;
3398 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003399
Richard Smith848e1f12013-02-01 08:12:08 +00003400 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003401 // We've already verified it's a power of 2, now let's make sure it's
3402 // 8192 or less.
3403 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003404 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003405 << E->getSourceRange();
3406 return;
3407 }
3408 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003409
Richard Smith44c247f2013-02-22 08:32:16 +00003410 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3411 ICE.take(), SpellingListIndex);
3412 AA->setPackExpansion(IsPackExpansion);
3413 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003414}
3415
Michael Hanaf02bbe2013-02-01 01:19:17 +00003416void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003417 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003418 // FIXME: Cache the number on the Attr object if non-dependent?
3419 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003420 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3421 SpellingListIndex);
3422 AA->setPackExpansion(IsPackExpansion);
3423 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003424}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003425
Richard Smith848e1f12013-02-01 08:12:08 +00003426void Sema::CheckAlignasUnderalignment(Decl *D) {
3427 assert(D->hasAttrs() && "no attributes on decl");
3428
3429 QualType Ty;
3430 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3431 Ty = VD->getType();
3432 else
3433 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003434 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003435 return;
3436
3437 // C++11 [dcl.align]p5, C11 6.7.5/4:
3438 // The combined effect of all alignment attributes in a declaration shall
3439 // not specify an alignment that is less strict than the alignment that
3440 // would otherwise be required for the entity being declared.
3441 AlignedAttr *AlignasAttr = 0;
3442 unsigned Align = 0;
3443 for (specific_attr_iterator<AlignedAttr>
3444 I = D->specific_attr_begin<AlignedAttr>(),
3445 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3446 if (I->isAlignmentDependent())
3447 return;
3448 if (I->isAlignas())
3449 AlignasAttr = *I;
3450 Align = std::max(Align, I->getAlignment(Context));
3451 }
3452
3453 if (AlignasAttr && Align) {
3454 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3455 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3456 if (NaturalAlign > RequestedAlign)
3457 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3458 << Ty << (unsigned)NaturalAlign.getQuantity();
3459 }
3460}
3461
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003462/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003463/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003464///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003465/// Despite what would be logical, the mode attribute is a decl attribute, not a
3466/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3467/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003468static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003469 // This attribute isn't documented, but glibc uses it. It changes
3470 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003471 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003472 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3473 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003474 return;
3475 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003476
Aaron Ballman00e99962013-08-31 01:11:41 +00003477 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3478 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003479
3480 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003481 if (Str.startswith("__") && Str.endswith("__"))
3482 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003483
3484 unsigned DestWidth = 0;
3485 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003486 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003487 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003488 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003489 switch (Str[0]) {
3490 case 'Q': DestWidth = 8; break;
3491 case 'H': DestWidth = 16; break;
3492 case 'S': DestWidth = 32; break;
3493 case 'D': DestWidth = 64; break;
3494 case 'X': DestWidth = 96; break;
3495 case 'T': DestWidth = 128; break;
3496 }
3497 if (Str[1] == 'F') {
3498 IntegerMode = false;
3499 } else if (Str[1] == 'C') {
3500 IntegerMode = false;
3501 ComplexMode = true;
3502 } else if (Str[1] != 'I') {
3503 DestWidth = 0;
3504 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003505 break;
3506 case 4:
3507 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3508 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003509 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003510 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003511 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003512 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003513 break;
3514 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003515 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003516 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003517 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003518 case 11:
3519 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003520 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003521 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003522 }
3523
3524 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003525 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003526 OldTy = TD->getUnderlyingType();
3527 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3528 OldTy = VD->getType();
3529 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003530 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003531 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003532 return;
3533 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003534
John McCall9dd450b2009-09-21 23:43:11 +00003535 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003536 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3537 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003538 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003539 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3540 } else if (ComplexMode) {
3541 if (!OldTy->isComplexType())
3542 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3543 } else {
3544 if (!OldTy->isFloatingType())
3545 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3546 }
3547
Mike Stump87c57ac2009-05-16 07:39:55 +00003548 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3549 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003550 // FIXME: Make sure floating-point mappings are accurate
3551 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003552 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003553 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003554 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003555 }
3556
3557 QualType NewTy;
3558
3559 if (IntegerMode)
3560 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3561 OldTy->isSignedIntegerType());
3562 else
3563 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3564
3565 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003566 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003567 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003568 }
3569
Eli Friedman4735374e2009-03-03 06:41:03 +00003570 if (ComplexMode) {
3571 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003572 }
3573
3574 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003575 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3576 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3577 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003578 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003579
3580 D->addAttr(::new (S.Context)
3581 ModeAttr(Attr.getRange(), S.Context, Name,
3582 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003583}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003584
Chandler Carruthedc2c642011-07-02 00:01:44 +00003585static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003586 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3587 if (!VD->hasGlobalStorage())
3588 S.Diag(Attr.getLoc(),
3589 diag::warn_attribute_requires_functions_or_static_globals)
3590 << Attr.getName();
3591 } else if (!isFunctionOrMethod(D)) {
3592 S.Diag(Attr.getLoc(),
3593 diag::warn_attribute_requires_functions_or_static_globals)
3594 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003595 return;
3596 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003597
Michael Han99315932013-01-24 16:46:58 +00003598 D->addAttr(::new (S.Context)
3599 NoDebugAttr(Attr.getRange(), S.Context,
3600 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003601}
3602
Chandler Carruthedc2c642011-07-02 00:01:44 +00003603static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003604 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003605 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003606 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003607 return;
3608 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003609
Michael Han99315932013-01-24 16:46:58 +00003610 D->addAttr(::new (S.Context)
3611 NoInlineAttr(Attr.getRange(), S.Context,
3612 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003613}
3614
Chandler Carruthedc2c642011-07-02 00:01:44 +00003615static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3616 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003617 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003619 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003620 return;
3621 }
3622
Michael Han99315932013-01-24 16:46:58 +00003623 D->addAttr(::new (S.Context)
3624 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3625 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003626}
3627
Chandler Carruthedc2c642011-07-02 00:01:44 +00003628static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003629 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003630 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003632 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003633 return;
3634 }
3635
Michael Han99315932013-01-24 16:46:58 +00003636 D->addAttr(::new (S.Context)
3637 CUDAConstantAttr(Attr.getRange(), S.Context,
3638 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003639 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003640 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003641 }
3642}
3643
Chandler Carruthedc2c642011-07-02 00:01:44 +00003644static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003645 if (S.LangOpts.CUDA) {
3646 // check the attribute arguments.
3647 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3649 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003650 return;
3651 }
3652
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003653 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003654 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003655 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003656 return;
3657 }
3658
Michael Han99315932013-01-24 16:46:58 +00003659 D->addAttr(::new (S.Context)
3660 CUDADeviceAttr(Attr.getRange(), S.Context,
3661 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003662 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003663 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003664 }
3665}
3666
Chandler Carruthedc2c642011-07-02 00:01:44 +00003667static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003668 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003669 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003671 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003672 return;
3673 }
3674
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003675 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003676 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003677 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003678 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003679 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3680 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003681 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003682 "void");
3683 } else {
3684 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3685 << FD->getType();
3686 }
3687 return;
3688 }
3689
Michael Han99315932013-01-24 16:46:58 +00003690 D->addAttr(::new (S.Context)
3691 CUDAGlobalAttr(Attr.getRange(), S.Context,
3692 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003693 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003694 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003695 }
3696}
3697
Chandler Carruthedc2c642011-07-02 00:01:44 +00003698static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003699 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003700 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003701 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003702 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003703 return;
3704 }
3705
Michael Han99315932013-01-24 16:46:58 +00003706 D->addAttr(::new (S.Context)
3707 CUDAHostAttr(Attr.getRange(), S.Context,
3708 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003709 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003710 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003711 }
3712}
3713
Chandler Carruthedc2c642011-07-02 00:01:44 +00003714static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003715 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003716 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003717 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003718 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003719 return;
3720 }
3721
Michael Han99315932013-01-24 16:46:58 +00003722 D->addAttr(::new (S.Context)
3723 CUDASharedAttr(Attr.getRange(), S.Context,
3724 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003725 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003726 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003727 }
3728}
3729
Chandler Carruthedc2c642011-07-02 00:01:44 +00003730static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003731 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003732 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003733 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003734 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003735 return;
3736 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003737
Douglas Gregor35b57532009-10-27 21:01:01 +00003738 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003739 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003740 return;
3741 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003742
Michael Han99315932013-01-24 16:46:58 +00003743 D->addAttr(::new (S.Context)
3744 GNUInlineAttr(Attr.getRange(), S.Context,
3745 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003746}
3747
Chandler Carruthedc2c642011-07-02 00:01:44 +00003748static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003749 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003750
Aaron Ballman02df2e02012-12-09 17:45:41 +00003751 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003752 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003753 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3754 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003755 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003756 return;
3757
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003758 if (!isa<ObjCMethodDecl>(D)) {
3759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3760 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003761 return;
3762 }
3763
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003764 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003765 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003766 D->addAttr(::new (S.Context)
3767 FastCallAttr(Attr.getRange(), S.Context,
3768 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003769 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003770 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003771 D->addAttr(::new (S.Context)
3772 StdCallAttr(Attr.getRange(), S.Context,
3773 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003774 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003775 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003776 D->addAttr(::new (S.Context)
3777 ThisCallAttr(Attr.getRange(), S.Context,
3778 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003779 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003780 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003781 D->addAttr(::new (S.Context)
3782 CDeclAttr(Attr.getRange(), S.Context,
3783 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003784 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003785 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003786 D->addAttr(::new (S.Context)
3787 PascalAttr(Attr.getRange(), S.Context,
3788 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003789 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003790 case AttributeList::AT_MSABI:
3791 D->addAttr(::new (S.Context)
3792 MSABIAttr(Attr.getRange(), S.Context,
3793 Attr.getAttributeSpellingListIndex()));
3794 return;
3795 case AttributeList::AT_SysVABI:
3796 D->addAttr(::new (S.Context)
3797 SysVABIAttr(Attr.getRange(), S.Context,
3798 Attr.getAttributeSpellingListIndex()));
3799 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003800 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003801 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003802 switch (CC) {
3803 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003804 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003805 break;
3806 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003807 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003808 break;
3809 default:
3810 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003811 }
3812
Michael Han99315932013-01-24 16:46:58 +00003813 D->addAttr(::new (S.Context)
3814 PcsAttr(Attr.getRange(), S.Context, PCS,
3815 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003816 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003817 }
Derek Schuffa2020962012-10-16 22:30:41 +00003818 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003819 D->addAttr(::new (S.Context)
3820 PnaclCallAttr(Attr.getRange(), S.Context,
3821 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003822 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003823 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003824 D->addAttr(::new (S.Context)
3825 IntelOclBiccAttr(Attr.getRange(), S.Context,
3826 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003827 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003828
Abramo Bagnara50099372010-04-30 13:10:51 +00003829 default:
3830 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003831 }
3832}
3833
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003834static void handleOpenCLKernelAttr(Sema &S, Decl *D,
3835 const AttributeList &Attr) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003836 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003837}
3838
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003839static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3840 const AttributeList &Attr) {
3841 uint32_t ArgNum;
3842 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003843 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003844
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003845 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3846 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003847}
3848
Aaron Ballman02df2e02012-12-09 17:45:41 +00003849bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3850 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003851 if (attr.isInvalid())
3852 return true;
3853
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003854 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003855 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003856 attr.setInvalid();
3857 return true;
3858 }
3859
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003860 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3861 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003862 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003863 case AttributeList::AT_CDecl: CC = CC_C; break;
3864 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3865 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3866 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3867 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003868 case AttributeList::AT_MSABI:
3869 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3870 CC_X86_64Win64;
3871 break;
3872 case AttributeList::AT_SysVABI:
3873 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3874 CC_C;
3875 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003876 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003877 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003878 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003879 attr.setInvalid();
3880 return true;
3881 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003882 if (StrRef == "aapcs") {
3883 CC = CC_AAPCS;
3884 break;
3885 } else if (StrRef == "aapcs-vfp") {
3886 CC = CC_AAPCS_VFP;
3887 break;
3888 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003889
3890 attr.setInvalid();
3891 Diag(attr.getLoc(), diag::err_invalid_pcs);
3892 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003893 }
Derek Schuffa2020962012-10-16 22:30:41 +00003894 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003895 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003896 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003897 }
3898
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003899 const TargetInfo &TI = Context.getTargetInfo();
3900 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3901 if (A == TargetInfo::CCCR_Warning) {
3902 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003903
3904 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3905 if (FD)
3906 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3907 TargetInfo::CCMT_NonMember;
3908 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003909 }
3910
John McCall3882ace2011-01-05 12:14:39 +00003911 return false;
3912}
3913
Chandler Carruthedc2c642011-07-02 00:01:44 +00003914static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003915 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003916
3917 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003918 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003919 return;
3920
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003921 if (!isa<ObjCMethodDecl>(D)) {
3922 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3923 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003924 return;
3925 }
Eli Friedman7044b762009-03-27 21:06:47 +00003926
Michael Han99315932013-01-24 16:46:58 +00003927 D->addAttr(::new (S.Context)
3928 RegparmAttr(Attr.getRange(), S.Context, numParams,
3929 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003930}
3931
3932/// Checks a regparm attribute, returning true if it is ill-formed and
3933/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003934bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3935 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003936 return true;
3937
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003938 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003939 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003940 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003941 }
Eli Friedman7044b762009-03-27 21:06:47 +00003942
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003943 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003944 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003945 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003946 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003947 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003948 }
3949
Douglas Gregore8bbc122011-09-02 00:18:52 +00003950 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003951 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003952 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003953 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003954 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003955 }
3956
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003957 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003958 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003959 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003960 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003961 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003962 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003963 }
3964
John McCall3882ace2011-01-05 12:14:39 +00003965 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003966}
3967
Chandler Carruthedc2c642011-07-02 00:01:44 +00003968static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003969 if (S.LangOpts.CUDA) {
3970 // check the attribute arguments.
3971 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003972 // FIXME: 0 is not okay.
3973 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003974 return;
3975 }
3976
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003977 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003978 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003979 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003980 return;
3981 }
3982
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003983 uint32_t MaxThreads, MinBlocks;
3984 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3985 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00003986 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003987
Michael Han99315932013-01-24 16:46:58 +00003988 D->addAttr(::new (S.Context)
3989 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003990 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00003991 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003992 } else {
3993 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3994 }
3995}
3996
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003997static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3998 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003999 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004000 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004001 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004002 return;
4003 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004004
4005 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004006 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004007
Aaron Ballman00e99962013-08-31 01:11:41 +00004008 StringRef AttrName = Attr.getName()->getName();
4009 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004010
4011 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4012 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4013 << Attr.getName() << ExpectedFunctionOrMethod;
4014 return;
4015 }
4016
4017 uint64_t ArgumentIdx;
4018 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4019 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004020 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004021 return;
4022
4023 uint64_t TypeTagIdx;
4024 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4025 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004026 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004027 return;
4028
4029 bool IsPointer = (AttrName == "pointer_with_type_tag");
4030 if (IsPointer) {
4031 // Ensure that buffer has a pointer type.
4032 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4033 if (!BufferTy->isPointerType()) {
4034 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004035 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004036 }
4037 }
4038
Michael Han99315932013-01-24 16:46:58 +00004039 D->addAttr(::new (S.Context)
4040 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4041 ArgumentIdx, TypeTagIdx, IsPointer,
4042 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004043}
4044
4045static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4046 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004047 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004048 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004049 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004050 return;
4051 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004052
4053 if (!checkAttributeNumArgs(S, Attr, 1))
4054 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004055
Aaron Ballman00e99962013-08-31 01:11:41 +00004056 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004057 TypeSourceInfo *MatchingCTypeLoc = 0;
4058 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4059 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004060
Michael Han99315932013-01-24 16:46:58 +00004061 D->addAttr(::new (S.Context)
4062 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004063 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004064 Attr.getLayoutCompatible(),
4065 Attr.getMustBeNull(),
4066 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004067}
4068
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004069//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004070// Checker-specific attribute handlers.
4071//===----------------------------------------------------------------------===//
4072
John McCalled433932011-01-25 03:31:58 +00004073static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004074 return type->isDependentType() ||
4075 type->isObjCObjectPointerType() ||
4076 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004077}
4078static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004079 return type->isDependentType() ||
4080 type->isPointerType() ||
4081 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004082}
4083
Chandler Carruthedc2c642011-07-02 00:01:44 +00004084static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004085 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004086 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004087 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004088 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004089 return;
4090 }
4091
4092 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004093 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004094 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4095 cf = false;
4096 } else {
4097 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4098 cf = true;
4099 }
4100
4101 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004102 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004103 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004104 return;
4105 }
4106
4107 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004108 param->addAttr(::new (S.Context)
4109 CFConsumedAttr(Attr.getRange(), S.Context,
4110 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004111 else
Michael Han99315932013-01-24 16:46:58 +00004112 param->addAttr(::new (S.Context)
4113 NSConsumedAttr(Attr.getRange(), S.Context,
4114 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004115}
4116
Chandler Carruthedc2c642011-07-02 00:01:44 +00004117static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4118 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004119 if (!isa<ObjCMethodDecl>(D)) {
4120 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004121 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004122 return;
4123 }
4124
Michael Han99315932013-01-24 16:46:58 +00004125 D->addAttr(::new (S.Context)
4126 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4127 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004128}
4129
Chandler Carruthedc2c642011-07-02 00:01:44 +00004130static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4131 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004132
John McCalled433932011-01-25 03:31:58 +00004133 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004134
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004135 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004136 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004137 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004138 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004139 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004140 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4141 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004142 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004143 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004144 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004145 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004146 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004147 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004148 return;
4149 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004150
John McCalled433932011-01-25 03:31:58 +00004151 bool typeOK;
4152 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004153 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004154 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004155 case AttributeList::AT_NSReturnsAutoreleased:
4156 case AttributeList::AT_NSReturnsRetained:
4157 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004158 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4159 cf = false;
4160 break;
4161
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004162 case AttributeList::AT_CFReturnsRetained:
4163 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004164 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4165 cf = true;
4166 break;
4167 }
4168
4169 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004170 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004171 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004172 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004174
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004175 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004176 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004177 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004178 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004179 D->addAttr(::new (S.Context)
4180 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4181 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004182 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004183 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004184 D->addAttr(::new (S.Context)
4185 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4186 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004187 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004189 D->addAttr(::new (S.Context)
4190 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4191 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004192 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004193 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004194 D->addAttr(::new (S.Context)
4195 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4196 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004197 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004198 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004199 D->addAttr(::new (S.Context)
4200 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4201 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004202 return;
4203 };
4204}
4205
John McCallcf166702011-07-22 08:53:00 +00004206static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4207 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004208 const int EP_ObjCMethod = 1;
4209 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004210
John McCallcf166702011-07-22 08:53:00 +00004211 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004212 QualType resultType;
4213
John McCallcf166702011-07-22 08:53:00 +00004214 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4215
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004216 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004217 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4218 if (!property) {
4219 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4220 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4221 return;
4222 }
4223 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004224 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004225 else
4226 // Check that the method returns a normal pointer.
4227 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004228
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004229 if (!resultType->isReferenceType() &&
4230 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004231 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004232 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004233 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4234 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004235
4236 // Drop the attribute.
4237 return;
4238 }
4239
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004240 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004241 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4242 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004243}
4244
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004245static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4246 const AttributeList &attr) {
4247 SourceLocation loc = attr.getLoc();
4248 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4249
4250 if (!method) {
4251 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4252 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4253 return;
4254 }
4255 DeclContext *DC = method->getDeclContext();
4256 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4257 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4258 << attr.getName() << 0;
4259 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4260 return;
4261 }
4262 if (method->getMethodFamily() == OMF_dealloc) {
4263 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4264 << attr.getName() << 1;
4265 return;
4266 }
4267
Michael Han99315932013-01-24 16:46:58 +00004268 method->addAttr(::new (S.Context)
4269 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4270 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004271}
4272
John McCall32f5fe12011-09-30 05:12:12 +00004273/// Handle cf_audited_transfer and cf_unknown_transfer.
4274static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4275 if (!isa<FunctionDecl>(D)) {
4276 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004277 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004278 return;
4279 }
4280
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004281 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004282
4283 // Check whether there's a conflicting attribute already present.
4284 Attr *Existing;
4285 if (IsAudited) {
4286 Existing = D->getAttr<CFUnknownTransferAttr>();
4287 } else {
4288 Existing = D->getAttr<CFAuditedTransferAttr>();
4289 }
4290 if (Existing) {
4291 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4292 << A.getName()
4293 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4294 << A.getRange() << Existing->getRange();
4295 return;
4296 }
4297
4298 // All clear; add the attribute.
4299 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004300 D->addAttr(::new (S.Context)
4301 CFAuditedTransferAttr(A.getRange(), S.Context,
4302 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004303 } else {
Michael Han99315932013-01-24 16:46:58 +00004304 D->addAttr(::new (S.Context)
4305 CFUnknownTransferAttr(A.getRange(), S.Context,
4306 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004307 }
4308}
4309
John McCallf1e8b342011-09-29 07:17:38 +00004310static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4311 const AttributeList &Attr) {
4312 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4313 if (!RD || RD->isUnion()) {
4314 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004315 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004316 }
4317
Aaron Ballman00e99962013-08-31 01:11:41 +00004318 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004319
4320 // In Objective-C, verify that the type names an Objective-C type.
4321 // We don't want to check this outside of ObjC because people sometimes
4322 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004323 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004324 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004325 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004326 Sema::LookupOrdinaryName);
4327 if (S.LookupName(R, Sc)) {
4328 NamedDecl *Target = R.getFoundDecl();
4329 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4330 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4331 S.Diag(Target->getLocStart(), diag::note_declared_at);
4332 }
4333 }
4334 }
4335
Michael Han99315932013-01-24 16:46:58 +00004336 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004337 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004338 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004339}
4340
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004341static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4342 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004343 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4345 << Attr.getName()
4346 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4347 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004348 return;
4349 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004350
4351 IdentifierLoc *Parm = 0;
4352 if (Attr.getNumArgs() == 1)
4353 Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
4354
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004355 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004356 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004357 return;
4358 }
4359
4360 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004361 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004362 Attr.getAttributeSpellingListIndex()));
4363}
4364
Chandler Carruthedc2c642011-07-02 00:01:44 +00004365static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4366 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004367 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004368
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004369 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004370 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004371}
4372
Chandler Carruthedc2c642011-07-02 00:01:44 +00004373static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4374 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004375 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004376 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004377 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004378 return;
4379 }
4380
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004381 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004382 QualType type = vd->getType();
4383
4384 if (!type->isDependentType() &&
4385 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004386 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004387 << type;
4388 return;
4389 }
4390
4391 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4392
4393 // If we have no lifetime yet, check the lifetime we're presumably
4394 // going to infer.
4395 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4396 lifetime = type->getObjCARCImplicitLifetime();
4397
4398 switch (lifetime) {
4399 case Qualifiers::OCL_None:
4400 assert(type->isDependentType() &&
4401 "didn't infer lifetime for non-dependent type?");
4402 break;
4403
4404 case Qualifiers::OCL_Weak: // meaningful
4405 case Qualifiers::OCL_Strong: // meaningful
4406 break;
4407
4408 case Qualifiers::OCL_ExplicitNone:
4409 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004410 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004411 << (lifetime == Qualifiers::OCL_Autoreleasing);
4412 break;
4413 }
4414
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004415 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004416 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4417 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004418}
4419
Francois Picheta83957a2010-12-19 06:50:37 +00004420//===----------------------------------------------------------------------===//
4421// Microsoft specific attribute handlers.
4422//===----------------------------------------------------------------------===//
4423
Reid Kleckner140c4a72013-05-17 14:04:52 +00004424// Check if MS extensions or some other language extensions are enabled. If
4425// not, issue a diagnostic that the given attribute is unused.
4426static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4427 bool OtherExtension = false) {
4428 if (S.LangOpts.MicrosoftExt || OtherExtension)
4429 return true;
4430 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4431 return false;
4432}
4433
Chandler Carruthedc2c642011-07-02 00:01:44 +00004434static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004435 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4436 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004437
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004438 StringRef StrRef;
4439 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004440 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004441 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004442
David Majnemer89085342013-08-09 08:56:20 +00004443 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4444 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004445 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4446 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004447
Reid Kleckner140c4a72013-05-17 14:04:52 +00004448 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004449 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004450 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004451 return;
4452 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004453
David Majnemer89085342013-08-09 08:56:20 +00004454 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004455 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004456 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004457 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004458 return;
4459 }
David Majnemer89085342013-08-09 08:56:20 +00004460 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004461 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004462 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004463 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004464 }
Francois Picheta83957a2010-12-19 06:50:37 +00004465
David Majnemer89085342013-08-09 08:56:20 +00004466 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4467 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004468}
4469
John McCall8d32c052012-05-22 21:28:12 +00004470static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004471 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004472 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004473
4474 AttributeList::Kind Kind = Attr.getKind();
4475 if (Kind == AttributeList::AT_SingleInheritance)
4476 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004477 ::new (S.Context)
4478 SingleInheritanceAttr(Attr.getRange(), S.Context,
4479 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004480 else if (Kind == AttributeList::AT_MultipleInheritance)
4481 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004482 ::new (S.Context)
4483 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4484 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004485 else if (Kind == AttributeList::AT_VirtualInheritance)
4486 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004487 ::new (S.Context)
4488 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4489 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004490}
4491
4492static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004493 if (!checkMicrosoftExt(S, Attr))
4494 return;
4495
4496 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballman317a77f2013-05-22 23:25:32 +00004497 if (Kind == AttributeList::AT_Win64)
Reid Kleckner140c4a72013-05-17 14:04:52 +00004498 D->addAttr(
4499 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4500 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004501}
4502
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004503static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004504 if (!checkMicrosoftExt(S, Attr))
4505 return;
4506 D->addAttr(::new (S.Context)
4507 ForceInlineAttr(Attr.getRange(), S.Context,
4508 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004509}
4510
Reid Klecknerb144d362013-05-20 14:02:37 +00004511static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4512 if (!checkMicrosoftExt(S, Attr))
4513 return;
4514 // Check linkage after possibly merging declaratinos. See
4515 // checkAttributesAfterMerging().
4516 D->addAttr(::new (S.Context)
4517 SelectAnyAttr(Attr.getRange(), S.Context,
4518 Attr.getAttributeSpellingListIndex()));
4519}
4520
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004521/// Handles semantic checking for features that are common to all attributes,
4522/// such as checking whether a parameter was properly specified, or the correct
4523/// number of arguments were passed, etc.
4524static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4525 const AttributeList &Attr) {
4526 // Several attributes carry different semantics than the parsing requires, so
4527 // those are opted out of the common handling.
4528 //
4529 // We also bail on unknown and ignored attributes because those are handled
4530 // as part of the target-specific handling logic.
4531 if (Attr.hasCustomParsing() ||
4532 Attr.getKind() == AttributeList::UnknownAttribute ||
4533 Attr.getKind() == AttributeList::IgnoredAttribute)
4534 return false;
4535
4536 // If there are no optional arguments, then checking for the argument count
4537 // is trivial.
4538 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4539 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4540 return true;
4541 return false;
4542}
4543
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004544//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004545// Top Level Sema Entry Points
4546//===----------------------------------------------------------------------===//
4547
Richard Smithf8a75c32013-08-29 00:47:48 +00004548/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4549/// the attribute applies to decls. If the attribute is a type attribute, just
4550/// silently ignore it if a GNU attribute.
4551static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4552 const AttributeList &Attr,
4553 bool IncludeCXX11Attributes) {
4554 if (Attr.isInvalid())
4555 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004556
Richard Smithf8a75c32013-08-29 00:47:48 +00004557 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4558 // instead.
4559 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4560 return;
4561
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004562 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4563 return;
4564
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004565 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004566 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4567 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4568 case AttributeList::AT_IBOutletCollection:
4569 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004570 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004571 case AttributeList::AT_ObjCGC:
4572 case AttributeList::AT_VectorSize:
4573 case AttributeList::AT_NeonVectorType:
4574 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004575 case AttributeList::AT_Ptr32:
4576 case AttributeList::AT_Ptr64:
4577 case AttributeList::AT_SPtr:
4578 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004579 // Ignore these, these are type attributes, handled by
4580 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004581 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004582 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4583 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4584 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4585 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004586 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004587 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004588 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004589 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004590 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4591 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4592 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004593 handleDependencyAttr(S, scope, D, Attr);
4594 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004595 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4596 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4597 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004598 case AttributeList::AT_CXX11NoReturn:
4599 handleCXX11NoReturnAttr(S, D, Attr);
4600 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004601 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004602 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004603 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004604 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4605 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004606 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004607 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004608 case AttributeList::AT_MinSize:
4609 handleMinSizeAttr(S, D, Attr);
4610 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004611 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4612 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4613 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004614 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4615 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004616 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4617 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004618 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004619 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004620 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4621 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004622 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004623 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4624 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004625 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004626 case AttributeList::AT_ownership_returns:
4627 case AttributeList::AT_ownership_takes:
4628 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004629 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004630 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4631 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4632 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4633 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4634 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4635 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4636 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004637
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004638 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004639 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004640 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004641 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004642
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004643 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004644 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4645
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004646 case AttributeList::AT_ObjCRequiresSuper:
4647 handleObjCRequiresSuperAttr(S, D, Attr); break;
4648
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004649 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004650 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004651
4652 case AttributeList::AT_ObjCBridge:
4653 handleObjCBridgeAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004654
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004655 case AttributeList::AT_CFAuditedTransfer:
4656 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004657 handleCFTransferAttr(S, D, Attr); break;
4658
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004659 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004660 case AttributeList::AT_CFConsumed:
4661 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4662 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004663 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004664
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004665 case AttributeList::AT_NSReturnsAutoreleased:
4666 case AttributeList::AT_NSReturnsNotRetained:
4667 case AttributeList::AT_CFReturnsNotRetained:
4668 case AttributeList::AT_NSReturnsRetained:
4669 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004670 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004671
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004672 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004673 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004674 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004675
Joey Goulyaba589c2013-03-08 09:42:32 +00004676 case AttributeList::AT_VecTypeHint:
4677 handleVecTypeHint(S, D, Attr); break;
4678
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004680 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004681
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004682 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4683 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4684 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004685 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004686 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004687 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004688 handleArcWeakrefUnavailableAttr (S, D, Attr);
4689 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004690 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004691 handleObjCRootClassAttr(S, D, Attr);
4692 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004693 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004694 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004695 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004696 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4697 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004698 handleReturnsTwiceAttr(S, D, Attr);
4699 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004700 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004701 case AttributeList::AT_Visibility:
4702 handleVisibilityAttr(S, D, Attr, false);
4703 break;
4704 case AttributeList::AT_TypeVisibility:
4705 handleVisibilityAttr(S, D, Attr, true);
4706 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004707 case AttributeList::AT_WarnUnused:
4708 handleWarnUnusedAttr(S, D, Attr);
4709 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004710 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004711 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004712 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4713 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4714 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4715 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004716 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004717 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004718 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004719 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004720 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004721 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004722 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004723 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004724 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4725 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4726 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4727 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4728 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4729 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4730 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4731 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4732 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004733 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004734 // Just ignore
4735 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004736 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004737 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004738 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004739 case AttributeList::AT_StdCall:
4740 case AttributeList::AT_CDecl:
4741 case AttributeList::AT_FastCall:
4742 case AttributeList::AT_ThisCall:
4743 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004744 case AttributeList::AT_MSABI:
4745 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004746 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004747 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004748 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004749 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004750 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004752 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004753 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004754 case AttributeList::AT_OpenCLImageAccess:
4755 handleOpenCLImageAccessAttr(S, D, Attr);
4756 break;
John McCall8d32c052012-05-22 21:28:12 +00004757
4758 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004759 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004760 handleMsStructAttr(S, D, Attr);
4761 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004762 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004763 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004764 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004765 case AttributeList::AT_SingleInheritance:
4766 case AttributeList::AT_MultipleInheritance:
4767 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004768 handleInheritanceAttr(S, D, Attr);
4769 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004770 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004771 handlePortabilityAttr(S, D, Attr);
4772 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004773 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004774 handleForceInlineAttr(S, D, Attr);
4775 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004776 case AttributeList::AT_SelectAny:
4777 handleSelectAnyAttr(S, D, Attr);
4778 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004779
4780 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004781 case AttributeList::AT_AssertExclusiveLock:
4782 handleAssertExclusiveLockAttr(S, D, Attr);
4783 break;
4784 case AttributeList::AT_AssertSharedLock:
4785 handleAssertSharedLockAttr(S, D, Attr);
4786 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004787 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004788 handleGuardedVarAttr(S, D, Attr);
4789 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004790 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004791 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004792 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004793 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004794 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004795 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004796 case AttributeList::AT_NoSanitizeAddress:
4797 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004798 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004799 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004800 handleNoThreadSafetyAnalysis(S, D, Attr);
4801 break;
4802 case AttributeList::AT_NoSanitizeThread:
4803 handleNoSanitizeThread(S, D, Attr);
4804 break;
4805 case AttributeList::AT_NoSanitizeMemory:
4806 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004807 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004808 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004809 handleLockableAttr(S, D, Attr);
4810 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004811 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004812 handleGuardedByAttr(S, D, Attr);
4813 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004814 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004815 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004818 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004819 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004820 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004821 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004822 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004823 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004824 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004825 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004826 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004827 handleLockReturnedAttr(S, D, Attr);
4828 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004829 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004830 handleLocksExcludedAttr(S, D, Attr);
4831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004832 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004833 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004834 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004835 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004836 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004838 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004839 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004840 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004841 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004842 handleUnlockFunAttr(S, D, Attr);
4843 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004844 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004845 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004846 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004847 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004848 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004849 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004850
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004851 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004852 case AttributeList::AT_Consumable:
4853 handleConsumableAttr(S, D, Attr);
4854 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004855 case AttributeList::AT_CallableWhen:
4856 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004857 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004858 case AttributeList::AT_ParamTypestate:
4859 handleParamTypestateAttr(S, D, Attr);
4860 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004861 case AttributeList::AT_ReturnTypestate:
4862 handleReturnTypestateAttr(S, D, Attr);
4863 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004864 case AttributeList::AT_SetTypestate:
4865 handleSetTypestateAttr(S, D, Attr);
4866 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004867 case AttributeList::AT_TestTypestate:
4868 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004869 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004870
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004871 // Type safety attributes.
4872 case AttributeList::AT_ArgumentWithTypeTag:
4873 handleArgumentWithTypeTagAttr(S, D, Attr);
4874 break;
4875 case AttributeList::AT_TypeTagForDatatype:
4876 handleTypeTagForDatatypeAttr(S, D, Attr);
4877 break;
4878
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004879 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004880 // Ask target about the attribute.
4881 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4882 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004883 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4884 diag::warn_unhandled_ms_attribute_ignored :
4885 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004886 break;
4887 }
4888}
4889
4890/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4891/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004892void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004893 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004894 bool IncludeCXX11Attributes) {
4895 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004896 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004897
4898 // GCC accepts
4899 // static int a9 __attribute__((weakref));
4900 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004901 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004902 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004903 cast<NamedDecl>(D)->getNameAsString();
4904 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004905 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004906 }
4907}
4908
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004909// Annotation attributes are the only attributes allowed after an access
4910// specifier.
4911bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4912 const AttributeList *AttrList) {
4913 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004914 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004915 handleAnnotateAttr(*this, ASDecl, *l);
4916 } else {
4917 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4918 return true;
4919 }
4920 }
4921
4922 return false;
4923}
4924
John McCall42856de2011-10-01 05:17:03 +00004925/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4926/// contains any decl attributes that we should warn about.
4927static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4928 for ( ; A; A = A->getNext()) {
4929 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004930 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004931 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4932
4933 if (A->getKind() == AttributeList::UnknownAttribute) {
4934 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4935 << A->getName() << A->getRange();
4936 } else {
4937 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4938 << A->getName() << A->getRange();
4939 }
4940 }
4941}
4942
4943/// checkUnusedDeclAttributes - Given a declarator which is not being
4944/// used to build a declaration, complain about any decl attributes
4945/// which might be lying around on it.
4946void Sema::checkUnusedDeclAttributes(Declarator &D) {
4947 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4948 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4949 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4950 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4951}
4952
Ryan Flynn7d470f32009-07-30 03:15:39 +00004953/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004954/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004955NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4956 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004957 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004958 NamedDecl *NewD = 0;
4959 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004960 FunctionDecl *NewFD;
4961 // FIXME: Missing call to CheckFunctionDeclaration().
4962 // FIXME: Mangling?
4963 // FIXME: Is the qualifier info correct?
4964 // FIXME: Is the DeclContext correct?
4965 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4966 Loc, Loc, DeclarationName(II),
4967 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004968 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004969 FD->hasPrototype(),
4970 false/*isConstexprSpecified*/);
4971 NewD = NewFD;
4972
4973 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004974 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004975
4976 // Fake up parameter variables; they are declared as if this were
4977 // a typedef.
4978 QualType FDTy = FD->getType();
4979 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4980 SmallVector<ParmVarDecl*, 16> Params;
4981 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4982 AE = FT->arg_type_end(); AI != AE; ++AI) {
4983 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4984 Param->setScopeInfo(0, Params.size());
4985 Params.push_back(Param);
4986 }
David Blaikie9c70e042011-09-21 18:16:56 +00004987 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004988 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004989 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4990 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004991 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004992 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004993 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004994 if (VD->getQualifier()) {
4995 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004996 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004997 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004998 }
4999 return NewD;
5000}
5001
James Dennett634962f2012-06-14 21:40:34 +00005002/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005003/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005004void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005005 if (W.getUsed()) return; // only do this once
5006 W.setUsed(true);
5007 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5008 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005009 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005010 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5011 NDId->getName()));
5012 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005013 WeakTopLevelDecl.push_back(NewD);
5014 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5015 // to insert Decl at TU scope, sorry.
5016 DeclContext *SavedContext = CurContext;
5017 CurContext = Context.getTranslationUnitDecl();
5018 PushOnScopeChains(NewD, S);
5019 CurContext = SavedContext;
5020 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005021 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005022 }
5023}
5024
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005025void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5026 // It's valid to "forward-declare" #pragma weak, in which case we
5027 // have to do this.
5028 LoadExternalWeakUndeclaredIdentifiers();
5029 if (!WeakUndeclaredIdentifiers.empty()) {
5030 NamedDecl *ND = NULL;
5031 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5032 if (VD->isExternC())
5033 ND = VD;
5034 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5035 if (FD->isExternC())
5036 ND = FD;
5037 if (ND) {
5038 if (IdentifierInfo *Id = ND->getIdentifier()) {
5039 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5040 = WeakUndeclaredIdentifiers.find(Id);
5041 if (I != WeakUndeclaredIdentifiers.end()) {
5042 WeakInfo W = I->second;
5043 DeclApplyPragmaWeak(S, ND, W);
5044 WeakUndeclaredIdentifiers[Id] = W;
5045 }
5046 }
5047 }
5048 }
5049}
5050
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005051/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5052/// it, apply them to D. This is a bit tricky because PD can have attributes
5053/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005054void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005055 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005056 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005057 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005058
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005059 // Walk the declarator structure, applying decl attributes that were in a type
5060 // position to the decl itself. This handles cases like:
5061 // int *__attr__(x)** D;
5062 // when X is a decl attribute.
5063 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5064 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005065 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005066
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005067 // Finally, apply any attributes on the decl itself.
5068 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005069 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005070}
John McCall28a6aea2009-11-04 02:18:39 +00005071
John McCall31168b02011-06-15 23:02:42 +00005072/// Is the given declaration allowed to use a forbidden type?
5073static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5074 // Private ivars are always okay. Unfortunately, people don't
5075 // always properly make their ivars private, even in system headers.
5076 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005077 // Function declarations in sys headers will be marked unavailable.
5078 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5079 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005080 return false;
5081
5082 // Require it to be declared in a system header.
5083 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5084}
5085
5086/// Handle a delayed forbidden-type diagnostic.
5087static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5088 Decl *decl) {
5089 if (decl && isForbiddenTypeAllowed(S, decl)) {
5090 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5091 "this system declaration uses an unsupported type"));
5092 return;
5093 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005094 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005095 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005096 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005097 // kind of forbidden type messages on unavailable functions.
5098 if (FD->hasAttr<UnavailableAttr>() &&
5099 diag.getForbiddenTypeDiagnostic() ==
5100 diag::err_arc_array_param_no_ownership) {
5101 diag.Triggered = true;
5102 return;
5103 }
5104 }
John McCall31168b02011-06-15 23:02:42 +00005105
5106 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5107 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5108 diag.Triggered = true;
5109}
5110
John McCall2ec85372012-05-07 06:16:41 +00005111void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5112 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005113 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005114 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005115
John McCall2ec85372012-05-07 06:16:41 +00005116 // When delaying diagnostics to run in the context of a parsed
5117 // declaration, we only want to actually emit anything if parsing
5118 // succeeds.
5119 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005120
John McCall2ec85372012-05-07 06:16:41 +00005121 // We emit all the active diagnostics in this pool or any of its
5122 // parents. In general, we'll get one pool for the decl spec
5123 // and a child pool for each declarator; in a decl group like:
5124 // deprecated_typedef foo, *bar, baz();
5125 // only the declarator pops will be passed decls. This is correct;
5126 // we really do need to consider delayed diagnostics from the decl spec
5127 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005128 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005129 do {
John McCall6347b682012-05-07 06:16:58 +00005130 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005131 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5132 // This const_cast is a bit lame. Really, Triggered should be mutable.
5133 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005134 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005135 continue;
5136
John McCallc1465822011-02-14 07:13:47 +00005137 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005138 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005139 // Don't bother giving deprecation diagnostics if the decl is invalid.
5140 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005141 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005142 break;
5143
5144 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005145 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005146 break;
John McCall31168b02011-06-15 23:02:42 +00005147
5148 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005149 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005150 break;
John McCall86121512010-01-27 03:50:35 +00005151 }
5152 }
John McCall2ec85372012-05-07 06:16:41 +00005153 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005154}
5155
John McCall6347b682012-05-07 06:16:58 +00005156/// Given a set of delayed diagnostics, re-emit them as if they had
5157/// been delayed in the current context instead of in the given pool.
5158/// Essentially, this just moves them to the current pool.
5159void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5160 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5161 assert(curPool && "re-emitting in undelayed context not supported");
5162 curPool->steal(pool);
5163}
5164
John McCall28a6aea2009-11-04 02:18:39 +00005165static bool isDeclDeprecated(Decl *D) {
5166 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005167 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005168 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005169 // A category implicitly has the availability of the interface.
5170 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5171 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005172 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5173 return false;
5174}
5175
Eli Friedman971bfa12012-08-08 21:52:41 +00005176static void
5177DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5178 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005179 const ObjCInterfaceDecl *UnknownObjCClass,
5180 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005181 DeclarationName Name = D->getDeclName();
5182 if (!Message.empty()) {
5183 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5184 S.Diag(D->getLocation(),
5185 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5186 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005187 if (ObjCPropery)
5188 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5189 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005190 } else if (!UnknownObjCClass) {
5191 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5192 S.Diag(D->getLocation(),
5193 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5194 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005195 if (ObjCPropery)
5196 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5197 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005198 } else {
5199 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5200 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5201 }
5202}
5203
John McCallb45a1e72010-08-26 02:13:20 +00005204void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005205 Decl *Ctx) {
5206 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005207 return;
5208
John McCall86121512010-01-27 03:50:35 +00005209 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005210 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5211 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005212 DD.getUnknownObjCClass(),
5213 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005214}
5215
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005216void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005217 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005218 const ObjCInterfaceDecl *UnknownObjCClass,
5219 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005220 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005221 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005222 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5223 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005224 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005225 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005226 return;
5227 }
5228
5229 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005230 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005231 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005232 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005233}