blob: 2e97a8aa9d4a89f650676ac4952ff138fcb0c06c [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000035namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000036 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000037 C,
38 Cpp,
39 ObjC
40 };
41}
42
Chris Lattner58418ff2008-06-29 00:16:31 +000043//===----------------------------------------------------------------------===//
44// Helper functions
45//===----------------------------------------------------------------------===//
46
Ted Kremenek527042b2009-08-14 20:49:40 +000047/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000048/// type (function or function-typed variable) or an Objective-C
49/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000050static bool isFunctionOrMethod(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000051 return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000052}
53
John McCall3882ace2011-01-05 12:14:39 +000054/// Return true if the given decl has a declarator that should have
55/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000056static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000057 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
59 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000060}
61
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000062/// hasFunctionProto - Return true if the given decl has a argument
63/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000066 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000067 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000068 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069}
70
Alp Toker601b22c2014-01-21 23:35:24 +000071/// getFunctionOrMethodNumParams - Return number of function or method
72/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000073/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000074static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000075 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000076 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000078 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000080}
81
Alp Toker601b22c2014-01-21 23:35:24 +000082static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000083 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000084 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000086 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000087
Chandler Carruthff4c4f02011-07-01 23:49:12 +000088 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000089}
90
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000092 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker314cc812014-01-25 16:55:45 +000093 return cast<FunctionProtoType>(FnTy)->getReturnType();
94 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000095}
96
Chandler Carruthff4c4f02011-07-01 23:49:12 +000097static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000098 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +000099 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000100 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000102 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000103 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000105 }
106}
107
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static bool isInstanceMethod(const Decl *D) {
109 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000110 return MethodDecl->isInstance();
111 return false;
112}
113
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000114static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000115 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000116 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000117 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000118
John McCall96fa4842010-05-17 21:00:27 +0000119 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
120 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000121 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000122
John McCall96fa4842010-05-17 21:00:27 +0000123 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000124
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000125 // FIXME: Should we walk the chain of classes?
126 return ClsName == &Ctx.Idents.get("NSString") ||
127 ClsName == &Ctx.Idents.get("NSMutableString");
128}
129
Daniel Dunbar980c6692008-09-26 03:32:58 +0000130static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000131 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000132 if (!PT)
133 return false;
134
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000135 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000136 if (!RT)
137 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Daniel Dunbar980c6692008-09-26 03:32:58 +0000139 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000140 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000141 return false;
142
143 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
144}
145
Richard Smithb87c4652013-10-31 21:23:20 +0000146static unsigned getNumAttributeArgs(const AttributeList &Attr) {
147 // FIXME: Include the type in the argument list.
148 return Attr.getNumArgs() + Attr.hasParsedType();
149}
150
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000151/// \brief Check if the attribute has exactly as many args as Num. May
152/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000153static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000154 unsigned Num) {
155 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
157 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000158 return false;
159 }
160
161 return true;
162}
163
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000164/// \brief Check if the attribute has at least as many args as Num. May
165/// output an error.
166static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000167 unsigned Num) {
168 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000169 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
170 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000171 return false;
172 }
173
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000174 return true;
175}
176
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000177/// \brief If Expr is a valid integer constant, get the value of the integer
178/// expression and return success or failure. May output an error.
179static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
180 const Expr *Expr, uint32_t &Val,
181 unsigned Idx = UINT_MAX) {
182 llvm::APSInt I(32);
183 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
184 !Expr->isIntegerConstantExpr(I, S.Context)) {
185 if (Idx != UINT_MAX)
186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
187 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
188 << Expr->getSourceRange();
189 else
190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
191 << Attr.getName() << AANT_ArgumentIntegerConstant
192 << Expr->getSourceRange();
193 return false;
194 }
195 Val = (uint32_t)I.getZExtValue();
196 return true;
197}
198
Aaron Ballmanfb763042013-12-02 18:05:46 +0000199/// \brief Diagnose mutually exclusive attributes when present on a given
200/// declaration. Returns true if diagnosed.
201template <typename AttrTy>
202static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000203 const AttributeList &Attr) {
204 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000205 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000206 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000207 return true;
208 }
209 return false;
210}
211
Alp Toker601b22c2014-01-21 23:35:24 +0000212/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000213/// instance method D. May output an error.
214///
215/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000216static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
217 const AttributeList &Attr,
218 unsigned AttrArgNum,
219 const Expr *IdxExpr,
220 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000221 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000222
223 // In C++ the implicit 'this' function parameter also counts.
224 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000225 bool HP = hasFunctionProto(D);
226 bool HasImplicitThisParam = isInstanceMethod(D);
227 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000228 unsigned NumParams =
229 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000230
231 llvm::APSInt IdxInt;
232 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
233 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
235 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
236 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000237 return false;
238 }
239
240 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000241 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
243 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000244 return false;
245 }
246 Idx--; // Convert to zero-based.
247 if (HasImplicitThisParam) {
248 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000249 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000250 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000251 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000252 return false;
253 }
254 --Idx;
255 }
256
257 return true;
258}
259
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000260/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
261/// If not emit an error and return false. If the argument is an identifier it
262/// will emit an error with a fixit hint and treat it as if it was a string
263/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000264bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
265 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000266 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000267 // Look for identifiers. If we have one emit a hint to fix it to a literal.
268 if (Attr.isArgIdent(ArgNum)) {
269 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000270 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000271 << Attr.getName() << AANT_ArgumentString
272 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000273 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000274 Str = Loc->Ident->getName();
275 if (ArgLocation)
276 *ArgLocation = Loc->Loc;
277 return true;
278 }
279
280 // Now check for an actual string literal.
281 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
282 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
283 if (ArgLocation)
284 *ArgLocation = ArgExpr->getLocStart();
285
286 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000287 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000288 << Attr.getName() << AANT_ArgumentString;
289 return false;
290 }
291
292 Str = Literal->getString();
293 return true;
294}
295
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000296/// \brief Applies the given attribute to the Decl without performing any
297/// additional semantic checking.
298template <typename AttrType>
299static void handleSimpleAttribute(Sema &S, Decl *D,
300 const AttributeList &Attr) {
301 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
302 Attr.getAttributeSpellingListIndex()));
303}
304
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000305/// \brief Check if the passed-in expression is of type int or bool.
306static bool isIntOrBool(Expr *Exp) {
307 QualType QT = Exp->getType();
308 return QT->isBooleanType() || QT->isIntegerType();
309}
310
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000311
312// Check to see if the type is a smart pointer of some kind. We assume
313// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000314static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
315 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
316 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000317 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000318 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000319
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000320 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
321 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000322 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000323 return false;
324
325 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000326}
327
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000328/// \brief Check if passed in Decl is a pointer type.
329/// Note that this function may produce an error message.
330/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000331static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
332 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000333 const ValueDecl *vd = cast<ValueDecl>(D);
334 QualType QT = vd->getType();
335 if (QT->isAnyPointerType())
336 return true;
337
338 if (const RecordType *RT = QT->getAs<RecordType>()) {
339 // If it's an incomplete type, it could be a smart pointer; skip it.
340 // (We don't want to force template instantiation if we can avoid it,
341 // since that would alter the order in which templates are instantiated.)
342 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000343 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000344
Aaron Ballman553e6812013-12-26 14:54:11 +0000345 if (threadSafetyCheckIsSmartPointer(S, RT))
346 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000347 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000348
349 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000350 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000351 return false;
352}
353
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000354/// \brief Checks that the passed in QualType either is of RecordType or points
355/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000356static const RecordType *getRecordType(QualType QT) {
357 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000358 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000359
360 // Now check if we point to record type.
361 if (const PointerType *PT = QT->getAs<PointerType>())
362 return PT->getPointeeType()->getAs<RecordType>();
363
364 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000365}
366
Aaron Ballman76050722014-04-04 15:13:57 +0000367static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000368 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000369
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000370 if (!RT)
371 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000372
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000373 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000374 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000375 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000376
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000377 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378 // FIXME -- Check the type that the smart pointer points to.
379 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000380 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000381
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000382 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000383 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000384 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000385 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000386
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000387 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000388 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
389 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000390 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
391 void *) {
392 return BS->getType()->getAs<RecordType>()
393 ->getDecl()->hasAttr<CapabilityAttr>();
394 }, 0, BPaths))
395 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000396 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000397 return false;
398}
399
Aaron Ballman76050722014-04-04 15:13:57 +0000400static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000401 const auto *TD = Ty->getAs<TypedefType>();
402 if (!TD)
403 return false;
404
405 TypedefNameDecl *TN = TD->getDecl();
406 if (!TN)
407 return false;
408
409 return TN->hasAttr<CapabilityAttr>();
410}
411
Aaron Ballman76050722014-04-04 15:13:57 +0000412static bool typeHasCapability(Sema &S, QualType Ty) {
413 if (checkTypedefTypeForCapability(Ty))
414 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000415
Aaron Ballman76050722014-04-04 15:13:57 +0000416 if (checkRecordTypeForCapability(S, Ty))
417 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000418
Aaron Ballman76050722014-04-04 15:13:57 +0000419 return false;
420}
421
422static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
423 // Capability expressions are simple expressions involving the boolean logic
424 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
425 // a DeclRefExpr is found, its type should be checked to determine whether it
426 // is a capability or not.
427
428 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
429 return typeHasCapability(S, E->getType());
430 else if (const auto *E = dyn_cast<CastExpr>(Ex))
431 return isCapabilityExpr(S, E->getSubExpr());
432 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
433 return isCapabilityExpr(S, E->getSubExpr());
434 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
435 if (E->getOpcode() == UO_LNot)
436 return isCapabilityExpr(S, E->getSubExpr());
437 return false;
438 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
439 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
440 return isCapabilityExpr(S, E->getLHS()) &&
441 isCapabilityExpr(S, E->getRHS());
442 return false;
443 }
444
445 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000446}
447
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000448/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
449/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000450/// \param Sidx The attribute argument index to start checking with.
451/// \param ParamIdxOk Whether an argument can be indexing into a function
452/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000453static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
454 const AttributeList &Attr,
455 SmallVectorImpl<Expr *> &Args,
456 int Sidx = 0,
457 bool ParamIdxOk = false) {
458 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000459 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000461 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000462 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000463 Args.push_back(ArgExp);
464 continue;
465 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000467 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000468 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000469 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000470 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000471 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000472 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000473 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000474 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000475
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000476 // We allow constant strings to be used as a placeholder for expressions
477 // that are not valid C++ syntax, but warn that they are ignored.
478 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
479 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000480 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000481 continue;
482 }
483
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000484 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000485
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000486 // A pointer to member expression of the form &MyClass::mu is treated
487 // specially -- we need to look at the type of the member.
488 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
489 if (UOp->getOpcode() == UO_AddrOf)
490 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
491 if (DRE->getDecl()->isCXXInstanceMember())
492 ArgTy = DRE->getDecl()->getType();
493
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000494 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000495 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000496
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000497 // Now check if we index into a record type function param.
498 if(!RT && ParamIdxOk) {
499 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000500 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
501 if(FD && IL) {
502 unsigned int NumParams = FD->getNumParams();
503 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
505 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
506 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
508 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000509 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000510 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000511 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000512 }
513 }
514
Aaron Ballman76050722014-04-04 15:13:57 +0000515 // If the type does not have a capability, see if the components of the
516 // expression have capabilities. This allows for writing C code where the
517 // capability may be on the type, and the expression is a capability
518 // boolean logic expression. Eg) requires_capability(A || B && !C)
519 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
520 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
521 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000522
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000523 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525}
526
Chris Lattner58418ff2008-06-29 00:16:31 +0000527//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000528// Attribute Implementations
529//===----------------------------------------------------------------------===//
530
Daniel Dunbar032db472008-07-31 22:40:48 +0000531// FIXME: All this manual attribute parsing code is gross. At the
532// least add some helper functions to check most argument patterns (#
533// and types of args).
534
Michael Hana9171bc2012-08-03 17:40:43 +0000535static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000536 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000537 if (!threadSafetyCheckIsPointer(S, D, Attr))
538 return;
539
Michael Han99315932013-01-24 16:46:58 +0000540 D->addAttr(::new (S.Context)
541 PtGuardedVarAttr(Attr.getRange(), S.Context,
542 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000543}
544
Michael Hana9171bc2012-08-03 17:40:43 +0000545static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
546 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000547 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 SmallVector<Expr*, 1> Args;
549 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000550 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000551 unsigned Size = Args.size();
552 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000553 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000554
Michael Han3be3b442012-07-23 18:48:41 +0000555 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000556
Michael Han3be3b442012-07-23 18:48:41 +0000557 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000558}
559
Michael Han3be3b442012-07-23 18:48:41 +0000560static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
561 Expr *Arg = 0;
562 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
563 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000564
Aaron Ballman36a53502014-01-16 13:03:14 +0000565 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
566 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000567}
568
Michael Hana9171bc2012-08-03 17:40:43 +0000569static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000570 const AttributeList &Attr) {
571 Expr *Arg = 0;
572 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
573 return;
574
575 if (!threadSafetyCheckIsPointer(S, D, Attr))
576 return;
577
578 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000579 S.Context, Arg,
580 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000581}
582
Michael Hana9171bc2012-08-03 17:40:43 +0000583static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
584 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000585 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000586 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000587 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000588
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000589 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000590 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591 if (!QT->isDependentType()) {
592 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000593 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000594 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000595 << Attr.getName();
596 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597 }
598 }
599
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000600 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000601 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000602 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000603 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000604
Michael Han3be3b442012-07-23 18:48:41 +0000605 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000606}
607
Michael Hana9171bc2012-08-03 17:40:43 +0000608static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000609 const AttributeList &Attr) {
610 SmallVector<Expr*, 1> Args;
611 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
612 return;
613
614 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000615 D->addAttr(::new (S.Context)
616 AcquiredAfterAttr(Attr.getRange(), S.Context,
617 StartArg, Args.size(),
618 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000619}
620
Michael Hana9171bc2012-08-03 17:40:43 +0000621static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000622 const AttributeList &Attr) {
623 SmallVector<Expr*, 1> Args;
624 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
625 return;
626
627 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000628 D->addAttr(::new (S.Context)
629 AcquiredBeforeAttr(Attr.getRange(), S.Context,
630 StartArg, Args.size(),
631 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000632}
633
Michael Hana9171bc2012-08-03 17:40:43 +0000634static bool checkLockFunAttrCommon(Sema &S, Decl *D,
635 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000636 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000637 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000638 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000639 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000640
Michael Han3be3b442012-07-23 18:48:41 +0000641 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000642}
643
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000644static void handleAssertSharedLockAttr(Sema &S, Decl *D,
645 const AttributeList &Attr) {
646 SmallVector<Expr*, 1> Args;
647 if (!checkLockFunAttrCommon(S, D, Attr, Args))
648 return;
649
650 unsigned Size = Args.size();
651 Expr **StartArg = Size == 0 ? 0 : &Args[0];
652 D->addAttr(::new (S.Context)
653 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
654 Attr.getAttributeSpellingListIndex()));
655}
656
657static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
658 const AttributeList &Attr) {
659 SmallVector<Expr*, 1> Args;
660 if (!checkLockFunAttrCommon(S, D, Attr, Args))
661 return;
662
663 unsigned Size = Args.size();
664 Expr **StartArg = Size == 0 ? 0 : &Args[0];
665 D->addAttr(::new (S.Context)
666 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
667 StartArg, Size,
668 Attr.getAttributeSpellingListIndex()));
669}
670
671
Michael Hana9171bc2012-08-03 17:40:43 +0000672static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
673 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000674 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000675 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000676 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000677
Aaron Ballman00e99962013-08-31 01:11:41 +0000678 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000680 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000681 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000682 }
683
684 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000685 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000686
Michael Han3be3b442012-07-23 18:48:41 +0000687 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000688}
689
Michael Hana9171bc2012-08-03 17:40:43 +0000690static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000691 const AttributeList &Attr) {
692 SmallVector<Expr*, 2> Args;
693 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
694 return;
695
Michael Han99315932013-01-24 16:46:58 +0000696 D->addAttr(::new (S.Context)
697 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000698 Attr.getArgAsExpr(0),
699 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000700 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000701}
702
Michael Hana9171bc2012-08-03 17:40:43 +0000703static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000704 const AttributeList &Attr) {
705 SmallVector<Expr*, 2> Args;
706 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
707 return;
708
Michael Han99315932013-01-24 16:46:58 +0000709 D->addAttr(::new (S.Context)
710 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000711 Attr.getArgAsExpr(0),
712 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000713 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000714}
715
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000716static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000717 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000718 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000719 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000720 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000721 unsigned Size = Args.size();
722 if (Size == 0)
723 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000724
Michael Han99315932013-01-24 16:46:58 +0000725 D->addAttr(::new (S.Context)
726 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
727 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000728}
729
730static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000731 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000732 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000733 return;
734
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000735 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000736 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000737 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000738 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000739 if (Size == 0)
740 return;
741 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000742
Michael Han99315932013-01-24 16:46:58 +0000743 D->addAttr(::new (S.Context)
744 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
745 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000746}
747
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000748static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
749 Expr *Cond = Attr.getArgAsExpr(0);
750 if (!Cond->isTypeDependent()) {
751 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
752 if (Converted.isInvalid())
753 return;
754 Cond = Converted.take();
755 }
756
757 StringRef Msg;
758 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
759 return;
760
761 SmallVector<PartialDiagnosticAt, 8> Diags;
762 if (!Cond->isValueDependent() &&
763 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
764 Diags)) {
765 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
766 for (int I = 0, N = Diags.size(); I != N; ++I)
767 S.Diag(Diags[I].first, Diags[I].second);
768 return;
769 }
770
771 D->addAttr(::new (S.Context)
772 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
773 Attr.getAttributeSpellingListIndex()));
774}
775
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000776static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000777 ConsumableAttr::ConsumedState DefaultState;
778
779 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000780 IdentifierLoc *IL = Attr.getArgAsIdent(0);
781 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
782 DefaultState)) {
783 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
784 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000785 return;
786 }
David Blaikie16f76d22013-09-06 01:28:43 +0000787 } else {
788 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
789 << Attr.getName() << AANT_ArgumentIdentifier;
790 return;
791 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000792
793 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000794 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000795 Attr.getAttributeSpellingListIndex()));
796}
797
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000798
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000799static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
800 const AttributeList &Attr) {
801 ASTContext &CurrContext = S.getASTContext();
802 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
803
804 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
805 if (!RD->hasAttr<ConsumableAttr>()) {
806 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
807 RD->getNameAsString();
808
809 return false;
810 }
811 }
812
813 return true;
814}
815
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000816
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000817static void handleCallableWhenAttr(Sema &S, Decl *D,
818 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000819 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
820 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000821
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000822 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
823 return;
824
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000825 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
826 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
827 CallableWhenAttr::ConsumedState CallableState;
828
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000829 StringRef StateString;
830 SourceLocation Loc;
831 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
832 return;
833
834 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000835 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000836 S.Diag(Loc, diag::warn_attribute_type_not_supported)
837 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000838 return;
839 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000840
841 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000842 }
843
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000844 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000845 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
846 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000847}
848
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000849
DeLesley Hutchins69391772013-10-17 23:23:53 +0000850static void handleParamTypestateAttr(Sema &S, Decl *D,
851 const AttributeList &Attr) {
852 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000853
DeLesley Hutchins69391772013-10-17 23:23:53 +0000854 ParamTypestateAttr::ConsumedState ParamState;
855
856 if (Attr.isArgIdent(0)) {
857 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
858 StringRef StateString = Ident->Ident->getName();
859
860 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
861 ParamState)) {
862 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
863 << Attr.getName() << StateString;
864 return;
865 }
866 } else {
867 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
868 Attr.getName() << AANT_ArgumentIdentifier;
869 return;
870 }
871
872 // FIXME: This check is currently being done in the analysis. It can be
873 // enabled here only after the parser propagates attributes at
874 // template specialization definition, not declaration.
875 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
876 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
877 //
878 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
879 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
880 // ReturnType.getAsString();
881 // return;
882 //}
883
884 D->addAttr(::new (S.Context)
885 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
886 Attr.getAttributeSpellingListIndex()));
887}
888
889
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000890static void handleReturnTypestateAttr(Sema &S, Decl *D,
891 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000892 if (!checkAttributeNumArgs(S, Attr, 1)) return;
893
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000894 ReturnTypestateAttr::ConsumedState ReturnState;
895
896 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000897 IdentifierLoc *IL = Attr.getArgAsIdent(0);
898 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
899 ReturnState)) {
900 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
901 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000902 return;
903 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000904 } else {
905 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
906 Attr.getName() << AANT_ArgumentIdentifier;
907 return;
908 }
909
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000910 // FIXME: This check is currently being done in the analysis. It can be
911 // enabled here only after the parser propagates attributes at
912 // template specialization definition, not declaration.
913 //QualType ReturnType;
914 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000915 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
916 // ReturnType = Param->getType();
917 //
918 //} else if (const CXXConstructorDecl *Constructor =
919 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000920 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
921 //
922 //} else {
923 //
924 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
925 //}
926 //
927 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
928 //
929 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
930 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
931 // ReturnType.getAsString();
932 // return;
933 //}
934
935 D->addAttr(::new (S.Context)
936 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
937 Attr.getAttributeSpellingListIndex()));
938}
939
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000940
941static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000942 if (!checkAttributeNumArgs(S, Attr, 1))
943 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000944
945 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
946 return;
947
948 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000949 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000950 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
951 StringRef Param = Ident->Ident->getName();
952 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
953 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
954 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000955 return;
956 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000957 } else {
958 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
959 Attr.getName() << AANT_ArgumentIdentifier;
960 return;
961 }
962
963 D->addAttr(::new (S.Context)
964 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
965 Attr.getAttributeSpellingListIndex()));
966}
967
Chris Wailes9385f9f2013-10-29 20:28:41 +0000968static void handleTestTypestateAttr(Sema &S, Decl *D,
969 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000970 if (!checkAttributeNumArgs(S, Attr, 1))
971 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000972
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000973 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
974 return;
975
Chris Wailes9385f9f2013-10-29 20:28:41 +0000976 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000977 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000978 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
979 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +0000980 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000981 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
982 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983 return;
984 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000985 } else {
986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
987 Attr.getName() << AANT_ArgumentIdentifier;
988 return;
989 }
990
991 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +0000992 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000993 Attr.getAttributeSpellingListIndex()));
994}
995
Chandler Carruthedc2c642011-07-02 00:01:44 +0000996static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
997 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000998 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000999 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001000}
1001
Chandler Carruthedc2c642011-07-02 00:01:44 +00001002static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001003 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001004 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1005 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001006 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001007 // If the alignment is less than or equal to 8 bits, the packed attribute
1008 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001009 if (!FD->getType()->isDependentType() &&
1010 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001011 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001012 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001013 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001014 else
Michael Han99315932013-01-24 16:46:58 +00001015 FD->addAttr(::new (S.Context)
1016 PackedAttr(Attr.getRange(), S.Context,
1017 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001018 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001019 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001020}
1021
Ted Kremenek7fd17232011-09-29 07:02:25 +00001022static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1023 // The IBOutlet/IBOutletCollection attributes only apply to instance
1024 // variables or properties of Objective-C classes. The outlet must also
1025 // have an object reference type.
1026 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1027 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001028 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001029 << Attr.getName() << VD->getType() << 0;
1030 return false;
1031 }
1032 }
1033 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1034 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001035 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001036 << Attr.getName() << PD->getType() << 1;
1037 return false;
1038 }
1039 }
1040 else {
1041 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1042 return false;
1043 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001044
Ted Kremenek7fd17232011-09-29 07:02:25 +00001045 return true;
1046}
1047
Chandler Carruthedc2c642011-07-02 00:01:44 +00001048static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001049 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001050 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001051
Michael Han99315932013-01-24 16:46:58 +00001052 D->addAttr(::new (S.Context)
1053 IBOutletAttr(Attr.getRange(), S.Context,
1054 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001055}
1056
Chandler Carruthedc2c642011-07-02 00:01:44 +00001057static void handleIBOutletCollection(Sema &S, Decl *D,
1058 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001059
1060 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001061 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1063 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001064 return;
1065 }
1066
Ted Kremenek7fd17232011-09-29 07:02:25 +00001067 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001068 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001069
Richard Smithb1f9a282013-10-31 01:56:18 +00001070 ParsedType PT;
1071
1072 if (Attr.hasParsedType())
1073 PT = Attr.getTypeArg();
1074 else {
1075 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1076 S.getScopeForContext(D->getDeclContext()->getParent()));
1077 if (!PT) {
1078 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1079 return;
1080 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001081 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001082
Richard Smithb87c4652013-10-31 21:23:20 +00001083 TypeSourceInfo *QTLoc = 0;
1084 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1085 if (!QTLoc)
1086 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001087
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001088 // Diagnose use of non-object type in iboutletcollection attribute.
1089 // FIXME. Gnu attribute extension ignores use of builtin types in
1090 // attributes. So, __attribute__((iboutletcollection(char))) will be
1091 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001092 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001093 S.Diag(Attr.getLoc(),
1094 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1095 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001096 return;
1097 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001098
Michael Han99315932013-01-24 16:46:58 +00001099 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001100 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001101 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001102}
1103
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001104static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001105 if (const RecordType *UT = T->getAsUnionType())
1106 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1107 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001108 for (const auto *I : UD->fields()) {
1109 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001110 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1111 T = QT;
1112 return;
1113 }
1114 }
1115 }
1116}
1117
Ted Kremenek9aedc152014-01-17 06:24:56 +00001118static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001119 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001120 T = T.getNonReferenceType();
1121 possibleTransparentUnionPointerType(T);
1122
1123 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001124 S.Diag(Attr.getLoc(),
1125 isReturnValue ? diag::warn_attribute_return_pointers_only
1126 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001127 << Attr.getName() << R;
1128 return false;
1129 }
1130 return true;
1131}
1132
Chandler Carruthedc2c642011-07-02 00:01:44 +00001133static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001134 SmallVector<unsigned, 8> NonNullArgs;
1135 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001136 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001137 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001138 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001139 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001140
1141 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001142 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001143 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1144 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001145 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001146
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001147 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001148 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001149
1150 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1151 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001152 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001153 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1154 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001155 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001156 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001157 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001158 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001159
Ted Kremenek22813f42010-10-21 18:49:36 +00001160 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001161 if (NonNullArgs.empty()) {
1162 // Warn the trivial case only if attribute is not coming from a
1163 // macro instantiation.
1164 if (Attr.getLoc().isFileID())
1165 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001166 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001167 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001168 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001169
Nick Lewyckye1121512013-01-24 01:12:16 +00001170 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001171 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001172 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001173 D->addAttr(::new (S.Context)
1174 NonNullAttr(Attr.getRange(), S.Context, start, size,
1175 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001176}
1177
Jordan Rosec9399072014-02-11 17:27:59 +00001178static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1179 const AttributeList &Attr) {
1180 if (Attr.getNumArgs() > 0) {
1181 if (D->getFunctionType()) {
1182 handleNonNullAttr(S, D, Attr);
1183 } else {
1184 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1185 << D->getSourceRange();
1186 }
1187 return;
1188 }
1189
1190 // Is the argument a pointer type?
1191 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1192 return;
1193
1194 D->addAttr(::new (S.Context)
1195 NonNullAttr(Attr.getRange(), S.Context, 0, 0,
1196 Attr.getAttributeSpellingListIndex()));
1197}
1198
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001199static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1200 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001201 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001202 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1203 /* isReturnValue */ true))
1204 return;
1205
1206 D->addAttr(::new (S.Context)
1207 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1208 Attr.getAttributeSpellingListIndex()));
1209}
1210
Chandler Carruthedc2c642011-07-02 00:01:44 +00001211static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001212 // This attribute must be applied to a function declaration. The first
1213 // argument to the attribute must be an identifier, the name of the resource,
1214 // for example: malloc. The following arguments must be argument indexes, the
1215 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001216 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001217 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001218 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001219
Aaron Ballman00e99962013-08-31 01:11:41 +00001220 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001221 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001222 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001223 return;
1224 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001225
Richard Smith852e9ce2013-11-27 01:46:48 +00001226 // Figure out our Kind.
1227 OwnershipAttr::OwnershipKind K =
1228 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1229 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001230
Richard Smith852e9ce2013-11-27 01:46:48 +00001231 // Check arguments.
1232 switch (K) {
1233 case OwnershipAttr::Takes:
1234 case OwnershipAttr::Holds:
1235 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001236 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1237 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001238 return;
1239 }
1240 break;
1241 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001242 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001243 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1244 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001245 return;
1246 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001247 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001248 }
1249
Richard Smith852e9ce2013-11-27 01:46:48 +00001250 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251
1252 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001253 StringRef ModuleName = Module->getName();
1254 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1255 ModuleName.size() > 4) {
1256 ModuleName = ModuleName.drop_front(2).drop_back(2);
1257 Module = &S.PP.getIdentifierTable().get(ModuleName);
1258 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001259
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001260 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001261 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1262 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001263 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001264 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001265 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001266
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001267 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001268 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001269 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001270 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001271 case OwnershipAttr::Takes:
1272 case OwnershipAttr::Holds:
1273 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1274 Err = 0;
1275 break;
1276 case OwnershipAttr::Returns:
1277 if (!T->isIntegerType())
1278 Err = 1;
1279 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001281 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001282 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001283 << Ex->getSourceRange();
1284 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001285 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001286
1287 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001288 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001289 // FIXME: A returns attribute should conflict with any returns attribute
1290 // with a different index too.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001291 if (I->getOwnKind() != K && I->args_end() !=
1292 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001293 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001294 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001295 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001296 }
1297 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001298 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001299 }
1300
1301 unsigned* start = OwnershipArgs.data();
1302 unsigned size = OwnershipArgs.size();
1303 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001304
Michael Han99315932013-01-24 16:46:58 +00001305 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001306 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001307 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001308}
1309
Chandler Carruthedc2c642011-07-02 00:01:44 +00001310static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001311 // Check the attribute arguments.
1312 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1314 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001315 return;
1316 }
1317
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001318 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001319
Rafael Espindolac18086a2010-02-23 22:00:30 +00001320 // gcc rejects
1321 // class c {
1322 // static int a __attribute__((weakref ("v2")));
1323 // static int b() __attribute__((weakref ("f3")));
1324 // };
1325 // and ignores the attributes of
1326 // void f(void) {
1327 // static int a __attribute__((weakref ("v2")));
1328 // }
1329 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001330 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001331 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1333 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001334 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001335 }
1336
1337 // The GCC manual says
1338 //
1339 // At present, a declaration to which `weakref' is attached can only
1340 // be `static'.
1341 //
1342 // It also says
1343 //
1344 // Without a TARGET,
1345 // given as an argument to `weakref' or to `alias', `weakref' is
1346 // equivalent to `weak'.
1347 //
1348 // gcc 4.4.1 will accept
1349 // int a7 __attribute__((weakref));
1350 // as
1351 // int a7 __attribute__((weak));
1352 // This looks like a bug in gcc. We reject that for now. We should revisit
1353 // it if this behaviour is actually used.
1354
Rafael Espindolac18086a2010-02-23 22:00:30 +00001355 // GCC rejects
1356 // static ((alias ("y"), weakref)).
1357 // Should we? How to check that weakref is before or after alias?
1358
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001359 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1360 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1361 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001362 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001363 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001364 // GCC will accept anything as the argument of weakref. Should we
1365 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001366 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1367 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001368
Michael Han99315932013-01-24 16:46:58 +00001369 D->addAttr(::new (S.Context)
1370 WeakRefAttr(Attr.getRange(), S.Context,
1371 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001372}
1373
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001374static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1375 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001376 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001377 return;
1378
Douglas Gregore8bbc122011-09-02 00:18:52 +00001379 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001380 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1381 return;
1382 }
1383
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001384 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001385
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001386 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001387 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001388}
1389
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001390static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001391 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001392 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001393
Michael Han99315932013-01-24 16:46:58 +00001394 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1395 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001396}
1397
1398static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001399 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001400 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001401
Michael Han99315932013-01-24 16:46:58 +00001402 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1403 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001404}
1405
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001406static void handleTLSModelAttr(Sema &S, Decl *D,
1407 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001408 StringRef Model;
1409 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001410 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001411 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001412 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001413
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001414 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001415 if (Model != "global-dynamic" && Model != "local-dynamic"
1416 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001417 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001418 return;
1419 }
1420
Michael Han99315932013-01-24 16:46:58 +00001421 D->addAttr(::new (S.Context)
1422 TLSModelAttr(Attr.getRange(), S.Context, Model,
1423 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001424}
1425
Chandler Carruthedc2c642011-07-02 00:01:44 +00001426static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001427 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001428 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001429 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001430 D->addAttr(::new (S.Context)
1431 MallocAttr(Attr.getRange(), S.Context,
1432 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001433 return;
1434 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001435 }
1436
Ted Kremenek08479ae2009-08-15 00:51:46 +00001437 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001438}
1439
Chandler Carruthedc2c642011-07-02 00:01:44 +00001440static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001441 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001442 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1443 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001444 return;
1445 }
1446
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001447 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1448 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001449}
1450
Chandler Carruthedc2c642011-07-02 00:01:44 +00001451static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001452 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001453
1454 if (S.CheckNoReturnAttr(attr)) return;
1455
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001456 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001457 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001458 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001459 return;
1460 }
1461
Michael Han99315932013-01-24 16:46:58 +00001462 D->addAttr(::new (S.Context)
1463 NoReturnAttr(attr.getRange(), S.Context,
1464 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001465}
1466
1467bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001468 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001469 attr.setInvalid();
1470 return true;
1471 }
1472
1473 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001474}
1475
Chandler Carruthedc2c642011-07-02 00:01:44 +00001476static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1477 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001478
1479 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1480 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001481 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1482 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001483 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1484 && !VD->getType()->isFunctionPointerType())) {
1485 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001486 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001487 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001488 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001489 return;
1490 }
1491 }
1492
Michael Han99315932013-01-24 16:46:58 +00001493 D->addAttr(::new (S.Context)
1494 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1495 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001496}
1497
John Thompsoncdb847ba2010-08-09 21:53:52 +00001498// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001499static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001500/*
1501 Returning a Vector Class in Registers
1502
Eric Christopherbc638a82010-12-01 22:13:54 +00001503 According to the PPU ABI specifications, a class with a single member of
1504 vector type is returned in memory when used as the return value of a function.
1505 This results in inefficient code when implementing vector classes. To return
1506 the value in a single vector register, add the vecreturn attribute to the
1507 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001508
1509 Example:
1510
1511 struct Vector
1512 {
1513 __vector float xyzw;
1514 } __attribute__((vecreturn));
1515
1516 Vector Add(Vector lhs, Vector rhs)
1517 {
1518 Vector result;
1519 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1520 return result; // This will be returned in a register
1521 }
1522*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001523 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1524 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001525 return;
1526 }
1527
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001528 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001529 int count = 0;
1530
1531 if (!isa<CXXRecordDecl>(record)) {
1532 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1533 return;
1534 }
1535
1536 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1537 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1538 return;
1539 }
1540
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001541 for (const auto *I : record->fields()) {
1542 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001543 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1544 return;
1545 }
1546 count++;
1547 }
1548
Michael Han99315932013-01-24 16:46:58 +00001549 D->addAttr(::new (S.Context)
1550 VecReturnAttr(Attr.getRange(), S.Context,
1551 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001552}
1553
Richard Smithe233fbf2013-01-28 22:42:45 +00001554static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1555 const AttributeList &Attr) {
1556 if (isa<ParmVarDecl>(D)) {
1557 // [[carries_dependency]] can only be applied to a parameter if it is a
1558 // parameter of a function declaration or lambda.
1559 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1560 S.Diag(Attr.getLoc(),
1561 diag::err_carries_dependency_param_not_function_decl);
1562 return;
1563 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001564 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001565
1566 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1567 Attr.getRange(), S.Context,
1568 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001569}
1570
Chandler Carruthedc2c642011-07-02 00:01:44 +00001571static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001572 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001573 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001574 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001575 return;
1576 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001577 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001578 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001579 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001580 return;
1581 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001582
Michael Han99315932013-01-24 16:46:58 +00001583 D->addAttr(::new (S.Context)
1584 UsedAttr(Attr.getRange(), S.Context,
1585 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001586}
1587
Chandler Carruthedc2c642011-07-02 00:01:44 +00001588static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001589 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001590 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001591 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1592 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001593 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001594 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001595
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001596 uint32_t priority = ConstructorAttr::DefaultPriority;
1597 if (Attr.getNumArgs() > 0 &&
1598 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1599 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001600
Michael Han99315932013-01-24 16:46:58 +00001601 D->addAttr(::new (S.Context)
1602 ConstructorAttr(Attr.getRange(), S.Context, priority,
1603 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001604}
1605
Chandler Carruthedc2c642011-07-02 00:01:44 +00001606static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001607 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001608 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001609 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1610 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001611 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001612 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001613
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001614 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001615 if (Attr.getNumArgs() > 0 &&
1616 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1617 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001618
Michael Han99315932013-01-24 16:46:58 +00001619 D->addAttr(::new (S.Context)
1620 DestructorAttr(Attr.getRange(), S.Context, priority,
1621 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001622}
1623
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001624template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001625static void handleAttrWithMessage(Sema &S, Decl *D,
1626 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001627 unsigned NumArgs = Attr.getNumArgs();
1628 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001629 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1630 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001631 return;
1632 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001633
1634 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001635 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001636 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001637 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001638
Michael Han99315932013-01-24 16:46:58 +00001639 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1640 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001641}
1642
Ted Kremenek438f8db2014-02-22 01:06:05 +00001643static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001644 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001645 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001646 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1647 << Attr.getName() << Attr.getRange();
1648 return;
1649 }
1650
Ted Kremenek28eace62013-11-23 01:01:34 +00001651 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001652 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1653 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001654}
1655
Jordy Rose740b0c22012-05-08 03:27:22 +00001656static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1657 IdentifierInfo *Platform,
1658 VersionTuple Introduced,
1659 VersionTuple Deprecated,
1660 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001661 StringRef PlatformName
1662 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1663 if (PlatformName.empty())
1664 PlatformName = Platform->getName();
1665
1666 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1667 // of these steps are needed).
1668 if (!Introduced.empty() && !Deprecated.empty() &&
1669 !(Introduced <= Deprecated)) {
1670 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1671 << 1 << PlatformName << Deprecated.getAsString()
1672 << 0 << Introduced.getAsString();
1673 return true;
1674 }
1675
1676 if (!Introduced.empty() && !Obsoleted.empty() &&
1677 !(Introduced <= Obsoleted)) {
1678 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1679 << 2 << PlatformName << Obsoleted.getAsString()
1680 << 0 << Introduced.getAsString();
1681 return true;
1682 }
1683
1684 if (!Deprecated.empty() && !Obsoleted.empty() &&
1685 !(Deprecated <= Obsoleted)) {
1686 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1687 << 2 << PlatformName << Obsoleted.getAsString()
1688 << 1 << Deprecated.getAsString();
1689 return true;
1690 }
1691
1692 return false;
1693}
1694
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001695/// \brief Check whether the two versions match.
1696///
1697/// If either version tuple is empty, then they are assumed to match. If
1698/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1699static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1700 bool BeforeIsOkay) {
1701 if (X.empty() || Y.empty())
1702 return true;
1703
1704 if (X == Y)
1705 return true;
1706
1707 if (BeforeIsOkay && X < Y)
1708 return true;
1709
1710 return false;
1711}
1712
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001713AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001714 IdentifierInfo *Platform,
1715 VersionTuple Introduced,
1716 VersionTuple Deprecated,
1717 VersionTuple Obsoleted,
1718 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001719 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001720 bool Override,
1721 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001722 VersionTuple MergedIntroduced = Introduced;
1723 VersionTuple MergedDeprecated = Deprecated;
1724 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001725 bool FoundAny = false;
1726
Rafael Espindolac67f2232012-05-10 02:50:16 +00001727 if (D->hasAttrs()) {
1728 AttrVec &Attrs = D->getAttrs();
1729 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1730 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1731 if (!OldAA) {
1732 ++i;
1733 continue;
1734 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001735
Rafael Espindolac67f2232012-05-10 02:50:16 +00001736 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1737 if (OldPlatform != Platform) {
1738 ++i;
1739 continue;
1740 }
1741
1742 FoundAny = true;
1743 VersionTuple OldIntroduced = OldAA->getIntroduced();
1744 VersionTuple OldDeprecated = OldAA->getDeprecated();
1745 VersionTuple OldObsoleted = OldAA->getObsoleted();
1746 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001747
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001748 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1749 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1750 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1751 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001752 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001753 if (Override) {
1754 int Which = -1;
1755 VersionTuple FirstVersion;
1756 VersionTuple SecondVersion;
1757 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1758 Which = 0;
1759 FirstVersion = OldIntroduced;
1760 SecondVersion = Introduced;
1761 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1762 Which = 1;
1763 FirstVersion = Deprecated;
1764 SecondVersion = OldDeprecated;
1765 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1766 Which = 2;
1767 FirstVersion = Obsoleted;
1768 SecondVersion = OldObsoleted;
1769 }
1770
1771 if (Which == -1) {
1772 Diag(OldAA->getLocation(),
1773 diag::warn_mismatched_availability_override_unavail)
1774 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1775 } else {
1776 Diag(OldAA->getLocation(),
1777 diag::warn_mismatched_availability_override)
1778 << Which
1779 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1780 << FirstVersion.getAsString() << SecondVersion.getAsString();
1781 }
1782 Diag(Range.getBegin(), diag::note_overridden_method);
1783 } else {
1784 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1785 Diag(Range.getBegin(), diag::note_previous_attribute);
1786 }
1787
Rafael Espindolac67f2232012-05-10 02:50:16 +00001788 Attrs.erase(Attrs.begin() + i);
1789 --e;
1790 continue;
1791 }
1792
1793 VersionTuple MergedIntroduced2 = MergedIntroduced;
1794 VersionTuple MergedDeprecated2 = MergedDeprecated;
1795 VersionTuple MergedObsoleted2 = MergedObsoleted;
1796
1797 if (MergedIntroduced2.empty())
1798 MergedIntroduced2 = OldIntroduced;
1799 if (MergedDeprecated2.empty())
1800 MergedDeprecated2 = OldDeprecated;
1801 if (MergedObsoleted2.empty())
1802 MergedObsoleted2 = OldObsoleted;
1803
1804 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1805 MergedIntroduced2, MergedDeprecated2,
1806 MergedObsoleted2)) {
1807 Attrs.erase(Attrs.begin() + i);
1808 --e;
1809 continue;
1810 }
1811
1812 MergedIntroduced = MergedIntroduced2;
1813 MergedDeprecated = MergedDeprecated2;
1814 MergedObsoleted = MergedObsoleted2;
1815 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001816 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001817 }
1818
1819 if (FoundAny &&
1820 MergedIntroduced == Introduced &&
1821 MergedDeprecated == Deprecated &&
1822 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001823 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001824
Ted Kremenekb5445722013-04-06 00:34:27 +00001825 // Only create a new attribute if !Override, but we want to do
1826 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001827 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001828 MergedDeprecated, MergedObsoleted) &&
1829 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001830 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1831 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001832 Obsoleted, IsUnavailable, Message,
1833 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001834 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001835 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001836}
1837
Chandler Carruthedc2c642011-07-02 00:01:44 +00001838static void handleAvailabilityAttr(Sema &S, Decl *D,
1839 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001840 if (!checkAttributeNumArgs(S, Attr, 1))
1841 return;
1842 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001843 unsigned Index = Attr.getAttributeSpellingListIndex();
1844
Aaron Ballman00e99962013-08-31 01:11:41 +00001845 IdentifierInfo *II = Platform->Ident;
1846 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1847 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1848 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001849
Rafael Espindolac231fab2013-01-08 21:30:32 +00001850 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1851 if (!ND) {
1852 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1853 return;
1854 }
1855
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001856 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1857 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1858 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001859 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001860 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001861 if (const StringLiteral *SE =
1862 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001863 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001864
Aaron Ballman00e99962013-08-31 01:11:41 +00001865 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001866 Introduced.Version,
1867 Deprecated.Version,
1868 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001869 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001870 /*Override=*/false,
1871 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001872 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001873 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001874}
1875
John McCalld041a9b2013-02-20 01:54:26 +00001876template <class T>
1877static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1878 typename T::VisibilityType value,
1879 unsigned attrSpellingListIndex) {
1880 T *existingAttr = D->getAttr<T>();
1881 if (existingAttr) {
1882 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1883 if (existingValue == value)
1884 return NULL;
1885 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1886 S.Diag(range.getBegin(), diag::note_previous_attribute);
1887 D->dropAttr<T>();
1888 }
1889 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1890}
1891
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001892VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001893 VisibilityAttr::VisibilityType Vis,
1894 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001895 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1896 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001897}
1898
John McCalld041a9b2013-02-20 01:54:26 +00001899TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1900 TypeVisibilityAttr::VisibilityType Vis,
1901 unsigned AttrSpellingListIndex) {
1902 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1903 AttrSpellingListIndex);
1904}
1905
1906static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1907 bool isTypeVisibility) {
1908 // Visibility attributes don't mean anything on a typedef.
1909 if (isa<TypedefNameDecl>(D)) {
1910 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1911 << Attr.getName();
1912 return;
1913 }
1914
1915 // 'type_visibility' can only go on a type or namespace.
1916 if (isTypeVisibility &&
1917 !(isa<TagDecl>(D) ||
1918 isa<ObjCInterfaceDecl>(D) ||
1919 isa<NamespaceDecl>(D))) {
1920 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1921 << Attr.getName() << ExpectedTypeOrNamespace;
1922 return;
1923 }
1924
Benjamin Kramer70370212013-09-09 15:08:57 +00001925 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001926 StringRef TypeStr;
1927 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001928 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001929 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001930
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001931 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001932 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001933 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001934 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001935 return;
1936 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001937
1938 // Complain about attempts to use protected visibility on targets
1939 // (like Darwin) that don't support it.
1940 if (type == VisibilityAttr::Protected &&
1941 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1943 type = VisibilityAttr::Default;
1944 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001945
Michael Han99315932013-01-24 16:46:58 +00001946 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001947 clang::Attr *newAttr;
1948 if (isTypeVisibility) {
1949 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1950 (TypeVisibilityAttr::VisibilityType) type,
1951 Index);
1952 } else {
1953 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1954 }
1955 if (newAttr)
1956 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001957}
1958
Chandler Carruthedc2c642011-07-02 00:01:44 +00001959static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1960 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001961 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001962 if (!Attr.isArgIdent(0)) {
1963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1964 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001965 return;
1966 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001967
Aaron Ballman682ee422013-09-11 19:47:58 +00001968 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1969 ObjCMethodFamilyAttr::FamilyKind F;
1970 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1971 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1972 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001973 return;
1974 }
1975
Alp Toker314cc812014-01-25 16:55:45 +00001976 if (F == ObjCMethodFamilyAttr::OMF_init &&
1977 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001978 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001979 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001980 // Ignore the attribute.
1981 return;
1982 }
1983
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001984 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001985 S.Context, F,
1986 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001987}
1988
Chandler Carruthedc2c642011-07-02 00:01:44 +00001989static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001990 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001991 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001992 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001993 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1994 return;
1995 }
1996 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001997 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1998 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001999 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002000 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2001 return;
2002 }
2003 }
2004 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002005 // It is okay to include this attribute on properties, e.g.:
2006 //
2007 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2008 //
2009 // In this case it follows tradition and suppresses an error in the above
2010 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002011 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002012 }
Michael Han99315932013-01-24 16:46:58 +00002013 D->addAttr(::new (S.Context)
2014 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2015 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002016}
2017
Chandler Carruthedc2c642011-07-02 00:01:44 +00002018static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002019 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002021 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002022 return;
2023 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002024
Aaron Ballman00e99962013-08-31 01:11:41 +00002025 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002026 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002027 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2028 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2029 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002030 return;
2031 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002032
Michael Han99315932013-01-24 16:46:58 +00002033 D->addAttr(::new (S.Context)
2034 BlocksAttr(Attr.getRange(), S.Context, type,
2035 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002036}
2037
Chandler Carruthedc2c642011-07-02 00:01:44 +00002038static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002039 // check the attribute arguments.
2040 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002041 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2042 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002043 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002044 }
2045
Aaron Ballman18a78382013-11-21 00:28:23 +00002046 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002047 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002048 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002049 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002050 if (E->isTypeDependent() || E->isValueDependent() ||
2051 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002053 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002054 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002055 return;
2056 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002057
John McCallb46f2872011-09-09 07:56:05 +00002058 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002059 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2060 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002061 return;
2062 }
John McCallb46f2872011-09-09 07:56:05 +00002063
2064 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002065 }
2066
Aaron Ballman18a78382013-11-21 00:28:23 +00002067 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002068 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002069 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002070 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002071 if (E->isTypeDependent() || E->isValueDependent() ||
2072 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002073 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002074 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002075 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 return;
2077 }
2078 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002079
John McCallb46f2872011-09-09 07:56:05 +00002080 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002081 // FIXME: This error message could be improved, it would be nice
2082 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002083 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2084 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002085 return;
2086 }
2087 }
2088
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002089 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002090 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002091 if (isa<FunctionNoProtoType>(FT)) {
2092 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2093 return;
2094 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002095
Chris Lattner9363e312009-03-17 23:03:47 +00002096 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002097 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002098 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002099 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002100 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002101 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002102 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002103 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002104 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002105 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2106 if (!BD->isVariadic()) {
2107 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2108 return;
2109 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002110 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002111 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002112 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002113 const FunctionType *FT = Ty->isFunctionPointerType()
2114 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002115 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002116 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002117 int m = Ty->isFunctionPointerType() ? 0 : 1;
2118 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002119 return;
2120 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002121 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002122 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002123 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002124 return;
2125 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002126 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002127 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002128 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002129 return;
2130 }
Michael Han99315932013-01-24 16:46:58 +00002131 D->addAttr(::new (S.Context)
2132 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2133 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002134}
2135
Chandler Carruthedc2c642011-07-02 00:01:44 +00002136static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002137 if (D->getFunctionType() &&
2138 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002139 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2140 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002141 return;
2142 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002143 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002144 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002145 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2146 << Attr.getName() << 1;
2147 return;
2148 }
2149
Michael Han99315932013-01-24 16:46:58 +00002150 D->addAttr(::new (S.Context)
2151 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2152 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002153}
2154
Chandler Carruthedc2c642011-07-02 00:01:44 +00002155static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002156 // weak_import only applies to variable & function declarations.
2157 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002158 if (!D->canBeWeakImported(isDef)) {
2159 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002160 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2161 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002162 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002163 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002164 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002165 // Nothing to warn about here.
2166 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002167 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002168 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002169
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002170 return;
2171 }
2172
Michael Han99315932013-01-24 16:46:58 +00002173 D->addAttr(::new (S.Context)
2174 WeakImportAttr(Attr.getRange(), S.Context,
2175 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002176}
2177
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002178// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002179template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002180static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002181 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002182 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002183 for (unsigned i = 0; i < 3; ++i) {
2184 const Expr *E = Attr.getArgAsExpr(i);
2185 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002186 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002187 if (WGSize[i] == 0) {
2188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2189 << Attr.getName() << E->getSourceRange();
2190 return;
2191 }
2192 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002193
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002194 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2195 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2196 Existing->getYDim() == WGSize[1] &&
2197 Existing->getZDim() == WGSize[2]))
2198 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002199
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002200 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2201 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002202 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002203}
2204
Joey Goulyaba589c2013-03-08 09:42:32 +00002205static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002206 if (!Attr.hasParsedType()) {
2207 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2208 << Attr.getName() << 1;
2209 return;
2210 }
2211
Richard Smithb87c4652013-10-31 21:23:20 +00002212 TypeSourceInfo *ParmTSI = 0;
2213 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2214 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002215
2216 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2217 (ParmType->isBooleanType() ||
2218 !ParmType->isIntegralType(S.getASTContext()))) {
2219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2220 << ParmType;
2221 return;
2222 }
2223
Aaron Ballmana9e05402013-12-02 22:16:55 +00002224 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002225 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002226 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2227 return;
2228 }
2229 }
2230
2231 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002232 ParmTSI,
2233 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002234}
2235
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002236SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002237 StringRef Name,
2238 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002239 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2240 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002241 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002242 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2243 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002244 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002245 }
Michael Han99315932013-01-24 16:46:58 +00002246 return ::new (Context) SectionAttr(Range, Context, Name,
2247 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002248}
2249
Chandler Carruthedc2c642011-07-02 00:01:44 +00002250static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002251 // Make sure that there is a string literal as the sections's single
2252 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002253 StringRef Str;
2254 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002255 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002256 return;
Mike Stump11289f42009-09-09 15:08:12 +00002257
Chris Lattner30ba6742009-08-10 19:03:04 +00002258 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002259 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002260 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002261 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002262 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002263 return;
2264 }
Mike Stump11289f42009-09-09 15:08:12 +00002265
Michael Han99315932013-01-24 16:46:58 +00002266 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002267 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002268 if (NewAttr)
2269 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002270}
2271
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002272
Chandler Carruthedc2c642011-07-02 00:01:44 +00002273static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002274 VarDecl *VD = cast<VarDecl>(D);
2275 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002276 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002277 return;
2278 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002279
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002280 Expr *E = Attr.getArgAsExpr(0);
2281 SourceLocation Loc = E->getExprLoc();
2282 FunctionDecl *FD = 0;
2283 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002284
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002285 // gcc only allows for simple identifiers. Since we support more than gcc, we
2286 // will warn the user.
2287 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2288 if (DRE->hasQualifier())
2289 S.Diag(Loc, diag::warn_cleanup_ext);
2290 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2291 NI = DRE->getNameInfo();
2292 if (!FD) {
2293 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2294 << NI.getName();
2295 return;
2296 }
2297 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2298 if (ULE->hasExplicitTemplateArgs())
2299 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002300 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2301 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002302 if (!FD) {
2303 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2304 << NI.getName();
2305 if (ULE->getType() == S.Context.OverloadTy)
2306 S.NoteAllOverloadCandidates(ULE);
2307 return;
2308 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002309 } else {
2310 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002311 return;
2312 }
2313
Anders Carlssond277d792009-01-31 01:16:18 +00002314 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002315 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2316 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002317 return;
2318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002319
Anders Carlsson723f55d2009-02-07 23:16:50 +00002320 // We're currently more strict than GCC about what function types we accept.
2321 // If this ever proves to be a problem it should be easy to fix.
2322 QualType Ty = S.Context.getPointerType(VD->getType());
2323 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002324 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2325 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002326 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2327 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002328 return;
2329 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002330
Michael Han99315932013-01-24 16:46:58 +00002331 D->addAttr(::new (S.Context)
2332 CleanupAttr(Attr.getRange(), S.Context, FD,
2333 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002334}
2335
Mike Stumpd3bb5572009-07-24 19:02:52 +00002336/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002337/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002338static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002339 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002340 uint64_t Idx;
2341 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002342 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002343
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002344 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002345 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002346
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002347 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2348 if (not_nsstring_type &&
2349 !isCFStringType(Ty, S.Context) &&
2350 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002351 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002352 // FIXME: Should highlight the actual expression that has the wrong type.
2353 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002354 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002355 << IdxExpr->getSourceRange();
2356 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002357 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002358 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002359 if (!isNSStringType(Ty, S.Context) &&
2360 !isCFStringType(Ty, S.Context) &&
2361 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002362 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002363 // FIXME: Should highlight the actual expression that has the wrong type.
2364 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002365 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002366 << IdxExpr->getSourceRange();
2367 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002368 }
2369
Alp Toker601b22c2014-01-21 23:35:24 +00002370 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002371 // because that has corrected for the implicit this parameter, and is zero-
2372 // based. The attribute expects what the user wrote explicitly.
2373 llvm::APSInt Val;
2374 IdxExpr->EvaluateAsInt(Val, S.Context);
2375
Michael Han99315932013-01-24 16:46:58 +00002376 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002377 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002378 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002379}
2380
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002381enum FormatAttrKind {
2382 CFStringFormat,
2383 NSStringFormat,
2384 StrftimeFormat,
2385 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002386 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002387 InvalidFormat
2388};
2389
2390/// getFormatAttrKind - Map from format attribute names to supported format
2391/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002392static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002393 return llvm::StringSwitch<FormatAttrKind>(Format)
2394 // Check for formats that get handled specially.
2395 .Case("NSString", NSStringFormat)
2396 .Case("CFString", CFStringFormat)
2397 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002398
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002399 // Otherwise, check for supported formats.
2400 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2401 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2402 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002403
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002404 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2405 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002406}
2407
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002408/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002409/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002410static void handleInitPriorityAttr(Sema &S, Decl *D,
2411 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002412 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002413 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2414 return;
2415 }
2416
Aaron Ballman4a611152013-11-27 16:34:09 +00002417 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002418 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2419 Attr.setInvalid();
2420 return;
2421 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002422 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002423 if (S.Context.getAsArrayType(T))
2424 T = S.Context.getBaseElementType(T);
2425 if (!T->getAs<RecordType>()) {
2426 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2427 Attr.setInvalid();
2428 return;
2429 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002430
2431 Expr *E = Attr.getArgAsExpr(0);
2432 uint32_t prioritynum;
2433 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002434 Attr.setInvalid();
2435 return;
2436 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002437
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002438 if (prioritynum < 101 || prioritynum > 65535) {
2439 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002440 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002441 Attr.setInvalid();
2442 return;
2443 }
Michael Han99315932013-01-24 16:46:58 +00002444 D->addAttr(::new (S.Context)
2445 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2446 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002447}
2448
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002449FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2450 IdentifierInfo *Format, int FormatIdx,
2451 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002452 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002453 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002454 for (auto *F : D->specific_attrs<FormatAttr>()) {
2455 if (F->getType() == Format &&
2456 F->getFormatIdx() == FormatIdx &&
2457 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002458 // If we don't have a valid location for this attribute, adopt the
2459 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002460 if (F->getLocation().isInvalid())
2461 F->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002462 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002463 }
2464 }
2465
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002466 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2467 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002468}
2469
Mike Stumpd3bb5572009-07-24 19:02:52 +00002470/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002471/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002472static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002473 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002475 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002476 return;
2477 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002478
Chandler Carruth743682b2010-11-16 08:35:43 +00002479 // In C++ the implicit 'this' function parameter also counts, and they are
2480 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002481 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002482 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002483
Aaron Ballman00e99962013-08-31 01:11:41 +00002484 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2485 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002486
2487 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002488 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002489 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002490 // If we've modified the string name, we need a new identifier for it.
2491 II = &S.Context.Idents.get(Format);
2492 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002493
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002494 // Check for supported formats.
2495 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002496
2497 if (Kind == IgnoredFormat)
2498 return;
2499
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002500 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002501 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002502 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002503 return;
2504 }
2505
2506 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002507 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002508 uint32_t Idx;
2509 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002510 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002511
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002512 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002514 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002515 return;
2516 }
2517
2518 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002519 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002520
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002521 if (HasImplicitThisParam) {
2522 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002523 S.Diag(Attr.getLoc(),
2524 diag::err_format_attribute_implicit_this_format_string)
2525 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002526 return;
2527 }
2528 ArgIdx--;
2529 }
Mike Stump11289f42009-09-09 15:08:12 +00002530
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002531 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002532 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002533
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002534 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002535 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002536 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2537 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002538 return;
2539 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002540 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002541 // FIXME: do we need to check if the type is NSString*? What are the
2542 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002543 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002544 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002545 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2546 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002547 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002548 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002549 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002550 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002551 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002552 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2553 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554 return;
2555 }
2556
2557 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002558 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002559 uint32_t FirstArg;
2560 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002561 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562
2563 // check if the function is variadic if the 3rd argument non-zero
2564 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002565 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566 ++NumArgs; // +1 for ...
2567 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002568 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569 return;
2570 }
2571 }
2572
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002573 // strftime requires FirstArg to be 0 because it doesn't read from any
2574 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002575 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002577 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2578 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 return;
2580 }
2581 // if 0 it disables parameter checking (to use with e.g. va_list)
2582 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002583 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002584 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585 return;
2586 }
2587
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002588 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002589 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002590 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002591 if (NewAttr)
2592 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593}
2594
Chandler Carruthedc2c642011-07-02 00:01:44 +00002595static void handleTransparentUnionAttr(Sema &S, Decl *D,
2596 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002597 // Try to find the underlying union declaration.
2598 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002599 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002600 if (TD && TD->getUnderlyingType()->isUnionType())
2601 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2602 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002603 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002604
2605 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002607 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 return;
2609 }
2610
John McCallf937c022011-10-07 06:10:15 +00002611 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002612 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002613 diag::warn_transparent_union_attribute_not_definition);
2614 return;
2615 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002617 RecordDecl::field_iterator Field = RD->field_begin(),
2618 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002619 if (Field == FieldEnd) {
2620 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2621 return;
2622 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002623
David Blaikie40ed2972012-06-06 20:45:41 +00002624 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002625 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002626 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002627 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002628 diag::warn_transparent_union_attribute_floating)
2629 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002630 return;
2631 }
2632
2633 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2634 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2635 for (; Field != FieldEnd; ++Field) {
2636 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002637 // FIXME: this isn't fully correct; we also need to test whether the
2638 // members of the union would all have the same calling convention as the
2639 // first member of the union. Checking just the size and alignment isn't
2640 // sufficient (consider structs passed on the stack instead of in registers
2641 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002642 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002643 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002644 // Warn if we drop the attribute.
2645 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002646 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002647 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002648 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002649 diag::warn_transparent_union_attribute_field_size_align)
2650 << isSize << Field->getDeclName() << FieldBits;
2651 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002652 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002653 diag::note_transparent_union_first_field_size_align)
2654 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002655 return;
2656 }
2657 }
2658
Michael Han99315932013-01-24 16:46:58 +00002659 RD->addAttr(::new (S.Context)
2660 TransparentUnionAttr(Attr.getRange(), S.Context,
2661 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662}
2663
Chandler Carruthedc2c642011-07-02 00:01:44 +00002664static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002665 // Make sure that there is a string literal as the annotation's single
2666 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002667 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002668 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002669 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002670
2671 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002672 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2673 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002674 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002675 }
Michael Han99315932013-01-24 16:46:58 +00002676
2677 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002678 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002679 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002680}
2681
Chandler Carruthedc2c642011-07-02 00:01:44 +00002682static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002683 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002684 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2686 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002687 return;
2688 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002689
Richard Smith848e1f12013-02-01 08:12:08 +00002690 if (Attr.getNumArgs() == 0) {
2691 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2692 true, 0, Attr.getAttributeSpellingListIndex()));
2693 return;
2694 }
2695
Aaron Ballman00e99962013-08-31 01:11:41 +00002696 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002697 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2698 S.Diag(Attr.getEllipsisLoc(),
2699 diag::err_pack_expansion_without_parameter_packs);
2700 return;
2701 }
2702
2703 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2704 return;
2705
2706 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2707 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002708}
2709
2710void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002711 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002712 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2713 SourceLocation AttrLoc = AttrRange.getBegin();
2714
Richard Smith1dba27c2013-01-29 09:02:09 +00002715 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002716 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002717 // C++11 [dcl.align]p1:
2718 // An alignment-specifier may be applied to a variable or to a class
2719 // data member, but it shall not be applied to a bit-field, a function
2720 // parameter, the formal parameter of a catch clause, or a variable
2721 // declared with the register storage class specifier. An
2722 // alignment-specifier may also be applied to the declaration of a class
2723 // or enumeration type.
2724 // C11 6.7.5/2:
2725 // An alignment attribute shall not be specified in a declaration of
2726 // a typedef, or a bit-field, or a function, or a parameter, or an
2727 // object declared with the register storage-class specifier.
2728 int DiagKind = -1;
2729 if (isa<ParmVarDecl>(D)) {
2730 DiagKind = 0;
2731 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2732 if (VD->getStorageClass() == SC_Register)
2733 DiagKind = 1;
2734 if (VD->isExceptionVariable())
2735 DiagKind = 2;
2736 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2737 if (FD->isBitField())
2738 DiagKind = 3;
2739 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002740 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002741 << (TmpAttr.isC11() ? ExpectedVariableOrField
2742 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002743 return;
2744 }
2745 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002746 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002747 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002748 return;
2749 }
2750 }
2751
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002752 if (E->isTypeDependent() || E->isValueDependent()) {
2753 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002754 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2755 AA->setPackExpansion(IsPackExpansion);
2756 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002757 return;
2758 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002759
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002760 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002761 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002762 ExprResult ICE
2763 = VerifyIntegerConstantExpression(E, &Alignment,
2764 diag::err_aligned_attribute_argument_not_int,
2765 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002766 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002767 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002768
2769 // C++11 [dcl.align]p2:
2770 // -- if the constant expression evaluates to zero, the alignment
2771 // specifier shall have no effect
2772 // C11 6.7.5p6:
2773 // An alignment specification of zero has no effect.
2774 if (!(TmpAttr.isAlignas() && !Alignment) &&
2775 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002776 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2777 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002778 return;
2779 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002780
David Majnemerabecae72014-02-12 20:36:10 +00002781 // Alignment calculations can wrap around if it's greater than 2**28.
2782 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2783 if (Alignment.getZExtValue() > MaxValidAlignment) {
2784 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2785 << E->getSourceRange();
2786 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002787 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002788
Richard Smith44c247f2013-02-22 08:32:16 +00002789 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2790 ICE.take(), SpellingListIndex);
2791 AA->setPackExpansion(IsPackExpansion);
2792 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002793}
2794
Michael Hanaf02bbe2013-02-01 01:19:17 +00002795void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002796 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002797 // FIXME: Cache the number on the Attr object if non-dependent?
2798 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002799 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2800 SpellingListIndex);
2801 AA->setPackExpansion(IsPackExpansion);
2802 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002803}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002804
Richard Smith848e1f12013-02-01 08:12:08 +00002805void Sema::CheckAlignasUnderalignment(Decl *D) {
2806 assert(D->hasAttrs() && "no attributes on decl");
2807
2808 QualType Ty;
2809 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2810 Ty = VD->getType();
2811 else
2812 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002813 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002814 return;
2815
2816 // C++11 [dcl.align]p5, C11 6.7.5/4:
2817 // The combined effect of all alignment attributes in a declaration shall
2818 // not specify an alignment that is less strict than the alignment that
2819 // would otherwise be required for the entity being declared.
2820 AlignedAttr *AlignasAttr = 0;
2821 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002822 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002823 if (I->isAlignmentDependent())
2824 return;
2825 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002826 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002827 Align = std::max(Align, I->getAlignment(Context));
2828 }
2829
2830 if (AlignasAttr && Align) {
2831 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2832 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2833 if (NaturalAlign > RequestedAlign)
2834 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2835 << Ty << (unsigned)NaturalAlign.getQuantity();
2836 }
2837}
2838
David Majnemer2c4e00a2014-01-29 22:07:36 +00002839bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002840 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002841 MSInheritanceAttr::Spelling SemanticSpelling) {
2842 assert(RD->hasDefinition() && "RD has no definition!");
2843
David Majnemer98c9ee22014-02-07 00:43:07 +00002844 // We may not have seen base specifiers or any virtual methods yet. We will
2845 // have to wait until the record is defined to catch any mismatches.
2846 if (!RD->getDefinition()->isCompleteDefinition())
2847 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002848
David Majnemer98c9ee22014-02-07 00:43:07 +00002849 // The unspecified model never matches what a definition could need.
2850 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2851 return false;
2852
David Majnemer4bb09802014-02-10 19:50:15 +00002853 if (BestCase) {
2854 if (RD->calculateInheritanceModel() == SemanticSpelling)
2855 return false;
2856 } else {
2857 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2858 return false;
2859 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002860
2861 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2862 << 0 /*definition*/;
2863 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2864 << RD->getNameAsString();
2865 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002866}
2867
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002868/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002869/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002870///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002871/// Despite what would be logical, the mode attribute is a decl attribute, not a
2872/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2873/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002874static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002875 // This attribute isn't documented, but glibc uses it. It changes
2876 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002877 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2879 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002880 return;
2881 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002882
Aaron Ballman00e99962013-08-31 01:11:41 +00002883 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2884 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002885
2886 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002887 if (Str.startswith("__") && Str.endswith("__"))
2888 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002889
2890 unsigned DestWidth = 0;
2891 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002892 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002893 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002894 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002895 switch (Str[0]) {
2896 case 'Q': DestWidth = 8; break;
2897 case 'H': DestWidth = 16; break;
2898 case 'S': DestWidth = 32; break;
2899 case 'D': DestWidth = 64; break;
2900 case 'X': DestWidth = 96; break;
2901 case 'T': DestWidth = 128; break;
2902 }
2903 if (Str[1] == 'F') {
2904 IntegerMode = false;
2905 } else if (Str[1] == 'C') {
2906 IntegerMode = false;
2907 ComplexMode = true;
2908 } else if (Str[1] != 'I') {
2909 DestWidth = 0;
2910 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002911 break;
2912 case 4:
2913 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2914 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002915 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002916 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002917 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002918 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002919 break;
2920 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002921 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002922 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002923 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002924 case 11:
2925 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002926 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002927 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002928 }
2929
2930 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002931 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002932 OldTy = TD->getUnderlyingType();
2933 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2934 OldTy = VD->getType();
2935 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002936 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002937 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002938 return;
2939 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002940
John McCall9dd450b2009-09-21 23:43:11 +00002941 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002942 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2943 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002944 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002945 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2946 } else if (ComplexMode) {
2947 if (!OldTy->isComplexType())
2948 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2949 } else {
2950 if (!OldTy->isFloatingType())
2951 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2952 }
2953
Mike Stump87c57ac2009-05-16 07:39:55 +00002954 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2955 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002956 // FIXME: Make sure floating-point mappings are accurate
2957 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002958 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002959 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002960 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002961 }
2962
2963 QualType NewTy;
2964
2965 if (IntegerMode)
2966 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2967 OldTy->isSignedIntegerType());
2968 else
2969 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2970
2971 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002972 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002973 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002974 }
2975
Eli Friedman4735374e2009-03-03 06:41:03 +00002976 if (ComplexMode) {
2977 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002978 }
2979
2980 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002981 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2982 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2983 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002984 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002985
2986 D->addAttr(::new (S.Context)
2987 ModeAttr(Attr.getRange(), S.Context, Name,
2988 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002989}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002990
Chandler Carruthedc2c642011-07-02 00:01:44 +00002991static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002992 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2993 if (!VD->hasGlobalStorage())
2994 S.Diag(Attr.getLoc(),
2995 diag::warn_attribute_requires_functions_or_static_globals)
2996 << Attr.getName();
2997 } else if (!isFunctionOrMethod(D)) {
2998 S.Diag(Attr.getLoc(),
2999 diag::warn_attribute_requires_functions_or_static_globals)
3000 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003001 return;
3002 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003003
Michael Han99315932013-01-24 16:46:58 +00003004 D->addAttr(::new (S.Context)
3005 NoDebugAttr(Attr.getRange(), S.Context,
3006 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003007}
3008
Paul Robinsonf0674352014-03-31 22:29:15 +00003009static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3010 const AttributeList &Attr) {
3011 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3012 return;
3013
3014 D->addAttr(::new (S.Context)
3015 AlwaysInlineAttr(Attr.getRange(), S.Context,
3016 Attr.getAttributeSpellingListIndex()));
3017}
3018
3019static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3020 const AttributeList &Attr) {
3021 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
3022 return;
3023
3024 D->addAttr(::new (S.Context)
3025 OptimizeNoneAttr(Attr.getRange(), S.Context,
3026 Attr.getAttributeSpellingListIndex()));
3027}
3028
Chandler Carruthedc2c642011-07-02 00:01:44 +00003029static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003030 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003031 if (!FD->getReturnType()->isVoidType()) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003032 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3033 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3034 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3035 << FD->getType()
Alp Toker42a16a62014-01-25 23:51:36 +00003036 << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
Aaron Ballman3aff6332013-12-02 19:30:36 +00003037 "void");
3038 } else {
3039 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3040 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003041 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003042 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003043 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003044
Aaron Ballman3aff6332013-12-02 19:30:36 +00003045 D->addAttr(::new (S.Context)
3046 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003047 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003048}
3049
Chandler Carruthedc2c642011-07-02 00:01:44 +00003050static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003051 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003052 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003053 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003054 return;
3055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003056
Michael Han99315932013-01-24 16:46:58 +00003057 D->addAttr(::new (S.Context)
3058 GNUInlineAttr(Attr.getRange(), S.Context,
3059 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003060}
3061
Chandler Carruthedc2c642011-07-02 00:01:44 +00003062static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003063 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003064
Aaron Ballman02df2e02012-12-09 17:45:41 +00003065 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003066 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003067 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3068 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003069 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003070 return;
3071
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003072 if (!isa<ObjCMethodDecl>(D)) {
3073 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3074 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003075 return;
3076 }
3077
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003078 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003079 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003080 D->addAttr(::new (S.Context)
3081 FastCallAttr(Attr.getRange(), S.Context,
3082 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003083 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003084 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003085 D->addAttr(::new (S.Context)
3086 StdCallAttr(Attr.getRange(), S.Context,
3087 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003088 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003089 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003090 D->addAttr(::new (S.Context)
3091 ThisCallAttr(Attr.getRange(), S.Context,
3092 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003093 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003094 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003095 D->addAttr(::new (S.Context)
3096 CDeclAttr(Attr.getRange(), S.Context,
3097 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003098 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003099 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003100 D->addAttr(::new (S.Context)
3101 PascalAttr(Attr.getRange(), S.Context,
3102 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003103 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003104 case AttributeList::AT_MSABI:
3105 D->addAttr(::new (S.Context)
3106 MSABIAttr(Attr.getRange(), S.Context,
3107 Attr.getAttributeSpellingListIndex()));
3108 return;
3109 case AttributeList::AT_SysVABI:
3110 D->addAttr(::new (S.Context)
3111 SysVABIAttr(Attr.getRange(), S.Context,
3112 Attr.getAttributeSpellingListIndex()));
3113 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003114 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003115 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003116 switch (CC) {
3117 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003118 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003119 break;
3120 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003121 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003122 break;
3123 default:
3124 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003125 }
3126
Michael Han99315932013-01-24 16:46:58 +00003127 D->addAttr(::new (S.Context)
3128 PcsAttr(Attr.getRange(), S.Context, PCS,
3129 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003130 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003131 }
Derek Schuffa2020962012-10-16 22:30:41 +00003132 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003133 D->addAttr(::new (S.Context)
3134 PnaclCallAttr(Attr.getRange(), S.Context,
3135 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003136 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003137 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003138 D->addAttr(::new (S.Context)
3139 IntelOclBiccAttr(Attr.getRange(), S.Context,
3140 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003141 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003142
Abramo Bagnara50099372010-04-30 13:10:51 +00003143 default:
3144 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003145 }
3146}
3147
Aaron Ballman02df2e02012-12-09 17:45:41 +00003148bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3149 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003150 if (attr.isInvalid())
3151 return true;
3152
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003153 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003154 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003155 attr.setInvalid();
3156 return true;
3157 }
3158
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003159 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003160 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003161 case AttributeList::AT_CDecl: CC = CC_C; break;
3162 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3163 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3164 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3165 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003166 case AttributeList::AT_MSABI:
3167 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3168 CC_X86_64Win64;
3169 break;
3170 case AttributeList::AT_SysVABI:
3171 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3172 CC_C;
3173 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003174 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003175 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003176 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003177 attr.setInvalid();
3178 return true;
3179 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003180 if (StrRef == "aapcs") {
3181 CC = CC_AAPCS;
3182 break;
3183 } else if (StrRef == "aapcs-vfp") {
3184 CC = CC_AAPCS_VFP;
3185 break;
3186 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003187
3188 attr.setInvalid();
3189 Diag(attr.getLoc(), diag::err_invalid_pcs);
3190 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003191 }
Derek Schuffa2020962012-10-16 22:30:41 +00003192 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003193 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003194 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003195 }
3196
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003197 const TargetInfo &TI = Context.getTargetInfo();
3198 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3199 if (A == TargetInfo::CCCR_Warning) {
3200 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003201
3202 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3203 if (FD)
3204 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3205 TargetInfo::CCMT_NonMember;
3206 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003207 }
3208
John McCall3882ace2011-01-05 12:14:39 +00003209 return false;
3210}
3211
John McCall3882ace2011-01-05 12:14:39 +00003212/// Checks a regparm attribute, returning true if it is ill-formed and
3213/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003214bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3215 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003216 return true;
3217
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003218 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003219 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003220 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003221 }
Eli Friedman7044b762009-03-27 21:06:47 +00003222
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003223 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003224 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003225 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003226 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003227 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003228 }
3229
Douglas Gregore8bbc122011-09-02 00:18:52 +00003230 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003231 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003232 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003233 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003234 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003235 }
3236
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003237 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003238 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003239 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003240 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003241 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003242 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003243 }
3244
John McCall3882ace2011-01-05 12:14:39 +00003245 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003246}
3247
Aaron Ballman66039932013-12-19 00:41:31 +00003248static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3249 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003250 // check the attribute arguments.
3251 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3252 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003253 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3254 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003255 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003256 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003257
Aaron Ballman66039932013-12-19 00:41:31 +00003258 uint32_t MaxThreads, MinBlocks = 0;
3259 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3260 return;
3261 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3262 Attr.getArgAsExpr(1),
3263 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003264 return;
3265
3266 D->addAttr(::new (S.Context)
3267 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3268 MaxThreads, MinBlocks,
3269 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003270}
3271
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003272static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3273 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003274 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003276 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003277 return;
3278 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003279
3280 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003281 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003282
Aaron Ballman00e99962013-08-31 01:11:41 +00003283 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003284
3285 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3287 << Attr.getName() << ExpectedFunctionOrMethod;
3288 return;
3289 }
3290
3291 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003292 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3293 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003294 return;
3295
3296 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003297 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3298 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003299 return;
3300
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003301 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003302 if (IsPointer) {
3303 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003304 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003305 if (!BufferTy->isPointerType()) {
3306 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003307 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003308 }
3309 }
3310
Michael Han99315932013-01-24 16:46:58 +00003311 D->addAttr(::new (S.Context)
3312 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3313 ArgumentIdx, TypeTagIdx, IsPointer,
3314 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003315}
3316
3317static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3318 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003319 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003321 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003322 return;
3323 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003324
3325 if (!checkAttributeNumArgs(S, Attr, 1))
3326 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003327
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003328 if (!isa<VarDecl>(D)) {
3329 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3330 << Attr.getName() << ExpectedVariable;
3331 return;
3332 }
3333
Aaron Ballman00e99962013-08-31 01:11:41 +00003334 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003335 TypeSourceInfo *MatchingCTypeLoc = 0;
3336 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3337 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003338
Michael Han99315932013-01-24 16:46:58 +00003339 D->addAttr(::new (S.Context)
3340 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003341 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003342 Attr.getLayoutCompatible(),
3343 Attr.getMustBeNull(),
3344 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003345}
3346
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003347//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003348// Checker-specific attribute handlers.
3349//===----------------------------------------------------------------------===//
3350
John McCalled433932011-01-25 03:31:58 +00003351static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003352 return type->isDependentType() ||
3353 type->isObjCObjectPointerType() ||
3354 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003355}
3356static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003357 return type->isDependentType() ||
3358 type->isPointerType() ||
3359 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003360}
3361
Chandler Carruthedc2c642011-07-02 00:01:44 +00003362static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003363 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003364 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003365
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003366 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003367 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3368 cf = false;
3369 } else {
3370 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3371 cf = true;
3372 }
3373
3374 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003375 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003376 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003377 return;
3378 }
3379
3380 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003381 param->addAttr(::new (S.Context)
3382 CFConsumedAttr(Attr.getRange(), S.Context,
3383 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003384 else
Michael Han99315932013-01-24 16:46:58 +00003385 param->addAttr(::new (S.Context)
3386 NSConsumedAttr(Attr.getRange(), S.Context,
3387 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003388}
3389
Chandler Carruthedc2c642011-07-02 00:01:44 +00003390static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3391 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003392
John McCalled433932011-01-25 03:31:58 +00003393 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003394
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003395 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003396 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003397 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003398 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003399 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003400 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3401 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003402 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003403 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003404 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003405 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003406 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003407 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003408 return;
3409 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003410
John McCalled433932011-01-25 03:31:58 +00003411 bool typeOK;
3412 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003413 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003414 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003415 case AttributeList::AT_NSReturnsAutoreleased:
3416 case AttributeList::AT_NSReturnsRetained:
3417 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003418 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3419 cf = false;
3420 break;
3421
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003422 case AttributeList::AT_CFReturnsRetained:
3423 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003424 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3425 cf = true;
3426 break;
3427 }
3428
3429 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003430 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003431 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003432 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003433 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003434
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003435 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003436 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003437 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003438 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003439 D->addAttr(::new (S.Context)
3440 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3441 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003442 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003443 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003444 D->addAttr(::new (S.Context)
3445 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3446 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003447 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003448 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003449 D->addAttr(::new (S.Context)
3450 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3451 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003452 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003453 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003454 D->addAttr(::new (S.Context)
3455 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3456 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003457 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003458 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003459 D->addAttr(::new (S.Context)
3460 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3461 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003462 return;
3463 };
3464}
3465
John McCallcf166702011-07-22 08:53:00 +00003466static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3467 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003468 const int EP_ObjCMethod = 1;
3469 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003470
John McCallcf166702011-07-22 08:53:00 +00003471 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003472 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003473 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003474 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003475 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003476 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003477
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003478 if (!resultType->isReferenceType() &&
3479 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003480 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003481 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003482 << attr.getName()
3483 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003484 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003485
3486 // Drop the attribute.
3487 return;
3488 }
3489
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003490 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003491 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3492 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003493}
3494
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003495static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3496 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003497 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003498
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003499 DeclContext *DC = method->getDeclContext();
3500 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3501 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3502 << attr.getName() << 0;
3503 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3504 return;
3505 }
3506 if (method->getMethodFamily() == OMF_dealloc) {
3507 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3508 << attr.getName() << 1;
3509 return;
3510 }
3511
Michael Han99315932013-01-24 16:46:58 +00003512 method->addAttr(::new (S.Context)
3513 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3514 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003515}
3516
Aaron Ballmanfb763042013-12-02 18:05:46 +00003517static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3518 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003519 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003520 return;
John McCall32f5fe12011-09-30 05:12:12 +00003521
Aaron Ballmanfb763042013-12-02 18:05:46 +00003522 D->addAttr(::new (S.Context)
3523 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3524 Attr.getAttributeSpellingListIndex()));
3525}
3526
3527static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3528 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003529 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003530 return;
3531
3532 D->addAttr(::new (S.Context)
3533 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3534 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003535}
3536
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003537static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3538 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003539 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003540
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003541 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003542 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003543 return;
3544 }
3545
3546 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003547 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003548 Attr.getAttributeSpellingListIndex()));
3549}
3550
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003551static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3552 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003553 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003554
3555 if (!Parm) {
3556 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3557 return;
3558 }
3559
3560 D->addAttr(::new (S.Context)
3561 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3562 Attr.getAttributeSpellingListIndex()));
3563}
3564
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003565static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3566 const AttributeList &Attr) {
3567 IdentifierInfo *RelatedClass =
3568 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3569 if (!RelatedClass) {
3570 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3571 return;
3572 }
3573 IdentifierInfo *ClassMethod =
3574 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3575 IdentifierInfo *InstanceMethod =
3576 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3577 D->addAttr(::new (S.Context)
3578 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3579 ClassMethod, InstanceMethod,
3580 Attr.getAttributeSpellingListIndex()));
3581}
3582
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003583static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3584 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003585 ObjCInterfaceDecl *IFace;
3586 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3587 IFace = CatDecl->getClassInterface();
3588 else
3589 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003590 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003591 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003592 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3593 Attr.getAttributeSpellingListIndex()));
3594}
3595
Chandler Carruthedc2c642011-07-02 00:01:44 +00003596static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3597 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003598 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003599
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003600 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003601 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003602}
3603
Chandler Carruthedc2c642011-07-02 00:01:44 +00003604static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3605 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003606 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003607 QualType type = vd->getType();
3608
3609 if (!type->isDependentType() &&
3610 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003611 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003612 << type;
3613 return;
3614 }
3615
3616 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3617
3618 // If we have no lifetime yet, check the lifetime we're presumably
3619 // going to infer.
3620 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3621 lifetime = type->getObjCARCImplicitLifetime();
3622
3623 switch (lifetime) {
3624 case Qualifiers::OCL_None:
3625 assert(type->isDependentType() &&
3626 "didn't infer lifetime for non-dependent type?");
3627 break;
3628
3629 case Qualifiers::OCL_Weak: // meaningful
3630 case Qualifiers::OCL_Strong: // meaningful
3631 break;
3632
3633 case Qualifiers::OCL_ExplicitNone:
3634 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003635 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003636 << (lifetime == Qualifiers::OCL_Autoreleasing);
3637 break;
3638 }
3639
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003640 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003641 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3642 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003643}
3644
Francois Picheta83957a2010-12-19 06:50:37 +00003645//===----------------------------------------------------------------------===//
3646// Microsoft specific attribute handlers.
3647//===----------------------------------------------------------------------===//
3648
Chandler Carruthedc2c642011-07-02 00:01:44 +00003649static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003650 if (!S.LangOpts.CPlusPlus) {
3651 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3652 << Attr.getName() << AttributeLangSupport::C;
3653 return;
3654 }
3655
Aaron Ballman60e705e2013-11-24 20:58:02 +00003656 if (!isa<CXXRecordDecl>(D)) {
3657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3658 << Attr.getName() << ExpectedClass;
3659 return;
3660 }
3661
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003662 StringRef StrRef;
3663 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003664 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003665 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003666
David Majnemer89085342013-08-09 08:56:20 +00003667 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3668 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003669 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3670 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003671
Reid Kleckner140c4a72013-05-17 14:04:52 +00003672 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003673 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003674 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003675 return;
3676 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003677
David Majnemer89085342013-08-09 08:56:20 +00003678 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003679 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003680 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003681 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003682 return;
3683 }
David Majnemer89085342013-08-09 08:56:20 +00003684 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003685 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003686 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003687 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003688 }
Francois Picheta83957a2010-12-19 06:50:37 +00003689
David Majnemer89085342013-08-09 08:56:20 +00003690 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3691 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003692}
3693
David Majnemer2c4e00a2014-01-29 22:07:36 +00003694static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3695 if (!S.LangOpts.CPlusPlus) {
3696 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3697 << Attr.getName() << AttributeLangSupport::C;
3698 return;
3699 }
3700 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003701 D, Attr.getRange(), /*BestCase=*/true,
3702 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003703 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3704 if (IA)
3705 D->addAttr(IA);
3706}
3707
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003708static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3709 const AttributeList &Attr) {
3710 VarDecl *VD = cast<VarDecl>(D);
3711 if (!S.Context.getTargetInfo().isTLSSupported()) {
3712 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3713 return;
3714 }
3715 if (VD->getTSCSpec() != TSCS_unspecified) {
3716 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3717 return;
3718 }
3719 if (VD->hasLocalStorage()) {
3720 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3721 return;
3722 }
3723 VD->addAttr(::new (S.Context) ThreadAttr(
3724 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3725}
3726
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003727static void handleARMInterruptAttr(Sema &S, Decl *D,
3728 const AttributeList &Attr) {
3729 // Check the attribute arguments.
3730 if (Attr.getNumArgs() > 1) {
3731 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3732 << Attr.getName() << 1;
3733 return;
3734 }
3735
3736 StringRef Str;
3737 SourceLocation ArgLoc;
3738
3739 if (Attr.getNumArgs() == 0)
3740 Str = "";
3741 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3742 return;
3743
3744 ARMInterruptAttr::InterruptType Kind;
3745 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3746 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3747 << Attr.getName() << Str << ArgLoc;
3748 return;
3749 }
3750
3751 unsigned Index = Attr.getAttributeSpellingListIndex();
3752 D->addAttr(::new (S.Context)
3753 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3754}
3755
3756static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3757 const AttributeList &Attr) {
3758 if (!checkAttributeNumArgs(S, Attr, 1))
3759 return;
3760
3761 if (!Attr.isArgExpr(0)) {
3762 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3763 << AANT_ArgumentIntegerConstant;
3764 return;
3765 }
3766
3767 // FIXME: Check for decl - it should be void ()(void).
3768
3769 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3770 llvm::APSInt NumParams(32);
3771 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3772 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3773 << Attr.getName() << AANT_ArgumentIntegerConstant
3774 << NumParamsExpr->getSourceRange();
3775 return;
3776 }
3777
3778 unsigned Num = NumParams.getLimitedValue(255);
3779 if ((Num & 1) || Num > 30) {
3780 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3781 << Attr.getName() << (int)NumParams.getSExtValue()
3782 << NumParamsExpr->getSourceRange();
3783 return;
3784 }
3785
Aaron Ballman36a53502014-01-16 13:03:14 +00003786 D->addAttr(::new (S.Context)
3787 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3788 Attr.getAttributeSpellingListIndex()));
3789 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003790}
3791
3792static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3793 // Dispatch the interrupt attribute based on the current target.
3794 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3795 handleMSP430InterruptAttr(S, D, Attr);
3796 else
3797 handleARMInterruptAttr(S, D, Attr);
3798}
3799
3800static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3801 const AttributeList& Attr) {
3802 // If we try to apply it to a function pointer, don't warn, but don't
3803 // do anything, either. It doesn't matter anyway, because there's nothing
3804 // special about calling a force_align_arg_pointer function.
3805 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3806 if (VD && VD->getType()->isFunctionPointerType())
3807 return;
3808 // Also don't warn on function pointer typedefs.
3809 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3810 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3811 TD->getUnderlyingType()->isFunctionType()))
3812 return;
3813 // Attribute can only be applied to function types.
3814 if (!isa<FunctionDecl>(D)) {
3815 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3816 << Attr.getName() << /* function */0;
3817 return;
3818 }
3819
Aaron Ballman36a53502014-01-16 13:03:14 +00003820 D->addAttr(::new (S.Context)
3821 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3822 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003823}
3824
3825DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3826 unsigned AttrSpellingListIndex) {
3827 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003828 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003829 return NULL;
3830 }
3831
3832 if (D->hasAttr<DLLImportAttr>())
3833 return NULL;
3834
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003835 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003836}
3837
3838static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3839 // Attribute can be applied only to functions or variables.
3840 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3841 if (!FD && !isa<VarDecl>(D)) {
3842 // Apparently Visual C++ thinks it is okay to not emit a warning
3843 // in this case, so only emit a warning when -fms-extensions is not
3844 // specified.
3845 if (!S.getLangOpts().MicrosoftExt)
3846 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003847 << Attr.getName() << ExpectedVariableOrFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003848 return;
3849 }
3850
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003851 unsigned Index = Attr.getAttributeSpellingListIndex();
3852 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
3853 if (NewAttr)
3854 D->addAttr(NewAttr);
3855}
3856
3857DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3858 unsigned AttrSpellingListIndex) {
3859 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003860 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003861 D->dropAttr<DLLImportAttr>();
3862 }
3863
3864 if (D->hasAttr<DLLExportAttr>())
3865 return NULL;
3866
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003867 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003868}
3869
3870static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003871 unsigned Index = Attr.getAttributeSpellingListIndex();
3872 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
3873 if (NewAttr)
3874 D->addAttr(NewAttr);
3875}
3876
David Majnemer2c4e00a2014-01-29 22:07:36 +00003877MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003878Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003879 unsigned AttrSpellingListIndex,
3880 MSInheritanceAttr::Spelling SemanticSpelling) {
3881 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3882 if (IA->getSemanticSpelling() == SemanticSpelling)
3883 return 0;
3884 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3885 << 1 /*previous declaration*/;
3886 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3887 D->dropAttr<MSInheritanceAttr>();
3888 }
3889
3890 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3891 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003892 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3893 SemanticSpelling)) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00003894 return 0;
3895 }
3896 } else {
3897 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3898 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3899 << 1 /*partial specialization*/;
3900 return 0;
3901 }
3902 if (RD->getDescribedClassTemplate()) {
3903 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3904 << 0 /*primary template*/;
3905 return 0;
3906 }
3907 }
3908
3909 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003910 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003911}
3912
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003913static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3914 // The capability attributes take a single string parameter for the name of
3915 // the capability they represent. The lockable attribute does not take any
3916 // parameters. However, semantically, both attributes represent the same
3917 // concept, and so they use the same semantic attribute. Eventually, the
3918 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003919 //
3920 // For backwards compatibility, any capability which has no specified string
3921 // literal will be considered a "mutex."
3922 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003923 SourceLocation LiteralLoc;
3924 if (Attr.getKind() == AttributeList::AT_Capability &&
3925 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3926 return;
3927
Aaron Ballman6c810072014-03-05 21:47:13 +00003928 // Currently, there are only two names allowed for a capability: role and
3929 // mutex (case insensitive). Diagnose other capability names.
3930 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3931 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3932
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003933 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3934 Attr.getAttributeSpellingListIndex()));
3935}
3936
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003937static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3938 const AttributeList &Attr) {
3939 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3940 Attr.getArgAsExpr(0),
3941 Attr.getAttributeSpellingListIndex()));
3942}
3943
3944static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3945 const AttributeList &Attr) {
3946 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003947 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003948 return;
3949
3950 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3951 S.Context,
3952 Args.data(), Args.size(),
3953 Attr.getAttributeSpellingListIndex()));
3954}
3955
3956static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3957 const AttributeList &Attr) {
3958 SmallVector<Expr*, 2> Args;
3959 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3960 return;
3961
3962 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3963 S.Context,
3964 Attr.getArgAsExpr(0),
3965 Args.data(),
3966 Args.size(),
3967 Attr.getAttributeSpellingListIndex()));
3968}
3969
3970static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3971 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003972 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003973 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003974 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003975
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003976 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
3977 Attr.getRange(), S.Context, Args.data(), Args.size(),
3978 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003979}
3980
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003981static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3982 const AttributeList &Attr) {
3983 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3984 return;
3985
3986 // check that all arguments are lockable objects
3987 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003988 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003989 if (Args.empty())
3990 return;
3991
3992 RequiresCapabilityAttr *RCA = ::new (S.Context)
3993 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
3994 Args.size(), Attr.getAttributeSpellingListIndex());
3995
3996 D->addAttr(RCA);
3997}
3998
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003999/// Handles semantic checking for features that are common to all attributes,
4000/// such as checking whether a parameter was properly specified, or the correct
4001/// number of arguments were passed, etc.
4002static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4003 const AttributeList &Attr) {
4004 // Several attributes carry different semantics than the parsing requires, so
4005 // those are opted out of the common handling.
4006 //
4007 // We also bail on unknown and ignored attributes because those are handled
4008 // as part of the target-specific handling logic.
4009 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004010 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004011 return false;
4012
Aaron Ballman3aff6332013-12-02 19:30:36 +00004013 // Check whether the attribute requires specific language extensions to be
4014 // enabled.
4015 if (!Attr.diagnoseLangOpts(S))
4016 return true;
4017
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004018 // If there are no optional arguments, then checking for the argument count
4019 // is trivial.
4020 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4021 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4022 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004023
4024 // Check whether the attribute appertains to the given subject.
4025 if (!Attr.diagnoseAppertainsTo(S, D))
4026 return true;
4027
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004028 return false;
4029}
4030
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004031//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004032// Top Level Sema Entry Points
4033//===----------------------------------------------------------------------===//
4034
Richard Smithf8a75c32013-08-29 00:47:48 +00004035/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4036/// the attribute applies to decls. If the attribute is a type attribute, just
4037/// silently ignore it if a GNU attribute.
4038static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4039 const AttributeList &Attr,
4040 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004041 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004042 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004043
Richard Smithf8a75c32013-08-29 00:47:48 +00004044 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4045 // instead.
4046 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4047 return;
4048
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004049 // Unknown attributes are automatically warned on. Target-specific attributes
4050 // which do not apply to the current target architecture are treated as
4051 // though they were unknown attributes.
4052 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4053 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004054 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4055 ? diag::warn_unhandled_ms_attribute_ignored
4056 : diag::warn_unknown_attribute_ignored)
4057 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004058 return;
4059 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004060
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004061 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4062 return;
4063
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004064 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004065 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004066 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004067 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004068 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004069 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004070 handleInterruptAttr(S, D, Attr);
4071 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004072 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004073 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4074 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004075 case AttributeList::AT_DLLExport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004076 handleDLLExportAttr(S, D, Attr);
4077 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004078 case AttributeList::AT_DLLImport:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004079 handleDLLImportAttr(S, D, Attr);
4080 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004081 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004082 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4083 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004084 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004085 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4086 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004087 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004088 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4089 break;
4090 case AttributeList::AT_IBOutlet:
4091 handleIBOutlet(S, D, Attr);
4092 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004093 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004094 handleIBOutletCollection(S, D, Attr);
4095 break;
4096 case AttributeList::AT_Alias:
4097 handleAliasAttr(S, D, Attr);
4098 break;
4099 case AttributeList::AT_Aligned:
4100 handleAlignedAttr(S, D, Attr);
4101 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004102 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004103 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004104 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004105 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004106 handleAnalyzerNoReturnAttr(S, D, Attr);
4107 break;
4108 case AttributeList::AT_TLSModel:
4109 handleTLSModelAttr(S, D, Attr);
4110 break;
4111 case AttributeList::AT_Annotate:
4112 handleAnnotateAttr(S, D, Attr);
4113 break;
4114 case AttributeList::AT_Availability:
4115 handleAvailabilityAttr(S, D, Attr);
4116 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004118 handleDependencyAttr(S, scope, D, Attr);
4119 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004120 case AttributeList::AT_Common:
4121 handleCommonAttr(S, D, Attr);
4122 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004123 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004124 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4125 break;
4126 case AttributeList::AT_Constructor:
4127 handleConstructorAttr(S, D, Attr);
4128 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004129 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004130 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4131 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004132 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004133 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004134 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004135 case AttributeList::AT_Destructor:
4136 handleDestructorAttr(S, D, Attr);
4137 break;
4138 case AttributeList::AT_EnableIf:
4139 handleEnableIfAttr(S, D, Attr);
4140 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004141 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004142 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004143 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004144 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004145 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004146 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004147 case AttributeList::AT_OptimizeNone:
4148 handleOptimizeNoneAttr(S, D, Attr);
4149 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004150 case AttributeList::AT_Format:
4151 handleFormatAttr(S, D, Attr);
4152 break;
4153 case AttributeList::AT_FormatArg:
4154 handleFormatArgAttr(S, D, Attr);
4155 break;
4156 case AttributeList::AT_CUDAGlobal:
4157 handleGlobalAttr(S, D, Attr);
4158 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004159 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004160 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4161 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004162 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004163 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4164 break;
4165 case AttributeList::AT_GNUInline:
4166 handleGNUInlineAttr(S, D, Attr);
4167 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004168 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004169 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004170 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004171 case AttributeList::AT_Malloc:
4172 handleMallocAttr(S, D, Attr);
4173 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004174 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004175 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4176 break;
4177 case AttributeList::AT_Mode:
4178 handleModeAttr(S, D, Attr);
4179 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004180 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004181 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4182 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004183 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004184 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4185 handleNonNullAttrParameter(S, PVD, Attr);
4186 else
4187 handleNonNullAttr(S, D, Attr);
4188 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004189 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004190 handleReturnsNonNullAttr(S, D, Attr);
4191 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004192 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004193 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4194 break;
4195 case AttributeList::AT_Ownership:
4196 handleOwnershipAttr(S, D, Attr);
4197 break;
4198 case AttributeList::AT_Cold:
4199 handleColdAttr(S, D, Attr);
4200 break;
4201 case AttributeList::AT_Hot:
4202 handleHotAttr(S, D, Attr);
4203 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004204 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004205 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4206 break;
4207 case AttributeList::AT_NoReturn:
4208 handleNoReturnAttr(S, D, Attr);
4209 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004210 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004211 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4212 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004213 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004214 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4215 break;
4216 case AttributeList::AT_VecReturn:
4217 handleVecReturnAttr(S, D, Attr);
4218 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004219
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004221 handleObjCOwnershipAttr(S, D, Attr);
4222 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004223 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004224 handleObjCPreciseLifetimeAttr(S, D, Attr);
4225 break;
John McCall31168b02011-06-15 23:02:42 +00004226
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004227 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004228 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4229 break;
John McCallcf166702011-07-22 08:53:00 +00004230
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004231 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004232 handleObjCRequiresSuperAttr(S, D, Attr);
4233 break;
4234
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004235 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004236 handleObjCBridgeAttr(S, scope, D, Attr);
4237 break;
4238
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004239 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004240 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4241 break;
4242
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004243 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004244 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4245 break;
John McCallf1e8b342011-09-29 07:17:38 +00004246
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004247 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004248 handleObjCDesignatedInitializer(S, D, Attr);
4249 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004250
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004251 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004252 handleCFAuditedTransferAttr(S, D, Attr);
4253 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004254 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004255 handleCFUnknownTransferAttr(S, D, Attr);
4256 break;
John McCall32f5fe12011-09-30 05:12:12 +00004257
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004258 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004259 case AttributeList::AT_NSConsumed:
4260 handleNSConsumedAttr(S, D, Attr);
4261 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004262 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004263 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4264 break;
John McCalled433932011-01-25 03:31:58 +00004265
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004266 case AttributeList::AT_NSReturnsAutoreleased:
4267 case AttributeList::AT_NSReturnsNotRetained:
4268 case AttributeList::AT_CFReturnsNotRetained:
4269 case AttributeList::AT_NSReturnsRetained:
4270 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004271 handleNSReturnsRetainedAttr(S, D, Attr);
4272 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004273 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004274 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4275 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004276 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004277 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4278 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004279 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004280 handleVecTypeHint(S, D, Attr);
4281 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004282
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004283 case AttributeList::AT_InitPriority:
4284 handleInitPriorityAttr(S, D, Attr);
4285 break;
4286
4287 case AttributeList::AT_Packed:
4288 handlePackedAttr(S, D, Attr);
4289 break;
4290 case AttributeList::AT_Section:
4291 handleSectionAttr(S, D, Attr);
4292 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004293 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004294 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004295 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004296 case AttributeList::AT_ArcWeakrefUnavailable:
4297 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4298 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004299 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004300 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4301 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004302 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004303 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004304 break;
4305 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004306 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4307 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004308 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004309 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4310 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004311 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004312 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4313 break;
4314 case AttributeList::AT_Used:
4315 handleUsedAttr(S, D, Attr);
4316 break;
John McCalld041a9b2013-02-20 01:54:26 +00004317 case AttributeList::AT_Visibility:
4318 handleVisibilityAttr(S, D, Attr, false);
4319 break;
4320 case AttributeList::AT_TypeVisibility:
4321 handleVisibilityAttr(S, D, Attr, true);
4322 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004323 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004324 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4325 break;
4326 case AttributeList::AT_WarnUnusedResult:
4327 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004328 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004329 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004330 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4331 break;
4332 case AttributeList::AT_WeakRef:
4333 handleWeakRefAttr(S, D, Attr);
4334 break;
4335 case AttributeList::AT_WeakImport:
4336 handleWeakImportAttr(S, D, Attr);
4337 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004338 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004339 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004340 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004341 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004342 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4343 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004344 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004345 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004346 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004347 case AttributeList::AT_ObjCNSObject:
4348 handleObjCNSObject(S, D, Attr);
4349 break;
4350 case AttributeList::AT_Blocks:
4351 handleBlocksAttr(S, D, Attr);
4352 break;
4353 case AttributeList::AT_Sentinel:
4354 handleSentinelAttr(S, D, Attr);
4355 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004356 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004357 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4358 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004359 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004360 handleSimpleAttribute<PureAttr>(S, D, Attr);
4361 break;
4362 case AttributeList::AT_Cleanup:
4363 handleCleanupAttr(S, D, Attr);
4364 break;
4365 case AttributeList::AT_NoDebug:
4366 handleNoDebugAttr(S, D, Attr);
4367 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004368 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004369 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4370 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004371 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004372 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4373 break;
4374 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4375 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4376 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004377 case AttributeList::AT_StdCall:
4378 case AttributeList::AT_CDecl:
4379 case AttributeList::AT_FastCall:
4380 case AttributeList::AT_ThisCall:
4381 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004382 case AttributeList::AT_MSABI:
4383 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004384 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004385 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004386 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004387 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004388 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004389 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004390 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4391 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004392 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004393 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4394 break;
John McCall8d32c052012-05-22 21:28:12 +00004395
4396 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004397 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004398 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004399 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004400 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004401 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004402 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004403 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004404 handleMSInheritanceAttr(S, D, Attr);
4405 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004406 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004407 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4408 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004409 case AttributeList::AT_Thread:
4410 handleDeclspecThreadAttr(S, D, Attr);
4411 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004412
4413 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004414 case AttributeList::AT_AssertExclusiveLock:
4415 handleAssertExclusiveLockAttr(S, D, Attr);
4416 break;
4417 case AttributeList::AT_AssertSharedLock:
4418 handleAssertSharedLockAttr(S, D, Attr);
4419 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004420 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004421 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4422 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004423 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004424 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004425 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004426 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004427 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4428 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004429 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004430 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004431 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004432 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004433 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004434 break;
4435 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004436 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004437 break;
4438 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004439 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004440 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004441 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004442 handleGuardedByAttr(S, D, Attr);
4443 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004444 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004445 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004446 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004447 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004448 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004449 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004450 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004451 handleLockReturnedAttr(S, D, Attr);
4452 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004453 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004454 handleLocksExcludedAttr(S, D, Attr);
4455 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004456 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004457 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004458 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004459 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004460 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004461 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004462 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004463 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004464 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004465
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004466 // Capability analysis attributes.
4467 case AttributeList::AT_Capability:
4468 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004469 handleCapabilityAttr(S, D, Attr);
4470 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004471 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004472 handleRequiresCapabilityAttr(S, D, Attr);
4473 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004474
4475 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004476 handleAssertCapabilityAttr(S, D, Attr);
4477 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004478 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004479 handleAcquireCapabilityAttr(S, D, Attr);
4480 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004481 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004482 handleReleaseCapabilityAttr(S, D, Attr);
4483 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004484 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004485 handleTryAcquireCapabilityAttr(S, D, Attr);
4486 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004487
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004488 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004489 case AttributeList::AT_Consumable:
4490 handleConsumableAttr(S, D, Attr);
4491 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004492 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004493 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4494 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004495 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004496 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4497 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004498 case AttributeList::AT_CallableWhen:
4499 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004500 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004501 case AttributeList::AT_ParamTypestate:
4502 handleParamTypestateAttr(S, D, Attr);
4503 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004504 case AttributeList::AT_ReturnTypestate:
4505 handleReturnTypestateAttr(S, D, Attr);
4506 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004507 case AttributeList::AT_SetTypestate:
4508 handleSetTypestateAttr(S, D, Attr);
4509 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004510 case AttributeList::AT_TestTypestate:
4511 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004512 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004513
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004514 // Type safety attributes.
4515 case AttributeList::AT_ArgumentWithTypeTag:
4516 handleArgumentWithTypeTagAttr(S, D, Attr);
4517 break;
4518 case AttributeList::AT_TypeTagForDatatype:
4519 handleTypeTagForDatatypeAttr(S, D, Attr);
4520 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004521 }
4522}
4523
4524/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4525/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004526void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004527 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004528 bool IncludeCXX11Attributes) {
4529 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004530 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004531
Joey Gouly2cd9db12013-12-13 16:15:28 +00004532 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004533 // GCC accepts
4534 // static int a9 __attribute__((weakref));
4535 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004536 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004537 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4538 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004539 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004540 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004541 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004542
4543 if (!D->hasAttr<OpenCLKernelAttr>()) {
4544 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004545 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4546 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004547 D->setInvalidDecl();
4548 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004549 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4550 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004551 D->setInvalidDecl();
4552 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004553 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4554 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004555 D->setInvalidDecl();
4556 }
4557 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004558}
4559
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004560// Annotation attributes are the only attributes allowed after an access
4561// specifier.
4562bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4563 const AttributeList *AttrList) {
4564 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004565 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004566 handleAnnotateAttr(*this, ASDecl, *l);
4567 } else {
4568 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4569 return true;
4570 }
4571 }
4572
4573 return false;
4574}
4575
John McCall42856de2011-10-01 05:17:03 +00004576/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4577/// contains any decl attributes that we should warn about.
4578static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4579 for ( ; A; A = A->getNext()) {
4580 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004581 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004582 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4583
4584 if (A->getKind() == AttributeList::UnknownAttribute) {
4585 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4586 << A->getName() << A->getRange();
4587 } else {
4588 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4589 << A->getName() << A->getRange();
4590 }
4591 }
4592}
4593
4594/// checkUnusedDeclAttributes - Given a declarator which is not being
4595/// used to build a declaration, complain about any decl attributes
4596/// which might be lying around on it.
4597void Sema::checkUnusedDeclAttributes(Declarator &D) {
4598 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4599 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4600 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4601 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4602}
4603
Ryan Flynn7d470f32009-07-30 03:15:39 +00004604/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004605/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004606NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4607 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004608 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004609 NamedDecl *NewD = 0;
4610 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004611 FunctionDecl *NewFD;
4612 // FIXME: Missing call to CheckFunctionDeclaration().
4613 // FIXME: Mangling?
4614 // FIXME: Is the qualifier info correct?
4615 // FIXME: Is the DeclContext correct?
4616 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4617 Loc, Loc, DeclarationName(II),
4618 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004619 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004620 FD->hasPrototype(),
4621 false/*isConstexprSpecified*/);
4622 NewD = NewFD;
4623
4624 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004625 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004626
4627 // Fake up parameter variables; they are declared as if this were
4628 // a typedef.
4629 QualType FDTy = FD->getType();
4630 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4631 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004632 for (const auto &AI : FT->param_types()) {
4633 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004634 Param->setScopeInfo(0, Params.size());
4635 Params.push_back(Param);
4636 }
David Blaikie9c70e042011-09-21 18:16:56 +00004637 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004638 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004639 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4640 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004641 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004642 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004643 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004644 if (VD->getQualifier()) {
4645 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004646 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004647 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004648 }
4649 return NewD;
4650}
4651
James Dennett634962f2012-06-14 21:40:34 +00004652/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004653/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004654void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004655 if (W.getUsed()) return; // only do this once
4656 W.setUsed(true);
4657 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4658 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004659 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004660 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4661 W.getLocation()));
4662 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004663 WeakTopLevelDecl.push_back(NewD);
4664 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4665 // to insert Decl at TU scope, sorry.
4666 DeclContext *SavedContext = CurContext;
4667 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004668 NewD->setDeclContext(CurContext);
4669 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004670 PushOnScopeChains(NewD, S);
4671 CurContext = SavedContext;
4672 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004673 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004674 }
4675}
4676
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004677void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4678 // It's valid to "forward-declare" #pragma weak, in which case we
4679 // have to do this.
4680 LoadExternalWeakUndeclaredIdentifiers();
4681 if (!WeakUndeclaredIdentifiers.empty()) {
4682 NamedDecl *ND = NULL;
4683 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4684 if (VD->isExternC())
4685 ND = VD;
4686 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4687 if (FD->isExternC())
4688 ND = FD;
4689 if (ND) {
4690 if (IdentifierInfo *Id = ND->getIdentifier()) {
4691 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4692 = WeakUndeclaredIdentifiers.find(Id);
4693 if (I != WeakUndeclaredIdentifiers.end()) {
4694 WeakInfo W = I->second;
4695 DeclApplyPragmaWeak(S, ND, W);
4696 WeakUndeclaredIdentifiers[Id] = W;
4697 }
4698 }
4699 }
4700 }
4701}
4702
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004703/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4704/// it, apply them to D. This is a bit tricky because PD can have attributes
4705/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004706void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004707 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004708 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004709 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004710
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004711 // Walk the declarator structure, applying decl attributes that were in a type
4712 // position to the decl itself. This handles cases like:
4713 // int *__attr__(x)** D;
4714 // when X is a decl attribute.
4715 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4716 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004717 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004718
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004719 // Finally, apply any attributes on the decl itself.
4720 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004721 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004722}
John McCall28a6aea2009-11-04 02:18:39 +00004723
John McCall31168b02011-06-15 23:02:42 +00004724/// Is the given declaration allowed to use a forbidden type?
4725static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4726 // Private ivars are always okay. Unfortunately, people don't
4727 // always properly make their ivars private, even in system headers.
4728 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004729 // Function declarations in sys headers will be marked unavailable.
4730 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4731 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004732 return false;
4733
4734 // Require it to be declared in a system header.
4735 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4736}
4737
4738/// Handle a delayed forbidden-type diagnostic.
4739static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4740 Decl *decl) {
4741 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004742 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4743 "this system declaration uses an unsupported type",
4744 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004745 return;
4746 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004747 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004748 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004749 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004750 // kind of forbidden type messages on unavailable functions.
4751 if (FD->hasAttr<UnavailableAttr>() &&
4752 diag.getForbiddenTypeDiagnostic() ==
4753 diag::err_arc_array_param_no_ownership) {
4754 diag.Triggered = true;
4755 return;
4756 }
4757 }
John McCall31168b02011-06-15 23:02:42 +00004758
4759 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4760 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4761 diag.Triggered = true;
4762}
4763
John McCall2ec85372012-05-07 06:16:41 +00004764void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4765 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004766 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004767 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004768
John McCall2ec85372012-05-07 06:16:41 +00004769 // When delaying diagnostics to run in the context of a parsed
4770 // declaration, we only want to actually emit anything if parsing
4771 // succeeds.
4772 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004773
John McCall2ec85372012-05-07 06:16:41 +00004774 // We emit all the active diagnostics in this pool or any of its
4775 // parents. In general, we'll get one pool for the decl spec
4776 // and a child pool for each declarator; in a decl group like:
4777 // deprecated_typedef foo, *bar, baz();
4778 // only the declarator pops will be passed decls. This is correct;
4779 // we really do need to consider delayed diagnostics from the decl spec
4780 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004781 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004782 do {
John McCall6347b682012-05-07 06:16:58 +00004783 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004784 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4785 // This const_cast is a bit lame. Really, Triggered should be mutable.
4786 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004787 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004788 continue;
4789
John McCallc1465822011-02-14 07:13:47 +00004790 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004791 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004792 case DelayedDiagnostic::Unavailable:
4793 // Don't bother giving deprecation/unavailable diagnostics if
4794 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004795 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004796 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004797 break;
4798
4799 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004800 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004801 break;
John McCall31168b02011-06-15 23:02:42 +00004802
4803 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004804 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004805 break;
John McCall86121512010-01-27 03:50:35 +00004806 }
4807 }
John McCall2ec85372012-05-07 06:16:41 +00004808 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004809}
4810
John McCall6347b682012-05-07 06:16:58 +00004811/// Given a set of delayed diagnostics, re-emit them as if they had
4812/// been delayed in the current context instead of in the given pool.
4813/// Essentially, this just moves them to the current pool.
4814void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4815 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4816 assert(curPool && "re-emitting in undelayed context not supported");
4817 curPool->steal(pool);
4818}
4819
John McCall28a6aea2009-11-04 02:18:39 +00004820static bool isDeclDeprecated(Decl *D) {
4821 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004822 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004823 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004824 // A category implicitly has the availability of the interface.
4825 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4826 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004827 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4828 return false;
4829}
4830
Ted Kremenekb79ee572013-12-18 23:30:06 +00004831static bool isDeclUnavailable(Decl *D) {
4832 do {
4833 if (D->isUnavailable())
4834 return true;
4835 // A category implicitly has the availability of the interface.
4836 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4837 return CatD->getClassInterface()->isUnavailable();
4838 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4839 return false;
4840}
4841
Eli Friedman971bfa12012-08-08 21:52:41 +00004842static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004843DoEmitAvailabilityWarning(Sema &S,
4844 DelayedDiagnostic::DDKind K,
4845 Decl *Ctx,
4846 const NamedDecl *D,
4847 StringRef Message,
4848 SourceLocation Loc,
4849 const ObjCInterfaceDecl *UnknownObjCClass,
4850 const ObjCPropertyDecl *ObjCProperty) {
4851
4852 // Diagnostics for deprecated or unavailable.
4853 unsigned diag, diag_message, diag_fwdclass_message;
4854
4855 // Matches 'diag::note_property_attribute' options.
4856 unsigned property_note_select;
4857
4858 // Matches diag::note_availability_specified_here.
4859 unsigned available_here_select_kind;
4860
4861 // Don't warn if our current context is deprecated or unavailable.
4862 switch (K) {
4863 case DelayedDiagnostic::Deprecation:
4864 if (isDeclDeprecated(Ctx))
4865 return;
4866 diag = diag::warn_deprecated;
4867 diag_message = diag::warn_deprecated_message;
4868 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4869 property_note_select = /* deprecated */ 0;
4870 available_here_select_kind = /* deprecated */ 2;
4871 break;
4872
4873 case DelayedDiagnostic::Unavailable:
4874 if (isDeclUnavailable(Ctx))
4875 return;
4876 diag = diag::err_unavailable;
4877 diag_message = diag::err_unavailable_message;
4878 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4879 property_note_select = /* unavailable */ 1;
4880 available_here_select_kind = /* unavailable */ 0;
4881 break;
4882
4883 default:
4884 llvm_unreachable("Neither a deprecation or unavailable kind");
4885 }
4886
Eli Friedman971bfa12012-08-08 21:52:41 +00004887 DeclarationName Name = D->getDeclName();
4888 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004889 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004890 if (ObjCProperty)
4891 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4892 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004893 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004894 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004895 if (ObjCProperty)
4896 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4897 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004898 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004899 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004900 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4901 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004902
4903 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4904 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004905}
4906
Ted Kremenekb79ee572013-12-18 23:30:06 +00004907void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4908 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004909 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004910 DoEmitAvailabilityWarning(*this,
4911 (DelayedDiagnostic::DDKind) DD.Kind,
4912 Ctx,
4913 DD.getDeprecationDecl(),
4914 DD.getDeprecationMessage(),
4915 DD.Loc,
4916 DD.getUnknownObjCClass(),
4917 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004918}
4919
Ted Kremenekb79ee572013-12-18 23:30:06 +00004920void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4921 NamedDecl *D, StringRef Message,
4922 SourceLocation Loc,
4923 const ObjCInterfaceDecl *UnknownObjCClass,
4924 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004925 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004926 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004927 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4928 UnknownObjCClass,
4929 ObjCProperty,
4930 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004931 return;
4932 }
4933
Ted Kremenekb79ee572013-12-18 23:30:06 +00004934 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4935 DelayedDiagnostic::DDKind K;
4936 switch (AD) {
4937 case AD_Deprecation:
4938 K = DelayedDiagnostic::Deprecation;
4939 break;
4940 case AD_Unavailable:
4941 K = DelayedDiagnostic::Unavailable;
4942 break;
4943 }
4944
4945 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4946 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004947}