blob: 8a2d126dd6110e35b7cdc517874fd329bdfa0006 [file] [log] [blame]
Chris Lattner6b6b5372008-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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindolad7a60ad2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1a343e22013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000032using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000034
John McCall883cc2c2011-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 Sadowskib51e0312011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballman37a89532013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +000057 ExpectedObjectiveCInterface,
58 ExpectedMethodOrProperty
John McCall883cc2c2011-03-02 12:29:23 +000059};
60
Chris Lattnere5c5ee12008-06-29 00:16:31 +000061//===----------------------------------------------------------------------===//
62// Helper functions
63//===----------------------------------------------------------------------===//
64
Chandler Carruth87c44602011-07-01 23:49:12 +000065static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000066 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000068 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000069 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000070 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000071 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000072 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000073 Ty = decl->getUnderlyingType();
74 else
75 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000076
Chris Lattner6b6b5372008-06-26 18:38:35 +000077 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000078 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000079 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000080 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000081
John McCall183700f2009-09-21 23:43:11 +000082 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000083}
84
Daniel Dunbar35682492008-09-26 04:12:28 +000085// FIXME: We should provide an abstraction around a method or function
86// to provide the following bits of information.
87
Nuno Lopesd20254f2009-12-20 23:11:08 +000088/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000089/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000090static bool isFunction(const Decl *D) {
91 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000092}
93
94/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000095/// type (function or function-typed variable) or an Objective-C
96/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000097static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000098 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000101/// isFunctionOrMethodOrBlock - Return true if the given decl has function
102/// type (function or function-typed variable) or an Objective-C
103/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool isFunctionOrMethodOrBlock(const Decl *D) {
105 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000106 return true;
107 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000109 QualType Ty = V->getType();
110 return Ty->isBlockPointerType();
111 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000112 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000113}
114
John McCall711c52b2011-01-05 12:14:39 +0000115/// Return true if the given decl has a declarator that should have
116/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000117static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000118 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
120 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000121}
122
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000123/// hasFunctionProto - Return true if the given decl has a argument
124/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000125/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000126static bool hasFunctionProto(const Decl *D) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000129 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000131 return true;
132 }
133}
134
135/// getFunctionOrMethodNumArgs - Return number of function or method
136/// arguments. It is an error to call this on a K&R function (use
137/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000138static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
139 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000140 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000142 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000143 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000144}
145
Chandler Carruth87c44602011-07-01 23:49:12 +0000146static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
147 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000148 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000149 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000150 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000153}
154
Chandler Carruth87c44602011-07-01 23:49:12 +0000155static QualType getFunctionOrMethodResultType(const Decl *D) {
156 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000157 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000158 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isFunctionOrMethodVariadic(const Decl *D) {
162 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000163 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000164 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000165 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000166 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000167 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000168 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000169 }
170}
171
Chandler Carruth87c44602011-07-01 23:49:12 +0000172static bool isInstanceMethod(const Decl *D) {
173 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000174 return MethodDecl->isInstance();
175 return false;
176}
177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000179 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000180 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000181 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000182
John McCall506b57e2010-05-17 21:00:27 +0000183 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
184 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000185 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000186
John McCall506b57e2010-05-17 21:00:27 +0000187 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000188
Chris Lattner6b6b5372008-06-26 18:38:35 +0000189 // FIXME: Should we walk the chain of classes?
190 return ClsName == &Ctx.Idents.get("NSString") ||
191 ClsName == &Ctx.Idents.get("NSMutableString");
192}
193
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000195 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000196 if (!PT)
197 return false;
198
Ted Kremenek6217b802009-07-29 21:53:49 +0000199 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000200 if (!RT)
201 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000202
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000204 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000205 return false;
206
207 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
208}
209
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000210/// \brief Check if the attribute has exactly as many args as Num. May
211/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000212static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
213 unsigned int Num) {
214 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000215 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
216 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000217 return false;
218 }
219
220 return true;
221}
222
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000223
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000224/// \brief Check if the attribute has at least as many args as Num. May
225/// output an error.
226static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
227 unsigned int Num) {
228 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000229 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
230 return false;
231 }
232
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000233 return true;
234}
235
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000236/// \brief Check if IdxExpr is a valid argument index for a function or
237/// instance method D. May output an error.
238///
239/// \returns true if IdxExpr is a valid index.
240static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
241 StringRef AttrName,
242 SourceLocation AttrLoc,
243 unsigned AttrArgNum,
244 const Expr *IdxExpr,
245 uint64_t &Idx)
246{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000247 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000248
249 // In C++ the implicit 'this' function parameter also counts.
250 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000251 bool HP = hasFunctionProto(D);
252 bool HasImplicitThisParam = isInstanceMethod(D);
253 bool IV = HP && isFunctionOrMethodVariadic(D);
254 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
255 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000256
257 llvm::APSInt IdxInt;
258 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
259 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000260 std::string Name = std::string("'") + AttrName.str() + std::string("'");
261 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000262 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000263 return false;
264 }
265
266 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000267 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000268 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
269 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
270 return false;
271 }
272 Idx--; // Convert to zero-based.
273 if (HasImplicitThisParam) {
274 if (Idx == 0) {
275 S.Diag(AttrLoc,
276 diag::err_attribute_invalid_implicit_this_argument)
277 << AttrName << IdxExpr->getSourceRange();
278 return false;
279 }
280 --Idx;
281 }
282
283 return true;
284}
285
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000286/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
287/// If not emit an error and return false. If the argument is an identifier it
288/// will emit an error with a fixit hint and treat it as if it was a string
289/// literal.
290static bool checkStringLiteralArgument(Sema &S, StringRef &Str,
291 const AttributeList &Attr,
292 unsigned ArgNum,
293 SourceLocation *ArgLocation = 0) {
294 // Look for identifiers. If we have one emit a hint to fix it to a literal.
295 if (Attr.isArgIdent(ArgNum)) {
296 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
297 S.Diag(Loc->Loc, diag::err_attribute_argument_type)
298 << Attr.getName() << AANT_ArgumentString
299 << FixItHint::CreateInsertion(Loc->Loc, "\"")
300 << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Loc->Loc), "\"");
301 Str = Loc->Ident->getName();
302 if (ArgLocation)
303 *ArgLocation = Loc->Loc;
304 return true;
305 }
306
307 // Now check for an actual string literal.
308 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
309 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
310 if (ArgLocation)
311 *ArgLocation = ArgExpr->getLocStart();
312
313 if (!Literal || !Literal->isAscii()) {
314 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
315 << Attr.getName() << AANT_ArgumentString;
316 return false;
317 }
318
319 Str = Literal->getString();
320 return true;
321}
322
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000323///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000324/// \brief Check if passed in Decl is a field or potentially shared global var
325/// \return true if the Decl is a field or potentially shared global variable
326///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000327static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000328 if (isa<FieldDecl>(D))
329 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000330 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000331 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000332
333 return false;
334}
335
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000336/// \brief Check if the passed-in expression is of type int or bool.
337static bool isIntOrBool(Expr *Exp) {
338 QualType QT = Exp->getType();
339 return QT->isBooleanType() || QT->isIntegerType();
340}
341
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000342
343// Check to see if the type is a smart pointer of some kind. We assume
344// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000345static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
346 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
347 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000348 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000349 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000350
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000351 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
352 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000353 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000354 return false;
355
356 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000357}
358
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000359/// \brief Check if passed in Decl is a pointer type.
360/// Note that this function may produce an error message.
361/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000362static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
363 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000364 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000365 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000366 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000367 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000368
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000369 if (const RecordType *RT = QT->getAs<RecordType>()) {
370 // If it's an incomplete type, it could be a smart pointer; skip it.
371 // (We don't want to force template instantiation if we can avoid it,
372 // since that would alter the order in which templates are instantiated.)
373 if (RT->isIncompleteType())
374 return true;
375
376 if (threadSafetyCheckIsSmartPointer(S, RT))
377 return true;
378 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000379
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000380 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000381 << Attr.getName()->getName() << QT;
382 } else {
383 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
384 << Attr.getName();
385 }
386 return false;
387}
388
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000389/// \brief Checks that the passed in QualType either is of RecordType or points
390/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000391static const RecordType *getRecordType(QualType QT) {
392 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000393 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000394
395 // Now check if we point to record type.
396 if (const PointerType *PT = QT->getAs<PointerType>())
397 return PT->getPointeeType()->getAs<RecordType>();
398
399 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000400}
401
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000402
Jordy Rosefad5de92012-05-08 03:27:22 +0000403static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
404 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000405 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
406 if (RT->getDecl()->getAttr<LockableAttr>())
407 return true;
408 return false;
409}
410
411
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000412/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000413/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000414static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
415 QualType Ty) {
416 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000417
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000418 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000419 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000420 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000421 << Attr.getName() << Ty.getAsString();
422 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000423 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000424
Michael Hanf1aae3b2012-08-03 17:40:43 +0000425 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000426 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000427 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000428
429 // Allow smart pointers to be used as lockable objects.
430 // FIXME -- Check the type that the smart pointer points to.
431 if (threadSafetyCheckIsSmartPointer(S, RT))
432 return;
433
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000434 // Check if the type is lockable.
435 RecordDecl *RD = RT->getDecl();
436 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000437 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000438
439 // Else check if any base classes are lockable.
440 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
441 CXXBasePaths BPaths(false, false);
442 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
443 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000444 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000445
446 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
447 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000448}
449
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000450/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000451/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000452/// \param Sidx The attribute argument index to start checking with.
453/// \param ParamIdxOk Whether an argument can be indexing into a function
454/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000455static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456 const AttributeList &Attr,
457 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000458 int Sidx = 0,
459 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000460 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000461 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000462
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000463 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000464 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000465 Args.push_back(ArgExp);
466 continue;
467 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000469 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000470 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000471 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000472 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000473 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000474 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000475 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000476 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000477
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000478 // We allow constant strings to be used as a placeholder for expressions
479 // that are not valid C++ syntax, but warn that they are ignored.
480 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
481 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000482 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000483 continue;
484 }
485
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000486 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000487
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000488 // A pointer to member expression of the form &MyClass::mu is treated
489 // specially -- we need to look at the type of the member.
490 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
491 if (UOp->getOpcode() == UO_AddrOf)
492 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
493 if (DRE->getDecl()->isCXXInstanceMember())
494 ArgTy = DRE->getDecl()->getType();
495
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000496 // First see if we can just cast to record type, or point to record type.
497 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000498
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000499 // Now check if we index into a record type function param.
500 if(!RT && ParamIdxOk) {
501 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000502 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
503 if(FD && IL) {
504 unsigned int NumParams = FD->getNumParams();
505 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000506 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
507 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
508 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000509 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
510 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000511 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000512 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000513 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000514 }
515 }
516
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000517 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000518
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000519 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000520 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000521}
522
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000523//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000524// Attribute Implementations
525//===----------------------------------------------------------------------===//
526
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000527// FIXME: All this manual attribute parsing code is gross. At the
528// least add some helper functions to check most argument patterns (#
529// and types of args).
530
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000531enum ThreadAttributeDeclKind {
532 ThreadExpectedFieldOrGlobalVar,
533 ThreadExpectedFunctionOrMethod,
534 ThreadExpectedClassOrStruct
535};
536
Michael Hanf1aae3b2012-08-03 17:40:43 +0000537static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000538 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000539 // D must be either a member field or global (potentially shared) variable.
540 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000541 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
542 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000543 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000544 }
545
Michael Handc691572012-07-23 18:48:41 +0000546 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000547}
548
Michael Handc691572012-07-23 18:48:41 +0000549static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
550 if (!checkGuardedVarAttrCommon(S, D, Attr))
551 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000552
Michael Han51d8c522013-01-24 16:46:58 +0000553 D->addAttr(::new (S.Context)
554 GuardedVarAttr(Attr.getRange(), S.Context,
555 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000556}
557
Michael Hanf1aae3b2012-08-03 17:40:43 +0000558static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000559 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000560 if (!checkGuardedVarAttrCommon(S, D, Attr))
561 return;
562
563 if (!threadSafetyCheckIsPointer(S, D, Attr))
564 return;
565
Michael Han51d8c522013-01-24 16:46:58 +0000566 D->addAttr(::new (S.Context)
567 PtGuardedVarAttr(Attr.getRange(), S.Context,
568 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000569}
570
Michael Hanf1aae3b2012-08-03 17:40:43 +0000571static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
572 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000573 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000574 // D must be either a member field or global (potentially shared) variable.
575 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000576 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
577 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000578 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000579 }
580
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000581 SmallVector<Expr*, 1> Args;
582 // check that all arguments are lockable objects
583 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
584 unsigned Size = Args.size();
585 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000586 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000587
Michael Handc691572012-07-23 18:48:41 +0000588 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000589
Michael Handc691572012-07-23 18:48:41 +0000590 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000591}
592
Michael Handc691572012-07-23 18:48:41 +0000593static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
594 Expr *Arg = 0;
595 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
596 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000597
Michael Handc691572012-07-23 18:48:41 +0000598 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
599}
600
Michael Hanf1aae3b2012-08-03 17:40:43 +0000601static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000602 const AttributeList &Attr) {
603 Expr *Arg = 0;
604 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
605 return;
606
607 if (!threadSafetyCheckIsPointer(S, D, Attr))
608 return;
609
610 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
611 S.Context, Arg));
612}
613
Michael Hanf1aae3b2012-08-03 17:40:43 +0000614static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000615 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000616 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000617 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000618 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
619 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000620 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000621 }
622
Michael Handc691572012-07-23 18:48:41 +0000623 return true;
624}
625
626static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
627 if (!checkLockableAttrCommon(S, D, Attr))
628 return;
629
630 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
631}
632
Michael Hanf1aae3b2012-08-03 17:40:43 +0000633static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000634 const AttributeList &Attr) {
635 if (!checkLockableAttrCommon(S, D, Attr))
636 return;
637
Michael Han51d8c522013-01-24 16:46:58 +0000638 D->addAttr(::new (S.Context)
639 ScopedLockableAttr(Attr.getRange(), S.Context,
640 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000641}
642
Kostya Serebryany85aee962013-02-26 06:58:27 +0000643static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
644 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000645 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000646 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
647 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000648 return;
649 }
650
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000651 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000652 S.Context));
653}
654
Kostya Serebryany85aee962013-02-26 06:58:27 +0000655static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000656 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000657 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000659 << Attr.getName() << ExpectedFunctionOrMethod;
660 return;
661 }
662
Michael Han51d8c522013-01-24 16:46:58 +0000663 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000664 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
665 Attr.getAttributeSpellingListIndex()));
666}
667
668static void handleNoSanitizeMemory(Sema &S, Decl *D,
669 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000670 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
672 << Attr.getName() << ExpectedFunctionOrMethod;
673 return;
674 }
675
676 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
677 S.Context));
678}
679
680static void handleNoSanitizeThread(Sema &S, Decl *D,
681 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000682 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
683 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
684 << Attr.getName() << ExpectedFunctionOrMethod;
685 return;
686 }
687
688 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
689 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000690}
691
Michael Hanf1aae3b2012-08-03 17:40:43 +0000692static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
693 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000694 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000695 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000696 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000697
698 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000699 ValueDecl *VD = dyn_cast<ValueDecl>(D);
700 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000701 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
702 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000703 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000704 }
705
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000706 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000707 QualType QT = VD->getType();
708 if (!QT->isDependentType()) {
709 const RecordType *RT = getRecordType(QT);
710 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000711 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000712 << Attr.getName();
713 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000714 }
715 }
716
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000717 // Check that all arguments are lockable objects.
718 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000719 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000720 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000721
Michael Handc691572012-07-23 18:48:41 +0000722 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000723}
724
Michael Hanf1aae3b2012-08-03 17:40:43 +0000725static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000726 const AttributeList &Attr) {
727 SmallVector<Expr*, 1> Args;
728 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
729 return;
730
731 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000732 D->addAttr(::new (S.Context)
733 AcquiredAfterAttr(Attr.getRange(), S.Context,
734 StartArg, Args.size(),
735 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000736}
737
Michael Hanf1aae3b2012-08-03 17:40:43 +0000738static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000739 const AttributeList &Attr) {
740 SmallVector<Expr*, 1> Args;
741 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
742 return;
743
744 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000745 D->addAttr(::new (S.Context)
746 AcquiredBeforeAttr(Attr.getRange(), S.Context,
747 StartArg, Args.size(),
748 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000749}
750
Michael Hanf1aae3b2012-08-03 17:40:43 +0000751static bool checkLockFunAttrCommon(Sema &S, Decl *D,
752 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000753 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000754 // zero or more arguments ok
755
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000756 // check that the attribute is applied to a function
757 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000758 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
759 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000760 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761 }
762
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000763 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000764 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000765
Michael Handc691572012-07-23 18:48:41 +0000766 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000767}
768
Michael Hanf1aae3b2012-08-03 17:40:43 +0000769static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000770 const AttributeList &Attr) {
771 SmallVector<Expr*, 1> Args;
772 if (!checkLockFunAttrCommon(S, D, Attr, Args))
773 return;
774
775 unsigned Size = Args.size();
776 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000777 D->addAttr(::new (S.Context)
778 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
779 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000780}
781
Michael Hanf1aae3b2012-08-03 17:40:43 +0000782static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000783 const AttributeList &Attr) {
784 SmallVector<Expr*, 1> Args;
785 if (!checkLockFunAttrCommon(S, D, Attr, Args))
786 return;
787
788 unsigned Size = Args.size();
789 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000790 D->addAttr(::new (S.Context)
791 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
792 StartArg, Size,
793 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000794}
795
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000796static void handleAssertSharedLockAttr(Sema &S, Decl *D,
797 const AttributeList &Attr) {
798 SmallVector<Expr*, 1> Args;
799 if (!checkLockFunAttrCommon(S, D, Attr, Args))
800 return;
801
802 unsigned Size = Args.size();
803 Expr **StartArg = Size == 0 ? 0 : &Args[0];
804 D->addAttr(::new (S.Context)
805 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
806 Attr.getAttributeSpellingListIndex()));
807}
808
809static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
810 const AttributeList &Attr) {
811 SmallVector<Expr*, 1> Args;
812 if (!checkLockFunAttrCommon(S, D, Attr, Args))
813 return;
814
815 unsigned Size = Args.size();
816 Expr **StartArg = Size == 0 ? 0 : &Args[0];
817 D->addAttr(::new (S.Context)
818 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
819 StartArg, Size,
820 Attr.getAttributeSpellingListIndex()));
821}
822
823
Michael Hanf1aae3b2012-08-03 17:40:43 +0000824static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
825 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000826 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000827 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000828 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000829
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000830 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000831 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
832 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000833 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000834 }
835
Aaron Ballman624421f2013-08-31 01:11:41 +0000836 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000837 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000838 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000839 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 }
841
842 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000843 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000844
Michael Handc691572012-07-23 18:48:41 +0000845 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000846}
847
Michael Hanf1aae3b2012-08-03 17:40:43 +0000848static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000849 const AttributeList &Attr) {
850 SmallVector<Expr*, 2> Args;
851 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
852 return;
853
Michael Han51d8c522013-01-24 16:46:58 +0000854 D->addAttr(::new (S.Context)
855 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000856 Attr.getArgAsExpr(0),
857 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000858 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000859}
860
Michael Hanf1aae3b2012-08-03 17:40:43 +0000861static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000862 const AttributeList &Attr) {
863 SmallVector<Expr*, 2> Args;
864 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
865 return;
866
Michael Han51d8c522013-01-24 16:46:58 +0000867 D->addAttr(::new (S.Context)
868 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000869 Attr.getArgAsExpr(0),
870 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000871 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000872}
873
Michael Hanf1aae3b2012-08-03 17:40:43 +0000874static bool checkLocksRequiredCommon(Sema &S, Decl *D,
875 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000876 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000877 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000878 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000879
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000880 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000881 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
882 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000883 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000884 }
885
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000887 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000888 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000889 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000890
Michael Handc691572012-07-23 18:48:41 +0000891 return true;
892}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000893
Michael Hanf1aae3b2012-08-03 17:40:43 +0000894static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000895 const AttributeList &Attr) {
896 SmallVector<Expr*, 1> Args;
897 if (!checkLocksRequiredCommon(S, D, Attr, Args))
898 return;
899
900 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000901 D->addAttr(::new (S.Context)
902 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
903 StartArg, Args.size(),
904 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000905}
906
Michael Hanf1aae3b2012-08-03 17:40:43 +0000907static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000908 const AttributeList &Attr) {
909 SmallVector<Expr*, 1> Args;
910 if (!checkLocksRequiredCommon(S, D, Attr, Args))
911 return;
912
913 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000914 D->addAttr(::new (S.Context)
915 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
916 StartArg, Args.size(),
917 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000918}
919
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000920static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000921 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000922 // zero or more arguments ok
923
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000924 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000925 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
926 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000927 return;
928 }
929
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000930 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000931 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000932 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000933 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000934 Expr **StartArg = Size == 0 ? 0 : &Args[0];
935
Michael Han51d8c522013-01-24 16:46:58 +0000936 D->addAttr(::new (S.Context)
937 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
938 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000939}
940
941static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000942 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000943 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000944 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
945 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000946 return;
947 }
948
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000949 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000950 SmallVector<Expr*, 1> Args;
951 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
952 unsigned Size = Args.size();
953 if (Size == 0)
954 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000955
Michael Han51d8c522013-01-24 16:46:58 +0000956 D->addAttr(::new (S.Context)
957 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
958 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000959}
960
961static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000962 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000963 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000964 return;
965
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000966 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000967 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
968 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000969 return;
970 }
971
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000972 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000973 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000974 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000975 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000976 if (Size == 0)
977 return;
978 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000979
Michael Han51d8c522013-01-24 16:46:58 +0000980 D->addAttr(::new (S.Context)
981 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
982 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000983}
984
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000985static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000986 ConsumableAttr::ConsumedState DefaultState;
987
988 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000989 IdentifierLoc *IL = Attr.getArgAsIdent(0);
990 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
991 DefaultState)) {
992 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
993 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +0000994 return;
995 }
David Blaikiea33ab602013-09-06 01:28:43 +0000996 } else {
997 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
998 << Attr.getName() << AANT_ArgumentIdentifier;
999 return;
1000 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001001
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001002 if (!isa<CXXRecordDecl>(D)) {
1003 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1004 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001005 return;
1006 }
1007
1008 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001009 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001010 Attr.getAttributeSpellingListIndex()));
1011}
1012
1013static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1014 const AttributeList &Attr) {
1015 ASTContext &CurrContext = S.getASTContext();
1016 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1017
1018 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1019 if (!RD->hasAttr<ConsumableAttr>()) {
1020 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1021 RD->getNameAsString();
1022
1023 return false;
1024 }
1025 }
1026
1027 return true;
1028}
1029
1030static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001031 if (!isa<CXXMethodDecl>(D)) {
1032 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1033 Attr.getName() << ExpectedMethod;
1034 return;
1035 }
1036
1037 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1038 return;
1039
1040 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001041 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001042 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001043}
1044
1045static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1046 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001047 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001048 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1049 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001050 return;
1051 }
1052
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001053 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1054 return;
1055
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001056 D->addAttr(::new (S.Context)
1057 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001058 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001059}
1060
1061static void handleTestsConsumedAttr(Sema &S, Decl *D,
1062 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001063 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001064 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1065 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001066 return;
1067 }
1068
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001069 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1070 return;
1071
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001072 D->addAttr(::new (S.Context)
1073 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001074 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001075}
1076
1077static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1078 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001079 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001080 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1081 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001082 return;
1083 }
1084
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001085 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1086 return;
1087
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001088 D->addAttr(::new (S.Context)
1089 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001090 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001091}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001092
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001093static void handleReturnTypestateAttr(Sema &S, Decl *D,
1094 const AttributeList &Attr) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001095 ReturnTypestateAttr::ConsumedState ReturnState;
1096
1097 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001098 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1099 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1100 ReturnState)) {
1101 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1102 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001103 return;
1104 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001105 } else {
1106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1107 Attr.getName() << AANT_ArgumentIdentifier;
1108 return;
1109 }
1110
1111 if (!isa<FunctionDecl>(D)) {
1112 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1113 Attr.getName() << ExpectedFunction;
1114 return;
1115 }
1116
1117 // FIXME: This check is currently being done in the analysis. It can be
1118 // enabled here only after the parser propagates attributes at
1119 // template specialization definition, not declaration.
1120 //QualType ReturnType;
1121 //
1122 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1123 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1124 //
1125 //} else {
1126 //
1127 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1128 //}
1129 //
1130 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1131 //
1132 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1133 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1134 // ReturnType.getAsString();
1135 // return;
1136 //}
1137
1138 D->addAttr(::new (S.Context)
1139 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1140 Attr.getAttributeSpellingListIndex()));
1141}
1142
Chandler Carruth1b03c872011-07-02 00:01:44 +00001143static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1144 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001145 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1146 if (TD == 0) {
1147 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1148 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001149 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001150 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001151 }
Mike Stumpbf916502009-07-24 19:02:52 +00001152
Richard Smitha4fa9002013-01-13 02:11:23 +00001153 // Remember this typedef decl, we will need it later for diagnostics.
1154 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001155}
1156
Chandler Carruth1b03c872011-07-02 00:01:44 +00001157static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001158 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001159 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001160 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001161 // If the alignment is less than or equal to 8 bits, the packed attribute
1162 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001163 if (!FD->getType()->isDependentType() &&
1164 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001165 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001166 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001167 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001168 else
Michael Han51d8c522013-01-24 16:46:58 +00001169 FD->addAttr(::new (S.Context)
1170 PackedAttr(Attr.getRange(), S.Context,
1171 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001172 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001173 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001174}
1175
Chandler Carruth1b03c872011-07-02 00:01:44 +00001176static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001177 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001178 RD->addAttr(::new (S.Context)
1179 MsStructAttr(Attr.getRange(), S.Context,
1180 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001181 else
1182 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1183}
1184
Chandler Carruth1b03c872011-07-02 00:01:44 +00001185static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001186 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001187 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001188 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001189 D->addAttr(::new (S.Context)
1190 IBActionAttr(Attr.getRange(), S.Context,
1191 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001192 return;
1193 }
1194
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001195 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001196}
1197
Ted Kremenek2f041d02011-09-29 07:02:25 +00001198static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1199 // The IBOutlet/IBOutletCollection attributes only apply to instance
1200 // variables or properties of Objective-C classes. The outlet must also
1201 // have an object reference type.
1202 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1203 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001204 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001205 << Attr.getName() << VD->getType() << 0;
1206 return false;
1207 }
1208 }
1209 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1210 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001211 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001212 << Attr.getName() << PD->getType() << 1;
1213 return false;
1214 }
1215 }
1216 else {
1217 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1218 return false;
1219 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001220
Ted Kremenek2f041d02011-09-29 07:02:25 +00001221 return true;
1222}
1223
Chandler Carruth1b03c872011-07-02 00:01:44 +00001224static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001225 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001226 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001227
Michael Han51d8c522013-01-24 16:46:58 +00001228 D->addAttr(::new (S.Context)
1229 IBOutletAttr(Attr.getRange(), S.Context,
1230 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001231}
1232
Chandler Carruth1b03c872011-07-02 00:01:44 +00001233static void handleIBOutletCollection(Sema &S, Decl *D,
1234 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001235
1236 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001237 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001238 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1239 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001240 return;
1241 }
1242
Ted Kremenek2f041d02011-09-29 07:02:25 +00001243 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001244 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001245
Aaron Ballman624421f2013-08-31 01:11:41 +00001246 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1247 IdentifierInfo *II;
1248 SourceLocation ILS;
1249 if (IL) {
1250 II = IL->Ident;
1251 ILS = IL->Loc;
1252 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001253 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001254 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001255
John McCallb3d87482010-08-24 05:47:05 +00001256 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001257 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001258 if (!TypeRep) {
1259 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1260 return;
1261 }
John McCallb3d87482010-08-24 05:47:05 +00001262 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001263 // Diagnose use of non-object type in iboutletcollection attribute.
1264 // FIXME. Gnu attribute extension ignores use of builtin types in
1265 // attributes. So, __attribute__((iboutletcollection(char))) will be
1266 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001267 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001268 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1269 return;
1270 }
Michael Han51d8c522013-01-24 16:46:58 +00001271 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001272 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001273 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001274}
1275
Chandler Carruthd309c812011-07-01 23:49:16 +00001276static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001277 if (const RecordType *UT = T->getAsUnionType())
1278 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1279 RecordDecl *UD = UT->getDecl();
1280 for (RecordDecl::field_iterator it = UD->field_begin(),
1281 itend = UD->field_end(); it != itend; ++it) {
1282 QualType QT = it->getType();
1283 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1284 T = QT;
1285 return;
1286 }
1287 }
1288 }
1289}
1290
Nuno Lopes587de5b2012-05-24 00:22:00 +00001291static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001292 if (!isFunctionOrMethod(D)) {
1293 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001294 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001295 return;
1296 }
1297
Nuno Lopes587de5b2012-05-24 00:22:00 +00001298 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1299 return;
1300
Nuno Lopes587de5b2012-05-24 00:22:00 +00001301 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001302 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001303 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001304 uint64_t Idx;
1305 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1306 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001307 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001308
1309 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001310 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001311 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1313 << Attr.getName() << AANT_ArgumentIntegerConstant
1314 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001315 return;
1316 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001317 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001318 }
1319
1320 // check if the function returns a pointer
1321 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1322 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001323 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001324 }
1325
Michael Han51d8c522013-01-24 16:46:58 +00001326 D->addAttr(::new (S.Context)
1327 AllocSizeAttr(Attr.getRange(), S.Context,
1328 SizeArgs.data(), SizeArgs.size(),
1329 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001330}
1331
Chandler Carruth1b03c872011-07-02 00:01:44 +00001332static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001333 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1334 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001335 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001336 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001337 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001338 return;
1339 }
Mike Stumpbf916502009-07-24 19:02:52 +00001340
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001341 SmallVector<unsigned, 8> NonNullArgs;
1342 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001343 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001344 uint64_t Idx;
1345 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1346 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001347 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001348
1349 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001350 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001351 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001352
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001353 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001354 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001355 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001356 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001357 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001358 }
Mike Stumpbf916502009-07-24 19:02:52 +00001359
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001360 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001361 }
Mike Stumpbf916502009-07-24 19:02:52 +00001362
1363 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1364 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001365 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001366 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1367 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001368 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001369 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001370 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001371 }
Mike Stumpbf916502009-07-24 19:02:52 +00001372
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001373 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001374 if (NonNullArgs.empty()) {
1375 // Warn the trivial case only if attribute is not coming from a
1376 // macro instantiation.
1377 if (Attr.getLoc().isFileID())
1378 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001379 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001380 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001381 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001382
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001383 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001384 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001385 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001386 D->addAttr(::new (S.Context)
1387 NonNullAttr(Attr.getRange(), S.Context, start, size,
1388 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001389}
1390
Aaron Ballmane1668a32013-09-16 18:11:41 +00001391static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1392 switch (K) {
1393 case OwnershipAttr::Holds: return "'ownership_holds'";
1394 case OwnershipAttr::Takes: return "'ownership_takes'";
1395 case OwnershipAttr::Returns: return "'ownership_returns'";
1396 }
1397 llvm_unreachable("unknown ownership");
1398}
1399
Chandler Carruth1b03c872011-07-02 00:01:44 +00001400static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001401 // This attribute must be applied to a function declaration. The first
1402 // argument to the attribute must be an identifier, the name of the resource,
1403 // for example: malloc. The following arguments must be argument indexes, the
1404 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001405 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballmane1668a32013-09-16 18:11:41 +00001406 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose2a479922010-08-12 08:54:03 +00001407 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001408
Aaron Ballman624421f2013-08-31 01:11:41 +00001409 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001410 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmane1668a32013-09-16 18:11:41 +00001411 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001412 return;
1413 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001414
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001415 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001416 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001417 switch (AL.getKind()) {
1418 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001419 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001420 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001421 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001422 return;
1423 }
Jordy Rose2a479922010-08-12 08:54:03 +00001424 break;
1425 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001426 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001427 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001428 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001429 return;
1430 }
Jordy Rose2a479922010-08-12 08:54:03 +00001431 break;
1432 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001433 K = OwnershipAttr::Returns;
Aaron Ballmane1668a32013-09-16 18:11:41 +00001434
Aaron Ballman624421f2013-08-31 01:11:41 +00001435 if (AL.getNumArgs() > 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001436 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001437 return;
1438 }
Jordy Rose2a479922010-08-12 08:54:03 +00001439 break;
1440 default:
1441 // This should never happen given how we are called.
1442 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001443 }
1444
Chandler Carruth87c44602011-07-01 23:49:12 +00001445 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001446 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1447 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001448 return;
1449 }
1450
Aaron Ballman624421f2013-08-31 01:11:41 +00001451 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001452
1453 // Normalize the argument, __foo__ becomes foo.
1454 if (Module.startswith("__") && Module.endswith("__"))
1455 Module = Module.substr(2, Module.size() - 4);
1456
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001457 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001458 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1459 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001460 uint64_t Idx;
1461 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001462 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001463 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001464
Aaron Ballmane1668a32013-09-16 18:11:41 +00001465 // Is the function argument a pointer type?
1466 QualType T = getFunctionOrMethodArgType(D, Idx);
1467 int Err = -1; // No error
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001468 switch (K) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001469 case OwnershipAttr::Takes:
1470 case OwnershipAttr::Holds:
1471 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1472 Err = 0;
1473 break;
1474 case OwnershipAttr::Returns:
1475 if (!T->isIntegerType())
1476 Err = 1;
1477 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001478 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001479 if (-1 != Err) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001480 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballmane1668a32013-09-16 18:11:41 +00001481 << Ex->getSourceRange();
1482 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001483 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001484
1485 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001486 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballmane1668a32013-09-16 18:11:41 +00001487 i = D->specific_attr_begin<OwnershipAttr>(),
1488 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1489 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1490 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1491 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1492 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1493 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001494 }
1495 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001496 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001497 }
1498
1499 unsigned* start = OwnershipArgs.data();
1500 unsigned size = OwnershipArgs.size();
1501 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001502
Michael Han51d8c522013-01-24 16:46:58 +00001503 D->addAttr(::new (S.Context)
1504 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1505 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001506}
1507
Chandler Carruth1b03c872011-07-02 00:01:44 +00001508static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001509 // Check the attribute arguments.
1510 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001511 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1512 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001513 return;
1514 }
1515
Chandler Carruth87c44602011-07-01 23:49:12 +00001516 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001517 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001518 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001519 return;
1520 }
1521
Chandler Carruth87c44602011-07-01 23:49:12 +00001522 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001523
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001524 // gcc rejects
1525 // class c {
1526 // static int a __attribute__((weakref ("v2")));
1527 // static int b() __attribute__((weakref ("f3")));
1528 // };
1529 // and ignores the attributes of
1530 // void f(void) {
1531 // static int a __attribute__((weakref ("v2")));
1532 // }
1533 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001534 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001535 if (!Ctx->isFileContext()) {
1536 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001537 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001538 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001539 }
1540
1541 // The GCC manual says
1542 //
1543 // At present, a declaration to which `weakref' is attached can only
1544 // be `static'.
1545 //
1546 // It also says
1547 //
1548 // Without a TARGET,
1549 // given as an argument to `weakref' or to `alias', `weakref' is
1550 // equivalent to `weak'.
1551 //
1552 // gcc 4.4.1 will accept
1553 // int a7 __attribute__((weakref));
1554 // as
1555 // int a7 __attribute__((weak));
1556 // This looks like a bug in gcc. We reject that for now. We should revisit
1557 // it if this behaviour is actually used.
1558
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001559 // GCC rejects
1560 // static ((alias ("y"), weakref)).
1561 // Should we? How to check that weakref is before or after alias?
1562
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001563 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1564 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1565 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001566 StringRef Str;
1567 if (Attr.getNumArgs() && checkStringLiteralArgument(S, Str, Attr, 0))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001568 // GCC will accept anything as the argument of weakref. Should we
1569 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001570 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1571 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001572
Michael Han51d8c522013-01-24 16:46:58 +00001573 D->addAttr(::new (S.Context)
1574 WeakRefAttr(Attr.getRange(), S.Context,
1575 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001576}
1577
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001578static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1579 StringRef Str;
1580 if (!checkStringLiteralArgument(S, Str, Attr, 0))
1581 return;
1582
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001583 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001584 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1585 return;
1586 }
1587
Chris Lattner6b6b5372008-06-26 18:38:35 +00001588 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001589
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001590 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001591 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001592}
1593
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001594static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001595 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1597 << Attr.getName() << ExpectedFunctionOrMethod;
1598 return;
1599 }
1600
Michael Han51d8c522013-01-24 16:46:58 +00001601 D->addAttr(::new (S.Context)
1602 MinSizeAttr(Attr.getRange(), S.Context,
1603 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001604}
1605
Benjamin Krameree409a92012-05-12 21:10:52 +00001606static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001607 if (!isa<FunctionDecl>(D)) {
1608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1609 << Attr.getName() << ExpectedFunction;
1610 return;
1611 }
1612
1613 if (D->hasAttr<HotAttr>()) {
1614 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1615 << Attr.getName() << "hot";
1616 return;
1617 }
1618
Michael Han51d8c522013-01-24 16:46:58 +00001619 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1620 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001621}
1622
1623static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001624 if (!isa<FunctionDecl>(D)) {
1625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1626 << Attr.getName() << ExpectedFunction;
1627 return;
1628 }
1629
1630 if (D->hasAttr<ColdAttr>()) {
1631 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1632 << Attr.getName() << "cold";
1633 return;
1634 }
1635
Michael Han51d8c522013-01-24 16:46:58 +00001636 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1637 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001638}
1639
Chandler Carruth1b03c872011-07-02 00:01:44 +00001640static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001641 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001643 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001644 return;
1645 }
1646
Michael Han51d8c522013-01-24 16:46:58 +00001647 D->addAttr(::new (S.Context)
1648 NakedAttr(Attr.getRange(), S.Context,
1649 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001650}
1651
Chandler Carruth1b03c872011-07-02 00:01:44 +00001652static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1653 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001654 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001656 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001657 return;
1658 }
Mike Stumpbf916502009-07-24 19:02:52 +00001659
Michael Han51d8c522013-01-24 16:46:58 +00001660 D->addAttr(::new (S.Context)
1661 AlwaysInlineAttr(Attr.getRange(), S.Context,
1662 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001663}
1664
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001665static void handleTLSModelAttr(Sema &S, Decl *D,
1666 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001667 StringRef Model;
1668 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001669 // Check that it is a string.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001670 if (!checkStringLiteralArgument(S, Model, Attr, 0, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001671 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001672
Richard Smith38afbc72013-04-13 02:43:54 +00001673 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1675 << Attr.getName() << ExpectedTLSVar;
1676 return;
1677 }
1678
1679 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001680 if (Model != "global-dynamic" && Model != "local-dynamic"
1681 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001682 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001683 return;
1684 }
1685
Michael Han51d8c522013-01-24 16:46:58 +00001686 D->addAttr(::new (S.Context)
1687 TLSModelAttr(Attr.getRange(), S.Context, Model,
1688 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001689}
1690
Chandler Carruth1b03c872011-07-02 00:01:44 +00001691static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001692 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001693 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001694 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001695 D->addAttr(::new (S.Context)
1696 MallocAttr(Attr.getRange(), S.Context,
1697 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001698 return;
1699 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001700 }
1701
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001702 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001703}
1704
Chandler Carruth1b03c872011-07-02 00:01:44 +00001705static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001706 D->addAttr(::new (S.Context)
1707 MayAliasAttr(Attr.getRange(), S.Context,
1708 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001709}
1710
Chandler Carruth1b03c872011-07-02 00:01:44 +00001711static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001712 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001713 D->addAttr(::new (S.Context)
1714 NoCommonAttr(Attr.getRange(), S.Context,
1715 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001716 else
1717 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001718 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001719}
1720
Chandler Carruth1b03c872011-07-02 00:01:44 +00001721static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001722 if (S.LangOpts.CPlusPlus) {
1723 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1724 return;
1725 }
1726
Chandler Carruth87c44602011-07-01 23:49:12 +00001727 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
1729 CommonAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001731 else
1732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001733 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001737 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001738
1739 if (S.CheckNoReturnAttr(attr)) return;
1740
Chandler Carruth87c44602011-07-01 23:49:12 +00001741 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001742 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001743 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001744 return;
1745 }
1746
Michael Han51d8c522013-01-24 16:46:58 +00001747 D->addAttr(::new (S.Context)
1748 NoReturnAttr(attr.getRange(), S.Context,
1749 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001750}
1751
1752bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001753 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001754 attr.setInvalid();
1755 return true;
1756 }
1757
1758 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001759}
1760
Chandler Carruth1b03c872011-07-02 00:01:44 +00001761static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1762 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001763
1764 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1765 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001766 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1767 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001768 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1769 && !VD->getType()->isFunctionPointerType())) {
1770 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001771 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001772 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001773 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001774 return;
1775 }
1776 }
1777
Michael Han51d8c522013-01-24 16:46:58 +00001778 D->addAttr(::new (S.Context)
1779 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1780 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001781}
1782
Richard Smithcd8ab512013-01-17 01:30:42 +00001783static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1784 const AttributeList &Attr) {
1785 // C++11 [dcl.attr.noreturn]p1:
1786 // The attribute may be applied to the declarator-id in a function
1787 // declaration.
1788 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1789 if (!FD) {
1790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1791 << Attr.getName() << ExpectedFunctionOrMethod;
1792 return;
1793 }
1794
Michael Han51d8c522013-01-24 16:46:58 +00001795 D->addAttr(::new (S.Context)
1796 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1797 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001798}
1799
John Thompson35cc9622010-08-09 21:53:52 +00001800// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001801static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001802/*
1803 Returning a Vector Class in Registers
1804
Eric Christopherf48f3672010-12-01 22:13:54 +00001805 According to the PPU ABI specifications, a class with a single member of
1806 vector type is returned in memory when used as the return value of a function.
1807 This results in inefficient code when implementing vector classes. To return
1808 the value in a single vector register, add the vecreturn attribute to the
1809 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001810
1811 Example:
1812
1813 struct Vector
1814 {
1815 __vector float xyzw;
1816 } __attribute__((vecreturn));
1817
1818 Vector Add(Vector lhs, Vector rhs)
1819 {
1820 Vector result;
1821 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1822 return result; // This will be returned in a register
1823 }
1824*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001825 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001827 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001828 return;
1829 }
1830
Chandler Carruth87c44602011-07-01 23:49:12 +00001831 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001832 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1833 return;
1834 }
1835
Chandler Carruth87c44602011-07-01 23:49:12 +00001836 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001837 int count = 0;
1838
1839 if (!isa<CXXRecordDecl>(record)) {
1840 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1841 return;
1842 }
1843
1844 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1845 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1846 return;
1847 }
1848
Eric Christopherf48f3672010-12-01 22:13:54 +00001849 for (RecordDecl::field_iterator iter = record->field_begin();
1850 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001851 if ((count == 1) || !iter->getType()->isVectorType()) {
1852 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1853 return;
1854 }
1855 count++;
1856 }
1857
Michael Han51d8c522013-01-24 16:46:58 +00001858 D->addAttr(::new (S.Context)
1859 VecReturnAttr(Attr.getRange(), S.Context,
1860 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001861}
1862
Richard Smith3a2b7a12013-01-28 22:42:45 +00001863static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1864 const AttributeList &Attr) {
1865 if (isa<ParmVarDecl>(D)) {
1866 // [[carries_dependency]] can only be applied to a parameter if it is a
1867 // parameter of a function declaration or lambda.
1868 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1869 S.Diag(Attr.getLoc(),
1870 diag::err_carries_dependency_param_not_function_decl);
1871 return;
1872 }
1873 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001874 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001875 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001876 return;
1877 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001878
1879 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1880 Attr.getRange(), S.Context,
1881 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001882}
1883
Chandler Carruth1b03c872011-07-02 00:01:44 +00001884static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001885 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001886 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001887 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001888 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001889 return;
1890 }
Mike Stumpbf916502009-07-24 19:02:52 +00001891
Michael Han51d8c522013-01-24 16:46:58 +00001892 D->addAttr(::new (S.Context)
1893 UnusedAttr(Attr.getRange(), S.Context,
1894 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001895}
1896
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001897static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1898 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001899 if (!isa<FunctionDecl>(D)) {
1900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1901 << Attr.getName() << ExpectedFunction;
1902 return;
1903 }
1904
Michael Han51d8c522013-01-24 16:46:58 +00001905 D->addAttr(::new (S.Context)
1906 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1907 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001908}
1909
Chandler Carruth1b03c872011-07-02 00:01:44 +00001910static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001911 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001912 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001913 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1914 return;
1915 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001916 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001917 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001918 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001919 return;
1920 }
Mike Stumpbf916502009-07-24 19:02:52 +00001921
Michael Han51d8c522013-01-24 16:46:58 +00001922 D->addAttr(::new (S.Context)
1923 UsedAttr(Attr.getRange(), S.Context,
1924 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001925}
1926
Chandler Carruth1b03c872011-07-02 00:01:44 +00001927static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001928 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001929 if (Attr.getNumArgs() > 1) {
1930 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001931 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001932 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001933
1934 int priority = 65535; // FIXME: Do not hardcode such constants.
1935 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001936 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001937 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001938 if (E->isTypeDependent() || E->isValueDependent() ||
1939 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001940 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001941 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001942 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001943 return;
1944 }
1945 priority = Idx.getZExtValue();
1946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Chandler Carruth87c44602011-07-01 23:49:12 +00001948 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001949 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001950 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001951 return;
1952 }
1953
Michael Han51d8c522013-01-24 16:46:58 +00001954 D->addAttr(::new (S.Context)
1955 ConstructorAttr(Attr.getRange(), S.Context, priority,
1956 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001957}
1958
Chandler Carruth1b03c872011-07-02 00:01:44 +00001959static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001960 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001961 if (Attr.getNumArgs() > 1) {
1962 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001963 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001964 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001965
1966 int priority = 65535; // FIXME: Do not hardcode such constants.
1967 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001968 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001969 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001970 if (E->isTypeDependent() || E->isValueDependent() ||
1971 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001972 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001973 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001974 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001975 return;
1976 }
1977 priority = Idx.getZExtValue();
1978 }
Mike Stumpbf916502009-07-24 19:02:52 +00001979
Chandler Carruth87c44602011-07-01 23:49:12 +00001980 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001981 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001982 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001983 return;
1984 }
1985
Michael Han51d8c522013-01-24 16:46:58 +00001986 D->addAttr(::new (S.Context)
1987 DestructorAttr(Attr.getRange(), S.Context, priority,
1988 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001989}
1990
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001991template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00001992static void handleAttrWithMessage(Sema &S, Decl *D,
1993 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001994 unsigned NumArgs = Attr.getNumArgs();
1995 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001996 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001997 return;
1998 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001999
2000 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002001 StringRef Str;
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002002 if (NumArgs == 1 && !checkStringLiteralArgument(S, Str, Attr, 0))
2003 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002004
Michael Han51d8c522013-01-24 16:46:58 +00002005 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2006 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002007}
2008
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002009static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2010 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002011 D->addAttr(::new (S.Context)
2012 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2013 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002014}
2015
Patrick Beardb2f68202012-04-06 18:12:22 +00002016static void handleObjCRootClassAttr(Sema &S, Decl *D,
2017 const AttributeList &Attr) {
2018 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002019 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2020 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002021 return;
2022 }
2023
Michael Han51d8c522013-01-24 16:46:58 +00002024 D->addAttr(::new (S.Context)
2025 ObjCRootClassAttr(Attr.getRange(), S.Context,
2026 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002027}
2028
Michael Han51d8c522013-01-24 16:46:58 +00002029static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2030 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002031 if (!isa<ObjCInterfaceDecl>(D)) {
2032 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2033 return;
2034 }
2035
Michael Han51d8c522013-01-24 16:46:58 +00002036 D->addAttr(::new (S.Context)
2037 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2038 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002039}
2040
Jordy Rosefad5de92012-05-08 03:27:22 +00002041static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2042 IdentifierInfo *Platform,
2043 VersionTuple Introduced,
2044 VersionTuple Deprecated,
2045 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002046 StringRef PlatformName
2047 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2048 if (PlatformName.empty())
2049 PlatformName = Platform->getName();
2050
2051 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2052 // of these steps are needed).
2053 if (!Introduced.empty() && !Deprecated.empty() &&
2054 !(Introduced <= Deprecated)) {
2055 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2056 << 1 << PlatformName << Deprecated.getAsString()
2057 << 0 << Introduced.getAsString();
2058 return true;
2059 }
2060
2061 if (!Introduced.empty() && !Obsoleted.empty() &&
2062 !(Introduced <= Obsoleted)) {
2063 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2064 << 2 << PlatformName << Obsoleted.getAsString()
2065 << 0 << Introduced.getAsString();
2066 return true;
2067 }
2068
2069 if (!Deprecated.empty() && !Obsoleted.empty() &&
2070 !(Deprecated <= Obsoleted)) {
2071 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2072 << 2 << PlatformName << Obsoleted.getAsString()
2073 << 1 << Deprecated.getAsString();
2074 return true;
2075 }
2076
2077 return false;
2078}
2079
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002080/// \brief Check whether the two versions match.
2081///
2082/// If either version tuple is empty, then they are assumed to match. If
2083/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2084static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2085 bool BeforeIsOkay) {
2086 if (X.empty() || Y.empty())
2087 return true;
2088
2089 if (X == Y)
2090 return true;
2091
2092 if (BeforeIsOkay && X < Y)
2093 return true;
2094
2095 return false;
2096}
2097
Rafael Espindola51be6e32013-01-08 22:04:34 +00002098AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002099 IdentifierInfo *Platform,
2100 VersionTuple Introduced,
2101 VersionTuple Deprecated,
2102 VersionTuple Obsoleted,
2103 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002104 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002105 bool Override,
2106 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002107 VersionTuple MergedIntroduced = Introduced;
2108 VersionTuple MergedDeprecated = Deprecated;
2109 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002110 bool FoundAny = false;
2111
Rafael Espindola98ae8342012-05-10 02:50:16 +00002112 if (D->hasAttrs()) {
2113 AttrVec &Attrs = D->getAttrs();
2114 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2115 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2116 if (!OldAA) {
2117 ++i;
2118 continue;
2119 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002120
Rafael Espindola98ae8342012-05-10 02:50:16 +00002121 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2122 if (OldPlatform != Platform) {
2123 ++i;
2124 continue;
2125 }
2126
2127 FoundAny = true;
2128 VersionTuple OldIntroduced = OldAA->getIntroduced();
2129 VersionTuple OldDeprecated = OldAA->getDeprecated();
2130 VersionTuple OldObsoleted = OldAA->getObsoleted();
2131 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002132
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002133 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2134 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2135 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2136 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002137 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002138 if (Override) {
2139 int Which = -1;
2140 VersionTuple FirstVersion;
2141 VersionTuple SecondVersion;
2142 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2143 Which = 0;
2144 FirstVersion = OldIntroduced;
2145 SecondVersion = Introduced;
2146 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2147 Which = 1;
2148 FirstVersion = Deprecated;
2149 SecondVersion = OldDeprecated;
2150 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2151 Which = 2;
2152 FirstVersion = Obsoleted;
2153 SecondVersion = OldObsoleted;
2154 }
2155
2156 if (Which == -1) {
2157 Diag(OldAA->getLocation(),
2158 diag::warn_mismatched_availability_override_unavail)
2159 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2160 } else {
2161 Diag(OldAA->getLocation(),
2162 diag::warn_mismatched_availability_override)
2163 << Which
2164 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2165 << FirstVersion.getAsString() << SecondVersion.getAsString();
2166 }
2167 Diag(Range.getBegin(), diag::note_overridden_method);
2168 } else {
2169 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2170 Diag(Range.getBegin(), diag::note_previous_attribute);
2171 }
2172
Rafael Espindola98ae8342012-05-10 02:50:16 +00002173 Attrs.erase(Attrs.begin() + i);
2174 --e;
2175 continue;
2176 }
2177
2178 VersionTuple MergedIntroduced2 = MergedIntroduced;
2179 VersionTuple MergedDeprecated2 = MergedDeprecated;
2180 VersionTuple MergedObsoleted2 = MergedObsoleted;
2181
2182 if (MergedIntroduced2.empty())
2183 MergedIntroduced2 = OldIntroduced;
2184 if (MergedDeprecated2.empty())
2185 MergedDeprecated2 = OldDeprecated;
2186 if (MergedObsoleted2.empty())
2187 MergedObsoleted2 = OldObsoleted;
2188
2189 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2190 MergedIntroduced2, MergedDeprecated2,
2191 MergedObsoleted2)) {
2192 Attrs.erase(Attrs.begin() + i);
2193 --e;
2194 continue;
2195 }
2196
2197 MergedIntroduced = MergedIntroduced2;
2198 MergedDeprecated = MergedDeprecated2;
2199 MergedObsoleted = MergedObsoleted2;
2200 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002201 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002202 }
2203
2204 if (FoundAny &&
2205 MergedIntroduced == Introduced &&
2206 MergedDeprecated == Deprecated &&
2207 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002208 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002209
Ted Kremenekcb344392013-04-06 00:34:27 +00002210 // Only create a new attribute if !Override, but we want to do
2211 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002212 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002213 MergedDeprecated, MergedObsoleted) &&
2214 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002215 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2216 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002217 Obsoleted, IsUnavailable, Message,
2218 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002219 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002220 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002221}
2222
Chandler Carruth1b03c872011-07-02 00:01:44 +00002223static void handleAvailabilityAttr(Sema &S, Decl *D,
2224 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002225 if (!checkAttributeNumArgs(S, Attr, 1))
2226 return;
2227 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002228 unsigned Index = Attr.getAttributeSpellingListIndex();
2229
Aaron Ballman624421f2013-08-31 01:11:41 +00002230 IdentifierInfo *II = Platform->Ident;
2231 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2232 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2233 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002234
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002235 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2236 if (!ND) {
2237 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2238 return;
2239 }
2240
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002241 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2242 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2243 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002244 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002245 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002246 if (const StringLiteral *SE =
2247 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002248 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002249
Aaron Ballman624421f2013-08-31 01:11:41 +00002250 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002251 Introduced.Version,
2252 Deprecated.Version,
2253 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002254 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002255 /*Override=*/false,
2256 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002257 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002258 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002259}
2260
John McCalld4c3d662013-02-20 01:54:26 +00002261template <class T>
2262static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2263 typename T::VisibilityType value,
2264 unsigned attrSpellingListIndex) {
2265 T *existingAttr = D->getAttr<T>();
2266 if (existingAttr) {
2267 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2268 if (existingValue == value)
2269 return NULL;
2270 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2271 S.Diag(range.getBegin(), diag::note_previous_attribute);
2272 D->dropAttr<T>();
2273 }
2274 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2275}
2276
Rafael Espindola599f1b72012-05-13 03:25:18 +00002277VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002278 VisibilityAttr::VisibilityType Vis,
2279 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002280 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2281 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002282}
2283
John McCalld4c3d662013-02-20 01:54:26 +00002284TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2285 TypeVisibilityAttr::VisibilityType Vis,
2286 unsigned AttrSpellingListIndex) {
2287 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2288 AttrSpellingListIndex);
2289}
2290
2291static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2292 bool isTypeVisibility) {
2293 // Visibility attributes don't mean anything on a typedef.
2294 if (isa<TypedefNameDecl>(D)) {
2295 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2296 << Attr.getName();
2297 return;
2298 }
2299
2300 // 'type_visibility' can only go on a type or namespace.
2301 if (isTypeVisibility &&
2302 !(isa<TagDecl>(D) ||
2303 isa<ObjCInterfaceDecl>(D) ||
2304 isa<NamespaceDecl>(D))) {
2305 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2306 << Attr.getName() << ExpectedTypeOrNamespace;
2307 return;
2308 }
2309
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002310 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002311 StringRef TypeStr;
2312 SourceLocation LiteralLoc;
2313 if (!checkStringLiteralArgument(S, TypeStr, Attr, 0, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002314 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002315
Sean Huntcf807c42010-08-18 23:23:40 +00002316 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002317 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002318 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002319 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002320 return;
2321 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002322
2323 // Complain about attempts to use protected visibility on targets
2324 // (like Darwin) that don't support it.
2325 if (type == VisibilityAttr::Protected &&
2326 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2327 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2328 type = VisibilityAttr::Default;
2329 }
Mike Stumpbf916502009-07-24 19:02:52 +00002330
Michael Han51d8c522013-01-24 16:46:58 +00002331 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002332 clang::Attr *newAttr;
2333 if (isTypeVisibility) {
2334 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2335 (TypeVisibilityAttr::VisibilityType) type,
2336 Index);
2337 } else {
2338 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2339 }
2340 if (newAttr)
2341 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002342}
2343
Chandler Carruth1b03c872011-07-02 00:01:44 +00002344static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2345 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002346 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2347 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002348 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002349 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002350 return;
2351 }
2352
Aaron Ballman624421f2013-08-31 01:11:41 +00002353 if (!Attr.isArgIdent(0)) {
2354 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2355 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002356 return;
2357 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002358
Aaron Ballmand0686072013-09-11 19:47:58 +00002359 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2360 ObjCMethodFamilyAttr::FamilyKind F;
2361 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2362 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2363 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002364 return;
2365 }
2366
Aaron Ballmand0686072013-09-11 19:47:58 +00002367 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002368 !method->getResultType()->isObjCObjectPointerType()) {
2369 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2370 << method->getResultType();
2371 // Ignore the attribute.
2372 return;
2373 }
2374
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002375 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002376 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002377}
2378
Chandler Carruth1b03c872011-07-02 00:01:44 +00002379static void handleObjCExceptionAttr(Sema &S, Decl *D,
2380 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002381 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2382 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002383 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2384 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002385 return;
2386 }
Mike Stumpbf916502009-07-24 19:02:52 +00002387
Michael Han51d8c522013-01-24 16:46:58 +00002388 D->addAttr(::new (S.Context)
2389 ObjCExceptionAttr(Attr.getRange(), S.Context,
2390 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002391}
2392
Chandler Carruth1b03c872011-07-02 00:01:44 +00002393static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002394 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002395 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002396 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002397 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2398 return;
2399 }
2400 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002401 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2402 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002403 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002404 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2405 return;
2406 }
2407 }
2408 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002409 // It is okay to include this attribute on properties, e.g.:
2410 //
2411 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2412 //
2413 // In this case it follows tradition and suppresses an error in the above
2414 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002415 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002416 }
Michael Han51d8c522013-01-24 16:46:58 +00002417 D->addAttr(::new (S.Context)
2418 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2419 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002420}
2421
Mike Stumpbf916502009-07-24 19:02:52 +00002422static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002423handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002424 if (!isa<FunctionDecl>(D)) {
2425 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2426 return;
2427 }
2428
Michael Han51d8c522013-01-24 16:46:58 +00002429 D->addAttr(::new (S.Context)
2430 OverloadableAttr(Attr.getRange(), S.Context,
2431 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002432}
2433
Chandler Carruth1b03c872011-07-02 00:01:44 +00002434static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002435 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002436 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002437 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002438 return;
2439 }
Mike Stumpbf916502009-07-24 19:02:52 +00002440
Aaron Ballman624421f2013-08-31 01:11:41 +00002441 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002442 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002443 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2444 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2445 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002446 return;
2447 }
Mike Stumpbf916502009-07-24 19:02:52 +00002448
Michael Han51d8c522013-01-24 16:46:58 +00002449 D->addAttr(::new (S.Context)
2450 BlocksAttr(Attr.getRange(), S.Context, type,
2451 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002452}
2453
Chandler Carruth1b03c872011-07-02 00:01:44 +00002454static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002455 // check the attribute arguments.
2456 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002457 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002458 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002459 }
2460
John McCall3323fad2011-09-09 07:56:05 +00002461 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002462 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002463 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002464 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002465 if (E->isTypeDependent() || E->isValueDependent() ||
2466 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002468 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002469 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002470 return;
2471 }
Mike Stumpbf916502009-07-24 19:02:52 +00002472
John McCall3323fad2011-09-09 07:56:05 +00002473 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002474 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2475 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002476 return;
2477 }
John McCall3323fad2011-09-09 07:56:05 +00002478
2479 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002480 }
2481
John McCall3323fad2011-09-09 07:56:05 +00002482 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002483 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002484 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002485 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002486 if (E->isTypeDependent() || E->isValueDependent() ||
2487 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002489 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002490 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002491 return;
2492 }
2493 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002494
John McCall3323fad2011-09-09 07:56:05 +00002495 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002496 // FIXME: This error message could be improved, it would be nice
2497 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002498 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2499 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002500 return;
2501 }
2502 }
2503
Chandler Carruth87c44602011-07-01 23:49:12 +00002504 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002505 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002506 if (isa<FunctionNoProtoType>(FT)) {
2507 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2508 return;
2509 }
Mike Stumpbf916502009-07-24 19:02:52 +00002510
Chris Lattner897cd902009-03-17 23:03:47 +00002511 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002512 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002513 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002514 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002515 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002516 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002517 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002518 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002519 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002520 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2521 if (!BD->isVariadic()) {
2522 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2523 return;
2524 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002525 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002526 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002527 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002528 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002529 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002530 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002531 int m = Ty->isFunctionPointerType() ? 0 : 1;
2532 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002533 return;
2534 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002535 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002536 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002537 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002538 return;
2539 }
Anders Carlsson77091822008-10-05 18:05:59 +00002540 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002541 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002542 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002543 return;
2544 }
Michael Han51d8c522013-01-24 16:46:58 +00002545 D->addAttr(::new (S.Context)
2546 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2547 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002548}
2549
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002550static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002551 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2552 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2553 else
2554 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2555}
2556
Chandler Carruth1b03c872011-07-02 00:01:44 +00002557static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002558 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002559 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002560 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002561 return;
2562 }
Mike Stumpbf916502009-07-24 19:02:52 +00002563
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002564 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2565 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2566 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002567 return;
2568 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002569 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2570 if (MD->getResultType()->isVoidType()) {
2571 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2572 << Attr.getName() << 1;
2573 return;
2574 }
2575
Michael Han51d8c522013-01-24 16:46:58 +00002576 D->addAttr(::new (S.Context)
2577 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2578 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002579}
2580
Chandler Carruth1b03c872011-07-02 00:01:44 +00002581static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002582 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002583 if (isa<CXXRecordDecl>(D)) {
2584 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2585 return;
2586 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2588 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002589 return;
2590 }
2591
Chandler Carruth87c44602011-07-01 23:49:12 +00002592 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002593
Michael Han51d8c522013-01-24 16:46:58 +00002594 nd->addAttr(::new (S.Context)
2595 WeakAttr(Attr.getRange(), S.Context,
2596 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002597}
2598
Chandler Carruth1b03c872011-07-02 00:01:44 +00002599static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002600 // weak_import only applies to variable & function declarations.
2601 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002602 if (!D->canBeWeakImported(isDef)) {
2603 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002604 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2605 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002606 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002607 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002608 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002609 // Nothing to warn about here.
2610 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002611 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002612 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002613
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002614 return;
2615 }
2616
Michael Han51d8c522013-01-24 16:46:58 +00002617 D->addAttr(::new (S.Context)
2618 WeakImportAttr(Attr.getRange(), S.Context,
2619 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002620}
2621
Tanya Lattner0df579e2012-07-09 22:06:01 +00002622// Handles reqd_work_group_size and work_group_size_hint.
2623static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002624 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002625 unsigned WGSize[3];
2626 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002627 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002628 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002629 if (E->isTypeDependent() || E->isValueDependent() ||
2630 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002631 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2632 << Attr.getName() << AANT_ArgumentIntegerConstant
2633 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002634 return;
2635 }
2636 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2637 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002638
2639 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2640 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2641 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2642 if (!(A->getXDim() == WGSize[0] &&
2643 A->getYDim() == WGSize[1] &&
2644 A->getZDim() == WGSize[2])) {
2645 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2646 Attr.getName();
2647 }
2648 }
2649
2650 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2651 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2652 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2653 if (!(A->getXDim() == WGSize[0] &&
2654 A->getYDim() == WGSize[1] &&
2655 A->getZDim() == WGSize[2])) {
2656 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2657 Attr.getName();
2658 }
2659 }
2660
2661 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2662 D->addAttr(::new (S.Context)
2663 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002664 WGSize[0], WGSize[1], WGSize[2],
2665 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002666 else
2667 D->addAttr(::new (S.Context)
2668 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002669 WGSize[0], WGSize[1], WGSize[2],
2670 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002671}
2672
Joey Gouly37453b92013-03-08 09:42:32 +00002673static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2674 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2675
Aaron Ballman624421f2013-08-31 01:11:41 +00002676 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002677 return;
2678
Aaron Ballman624421f2013-08-31 01:11:41 +00002679 if (!Attr.hasParsedType()) {
2680 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2681 << Attr.getName() << 1;
2682 return;
2683 }
2684
Joey Gouly37453b92013-03-08 09:42:32 +00002685 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2686
2687 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2688 (ParmType->isBooleanType() ||
2689 !ParmType->isIntegralType(S.getASTContext()))) {
2690 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2691 << ParmType;
2692 return;
2693 }
2694
2695 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2696 D->hasAttr<VecTypeHintAttr>()) {
2697 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2698 if (A->getTypeHint() != ParmType) {
2699 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2700 return;
2701 }
2702 }
2703
2704 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2705 ParmType, Attr.getLoc()));
2706}
2707
Rafael Espindola599f1b72012-05-13 03:25:18 +00002708SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002709 StringRef Name,
2710 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002711 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2712 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002713 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002714 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2715 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002716 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002717 }
Michael Han51d8c522013-01-24 16:46:58 +00002718 return ::new (Context) SectionAttr(Range, Context, Name,
2719 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002720}
2721
Chandler Carruth1b03c872011-07-02 00:01:44 +00002722static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002723 // Make sure that there is a string literal as the sections's single
2724 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002725 StringRef Str;
2726 SourceLocation LiteralLoc;
2727 if (!checkStringLiteralArgument(S, Str, Attr, 0, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002728 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Chris Lattner797c3c42009-08-10 19:03:04 +00002730 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002731 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002732 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002733 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002734 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002735 return;
2736 }
Mike Stump1eb44332009-09-09 15:08:12 +00002737
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002738 // This attribute cannot be applied to local variables.
2739 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002740 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002741 return;
2742 }
Michael Han51d8c522013-01-24 16:46:58 +00002743
2744 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002745 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002746 if (NewAttr)
2747 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002748}
2749
Chris Lattner6b6b5372008-06-26 18:38:35 +00002750
Chandler Carruth1b03c872011-07-02 00:01:44 +00002751static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002752 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002753 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002754 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002755 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002756 D->addAttr(::new (S.Context)
2757 NoThrowAttr(Attr.getRange(), S.Context,
2758 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002759 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002760}
2761
Chandler Carruth1b03c872011-07-02 00:01:44 +00002762static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002763 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002764 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002765 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002766 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002767 D->addAttr(::new (S.Context)
2768 ConstAttr(Attr.getRange(), S.Context,
2769 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002770 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002771}
2772
Chandler Carruth1b03c872011-07-02 00:01:44 +00002773static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002774 D->addAttr(::new (S.Context)
2775 PureAttr(Attr.getRange(), S.Context,
2776 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002777}
2778
Chandler Carruth1b03c872011-07-02 00:01:44 +00002779static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002780 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002781 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002782 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002783 return;
2784 }
Mike Stumpbf916502009-07-24 19:02:52 +00002785
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002786 Expr *E = Attr.getArgAsExpr(0);
2787 SourceLocation Loc = E->getExprLoc();
2788 FunctionDecl *FD = 0;
2789 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002790
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002791 // gcc only allows for simple identifiers. Since we support more than gcc, we
2792 // will warn the user.
2793 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2794 if (DRE->hasQualifier())
2795 S.Diag(Loc, diag::warn_cleanup_ext);
2796 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2797 NI = DRE->getNameInfo();
2798 if (!FD) {
2799 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2800 << NI.getName();
2801 return;
2802 }
2803 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2804 if (ULE->hasExplicitTemplateArgs())
2805 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002806
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002807 // This will diagnose the case where the function cannot be found.
2808 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2809 NI = ULE->getNameInfo();
2810 } else {
2811 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002812 return;
2813 }
2814
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002815 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002816 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2817 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002818 return;
2819 }
Mike Stumpbf916502009-07-24 19:02:52 +00002820
Anders Carlsson89941c12009-02-07 23:16:50 +00002821 // We're currently more strict than GCC about what function types we accept.
2822 // If this ever proves to be a problem it should be easy to fix.
2823 QualType Ty = S.Context.getPointerType(VD->getType());
2824 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002825 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2826 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002827 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2828 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002829 return;
2830 }
Mike Stumpbf916502009-07-24 19:02:52 +00002831
Michael Han51d8c522013-01-24 16:46:58 +00002832 D->addAttr(::new (S.Context)
2833 CleanupAttr(Attr.getRange(), S.Context, FD,
2834 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002835}
2836
Mike Stumpbf916502009-07-24 19:02:52 +00002837/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002838/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002839static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002840 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002841 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002842 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002843 return;
2844 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002845
Aaron Ballman624421f2013-08-31 01:11:41 +00002846 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002847 uint64_t ArgIdx;
2848 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2849 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002850 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002851
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002852 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002853 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002854
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002855 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2856 if (not_nsstring_type &&
2857 !isCFStringType(Ty, S.Context) &&
2858 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002859 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002860 // FIXME: Should highlight the actual expression that has the wrong type.
2861 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002862 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002863 << IdxExpr->getSourceRange();
2864 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002865 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002866 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002867 if (!isNSStringType(Ty, S.Context) &&
2868 !isCFStringType(Ty, S.Context) &&
2869 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002870 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002871 // FIXME: Should highlight the actual expression that has the wrong type.
2872 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002873 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002874 << IdxExpr->getSourceRange();
2875 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002876 }
2877
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002878 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2879 // because that has corrected for the implicit this parameter, and is zero-
2880 // based. The attribute expects what the user wrote explicitly.
2881 llvm::APSInt Val;
2882 IdxExpr->EvaluateAsInt(Val, S.Context);
2883
Michael Han51d8c522013-01-24 16:46:58 +00002884 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002885 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002886 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002887}
2888
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002889enum FormatAttrKind {
2890 CFStringFormat,
2891 NSStringFormat,
2892 StrftimeFormat,
2893 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002894 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002895 InvalidFormat
2896};
2897
2898/// getFormatAttrKind - Map from format attribute names to supported format
2899/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002900static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002901 return llvm::StringSwitch<FormatAttrKind>(Format)
2902 // Check for formats that get handled specially.
2903 .Case("NSString", NSStringFormat)
2904 .Case("CFString", CFStringFormat)
2905 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002906
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002907 // Otherwise, check for supported formats.
2908 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2909 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2910 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002911
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002912 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2913 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002914}
2915
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002916/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002917/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002918static void handleInitPriorityAttr(Sema &S, Decl *D,
2919 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002920 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002921 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2922 return;
2923 }
2924
Chandler Carruth87c44602011-07-01 23:49:12 +00002925 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002926 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2927 Attr.setInvalid();
2928 return;
2929 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002930 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002931 if (S.Context.getAsArrayType(T))
2932 T = S.Context.getBaseElementType(T);
2933 if (!T->getAs<RecordType>()) {
2934 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2935 Attr.setInvalid();
2936 return;
2937 }
2938
Aaron Ballman624421f2013-08-31 01:11:41 +00002939 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002940
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002941 llvm::APSInt priority(32);
2942 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2943 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002944 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2945 << Attr.getName() << AANT_ArgumentIntegerConstant
2946 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002947 Attr.setInvalid();
2948 return;
2949 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002950 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002951 if (prioritynum < 101 || prioritynum > 65535) {
2952 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2953 << priorityExpr->getSourceRange();
2954 Attr.setInvalid();
2955 return;
2956 }
Michael Han51d8c522013-01-24 16:46:58 +00002957 D->addAttr(::new (S.Context)
2958 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2959 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002960}
2961
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00002962FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2963 IdentifierInfo *Format, int FormatIdx,
2964 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00002965 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002966 // Check whether we already have an equivalent format attribute.
2967 for (specific_attr_iterator<FormatAttr>
2968 i = D->specific_attr_begin<FormatAttr>(),
2969 e = D->specific_attr_end<FormatAttr>();
2970 i != e ; ++i) {
2971 FormatAttr *f = *i;
2972 if (f->getType() == Format &&
2973 f->getFormatIdx() == FormatIdx &&
2974 f->getFirstArg() == FirstArg) {
2975 // If we don't have a valid location for this attribute, adopt the
2976 // location.
2977 if (f->getLocation().isInvalid())
2978 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002979 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002980 }
2981 }
2982
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00002983 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2984 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002985}
2986
Mike Stumpbf916502009-07-24 19:02:52 +00002987/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002988/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002989static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002990 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002991 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002992 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002993 return;
2994 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002995
Chandler Carruth87c44602011-07-01 23:49:12 +00002996 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002998 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002999 return;
3000 }
3001
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003002 // In C++ the implicit 'this' function parameter also counts, and they are
3003 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003004 bool HasImplicitThisParam = isInstanceMethod(D);
3005 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003006 unsigned FirstIdx = 1;
3007
Aaron Ballman624421f2013-08-31 01:11:41 +00003008 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3009 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003010
3011 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003012 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003013 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003014 // If we've modified the string name, we need a new identifier for it.
3015 II = &S.Context.Idents.get(Format);
3016 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003017
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003018 // Check for supported formats.
3019 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003020
3021 if (Kind == IgnoredFormat)
3022 return;
3023
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003024 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003025 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003026 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003027 return;
3028 }
3029
3030 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003031 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003032 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003033 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3034 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003036 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003037 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003038 return;
3039 }
3040
3041 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003042 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003043 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003044 return;
3045 }
3046
3047 // FIXME: Do we need to bounds check?
3048 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003049
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003050 if (HasImplicitThisParam) {
3051 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003052 S.Diag(Attr.getLoc(),
3053 diag::err_format_attribute_implicit_this_format_string)
3054 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003055 return;
3056 }
3057 ArgIdx--;
3058 }
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Chris Lattner6b6b5372008-06-26 18:38:35 +00003060 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003061 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003062
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003063 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003064 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003065 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3066 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003067 return;
3068 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003069 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003070 // FIXME: do we need to check if the type is NSString*? What are the
3071 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003072 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003073 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003074 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3075 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003076 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003077 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003078 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003079 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003080 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003081 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3082 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003083 return;
3084 }
3085
3086 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003087 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003088 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003089 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3090 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003091 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003092 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003093 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003094 return;
3095 }
3096
3097 // check if the function is variadic if the 3rd argument non-zero
3098 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003099 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003100 ++NumArgs; // +1 for ...
3101 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003102 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003103 return;
3104 }
3105 }
3106
Chris Lattner3c73c412008-11-19 08:23:25 +00003107 // strftime requires FirstArg to be 0 because it doesn't read from any
3108 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003109 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003110 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003111 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3112 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003113 return;
3114 }
3115 // if 0 it disables parameter checking (to use with e.g. va_list)
3116 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003118 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003119 return;
3120 }
3121
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003122 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003123 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003124 FirstArg.getZExtValue(),
3125 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003126 if (NewAttr)
3127 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003128}
3129
Chandler Carruth1b03c872011-07-02 00:01:44 +00003130static void handleTransparentUnionAttr(Sema &S, Decl *D,
3131 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003132 // Try to find the underlying union declaration.
3133 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003134 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003135 if (TD && TD->getUnderlyingType()->isUnionType())
3136 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3137 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003138 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003139
3140 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003141 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003142 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003143 return;
3144 }
3145
John McCall5e1cdac2011-10-07 06:10:15 +00003146 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003147 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003148 diag::warn_transparent_union_attribute_not_definition);
3149 return;
3150 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003151
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003152 RecordDecl::field_iterator Field = RD->field_begin(),
3153 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003154 if (Field == FieldEnd) {
3155 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3156 return;
3157 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003158
David Blaikie581deb32012-06-06 20:45:41 +00003159 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003160 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003161 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003162 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003163 diag::warn_transparent_union_attribute_floating)
3164 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003165 return;
3166 }
3167
3168 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3169 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3170 for (; Field != FieldEnd; ++Field) {
3171 QualType FieldType = Field->getType();
3172 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3173 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3174 // Warn if we drop the attribute.
3175 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003176 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003177 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003178 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003179 diag::warn_transparent_union_attribute_field_size_align)
3180 << isSize << Field->getDeclName() << FieldBits;
3181 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003182 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003183 diag::note_transparent_union_first_field_size_align)
3184 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003185 return;
3186 }
3187 }
3188
Michael Han51d8c522013-01-24 16:46:58 +00003189 RD->addAttr(::new (S.Context)
3190 TransparentUnionAttr(Attr.getRange(), S.Context,
3191 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003192}
3193
Chandler Carruth1b03c872011-07-02 00:01:44 +00003194static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003195 // Make sure that there is a string literal as the annotation's single
3196 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003197 StringRef Str;
3198 if (!checkStringLiteralArgument(S, Str, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003199 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003200
3201 // Don't duplicate annotations that are already set.
3202 for (specific_attr_iterator<AnnotateAttr>
3203 i = D->specific_attr_begin<AnnotateAttr>(),
3204 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003205 if ((*i)->getAnnotation() == Str)
3206 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003207 }
Michael Han51d8c522013-01-24 16:46:58 +00003208
3209 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003210 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003211 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003212}
3213
Chandler Carruth1b03c872011-07-02 00:01:44 +00003214static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003215 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003216 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3218 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003219 return;
3220 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003221
Richard Smithbe507b62013-02-01 08:12:08 +00003222 if (Attr.getNumArgs() == 0) {
3223 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3224 true, 0, Attr.getAttributeSpellingListIndex()));
3225 return;
3226 }
3227
Aaron Ballman624421f2013-08-31 01:11:41 +00003228 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003229 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3230 S.Diag(Attr.getEllipsisLoc(),
3231 diag::err_pack_expansion_without_parameter_packs);
3232 return;
3233 }
3234
3235 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3236 return;
3237
3238 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3239 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003240}
3241
3242void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003243 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003244 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3245 SourceLocation AttrLoc = AttrRange.getBegin();
3246
Richard Smith4cd81c52013-01-29 09:02:09 +00003247 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003248 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003249 // C++11 [dcl.align]p1:
3250 // An alignment-specifier may be applied to a variable or to a class
3251 // data member, but it shall not be applied to a bit-field, a function
3252 // parameter, the formal parameter of a catch clause, or a variable
3253 // declared with the register storage class specifier. An
3254 // alignment-specifier may also be applied to the declaration of a class
3255 // or enumeration type.
3256 // C11 6.7.5/2:
3257 // An alignment attribute shall not be specified in a declaration of
3258 // a typedef, or a bit-field, or a function, or a parameter, or an
3259 // object declared with the register storage-class specifier.
3260 int DiagKind = -1;
3261 if (isa<ParmVarDecl>(D)) {
3262 DiagKind = 0;
3263 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3264 if (VD->getStorageClass() == SC_Register)
3265 DiagKind = 1;
3266 if (VD->isExceptionVariable())
3267 DiagKind = 2;
3268 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3269 if (FD->isBitField())
3270 DiagKind = 3;
3271 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003272 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3273 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003274 << (TmpAttr.isC11() ? ExpectedVariableOrField
3275 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003276 return;
3277 }
3278 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003279 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003280 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003281 return;
3282 }
3283 }
3284
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003285 if (E->isTypeDependent() || E->isValueDependent()) {
3286 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003287 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3288 AA->setPackExpansion(IsPackExpansion);
3289 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003290 return;
3291 }
Michael Hana31f65b2013-02-01 01:19:17 +00003292
Sean Huntcf807c42010-08-18 23:23:40 +00003293 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003294 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003295 ExprResult ICE
3296 = VerifyIntegerConstantExpression(E, &Alignment,
3297 diag::err_aligned_attribute_argument_not_int,
3298 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003299 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003300 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003301
3302 // C++11 [dcl.align]p2:
3303 // -- if the constant expression evaluates to zero, the alignment
3304 // specifier shall have no effect
3305 // C11 6.7.5p6:
3306 // An alignment specification of zero has no effect.
3307 if (!(TmpAttr.isAlignas() && !Alignment) &&
3308 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003309 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3310 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003311 return;
3312 }
Michael Hana31f65b2013-02-01 01:19:17 +00003313
Richard Smithbe507b62013-02-01 08:12:08 +00003314 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003315 // We've already verified it's a power of 2, now let's make sure it's
3316 // 8192 or less.
3317 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003318 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003319 << E->getSourceRange();
3320 return;
3321 }
3322 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003323
Richard Smithf6565a92013-02-22 08:32:16 +00003324 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3325 ICE.take(), SpellingListIndex);
3326 AA->setPackExpansion(IsPackExpansion);
3327 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003328}
3329
Michael Hana31f65b2013-02-01 01:19:17 +00003330void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003331 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003332 // FIXME: Cache the number on the Attr object if non-dependent?
3333 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003334 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3335 SpellingListIndex);
3336 AA->setPackExpansion(IsPackExpansion);
3337 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003338}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003339
Richard Smithbe507b62013-02-01 08:12:08 +00003340void Sema::CheckAlignasUnderalignment(Decl *D) {
3341 assert(D->hasAttrs() && "no attributes on decl");
3342
3343 QualType Ty;
3344 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3345 Ty = VD->getType();
3346 else
3347 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003348 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003349 return;
3350
3351 // C++11 [dcl.align]p5, C11 6.7.5/4:
3352 // The combined effect of all alignment attributes in a declaration shall
3353 // not specify an alignment that is less strict than the alignment that
3354 // would otherwise be required for the entity being declared.
3355 AlignedAttr *AlignasAttr = 0;
3356 unsigned Align = 0;
3357 for (specific_attr_iterator<AlignedAttr>
3358 I = D->specific_attr_begin<AlignedAttr>(),
3359 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3360 if (I->isAlignmentDependent())
3361 return;
3362 if (I->isAlignas())
3363 AlignasAttr = *I;
3364 Align = std::max(Align, I->getAlignment(Context));
3365 }
3366
3367 if (AlignasAttr && Align) {
3368 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3369 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3370 if (NaturalAlign > RequestedAlign)
3371 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3372 << Ty << (unsigned)NaturalAlign.getQuantity();
3373 }
3374}
3375
Chandler Carruthd309c812011-07-01 23:49:16 +00003376/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003377/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003378///
Mike Stumpbf916502009-07-24 19:02:52 +00003379/// Despite what would be logical, the mode attribute is a decl attribute, not a
3380/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3381/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003382static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003383 // This attribute isn't documented, but glibc uses it. It changes
3384 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003385 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3387 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003388 return;
3389 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003390
Aaron Ballman624421f2013-08-31 01:11:41 +00003391 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3392 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003393
3394 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003395 if (Str.startswith("__") && Str.endswith("__"))
3396 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003397
3398 unsigned DestWidth = 0;
3399 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003400 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003401 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003402 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003403 switch (Str[0]) {
3404 case 'Q': DestWidth = 8; break;
3405 case 'H': DestWidth = 16; break;
3406 case 'S': DestWidth = 32; break;
3407 case 'D': DestWidth = 64; break;
3408 case 'X': DestWidth = 96; break;
3409 case 'T': DestWidth = 128; break;
3410 }
3411 if (Str[1] == 'F') {
3412 IntegerMode = false;
3413 } else if (Str[1] == 'C') {
3414 IntegerMode = false;
3415 ComplexMode = true;
3416 } else if (Str[1] != 'I') {
3417 DestWidth = 0;
3418 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003419 break;
3420 case 4:
3421 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3422 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003423 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003424 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003425 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003426 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003427 break;
3428 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003429 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003430 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003431 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003432 case 11:
3433 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003434 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003435 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003436 }
3437
3438 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003439 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003440 OldTy = TD->getUnderlyingType();
3441 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3442 OldTy = VD->getType();
3443 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003444 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003445 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003446 return;
3447 }
Eli Friedman73397492009-03-03 06:41:03 +00003448
John McCall183700f2009-09-21 23:43:11 +00003449 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003450 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3451 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003452 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003453 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3454 } else if (ComplexMode) {
3455 if (!OldTy->isComplexType())
3456 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3457 } else {
3458 if (!OldTy->isFloatingType())
3459 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3460 }
3461
Mike Stump390b4cc2009-05-16 07:39:55 +00003462 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3463 // and friends, at least with glibc.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003464 // FIXME: Make sure floating-point mappings are accurate
3465 // FIXME: Support XF and TF types
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003466 if (!DestWidth) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003467 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003468 return;
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003469 }
3470
3471 QualType NewTy;
3472
3473 if (IntegerMode)
3474 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3475 OldTy->isSignedIntegerType());
3476 else
3477 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3478
3479 if (NewTy.isNull()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003480 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003481 return;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003482 }
3483
Eli Friedman73397492009-03-03 06:41:03 +00003484 if (ComplexMode) {
3485 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003486 }
3487
3488 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003489 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3490 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3491 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003492 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003493
3494 D->addAttr(::new (S.Context)
3495 ModeAttr(Attr.getRange(), S.Context, Name,
3496 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003497}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003498
Chandler Carruth1b03c872011-07-02 00:01:44 +00003499static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003500 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3501 if (!VD->hasGlobalStorage())
3502 S.Diag(Attr.getLoc(),
3503 diag::warn_attribute_requires_functions_or_static_globals)
3504 << Attr.getName();
3505 } else if (!isFunctionOrMethod(D)) {
3506 S.Diag(Attr.getLoc(),
3507 diag::warn_attribute_requires_functions_or_static_globals)
3508 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003509 return;
3510 }
Mike Stumpbf916502009-07-24 19:02:52 +00003511
Michael Han51d8c522013-01-24 16:46:58 +00003512 D->addAttr(::new (S.Context)
3513 NoDebugAttr(Attr.getRange(), S.Context,
3514 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003515}
3516
Chandler Carruth1b03c872011-07-02 00:01:44 +00003517static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003518 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003519 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003520 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003521 return;
3522 }
Mike Stumpbf916502009-07-24 19:02:52 +00003523
Michael Han51d8c522013-01-24 16:46:58 +00003524 D->addAttr(::new (S.Context)
3525 NoInlineAttr(Attr.getRange(), S.Context,
3526 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003527}
3528
Chandler Carruth1b03c872011-07-02 00:01:44 +00003529static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3530 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003531 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003533 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003534 return;
3535 }
3536
Michael Han51d8c522013-01-24 16:46:58 +00003537 D->addAttr(::new (S.Context)
3538 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3539 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003540}
3541
Chandler Carruth1b03c872011-07-02 00:01:44 +00003542static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003543 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003544 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003546 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003547 return;
3548 }
3549
Michael Han51d8c522013-01-24 16:46:58 +00003550 D->addAttr(::new (S.Context)
3551 CUDAConstantAttr(Attr.getRange(), S.Context,
3552 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003553 } else {
3554 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3555 }
3556}
3557
Chandler Carruth1b03c872011-07-02 00:01:44 +00003558static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003559 if (S.LangOpts.CUDA) {
3560 // check the attribute arguments.
3561 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3563 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003564 return;
3565 }
3566
Chandler Carruth87c44602011-07-01 23:49:12 +00003567 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003568 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003569 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003570 return;
3571 }
3572
Michael Han51d8c522013-01-24 16:46:58 +00003573 D->addAttr(::new (S.Context)
3574 CUDADeviceAttr(Attr.getRange(), S.Context,
3575 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003576 } else {
3577 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3578 }
3579}
3580
Chandler Carruth1b03c872011-07-02 00:01:44 +00003581static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003582 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003583 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003585 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003586 return;
3587 }
3588
Chandler Carruth87c44602011-07-01 23:49:12 +00003589 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003590 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003591 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003592 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003593 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3594 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003595 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003596 "void");
3597 } else {
3598 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3599 << FD->getType();
3600 }
3601 return;
3602 }
3603
Michael Han51d8c522013-01-24 16:46:58 +00003604 D->addAttr(::new (S.Context)
3605 CUDAGlobalAttr(Attr.getRange(), S.Context,
3606 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003607 } else {
3608 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3609 }
3610}
3611
Chandler Carruth1b03c872011-07-02 00:01:44 +00003612static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003613 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003614 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003616 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003617 return;
3618 }
3619
Michael Han51d8c522013-01-24 16:46:58 +00003620 D->addAttr(::new (S.Context)
3621 CUDAHostAttr(Attr.getRange(), S.Context,
3622 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003623 } else {
3624 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3625 }
3626}
3627
Chandler Carruth1b03c872011-07-02 00:01:44 +00003628static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003629 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003630 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003632 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003633 return;
3634 }
3635
Michael Han51d8c522013-01-24 16:46:58 +00003636 D->addAttr(::new (S.Context)
3637 CUDASharedAttr(Attr.getRange(), S.Context,
3638 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003639 } else {
3640 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3641 }
3642}
3643
Chandler Carruth1b03c872011-07-02 00:01:44 +00003644static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003645 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003646 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003648 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003649 return;
3650 }
Mike Stumpbf916502009-07-24 19:02:52 +00003651
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003652 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003653 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003654 return;
3655 }
Mike Stumpbf916502009-07-24 19:02:52 +00003656
Michael Han51d8c522013-01-24 16:46:58 +00003657 D->addAttr(::new (S.Context)
3658 GNUInlineAttr(Attr.getRange(), S.Context,
3659 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003660}
3661
Chandler Carruth1b03c872011-07-02 00:01:44 +00003662static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003663 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003664
Aaron Ballmanfff32482012-12-09 17:45:41 +00003665 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003666 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003667 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3668 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003669 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003670 return;
3671
Chandler Carruth87c44602011-07-01 23:49:12 +00003672 if (!isa<ObjCMethodDecl>(D)) {
3673 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3674 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003675 return;
3676 }
3677
Chandler Carruth87c44602011-07-01 23:49:12 +00003678 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003679 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003680 D->addAttr(::new (S.Context)
3681 FastCallAttr(Attr.getRange(), S.Context,
3682 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003683 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003684 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003685 D->addAttr(::new (S.Context)
3686 StdCallAttr(Attr.getRange(), S.Context,
3687 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003688 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003689 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003690 D->addAttr(::new (S.Context)
3691 ThisCallAttr(Attr.getRange(), S.Context,
3692 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003693 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003694 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003695 D->addAttr(::new (S.Context)
3696 CDeclAttr(Attr.getRange(), S.Context,
3697 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003698 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003699 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003700 D->addAttr(::new (S.Context)
3701 PascalAttr(Attr.getRange(), S.Context,
3702 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003703 return;
Charles Davise8519c32013-08-30 04:39:01 +00003704 case AttributeList::AT_MSABI:
3705 D->addAttr(::new (S.Context)
3706 MSABIAttr(Attr.getRange(), S.Context,
3707 Attr.getAttributeSpellingListIndex()));
3708 return;
3709 case AttributeList::AT_SysVABI:
3710 D->addAttr(::new (S.Context)
3711 SysVABIAttr(Attr.getRange(), S.Context,
3712 Attr.getAttributeSpellingListIndex()));
3713 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003714 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003715 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003716 switch (CC) {
3717 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003718 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003719 break;
3720 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003721 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003722 break;
3723 default:
3724 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003725 }
3726
Michael Han51d8c522013-01-24 16:46:58 +00003727 D->addAttr(::new (S.Context)
3728 PcsAttr(Attr.getRange(), S.Context, PCS,
3729 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003730 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003731 }
Derek Schuff263366f2012-10-16 22:30:41 +00003732 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003733 D->addAttr(::new (S.Context)
3734 PnaclCallAttr(Attr.getRange(), S.Context,
3735 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003736 return;
Guy Benyei38980082012-12-25 08:53:55 +00003737 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003738 D->addAttr(::new (S.Context)
3739 IntelOclBiccAttr(Attr.getRange(), S.Context,
3740 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003741 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003742
Abramo Bagnarae215f722010-04-30 13:10:51 +00003743 default:
3744 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003745 }
3746}
3747
Chandler Carruth1b03c872011-07-02 00:01:44 +00003748static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003749 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003750}
3751
Guy Benyei1db70402013-03-24 13:58:12 +00003752static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003753 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003754 llvm::APSInt ArgNum(32);
3755 if (E->isTypeDependent() || E->isValueDependent() ||
3756 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003757 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3758 << Attr.getName() << AANT_ArgumentIntegerConstant
3759 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003760 return;
3761 }
3762
3763 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3764 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3765}
3766
Aaron Ballmanfff32482012-12-09 17:45:41 +00003767bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3768 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003769 if (attr.isInvalid())
3770 return true;
3771
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003772 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003773 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003774 attr.setInvalid();
3775 return true;
3776 }
3777
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003778 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3779 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003780 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003781 case AttributeList::AT_CDecl: CC = CC_C; break;
3782 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3783 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3784 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3785 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003786 case AttributeList::AT_MSABI:
3787 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3788 CC_X86_64Win64;
3789 break;
3790 case AttributeList::AT_SysVABI:
3791 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3792 CC_C;
3793 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003794 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003795 StringRef StrRef;
3796 if (!checkStringLiteralArgument(*this, StrRef, attr, 0)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003797 attr.setInvalid();
3798 return true;
3799 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003800 if (StrRef == "aapcs") {
3801 CC = CC_AAPCS;
3802 break;
3803 } else if (StrRef == "aapcs-vfp") {
3804 CC = CC_AAPCS_VFP;
3805 break;
3806 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003807
3808 attr.setInvalid();
3809 Diag(attr.getLoc(), diag::err_invalid_pcs);
3810 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003811 }
Derek Schuff263366f2012-10-16 22:30:41 +00003812 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003813 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003814 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003815 }
3816
Aaron Ballman82bfa192012-10-02 14:26:08 +00003817 const TargetInfo &TI = Context.getTargetInfo();
3818 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3819 if (A == TargetInfo::CCCR_Warning) {
3820 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003821
3822 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3823 if (FD)
3824 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3825 TargetInfo::CCMT_NonMember;
3826 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003827 }
3828
John McCall711c52b2011-01-05 12:14:39 +00003829 return false;
3830}
3831
Chandler Carruth1b03c872011-07-02 00:01:44 +00003832static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003833 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003834
3835 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003836 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003837 return;
3838
Chandler Carruth87c44602011-07-01 23:49:12 +00003839 if (!isa<ObjCMethodDecl>(D)) {
3840 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3841 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003842 return;
3843 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003844
Michael Han51d8c522013-01-24 16:46:58 +00003845 D->addAttr(::new (S.Context)
3846 RegparmAttr(Attr.getRange(), S.Context, numParams,
3847 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003848}
3849
3850/// Checks a regparm attribute, returning true if it is ill-formed and
3851/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003852bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3853 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003854 return true;
3855
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003856 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003857 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003858 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003859 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003860
Aaron Ballman624421f2013-08-31 01:11:41 +00003861 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003862 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003863 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003864 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003865 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3866 << Attr.getName() << AANT_ArgumentIntegerConstant
3867 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003868 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003869 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003870 }
3871
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003872 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003873 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003874 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003875 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003876 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003877 }
3878
John McCall711c52b2011-01-05 12:14:39 +00003879 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003880 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003881 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003882 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003883 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003884 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003885 }
3886
John McCall711c52b2011-01-05 12:14:39 +00003887 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003888}
3889
Chandler Carruth1b03c872011-07-02 00:01:44 +00003890static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003891 if (S.LangOpts.CUDA) {
3892 // check the attribute arguments.
3893 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003894 // FIXME: 0 is not okay.
3895 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003896 return;
3897 }
3898
Chandler Carruth87c44602011-07-01 23:49:12 +00003899 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003901 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003902 return;
3903 }
3904
Aaron Ballman624421f2013-08-31 01:11:41 +00003905 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003906 llvm::APSInt MaxThreads(32);
3907 if (MaxThreadsExpr->isTypeDependent() ||
3908 MaxThreadsExpr->isValueDependent() ||
3909 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003911 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003912 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003913 return;
3914 }
3915
3916 llvm::APSInt MinBlocks(32);
3917 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003918 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003919 if (MinBlocksExpr->isTypeDependent() ||
3920 MinBlocksExpr->isValueDependent() ||
3921 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003922 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003923 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003924 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003925 return;
3926 }
3927 }
3928
Michael Han51d8c522013-01-24 16:46:58 +00003929 D->addAttr(::new (S.Context)
3930 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3931 MaxThreads.getZExtValue(),
3932 MinBlocks.getZExtValue(),
3933 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00003934 } else {
3935 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3936 }
3937}
3938
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003939static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3940 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003941 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003943 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003944 return;
3945 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003946
3947 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003948 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003949
Aaron Ballman624421f2013-08-31 01:11:41 +00003950 StringRef AttrName = Attr.getName()->getName();
3951 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003952
3953 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3955 << Attr.getName() << ExpectedFunctionOrMethod;
3956 return;
3957 }
3958
3959 uint64_t ArgumentIdx;
3960 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3961 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00003962 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003963 return;
3964
3965 uint64_t TypeTagIdx;
3966 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3967 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00003968 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003969 return;
3970
3971 bool IsPointer = (AttrName == "pointer_with_type_tag");
3972 if (IsPointer) {
3973 // Ensure that buffer has a pointer type.
3974 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3975 if (!BufferTy->isPointerType()) {
3976 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00003977 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003978 }
3979 }
3980
Michael Han51d8c522013-01-24 16:46:58 +00003981 D->addAttr(::new (S.Context)
3982 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3983 ArgumentIdx, TypeTagIdx, IsPointer,
3984 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003985}
3986
3987static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3988 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003989 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003990 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003991 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003992 return;
3993 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003994
3995 if (!checkAttributeNumArgs(S, Attr, 1))
3996 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003997
Aaron Ballman624421f2013-08-31 01:11:41 +00003998 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003999 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4000
Michael Han51d8c522013-01-24 16:46:58 +00004001 D->addAttr(::new (S.Context)
4002 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4003 MatchingCType,
4004 Attr.getLayoutCompatible(),
4005 Attr.getMustBeNull(),
4006 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004007}
4008
Chris Lattner0744e5f2008-06-29 00:23:49 +00004009//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004010// Checker-specific attribute handlers.
4011//===----------------------------------------------------------------------===//
4012
John McCallc7ad3812011-01-25 03:31:58 +00004013static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004014 return type->isDependentType() ||
4015 type->isObjCObjectPointerType() ||
4016 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004017}
4018static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004019 return type->isDependentType() ||
4020 type->isPointerType() ||
4021 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004022}
4023
Chandler Carruth1b03c872011-07-02 00:01:44 +00004024static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004025 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004026 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004027 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004028 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004029 return;
4030 }
4031
4032 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004033 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004034 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4035 cf = false;
4036 } else {
4037 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4038 cf = true;
4039 }
4040
4041 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004042 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004043 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004044 return;
4045 }
4046
4047 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004048 param->addAttr(::new (S.Context)
4049 CFConsumedAttr(Attr.getRange(), S.Context,
4050 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004051 else
Michael Han51d8c522013-01-24 16:46:58 +00004052 param->addAttr(::new (S.Context)
4053 NSConsumedAttr(Attr.getRange(), S.Context,
4054 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004055}
4056
Chandler Carruth1b03c872011-07-02 00:01:44 +00004057static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4058 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004059 if (!isa<ObjCMethodDecl>(D)) {
4060 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004061 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004062 return;
4063 }
4064
Michael Han51d8c522013-01-24 16:46:58 +00004065 D->addAttr(::new (S.Context)
4066 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4067 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004068}
4069
Chandler Carruth1b03c872011-07-02 00:01:44 +00004070static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4071 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004072
John McCallc7ad3812011-01-25 03:31:58 +00004073 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004074
Chandler Carruth87c44602011-07-01 23:49:12 +00004075 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004076 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004077 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004078 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004079 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004080 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4081 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004082 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004083 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004084 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004085 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004086 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004087 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004088 return;
4089 }
Mike Stumpbf916502009-07-24 19:02:52 +00004090
John McCallc7ad3812011-01-25 03:31:58 +00004091 bool typeOK;
4092 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004093 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004094 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004095 case AttributeList::AT_NSReturnsAutoreleased:
4096 case AttributeList::AT_NSReturnsRetained:
4097 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004098 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4099 cf = false;
4100 break;
4101
Sean Hunt8e083e72012-06-19 23:57:03 +00004102 case AttributeList::AT_CFReturnsRetained:
4103 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004104 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4105 cf = true;
4106 break;
4107 }
4108
4109 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004110 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004111 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004112 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004113 }
Mike Stumpbf916502009-07-24 19:02:52 +00004114
Chandler Carruth87c44602011-07-01 23:49:12 +00004115 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004116 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004117 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004118 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004119 D->addAttr(::new (S.Context)
4120 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4121 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004122 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004123 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004124 D->addAttr(::new (S.Context)
4125 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4126 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004127 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004128 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004129 D->addAttr(::new (S.Context)
4130 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4131 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004132 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004133 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004134 D->addAttr(::new (S.Context)
4135 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4136 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004137 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004138 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004139 D->addAttr(::new (S.Context)
4140 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4141 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004142 return;
4143 };
4144}
4145
John McCalldc7c5ad2011-07-22 08:53:00 +00004146static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4147 const AttributeList &attr) {
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004148 enum {
4149 EP_ObjCMethod = 1,
4150 EP_ObjCProperty
4151 };
4152
John McCalldc7c5ad2011-07-22 08:53:00 +00004153 SourceLocation loc = attr.getLoc();
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004154 QualType resultType;
4155
John McCalldc7c5ad2011-07-22 08:53:00 +00004156 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4157
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004158 if (!method) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004159 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4160 if (!property) {
4161 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4162 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4163 return;
4164 }
4165 resultType = property->getType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004166 }
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004167 else
4168 // Check that the method returns a normal pointer.
4169 resultType = method->getResultType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004170
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004171 if (!resultType->isReferenceType() &&
4172 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004173 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalldc7c5ad2011-07-22 08:53:00 +00004174 << SourceRange(loc)
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004175 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4176 << /*non-retainable pointer*/ 2;
John McCalldc7c5ad2011-07-22 08:53:00 +00004177
4178 // Drop the attribute.
4179 return;
4180 }
4181
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004182 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004183 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4184 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004185}
4186
Fariborz Jahanian84101132012-09-07 23:46:23 +00004187static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4188 const AttributeList &attr) {
4189 SourceLocation loc = attr.getLoc();
4190 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4191
4192 if (!method) {
4193 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4194 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4195 return;
4196 }
4197 DeclContext *DC = method->getDeclContext();
4198 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4199 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4200 << attr.getName() << 0;
4201 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4202 return;
4203 }
4204 if (method->getMethodFamily() == OMF_dealloc) {
4205 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4206 << attr.getName() << 1;
4207 return;
4208 }
4209
Michael Han51d8c522013-01-24 16:46:58 +00004210 method->addAttr(::new (S.Context)
4211 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4212 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004213}
4214
John McCall8dfac0b2011-09-30 05:12:12 +00004215/// Handle cf_audited_transfer and cf_unknown_transfer.
4216static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4217 if (!isa<FunctionDecl>(D)) {
4218 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004219 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004220 return;
4221 }
4222
Sean Hunt8e083e72012-06-19 23:57:03 +00004223 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004224
4225 // Check whether there's a conflicting attribute already present.
4226 Attr *Existing;
4227 if (IsAudited) {
4228 Existing = D->getAttr<CFUnknownTransferAttr>();
4229 } else {
4230 Existing = D->getAttr<CFAuditedTransferAttr>();
4231 }
4232 if (Existing) {
4233 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4234 << A.getName()
4235 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4236 << A.getRange() << Existing->getRange();
4237 return;
4238 }
4239
4240 // All clear; add the attribute.
4241 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004242 D->addAttr(::new (S.Context)
4243 CFAuditedTransferAttr(A.getRange(), S.Context,
4244 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004245 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004246 D->addAttr(::new (S.Context)
4247 CFUnknownTransferAttr(A.getRange(), S.Context,
4248 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004249 }
4250}
4251
John McCallfe98da02011-09-29 07:17:38 +00004252static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4253 const AttributeList &Attr) {
4254 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4255 if (!RD || RD->isUnion()) {
4256 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004257 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004258 }
4259
Aaron Ballman624421f2013-08-31 01:11:41 +00004260 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004261
4262 // In Objective-C, verify that the type names an Objective-C type.
4263 // We don't want to check this outside of ObjC because people sometimes
4264 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004265 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004266 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004267 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004268 Sema::LookupOrdinaryName);
4269 if (S.LookupName(R, Sc)) {
4270 NamedDecl *Target = R.getFoundDecl();
4271 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4272 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4273 S.Diag(Target->getLocStart(), diag::note_declared_at);
4274 }
4275 }
4276 }
4277
Michael Han51d8c522013-01-24 16:46:58 +00004278 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004279 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004280 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004281}
4282
Chandler Carruth1b03c872011-07-02 00:01:44 +00004283static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4284 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004285 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004286
Chandler Carruth87c44602011-07-01 23:49:12 +00004287 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004288 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004289}
4290
Chandler Carruth1b03c872011-07-02 00:01:44 +00004291static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4292 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004293 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004294 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004295 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004296 return;
4297 }
4298
Chandler Carruth87c44602011-07-01 23:49:12 +00004299 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004300 QualType type = vd->getType();
4301
4302 if (!type->isDependentType() &&
4303 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004304 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004305 << type;
4306 return;
4307 }
4308
4309 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4310
4311 // If we have no lifetime yet, check the lifetime we're presumably
4312 // going to infer.
4313 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4314 lifetime = type->getObjCARCImplicitLifetime();
4315
4316 switch (lifetime) {
4317 case Qualifiers::OCL_None:
4318 assert(type->isDependentType() &&
4319 "didn't infer lifetime for non-dependent type?");
4320 break;
4321
4322 case Qualifiers::OCL_Weak: // meaningful
4323 case Qualifiers::OCL_Strong: // meaningful
4324 break;
4325
4326 case Qualifiers::OCL_ExplicitNone:
4327 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004328 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004329 << (lifetime == Qualifiers::OCL_Autoreleasing);
4330 break;
4331 }
4332
Chandler Carruth87c44602011-07-01 23:49:12 +00004333 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004334 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4335 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004336}
4337
Francois Pichet11542142010-12-19 06:50:37 +00004338//===----------------------------------------------------------------------===//
4339// Microsoft specific attribute handlers.
4340//===----------------------------------------------------------------------===//
4341
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004342// Check if MS extensions or some other language extensions are enabled. If
4343// not, issue a diagnostic that the given attribute is unused.
4344static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4345 bool OtherExtension = false) {
4346 if (S.LangOpts.MicrosoftExt || OtherExtension)
4347 return true;
4348 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4349 return false;
4350}
4351
Chandler Carruth1b03c872011-07-02 00:01:44 +00004352static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004353 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4354 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004355
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004356 StringRef StrRef;
4357 SourceLocation LiteralLoc;
4358 if (!checkStringLiteralArgument(S, StrRef, Attr, 0, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004359 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004360
David Majnemerbb0ed292013-08-09 08:56:20 +00004361 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4362 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004363 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4364 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004365
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004366 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004367 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004368 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004369 return;
4370 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004371
David Majnemerbb0ed292013-08-09 08:56:20 +00004372 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004373 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004374 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004375 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004376 return;
4377 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004378 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004379 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004380 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004381 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004382 }
Francois Pichet11542142010-12-19 06:50:37 +00004383
David Majnemerbb0ed292013-08-09 08:56:20 +00004384 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4385 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004386}
4387
John McCallc052dbb2012-05-22 21:28:12 +00004388static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004389 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004390 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004391
4392 AttributeList::Kind Kind = Attr.getKind();
4393 if (Kind == AttributeList::AT_SingleInheritance)
4394 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004395 ::new (S.Context)
4396 SingleInheritanceAttr(Attr.getRange(), S.Context,
4397 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004398 else if (Kind == AttributeList::AT_MultipleInheritance)
4399 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004400 ::new (S.Context)
4401 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4402 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004403 else if (Kind == AttributeList::AT_VirtualInheritance)
4404 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004405 ::new (S.Context)
4406 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4407 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004408}
4409
4410static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004411 if (!checkMicrosoftExt(S, Attr))
4412 return;
4413
4414 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004415 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004416 D->addAttr(
4417 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4418 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004419}
4420
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004421static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004422 if (!checkMicrosoftExt(S, Attr))
4423 return;
4424 D->addAttr(::new (S.Context)
4425 ForceInlineAttr(Attr.getRange(), S.Context,
4426 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004427}
4428
Reid Klecknera7225342013-05-20 14:02:37 +00004429static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4430 if (!checkMicrosoftExt(S, Attr))
4431 return;
4432 // Check linkage after possibly merging declaratinos. See
4433 // checkAttributesAfterMerging().
4434 D->addAttr(::new (S.Context)
4435 SelectAnyAttr(Attr.getRange(), S.Context,
4436 Attr.getAttributeSpellingListIndex()));
4437}
4438
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004439/// Handles semantic checking for features that are common to all attributes,
4440/// such as checking whether a parameter was properly specified, or the correct
4441/// number of arguments were passed, etc.
4442static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4443 const AttributeList &Attr) {
4444 // Several attributes carry different semantics than the parsing requires, so
4445 // those are opted out of the common handling.
4446 //
4447 // We also bail on unknown and ignored attributes because those are handled
4448 // as part of the target-specific handling logic.
4449 if (Attr.hasCustomParsing() ||
4450 Attr.getKind() == AttributeList::UnknownAttribute ||
4451 Attr.getKind() == AttributeList::IgnoredAttribute)
4452 return false;
4453
4454 // If there are no optional arguments, then checking for the argument count
4455 // is trivial.
4456 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4457 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4458 return true;
4459 return false;
4460}
4461
Ted Kremenekb71368d2009-05-09 02:44:38 +00004462//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004463// Top Level Sema Entry Points
4464//===----------------------------------------------------------------------===//
4465
Richard Smith4a97b8e2013-08-29 00:47:48 +00004466/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4467/// the attribute applies to decls. If the attribute is a type attribute, just
4468/// silently ignore it if a GNU attribute.
4469static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4470 const AttributeList &Attr,
4471 bool IncludeCXX11Attributes) {
4472 if (Attr.isInvalid())
4473 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004474
Richard Smith4a97b8e2013-08-29 00:47:48 +00004475 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4476 // instead.
4477 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4478 return;
4479
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004480 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4481 return;
4482
Chris Lattner803d0802008-06-29 00:43:07 +00004483 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004484 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4485 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4486 case AttributeList::AT_IBOutletCollection:
4487 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004488 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004489 case AttributeList::AT_ObjCGC:
4490 case AttributeList::AT_VectorSize:
4491 case AttributeList::AT_NeonVectorType:
4492 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004493 case AttributeList::AT_Ptr32:
4494 case AttributeList::AT_Ptr64:
4495 case AttributeList::AT_SPtr:
4496 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004497 // Ignore these, these are type attributes, handled by
4498 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004499 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004500 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4501 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4502 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4503 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004504 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004505 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004506 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004507 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004508 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4509 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4510 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004511 handleDependencyAttr(S, scope, D, Attr);
4512 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004513 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4514 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4515 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004516 case AttributeList::AT_CXX11NoReturn:
4517 handleCXX11NoReturnAttr(S, D, Attr);
4518 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004519 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004520 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004521 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004522 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4523 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004524 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004525 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004526 case AttributeList::AT_MinSize:
4527 handleMinSizeAttr(S, D, Attr);
4528 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004529 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4530 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4531 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004532 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4533 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004534 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4535 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004536 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004537 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004538 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4539 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004540 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004541 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4542 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004543 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004544 case AttributeList::AT_ownership_returns:
4545 case AttributeList::AT_ownership_takes:
4546 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004547 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004548 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4549 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4550 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4551 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4552 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4553 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4554 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004555
Sean Hunt8e083e72012-06-19 23:57:03 +00004556 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004557 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004558 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004559 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004560
Sean Hunt8e083e72012-06-19 23:57:03 +00004561 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004562 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4563
Fariborz Jahanian84101132012-09-07 23:46:23 +00004564 case AttributeList::AT_ObjCRequiresSuper:
4565 handleObjCRequiresSuperAttr(S, D, Attr); break;
4566
Sean Hunt8e083e72012-06-19 23:57:03 +00004567 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004568 handleNSBridgedAttr(S, scope, D, Attr); break;
4569
Sean Hunt8e083e72012-06-19 23:57:03 +00004570 case AttributeList::AT_CFAuditedTransfer:
4571 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004572 handleCFTransferAttr(S, D, Attr); break;
4573
Ted Kremenekb71368d2009-05-09 02:44:38 +00004574 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004575 case AttributeList::AT_CFConsumed:
4576 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4577 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004578 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004579
Sean Hunt8e083e72012-06-19 23:57:03 +00004580 case AttributeList::AT_NSReturnsAutoreleased:
4581 case AttributeList::AT_NSReturnsNotRetained:
4582 case AttributeList::AT_CFReturnsNotRetained:
4583 case AttributeList::AT_NSReturnsRetained:
4584 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004585 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004586
Tanya Lattner0df579e2012-07-09 22:06:01 +00004587 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004588 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004589 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004590
Joey Gouly37453b92013-03-08 09:42:32 +00004591 case AttributeList::AT_VecTypeHint:
4592 handleVecTypeHint(S, D, Attr); break;
4593
Sean Hunt8e083e72012-06-19 23:57:03 +00004594 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004595 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004596
Sean Hunt8e083e72012-06-19 23:57:03 +00004597 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4598 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4599 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004600 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004601 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004602 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004603 handleArcWeakrefUnavailableAttr (S, D, Attr);
4604 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004605 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004606 handleObjCRootClassAttr(S, D, Attr);
4607 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004608 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004609 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004610 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004611 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4612 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004613 handleReturnsTwiceAttr(S, D, Attr);
4614 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004615 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004616 case AttributeList::AT_Visibility:
4617 handleVisibilityAttr(S, D, Attr, false);
4618 break;
4619 case AttributeList::AT_TypeVisibility:
4620 handleVisibilityAttr(S, D, Attr, true);
4621 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004622 case AttributeList::AT_WarnUnused:
4623 handleWarnUnusedAttr(S, D, Attr);
4624 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004625 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004626 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004627 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4628 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4629 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4630 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004631 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004632 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004633 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004634 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004635 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004636 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004637 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004638 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004639 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4640 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4641 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4642 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4643 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4644 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4645 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4646 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4647 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004648 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004649 // Just ignore
4650 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004651 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004652 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004653 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004654 case AttributeList::AT_StdCall:
4655 case AttributeList::AT_CDecl:
4656 case AttributeList::AT_FastCall:
4657 case AttributeList::AT_ThisCall:
4658 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004659 case AttributeList::AT_MSABI:
4660 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004661 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004662 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004663 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004664 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004665 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004666 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004667 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004668 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004669 case AttributeList::AT_OpenCLImageAccess:
4670 handleOpenCLImageAccessAttr(S, D, Attr);
4671 break;
John McCallc052dbb2012-05-22 21:28:12 +00004672
4673 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004674 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004675 handleMsStructAttr(S, D, Attr);
4676 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004677 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004678 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004679 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004680 case AttributeList::AT_SingleInheritance:
4681 case AttributeList::AT_MultipleInheritance:
4682 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004683 handleInheritanceAttr(S, D, Attr);
4684 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004685 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004686 handlePortabilityAttr(S, D, Attr);
4687 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004688 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004689 handleForceInlineAttr(S, D, Attr);
4690 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004691 case AttributeList::AT_SelectAny:
4692 handleSelectAnyAttr(S, D, Attr);
4693 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004694
4695 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004696 case AttributeList::AT_AssertExclusiveLock:
4697 handleAssertExclusiveLockAttr(S, D, Attr);
4698 break;
4699 case AttributeList::AT_AssertSharedLock:
4700 handleAssertSharedLockAttr(S, D, Attr);
4701 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004702 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004703 handleGuardedVarAttr(S, D, Attr);
4704 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004705 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004706 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004707 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004708 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004709 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004710 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004711 case AttributeList::AT_NoSanitizeAddress:
4712 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004713 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004714 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004715 handleNoThreadSafetyAnalysis(S, D, Attr);
4716 break;
4717 case AttributeList::AT_NoSanitizeThread:
4718 handleNoSanitizeThread(S, D, Attr);
4719 break;
4720 case AttributeList::AT_NoSanitizeMemory:
4721 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004722 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004723 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004724 handleLockableAttr(S, D, Attr);
4725 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004726 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004727 handleGuardedByAttr(S, D, Attr);
4728 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004729 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004730 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004731 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004732 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004733 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004734 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004735 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004736 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004737 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004738 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004739 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004740 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004741 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004742 handleLockReturnedAttr(S, D, Attr);
4743 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004744 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004745 handleLocksExcludedAttr(S, D, Attr);
4746 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004747 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004748 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004749 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004750 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004751 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004752 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004753 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004754 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004755 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004757 handleUnlockFunAttr(S, D, Attr);
4758 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004759 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004760 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004761 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004762 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004763 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004764 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004765
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004766 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004767 case AttributeList::AT_Consumable:
4768 handleConsumableAttr(S, D, Attr);
4769 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004770 case AttributeList::AT_Consumes:
4771 handleConsumesAttr(S, D, Attr);
4772 break;
4773 case AttributeList::AT_CallableWhenUnconsumed:
4774 handleCallableWhenUnconsumedAttr(S, D, Attr);
4775 break;
4776 case AttributeList::AT_TestsConsumed:
4777 handleTestsConsumedAttr(S, D, Attr);
4778 break;
4779 case AttributeList::AT_TestsUnconsumed:
4780 handleTestsUnconsumedAttr(S, D, Attr);
4781 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004782 case AttributeList::AT_ReturnTypestate:
4783 handleReturnTypestateAttr(S, D, Attr);
4784 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004785
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004786 // Type safety attributes.
4787 case AttributeList::AT_ArgumentWithTypeTag:
4788 handleArgumentWithTypeTagAttr(S, D, Attr);
4789 break;
4790 case AttributeList::AT_TypeTagForDatatype:
4791 handleTypeTagForDatatypeAttr(S, D, Attr);
4792 break;
4793
Chris Lattner803d0802008-06-29 00:43:07 +00004794 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004795 // Ask target about the attribute.
4796 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4797 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004798 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4799 diag::warn_unhandled_ms_attribute_ignored :
4800 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004801 break;
4802 }
4803}
4804
4805/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4806/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004807void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004808 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004809 bool IncludeCXX11Attributes) {
4810 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004811 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004812
4813 // GCC accepts
4814 // static int a9 __attribute__((weakref));
4815 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004816 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004817 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004818 cast<NamedDecl>(D)->getNameAsString();
4819 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004820 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004821 }
4822}
4823
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004824// Annotation attributes are the only attributes allowed after an access
4825// specifier.
4826bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4827 const AttributeList *AttrList) {
4828 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004830 handleAnnotateAttr(*this, ASDecl, *l);
4831 } else {
4832 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4833 return true;
4834 }
4835 }
4836
4837 return false;
4838}
4839
John McCalle82247a2011-10-01 05:17:03 +00004840/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4841/// contains any decl attributes that we should warn about.
4842static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4843 for ( ; A; A = A->getNext()) {
4844 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004845 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004846 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4847
4848 if (A->getKind() == AttributeList::UnknownAttribute) {
4849 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4850 << A->getName() << A->getRange();
4851 } else {
4852 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4853 << A->getName() << A->getRange();
4854 }
4855 }
4856}
4857
4858/// checkUnusedDeclAttributes - Given a declarator which is not being
4859/// used to build a declaration, complain about any decl attributes
4860/// which might be lying around on it.
4861void Sema::checkUnusedDeclAttributes(Declarator &D) {
4862 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4863 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4864 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4865 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4866}
4867
Ryan Flynne25ff832009-07-30 03:15:39 +00004868/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004869/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004870NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4871 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004872 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004873 NamedDecl *NewD = 0;
4874 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004875 FunctionDecl *NewFD;
4876 // FIXME: Missing call to CheckFunctionDeclaration().
4877 // FIXME: Mangling?
4878 // FIXME: Is the qualifier info correct?
4879 // FIXME: Is the DeclContext correct?
4880 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4881 Loc, Loc, DeclarationName(II),
4882 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004883 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004884 FD->hasPrototype(),
4885 false/*isConstexprSpecified*/);
4886 NewD = NewFD;
4887
4888 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004889 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004890
4891 // Fake up parameter variables; they are declared as if this were
4892 // a typedef.
4893 QualType FDTy = FD->getType();
4894 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4895 SmallVector<ParmVarDecl*, 16> Params;
4896 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4897 AE = FT->arg_type_end(); AI != AE; ++AI) {
4898 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4899 Param->setScopeInfo(0, Params.size());
4900 Params.push_back(Param);
4901 }
David Blaikie4278c652011-09-21 18:16:56 +00004902 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004903 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004904 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4905 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004906 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004907 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004908 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00004909 if (VD->getQualifier()) {
4910 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004911 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004912 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004913 }
4914 return NewD;
4915}
4916
James Dennett1dfbd922012-06-14 21:40:34 +00004917/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004918/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004919void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004920 if (W.getUsed()) return; // only do this once
4921 W.setUsed(true);
4922 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4923 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004924 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004925 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4926 NDId->getName()));
4927 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004928 WeakTopLevelDecl.push_back(NewD);
4929 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4930 // to insert Decl at TU scope, sorry.
4931 DeclContext *SavedContext = CurContext;
4932 CurContext = Context.getTranslationUnitDecl();
4933 PushOnScopeChains(NewD, S);
4934 CurContext = SavedContext;
4935 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004936 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004937 }
4938}
4939
Rafael Espindola65611bf2013-03-02 21:41:48 +00004940void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4941 // It's valid to "forward-declare" #pragma weak, in which case we
4942 // have to do this.
4943 LoadExternalWeakUndeclaredIdentifiers();
4944 if (!WeakUndeclaredIdentifiers.empty()) {
4945 NamedDecl *ND = NULL;
4946 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4947 if (VD->isExternC())
4948 ND = VD;
4949 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4950 if (FD->isExternC())
4951 ND = FD;
4952 if (ND) {
4953 if (IdentifierInfo *Id = ND->getIdentifier()) {
4954 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4955 = WeakUndeclaredIdentifiers.find(Id);
4956 if (I != WeakUndeclaredIdentifiers.end()) {
4957 WeakInfo W = I->second;
4958 DeclApplyPragmaWeak(S, ND, W);
4959 WeakUndeclaredIdentifiers[Id] = W;
4960 }
4961 }
4962 }
4963 }
4964}
4965
Chris Lattner0744e5f2008-06-29 00:23:49 +00004966/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4967/// it, apply them to D. This is a bit tricky because PD can have attributes
4968/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004969void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00004970 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004971 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004972 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00004973
Chris Lattner0744e5f2008-06-29 00:23:49 +00004974 // Walk the declarator structure, applying decl attributes that were in a type
4975 // position to the decl itself. This handles cases like:
4976 // int *__attr__(x)** D;
4977 // when X is a decl attribute.
4978 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4979 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004980 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00004981
Chris Lattner0744e5f2008-06-29 00:23:49 +00004982 // Finally, apply any attributes on the decl itself.
4983 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004984 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004985}
John McCall54abf7d2009-11-04 02:18:39 +00004986
John McCallf85e1932011-06-15 23:02:42 +00004987/// Is the given declaration allowed to use a forbidden type?
4988static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4989 // Private ivars are always okay. Unfortunately, people don't
4990 // always properly make their ivars private, even in system headers.
4991 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004992 // Function declarations in sys headers will be marked unavailable.
4993 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4994 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004995 return false;
4996
4997 // Require it to be declared in a system header.
4998 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4999}
5000
5001/// Handle a delayed forbidden-type diagnostic.
5002static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5003 Decl *decl) {
5004 if (decl && isForbiddenTypeAllowed(S, decl)) {
5005 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5006 "this system declaration uses an unsupported type"));
5007 return;
5008 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005009 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005010 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005011 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005012 // kind of forbidden type messages on unavailable functions.
5013 if (FD->hasAttr<UnavailableAttr>() &&
5014 diag.getForbiddenTypeDiagnostic() ==
5015 diag::err_arc_array_param_no_ownership) {
5016 diag.Triggered = true;
5017 return;
5018 }
5019 }
John McCallf85e1932011-06-15 23:02:42 +00005020
5021 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5022 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5023 diag.Triggered = true;
5024}
5025
John McCall92576642012-05-07 06:16:41 +00005026void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5027 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005028 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005029 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005030
John McCall92576642012-05-07 06:16:41 +00005031 // When delaying diagnostics to run in the context of a parsed
5032 // declaration, we only want to actually emit anything if parsing
5033 // succeeds.
5034 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005035
John McCall92576642012-05-07 06:16:41 +00005036 // We emit all the active diagnostics in this pool or any of its
5037 // parents. In general, we'll get one pool for the decl spec
5038 // and a child pool for each declarator; in a decl group like:
5039 // deprecated_typedef foo, *bar, baz();
5040 // only the declarator pops will be passed decls. This is correct;
5041 // we really do need to consider delayed diagnostics from the decl spec
5042 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005043 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005044 do {
John McCall13489672012-05-07 06:16:58 +00005045 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005046 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5047 // This const_cast is a bit lame. Really, Triggered should be mutable.
5048 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005049 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005050 continue;
5051
John McCalleee1d542011-02-14 07:13:47 +00005052 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005053 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005054 // Don't bother giving deprecation diagnostics if the decl is invalid.
5055 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005056 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005057 break;
5058
5059 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005060 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005061 break;
John McCallf85e1932011-06-15 23:02:42 +00005062
5063 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005064 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005065 break;
John McCall2f514482010-01-27 03:50:35 +00005066 }
5067 }
John McCall92576642012-05-07 06:16:41 +00005068 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005069}
5070
John McCall13489672012-05-07 06:16:58 +00005071/// Given a set of delayed diagnostics, re-emit them as if they had
5072/// been delayed in the current context instead of in the given pool.
5073/// Essentially, this just moves them to the current pool.
5074void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5075 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5076 assert(curPool && "re-emitting in undelayed context not supported");
5077 curPool->steal(pool);
5078}
5079
John McCall54abf7d2009-11-04 02:18:39 +00005080static bool isDeclDeprecated(Decl *D) {
5081 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005082 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005083 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005084 // A category implicitly has the availability of the interface.
5085 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5086 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005087 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5088 return false;
5089}
5090
Eli Friedmanc3b23082012-08-08 21:52:41 +00005091static void
5092DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5093 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005094 const ObjCInterfaceDecl *UnknownObjCClass,
5095 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005096 DeclarationName Name = D->getDeclName();
5097 if (!Message.empty()) {
5098 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5099 S.Diag(D->getLocation(),
5100 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5101 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005102 if (ObjCPropery)
5103 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5104 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005105 } else if (!UnknownObjCClass) {
5106 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5107 S.Diag(D->getLocation(),
5108 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5109 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005110 if (ObjCPropery)
5111 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5112 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005113 } else {
5114 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5115 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5116 }
5117}
5118
John McCall9c3087b2010-08-26 02:13:20 +00005119void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005120 Decl *Ctx) {
5121 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005122 return;
5123
John McCall2f514482010-01-27 03:50:35 +00005124 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005125 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5126 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005127 DD.getUnknownObjCClass(),
5128 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005129}
5130
Chris Lattner5f9e2722011-07-23 10:55:15 +00005131void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005132 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005133 const ObjCInterfaceDecl *UnknownObjCClass,
5134 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005135 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005136 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005137 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5138 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005139 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005140 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005141 return;
5142 }
5143
5144 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005145 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005146 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005147 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005148}