blob: 33577fb86f77516e093f412443c3ee6bfcd56c50 [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) {
Craig Topperc3ec1492014-05-26 06:22:03 +000051 return (D->getFunctionType() != nullptr) || 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
Alp Toker03376dc2014-07-07 09:02:20 +000088 return cast<ObjCMethodDecl>(D)->parameters()[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())
Aaron Ballman6288d062014-07-11 16:31:29 +000093 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +000094 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 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000195
196 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000197 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
198 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000199 return false;
200 }
201
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000202 Val = (uint32_t)I.getZExtValue();
203 return true;
204}
205
Aaron Ballmanfb763042013-12-02 18:05:46 +0000206/// \brief Diagnose mutually exclusive attributes when present on a given
207/// declaration. Returns true if diagnosed.
208template <typename AttrTy>
209static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000210 const AttributeList &Attr) {
211 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000212 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000213 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000214 return true;
215 }
216 return false;
217}
218
Alp Toker601b22c2014-01-21 23:35:24 +0000219/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000220/// instance method D. May output an error.
221///
222/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000223static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
224 const AttributeList &Attr,
225 unsigned AttrArgNum,
226 const Expr *IdxExpr,
227 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000228 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000229
230 // In C++ the implicit 'this' function parameter also counts.
231 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000232 bool HP = hasFunctionProto(D);
233 bool HasImplicitThisParam = isInstanceMethod(D);
234 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000235 unsigned NumParams =
236 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000237
238 llvm::APSInt IdxInt;
239 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
240 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
242 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
243 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000244 return false;
245 }
246
247 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000248 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
250 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000251 return false;
252 }
253 Idx--; // Convert to zero-based.
254 if (HasImplicitThisParam) {
255 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000256 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000257 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000258 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000259 return false;
260 }
261 --Idx;
262 }
263
264 return true;
265}
266
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000267/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
268/// If not emit an error and return false. If the argument is an identifier it
269/// will emit an error with a fixit hint and treat it as if it was a string
270/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000271bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
272 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000273 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000274 // Look for identifiers. If we have one emit a hint to fix it to a literal.
275 if (Attr.isArgIdent(ArgNum)) {
276 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000277 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000278 << Attr.getName() << AANT_ArgumentString
279 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000280 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000281 Str = Loc->Ident->getName();
282 if (ArgLocation)
283 *ArgLocation = Loc->Loc;
284 return true;
285 }
286
287 // Now check for an actual string literal.
288 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
289 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
290 if (ArgLocation)
291 *ArgLocation = ArgExpr->getLocStart();
292
293 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000294 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000295 << Attr.getName() << AANT_ArgumentString;
296 return false;
297 }
298
299 Str = Literal->getString();
300 return true;
301}
302
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000303/// \brief Applies the given attribute to the Decl without performing any
304/// additional semantic checking.
305template <typename AttrType>
306static void handleSimpleAttribute(Sema &S, Decl *D,
307 const AttributeList &Attr) {
308 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
309 Attr.getAttributeSpellingListIndex()));
310}
311
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000312/// \brief Check if the passed-in expression is of type int or bool.
313static bool isIntOrBool(Expr *Exp) {
314 QualType QT = Exp->getType();
315 return QT->isBooleanType() || QT->isIntegerType();
316}
317
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000318
319// Check to see if the type is a smart pointer of some kind. We assume
320// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000321static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
322 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
323 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000324 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000325 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000326
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000327 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
328 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000329 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000330 return false;
331
332 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000333}
334
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000335/// \brief Check if passed in Decl is a pointer type.
336/// Note that this function may produce an error message.
337/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000338static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
339 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000340 const ValueDecl *vd = cast<ValueDecl>(D);
341 QualType QT = vd->getType();
342 if (QT->isAnyPointerType())
343 return true;
344
345 if (const RecordType *RT = QT->getAs<RecordType>()) {
346 // If it's an incomplete type, it could be a smart pointer; skip it.
347 // (We don't want to force template instantiation if we can avoid it,
348 // since that would alter the order in which templates are instantiated.)
349 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000350 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000351
Aaron Ballman553e6812013-12-26 14:54:11 +0000352 if (threadSafetyCheckIsSmartPointer(S, RT))
353 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000354 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000355
356 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000357 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000358 return false;
359}
360
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000361/// \brief Checks that the passed in QualType either is of RecordType or points
362/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000363static const RecordType *getRecordType(QualType QT) {
364 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000365 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000366
367 // Now check if we point to record type.
368 if (const PointerType *PT = QT->getAs<PointerType>())
369 return PT->getPointeeType()->getAs<RecordType>();
370
Craig Topperc3ec1492014-05-26 06:22:03 +0000371 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000372}
373
Aaron Ballman76050722014-04-04 15:13:57 +0000374static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000375 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000376
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000377 if (!RT)
378 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000379
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000380 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000381 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000382 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000383
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000384 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000385 // FIXME -- Check the type that the smart pointer points to.
386 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000387 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000388
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000389 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000390 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000391 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000392 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000393
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000394 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000395 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
396 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000397 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
398 void *) {
399 return BS->getType()->getAs<RecordType>()
400 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000401 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000402 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000403 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000404 return false;
405}
406
Aaron Ballman76050722014-04-04 15:13:57 +0000407static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000408 const auto *TD = Ty->getAs<TypedefType>();
409 if (!TD)
410 return false;
411
412 TypedefNameDecl *TN = TD->getDecl();
413 if (!TN)
414 return false;
415
416 return TN->hasAttr<CapabilityAttr>();
417}
418
Aaron Ballman76050722014-04-04 15:13:57 +0000419static bool typeHasCapability(Sema &S, QualType Ty) {
420 if (checkTypedefTypeForCapability(Ty))
421 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000422
Aaron Ballman76050722014-04-04 15:13:57 +0000423 if (checkRecordTypeForCapability(S, Ty))
424 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000425
Aaron Ballman76050722014-04-04 15:13:57 +0000426 return false;
427}
428
429static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
430 // Capability expressions are simple expressions involving the boolean logic
431 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
432 // a DeclRefExpr is found, its type should be checked to determine whether it
433 // is a capability or not.
434
435 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
436 return typeHasCapability(S, E->getType());
437 else if (const auto *E = dyn_cast<CastExpr>(Ex))
438 return isCapabilityExpr(S, E->getSubExpr());
439 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
440 return isCapabilityExpr(S, E->getSubExpr());
441 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
442 if (E->getOpcode() == UO_LNot)
443 return isCapabilityExpr(S, E->getSubExpr());
444 return false;
445 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
446 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
447 return isCapabilityExpr(S, E->getLHS()) &&
448 isCapabilityExpr(S, E->getRHS());
449 return false;
450 }
451
452 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000453}
454
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000455/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
456/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000457/// \param Sidx The attribute argument index to start checking with.
458/// \param ParamIdxOk Whether an argument can be indexing into a function
459/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000460static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
461 const AttributeList &Attr,
462 SmallVectorImpl<Expr *> &Args,
463 int Sidx = 0,
464 bool ParamIdxOk = false) {
465 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000466 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000468 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000469 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000470 Args.push_back(ArgExp);
471 continue;
472 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000473
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000474 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000475 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000476 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000477 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000478 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000479 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000480 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000481 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000482
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000483 // We allow constant strings to be used as a placeholder for expressions
484 // that are not valid C++ syntax, but warn that they are ignored.
485 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
486 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000487 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000488 continue;
489 }
490
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000491 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000492
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000493 // A pointer to member expression of the form &MyClass::mu is treated
494 // specially -- we need to look at the type of the member.
495 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
496 if (UOp->getOpcode() == UO_AddrOf)
497 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
498 if (DRE->getDecl()->isCXXInstanceMember())
499 ArgTy = DRE->getDecl()->getType();
500
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000501 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000502 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000503
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504 // Now check if we index into a record type function param.
505 if(!RT && ParamIdxOk) {
506 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000507 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
508 if(FD && IL) {
509 unsigned int NumParams = FD->getNumParams();
510 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000511 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
512 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
513 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
515 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000516 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000517 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000518 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000519 }
520 }
521
Aaron Ballman76050722014-04-04 15:13:57 +0000522 // If the type does not have a capability, see if the components of the
523 // expression have capabilities. This allows for writing C code where the
524 // capability may be on the type, and the expression is a capability
525 // boolean logic expression. Eg) requires_capability(A || B && !C)
526 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
527 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
528 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000530 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000532}
533
Chris Lattner58418ff2008-06-29 00:16:31 +0000534//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000535// Attribute Implementations
536//===----------------------------------------------------------------------===//
537
Daniel Dunbar032db472008-07-31 22:40:48 +0000538// FIXME: All this manual attribute parsing code is gross. At the
539// least add some helper functions to check most argument patterns (#
540// and types of args).
541
Michael Hana9171bc2012-08-03 17:40:43 +0000542static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000543 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000544 if (!threadSafetyCheckIsPointer(S, D, Attr))
545 return;
546
Michael Han99315932013-01-24 16:46:58 +0000547 D->addAttr(::new (S.Context)
548 PtGuardedVarAttr(Attr.getRange(), S.Context,
549 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000550}
551
Michael Hana9171bc2012-08-03 17:40:43 +0000552static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
553 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000554 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000555 SmallVector<Expr*, 1> Args;
556 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000557 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000558 unsigned Size = Args.size();
559 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000560 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000561
Michael Han3be3b442012-07-23 18:48:41 +0000562 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000563
Michael Han3be3b442012-07-23 18:48:41 +0000564 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000565}
566
Michael Han3be3b442012-07-23 18:48:41 +0000567static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000568 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000569 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
570 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000571
Aaron Ballman36a53502014-01-16 13:03:14 +0000572 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
573 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000574}
575
Michael Hana9171bc2012-08-03 17:40:43 +0000576static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000577 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000578 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000579 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
580 return;
581
582 if (!threadSafetyCheckIsPointer(S, D, Attr))
583 return;
584
585 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000586 S.Context, Arg,
587 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000588}
589
Michael Hana9171bc2012-08-03 17:40:43 +0000590static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
591 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000592 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000593 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000594 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000595
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000596 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000597 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000598 if (!QT->isDependentType()) {
599 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000600 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000601 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000602 << Attr.getName();
603 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000604 }
605 }
606
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000607 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000608 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000609 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000610 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000611
Michael Han3be3b442012-07-23 18:48:41 +0000612 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000613}
614
Michael Hana9171bc2012-08-03 17:40:43 +0000615static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000616 const AttributeList &Attr) {
617 SmallVector<Expr*, 1> Args;
618 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
619 return;
620
621 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000622 D->addAttr(::new (S.Context)
623 AcquiredAfterAttr(Attr.getRange(), S.Context,
624 StartArg, Args.size(),
625 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000626}
627
Michael Hana9171bc2012-08-03 17:40:43 +0000628static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000629 const AttributeList &Attr) {
630 SmallVector<Expr*, 1> Args;
631 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
632 return;
633
634 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000635 D->addAttr(::new (S.Context)
636 AcquiredBeforeAttr(Attr.getRange(), S.Context,
637 StartArg, Args.size(),
638 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000639}
640
Michael Hana9171bc2012-08-03 17:40:43 +0000641static bool checkLockFunAttrCommon(Sema &S, Decl *D,
642 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000643 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000644 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000645 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000646 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000647
Michael Han3be3b442012-07-23 18:48:41 +0000648 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000649}
650
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000651static void handleAssertSharedLockAttr(Sema &S, Decl *D,
652 const AttributeList &Attr) {
653 SmallVector<Expr*, 1> Args;
654 if (!checkLockFunAttrCommon(S, D, Attr, Args))
655 return;
656
657 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000658 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000659 D->addAttr(::new (S.Context)
660 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
661 Attr.getAttributeSpellingListIndex()));
662}
663
664static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
665 const AttributeList &Attr) {
666 SmallVector<Expr*, 1> Args;
667 if (!checkLockFunAttrCommon(S, D, Attr, Args))
668 return;
669
670 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000671 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000672 D->addAttr(::new (S.Context)
673 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
674 StartArg, Size,
675 Attr.getAttributeSpellingListIndex()));
676}
677
678
Michael Hana9171bc2012-08-03 17:40:43 +0000679static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
680 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000681 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000682 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000683 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000684
Aaron Ballman00e99962013-08-31 01:11:41 +0000685 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000686 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000687 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000688 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000689 }
690
691 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000692 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000693
Michael Han3be3b442012-07-23 18:48:41 +0000694 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000695}
696
Michael Hana9171bc2012-08-03 17:40:43 +0000697static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000698 const AttributeList &Attr) {
699 SmallVector<Expr*, 2> Args;
700 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
701 return;
702
Michael Han99315932013-01-24 16:46:58 +0000703 D->addAttr(::new (S.Context)
704 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000705 Attr.getArgAsExpr(0),
706 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000707 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000708}
709
Michael Hana9171bc2012-08-03 17:40:43 +0000710static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000711 const AttributeList &Attr) {
712 SmallVector<Expr*, 2> Args;
713 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
714 return;
715
Michael Han99315932013-01-24 16:46:58 +0000716 D->addAttr(::new (S.Context)
717 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000718 Attr.getArgAsExpr(0),
719 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000720 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000721}
722
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000723static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000724 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000725 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000726 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000727 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000728 unsigned Size = Args.size();
729 if (Size == 0)
730 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000731
Michael Han99315932013-01-24 16:46:58 +0000732 D->addAttr(::new (S.Context)
733 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
734 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735}
736
737static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000738 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000739 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000740 return;
741
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000742 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000743 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000744 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000745 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000746 if (Size == 0)
747 return;
748 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000749
Michael Han99315932013-01-24 16:46:58 +0000750 D->addAttr(::new (S.Context)
751 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
752 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000753}
754
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000755static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
756 Expr *Cond = Attr.getArgAsExpr(0);
757 if (!Cond->isTypeDependent()) {
758 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
759 if (Converted.isInvalid())
760 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000761 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000762 }
763
764 StringRef Msg;
765 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
766 return;
767
768 SmallVector<PartialDiagnosticAt, 8> Diags;
769 if (!Cond->isValueDependent() &&
770 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
771 Diags)) {
772 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
773 for (int I = 0, N = Diags.size(); I != N; ++I)
774 S.Diag(Diags[I].first, Diags[I].second);
775 return;
776 }
777
778 D->addAttr(::new (S.Context)
779 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
780 Attr.getAttributeSpellingListIndex()));
781}
782
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000783static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000784 ConsumableAttr::ConsumedState DefaultState;
785
786 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000787 IdentifierLoc *IL = Attr.getArgAsIdent(0);
788 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
789 DefaultState)) {
790 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
791 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000792 return;
793 }
David Blaikie16f76d22013-09-06 01:28:43 +0000794 } else {
795 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
796 << Attr.getName() << AANT_ArgumentIdentifier;
797 return;
798 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000799
800 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000801 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000802 Attr.getAttributeSpellingListIndex()));
803}
804
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000805
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000806static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
807 const AttributeList &Attr) {
808 ASTContext &CurrContext = S.getASTContext();
809 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
810
811 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
812 if (!RD->hasAttr<ConsumableAttr>()) {
813 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
814 RD->getNameAsString();
815
816 return false;
817 }
818 }
819
820 return true;
821}
822
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000823
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000824static void handleCallableWhenAttr(Sema &S, Decl *D,
825 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000826 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
827 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000828
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000829 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
830 return;
831
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000832 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
833 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
834 CallableWhenAttr::ConsumedState CallableState;
835
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000836 StringRef StateString;
837 SourceLocation Loc;
838 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
839 return;
840
841 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000842 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000843 S.Diag(Loc, diag::warn_attribute_type_not_supported)
844 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000845 return;
846 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000847
848 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000849 }
850
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000851 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000852 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
853 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000854}
855
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000856
DeLesley Hutchins69391772013-10-17 23:23:53 +0000857static void handleParamTypestateAttr(Sema &S, Decl *D,
858 const AttributeList &Attr) {
859 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000860
DeLesley Hutchins69391772013-10-17 23:23:53 +0000861 ParamTypestateAttr::ConsumedState ParamState;
862
863 if (Attr.isArgIdent(0)) {
864 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
865 StringRef StateString = Ident->Ident->getName();
866
867 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
868 ParamState)) {
869 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
870 << Attr.getName() << StateString;
871 return;
872 }
873 } else {
874 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
875 Attr.getName() << AANT_ArgumentIdentifier;
876 return;
877 }
878
879 // FIXME: This check is currently being done in the analysis. It can be
880 // enabled here only after the parser propagates attributes at
881 // template specialization definition, not declaration.
882 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
883 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
884 //
885 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
886 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
887 // ReturnType.getAsString();
888 // return;
889 //}
890
891 D->addAttr(::new (S.Context)
892 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
893 Attr.getAttributeSpellingListIndex()));
894}
895
896
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000897static void handleReturnTypestateAttr(Sema &S, Decl *D,
898 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000899 if (!checkAttributeNumArgs(S, Attr, 1)) return;
900
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000901 ReturnTypestateAttr::ConsumedState ReturnState;
902
903 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000904 IdentifierLoc *IL = Attr.getArgAsIdent(0);
905 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
906 ReturnState)) {
907 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
908 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000909 return;
910 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000911 } else {
912 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
913 Attr.getName() << AANT_ArgumentIdentifier;
914 return;
915 }
916
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000917 // FIXME: This check is currently being done in the analysis. It can be
918 // enabled here only after the parser propagates attributes at
919 // template specialization definition, not declaration.
920 //QualType ReturnType;
921 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000922 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
923 // ReturnType = Param->getType();
924 //
925 //} else if (const CXXConstructorDecl *Constructor =
926 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000927 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
928 //
929 //} else {
930 //
931 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
932 //}
933 //
934 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
935 //
936 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
937 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
938 // ReturnType.getAsString();
939 // return;
940 //}
941
942 D->addAttr(::new (S.Context)
943 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
944 Attr.getAttributeSpellingListIndex()));
945}
946
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000947
948static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000949 if (!checkAttributeNumArgs(S, Attr, 1))
950 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000951
952 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
953 return;
954
955 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000956 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000957 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
958 StringRef Param = Ident->Ident->getName();
959 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
960 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
961 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000962 return;
963 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000964 } else {
965 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
966 Attr.getName() << AANT_ArgumentIdentifier;
967 return;
968 }
969
970 D->addAttr(::new (S.Context)
971 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
972 Attr.getAttributeSpellingListIndex()));
973}
974
Chris Wailes9385f9f2013-10-29 20:28:41 +0000975static void handleTestTypestateAttr(Sema &S, Decl *D,
976 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000977 if (!checkAttributeNumArgs(S, Attr, 1))
978 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000979
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000980 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
981 return;
982
Chris Wailes9385f9f2013-10-29 20:28:41 +0000983 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000984 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000985 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
986 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +0000987 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000988 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
989 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000990 return;
991 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000992 } else {
993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
994 Attr.getName() << AANT_ArgumentIdentifier;
995 return;
996 }
997
998 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +0000999 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001000 Attr.getAttributeSpellingListIndex()));
1001}
1002
Chandler Carruthedc2c642011-07-02 00:01:44 +00001003static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1004 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001005 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001006 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001007}
1008
Chandler Carruthedc2c642011-07-02 00:01:44 +00001009static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001010 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001011 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1012 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001013 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001014 // If the alignment is less than or equal to 8 bits, the packed attribute
1015 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001016 if (!FD->getType()->isDependentType() &&
1017 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001018 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001019 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001020 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001021 else
Michael Han99315932013-01-24 16:46:58 +00001022 FD->addAttr(::new (S.Context)
1023 PackedAttr(Attr.getRange(), S.Context,
1024 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001025 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001026 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001027}
1028
Ted Kremenek7fd17232011-09-29 07:02:25 +00001029static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1030 // The IBOutlet/IBOutletCollection attributes only apply to instance
1031 // variables or properties of Objective-C classes. The outlet must also
1032 // have an object reference type.
1033 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1034 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001035 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001036 << Attr.getName() << VD->getType() << 0;
1037 return false;
1038 }
1039 }
1040 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1041 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001042 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001043 << Attr.getName() << PD->getType() << 1;
1044 return false;
1045 }
1046 }
1047 else {
1048 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1049 return false;
1050 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001051
Ted Kremenek7fd17232011-09-29 07:02:25 +00001052 return true;
1053}
1054
Chandler Carruthedc2c642011-07-02 00:01:44 +00001055static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001056 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001057 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001058
Michael Han99315932013-01-24 16:46:58 +00001059 D->addAttr(::new (S.Context)
1060 IBOutletAttr(Attr.getRange(), S.Context,
1061 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001062}
1063
Chandler Carruthedc2c642011-07-02 00:01:44 +00001064static void handleIBOutletCollection(Sema &S, Decl *D,
1065 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001066
1067 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001068 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001069 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1070 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001071 return;
1072 }
1073
Ted Kremenek7fd17232011-09-29 07:02:25 +00001074 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001075 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001076
Richard Smithb1f9a282013-10-31 01:56:18 +00001077 ParsedType PT;
1078
1079 if (Attr.hasParsedType())
1080 PT = Attr.getTypeArg();
1081 else {
1082 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1083 S.getScopeForContext(D->getDeclContext()->getParent()));
1084 if (!PT) {
1085 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1086 return;
1087 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001088 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001089
Craig Topperc3ec1492014-05-26 06:22:03 +00001090 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001091 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1092 if (!QTLoc)
1093 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001094
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001095 // Diagnose use of non-object type in iboutletcollection attribute.
1096 // FIXME. Gnu attribute extension ignores use of builtin types in
1097 // attributes. So, __attribute__((iboutletcollection(char))) will be
1098 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001099 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001100 S.Diag(Attr.getLoc(),
1101 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1102 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001103 return;
1104 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001105
Michael Han99315932013-01-24 16:46:58 +00001106 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001107 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001108 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001109}
1110
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001111static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001112 if (const RecordType *UT = T->getAsUnionType())
1113 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1114 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001115 for (const auto *I : UD->fields()) {
1116 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001117 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1118 T = QT;
1119 return;
1120 }
1121 }
1122 }
1123}
1124
Ted Kremenek9aedc152014-01-17 06:24:56 +00001125static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001126 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001127 T = T.getNonReferenceType();
1128 possibleTransparentUnionPointerType(T);
1129
1130 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001131 S.Diag(Attr.getLoc(),
1132 isReturnValue ? diag::warn_attribute_return_pointers_only
1133 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001134 << Attr.getName() << R;
1135 return false;
1136 }
1137 return true;
1138}
1139
Chandler Carruthedc2c642011-07-02 00:01:44 +00001140static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001141 SmallVector<unsigned, 8> NonNullArgs;
1142 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001143 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001144 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001145 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001146 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001147
1148 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001149 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001150 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1151 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001152 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001153
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001154 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001155 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001156
1157 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1158 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001159 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001160 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1161 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001162 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001163 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001164 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001165 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001166
Ted Kremenek22813f42010-10-21 18:49:36 +00001167 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001168 if (NonNullArgs.empty()) {
1169 // Warn the trivial case only if attribute is not coming from a
1170 // macro instantiation.
1171 if (Attr.getLoc().isFileID())
1172 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001173 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001174 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001175 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001176
Nick Lewyckye1121512013-01-24 01:12:16 +00001177 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001178 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001179 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001180 D->addAttr(::new (S.Context)
1181 NonNullAttr(Attr.getRange(), S.Context, start, size,
1182 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183}
1184
Jordan Rosec9399072014-02-11 17:27:59 +00001185static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1186 const AttributeList &Attr) {
1187 if (Attr.getNumArgs() > 0) {
1188 if (D->getFunctionType()) {
1189 handleNonNullAttr(S, D, Attr);
1190 } else {
1191 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1192 << D->getSourceRange();
1193 }
1194 return;
1195 }
1196
1197 // Is the argument a pointer type?
1198 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1199 return;
1200
1201 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001202 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001203 Attr.getAttributeSpellingListIndex()));
1204}
1205
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001206static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1207 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001208 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001209 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1210 /* isReturnValue */ true))
1211 return;
1212
1213 D->addAttr(::new (S.Context)
1214 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1215 Attr.getAttributeSpellingListIndex()));
1216}
1217
Chandler Carruthedc2c642011-07-02 00:01:44 +00001218static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001219 // This attribute must be applied to a function declaration. The first
1220 // argument to the attribute must be an identifier, the name of the resource,
1221 // for example: malloc. The following arguments must be argument indexes, the
1222 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001223 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001224 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001225 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001226
Aaron Ballman00e99962013-08-31 01:11:41 +00001227 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001228 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001229 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001230 return;
1231 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001232
Richard Smith852e9ce2013-11-27 01:46:48 +00001233 // Figure out our Kind.
1234 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001235 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001236 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001237
Richard Smith852e9ce2013-11-27 01:46:48 +00001238 // Check arguments.
1239 switch (K) {
1240 case OwnershipAttr::Takes:
1241 case OwnershipAttr::Holds:
1242 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001243 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1244 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001245 return;
1246 }
1247 break;
1248 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001249 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001250 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1251 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001252 return;
1253 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001254 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255 }
1256
Richard Smith852e9ce2013-11-27 01:46:48 +00001257 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001258
1259 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001260 StringRef ModuleName = Module->getName();
1261 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1262 ModuleName.size() > 4) {
1263 ModuleName = ModuleName.drop_front(2).drop_back(2);
1264 Module = &S.PP.getIdentifierTable().get(ModuleName);
1265 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001266
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001267 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001268 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1269 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001270 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001271 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001272 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001273
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001274 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001275 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001276 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001277 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001278 case OwnershipAttr::Takes:
1279 case OwnershipAttr::Holds:
1280 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1281 Err = 0;
1282 break;
1283 case OwnershipAttr::Returns:
1284 if (!T->isIntegerType())
1285 Err = 1;
1286 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001287 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001288 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001289 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001290 << Ex->getSourceRange();
1291 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001292 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001293
1294 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001295 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001296 // FIXME: A returns attribute should conflict with any returns attribute
1297 // with a different index too.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001298 if (I->getOwnKind() != K && I->args_end() !=
1299 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001300 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001301 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001302 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303 }
1304 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001305 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001306 }
1307
1308 unsigned* start = OwnershipArgs.data();
1309 unsigned size = OwnershipArgs.size();
1310 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001311
Michael Han99315932013-01-24 16:46:58 +00001312 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001313 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001314 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001315}
1316
Chandler Carruthedc2c642011-07-02 00:01:44 +00001317static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001318 // Check the attribute arguments.
1319 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001320 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1321 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001322 return;
1323 }
1324
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001325 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001326
Rafael Espindolac18086a2010-02-23 22:00:30 +00001327 // gcc rejects
1328 // class c {
1329 // static int a __attribute__((weakref ("v2")));
1330 // static int b() __attribute__((weakref ("f3")));
1331 // };
1332 // and ignores the attributes of
1333 // void f(void) {
1334 // static int a __attribute__((weakref ("v2")));
1335 // }
1336 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001337 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001338 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001339 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1340 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001341 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001342 }
1343
1344 // The GCC manual says
1345 //
1346 // At present, a declaration to which `weakref' is attached can only
1347 // be `static'.
1348 //
1349 // It also says
1350 //
1351 // Without a TARGET,
1352 // given as an argument to `weakref' or to `alias', `weakref' is
1353 // equivalent to `weak'.
1354 //
1355 // gcc 4.4.1 will accept
1356 // int a7 __attribute__((weakref));
1357 // as
1358 // int a7 __attribute__((weak));
1359 // This looks like a bug in gcc. We reject that for now. We should revisit
1360 // it if this behaviour is actually used.
1361
Rafael Espindolac18086a2010-02-23 22:00:30 +00001362 // GCC rejects
1363 // static ((alias ("y"), weakref)).
1364 // Should we? How to check that weakref is before or after alias?
1365
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001366 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1367 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1368 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001369 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001370 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001371 // GCC will accept anything as the argument of weakref. Should we
1372 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001373 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1374 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001375
Michael Han99315932013-01-24 16:46:58 +00001376 D->addAttr(::new (S.Context)
1377 WeakRefAttr(Attr.getRange(), S.Context,
1378 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001379}
1380
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001381static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1382 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001383 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001384 return;
1385
Douglas Gregore8bbc122011-09-02 00:18:52 +00001386 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001387 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1388 return;
1389 }
1390
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001391 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001392
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001393 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001394 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001395}
1396
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001397static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001398 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001399 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001400
Michael Han99315932013-01-24 16:46:58 +00001401 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1402 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001403}
1404
1405static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001406 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001407 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001408
Michael Han99315932013-01-24 16:46:58 +00001409 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1410 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001411}
1412
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001413static void handleTLSModelAttr(Sema &S, Decl *D,
1414 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001415 StringRef Model;
1416 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001417 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001418 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001419 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001420
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001421 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001422 if (Model != "global-dynamic" && Model != "local-dynamic"
1423 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001424 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001425 return;
1426 }
1427
Michael Han99315932013-01-24 16:46:58 +00001428 D->addAttr(::new (S.Context)
1429 TLSModelAttr(Attr.getRange(), S.Context, Model,
1430 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001431}
1432
Chandler Carruthedc2c642011-07-02 00:01:44 +00001433static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001434 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001435 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001436 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001437 D->addAttr(::new (S.Context)
1438 MallocAttr(Attr.getRange(), S.Context,
1439 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001440 return;
1441 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001442 }
1443
Ted Kremenek08479ae2009-08-15 00:51:46 +00001444 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001445}
1446
Chandler Carruthedc2c642011-07-02 00:01:44 +00001447static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001448 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001449 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1450 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001451 return;
1452 }
1453
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001454 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1455 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001456}
1457
Chandler Carruthedc2c642011-07-02 00:01:44 +00001458static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001459 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001460
1461 if (S.CheckNoReturnAttr(attr)) return;
1462
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001463 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001464 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001465 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001466 return;
1467 }
1468
Michael Han99315932013-01-24 16:46:58 +00001469 D->addAttr(::new (S.Context)
1470 NoReturnAttr(attr.getRange(), S.Context,
1471 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001472}
1473
1474bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001475 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001476 attr.setInvalid();
1477 return true;
1478 }
1479
1480 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001481}
1482
Chandler Carruthedc2c642011-07-02 00:01:44 +00001483static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1484 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001485
1486 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1487 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001488 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1489 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001490 if (!VD || (!VD->getType()->isBlockPointerType() &&
1491 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001492 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001493 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001494 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001495 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001496 return;
1497 }
1498 }
1499
Michael Han99315932013-01-24 16:46:58 +00001500 D->addAttr(::new (S.Context)
1501 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1502 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001503}
1504
John Thompsoncdb847ba2010-08-09 21:53:52 +00001505// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001506static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001507/*
1508 Returning a Vector Class in Registers
1509
Eric Christopherbc638a82010-12-01 22:13:54 +00001510 According to the PPU ABI specifications, a class with a single member of
1511 vector type is returned in memory when used as the return value of a function.
1512 This results in inefficient code when implementing vector classes. To return
1513 the value in a single vector register, add the vecreturn attribute to the
1514 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001515
1516 Example:
1517
1518 struct Vector
1519 {
1520 __vector float xyzw;
1521 } __attribute__((vecreturn));
1522
1523 Vector Add(Vector lhs, Vector rhs)
1524 {
1525 Vector result;
1526 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1527 return result; // This will be returned in a register
1528 }
1529*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001530 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1531 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001532 return;
1533 }
1534
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001535 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001536 int count = 0;
1537
1538 if (!isa<CXXRecordDecl>(record)) {
1539 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1540 return;
1541 }
1542
1543 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1544 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1545 return;
1546 }
1547
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001548 for (const auto *I : record->fields()) {
1549 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1551 return;
1552 }
1553 count++;
1554 }
1555
Michael Han99315932013-01-24 16:46:58 +00001556 D->addAttr(::new (S.Context)
1557 VecReturnAttr(Attr.getRange(), S.Context,
1558 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001559}
1560
Richard Smithe233fbf2013-01-28 22:42:45 +00001561static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1562 const AttributeList &Attr) {
1563 if (isa<ParmVarDecl>(D)) {
1564 // [[carries_dependency]] can only be applied to a parameter if it is a
1565 // parameter of a function declaration or lambda.
1566 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1567 S.Diag(Attr.getLoc(),
1568 diag::err_carries_dependency_param_not_function_decl);
1569 return;
1570 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001571 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001572
1573 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1574 Attr.getRange(), S.Context,
1575 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001576}
1577
Chandler Carruthedc2c642011-07-02 00:01:44 +00001578static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001579 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001580 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001581 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001582 return;
1583 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001584 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001585 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001586 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001587 return;
1588 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001589
Michael Han99315932013-01-24 16:46:58 +00001590 D->addAttr(::new (S.Context)
1591 UsedAttr(Attr.getRange(), S.Context,
1592 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001593}
1594
Chandler Carruthedc2c642011-07-02 00:01:44 +00001595static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001596 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001597 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001598 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1599 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001600 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001601 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001602
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001603 uint32_t priority = ConstructorAttr::DefaultPriority;
1604 if (Attr.getNumArgs() > 0 &&
1605 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1606 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001607
Michael Han99315932013-01-24 16:46:58 +00001608 D->addAttr(::new (S.Context)
1609 ConstructorAttr(Attr.getRange(), S.Context, priority,
1610 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001611}
1612
Chandler Carruthedc2c642011-07-02 00:01:44 +00001613static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001614 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001615 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001616 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1617 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001618 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001619 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001620
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001621 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001622 if (Attr.getNumArgs() > 0 &&
1623 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1624 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001625
Michael Han99315932013-01-24 16:46:58 +00001626 D->addAttr(::new (S.Context)
1627 DestructorAttr(Attr.getRange(), S.Context, priority,
1628 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001629}
1630
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001631template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001632static void handleAttrWithMessage(Sema &S, Decl *D,
1633 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001634 unsigned NumArgs = Attr.getNumArgs();
1635 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001636 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1637 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001638 return;
1639 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001640
1641 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001642 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001643 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001644 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001645
Michael Han99315932013-01-24 16:46:58 +00001646 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1647 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001648}
1649
Ted Kremenek438f8db2014-02-22 01:06:05 +00001650static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001651 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001652 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001653 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1654 << Attr.getName() << Attr.getRange();
1655 return;
1656 }
1657
Ted Kremenek28eace62013-11-23 01:01:34 +00001658 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001659 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1660 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001661}
1662
Jordy Rose740b0c22012-05-08 03:27:22 +00001663static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1664 IdentifierInfo *Platform,
1665 VersionTuple Introduced,
1666 VersionTuple Deprecated,
1667 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001668 StringRef PlatformName
1669 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1670 if (PlatformName.empty())
1671 PlatformName = Platform->getName();
1672
1673 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1674 // of these steps are needed).
1675 if (!Introduced.empty() && !Deprecated.empty() &&
1676 !(Introduced <= Deprecated)) {
1677 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1678 << 1 << PlatformName << Deprecated.getAsString()
1679 << 0 << Introduced.getAsString();
1680 return true;
1681 }
1682
1683 if (!Introduced.empty() && !Obsoleted.empty() &&
1684 !(Introduced <= Obsoleted)) {
1685 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1686 << 2 << PlatformName << Obsoleted.getAsString()
1687 << 0 << Introduced.getAsString();
1688 return true;
1689 }
1690
1691 if (!Deprecated.empty() && !Obsoleted.empty() &&
1692 !(Deprecated <= Obsoleted)) {
1693 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1694 << 2 << PlatformName << Obsoleted.getAsString()
1695 << 1 << Deprecated.getAsString();
1696 return true;
1697 }
1698
1699 return false;
1700}
1701
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001702/// \brief Check whether the two versions match.
1703///
1704/// If either version tuple is empty, then they are assumed to match. If
1705/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1706static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1707 bool BeforeIsOkay) {
1708 if (X.empty() || Y.empty())
1709 return true;
1710
1711 if (X == Y)
1712 return true;
1713
1714 if (BeforeIsOkay && X < Y)
1715 return true;
1716
1717 return false;
1718}
1719
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001720AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001721 IdentifierInfo *Platform,
1722 VersionTuple Introduced,
1723 VersionTuple Deprecated,
1724 VersionTuple Obsoleted,
1725 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001726 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001727 bool Override,
1728 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001729 VersionTuple MergedIntroduced = Introduced;
1730 VersionTuple MergedDeprecated = Deprecated;
1731 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001732 bool FoundAny = false;
1733
Rafael Espindolac67f2232012-05-10 02:50:16 +00001734 if (D->hasAttrs()) {
1735 AttrVec &Attrs = D->getAttrs();
1736 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1737 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1738 if (!OldAA) {
1739 ++i;
1740 continue;
1741 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001742
Rafael Espindolac67f2232012-05-10 02:50:16 +00001743 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1744 if (OldPlatform != Platform) {
1745 ++i;
1746 continue;
1747 }
1748
1749 FoundAny = true;
1750 VersionTuple OldIntroduced = OldAA->getIntroduced();
1751 VersionTuple OldDeprecated = OldAA->getDeprecated();
1752 VersionTuple OldObsoleted = OldAA->getObsoleted();
1753 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001754
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001755 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1756 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1757 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1758 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001759 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001760 if (Override) {
1761 int Which = -1;
1762 VersionTuple FirstVersion;
1763 VersionTuple SecondVersion;
1764 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1765 Which = 0;
1766 FirstVersion = OldIntroduced;
1767 SecondVersion = Introduced;
1768 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1769 Which = 1;
1770 FirstVersion = Deprecated;
1771 SecondVersion = OldDeprecated;
1772 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1773 Which = 2;
1774 FirstVersion = Obsoleted;
1775 SecondVersion = OldObsoleted;
1776 }
1777
1778 if (Which == -1) {
1779 Diag(OldAA->getLocation(),
1780 diag::warn_mismatched_availability_override_unavail)
1781 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1782 } else {
1783 Diag(OldAA->getLocation(),
1784 diag::warn_mismatched_availability_override)
1785 << Which
1786 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1787 << FirstVersion.getAsString() << SecondVersion.getAsString();
1788 }
1789 Diag(Range.getBegin(), diag::note_overridden_method);
1790 } else {
1791 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1792 Diag(Range.getBegin(), diag::note_previous_attribute);
1793 }
1794
Rafael Espindolac67f2232012-05-10 02:50:16 +00001795 Attrs.erase(Attrs.begin() + i);
1796 --e;
1797 continue;
1798 }
1799
1800 VersionTuple MergedIntroduced2 = MergedIntroduced;
1801 VersionTuple MergedDeprecated2 = MergedDeprecated;
1802 VersionTuple MergedObsoleted2 = MergedObsoleted;
1803
1804 if (MergedIntroduced2.empty())
1805 MergedIntroduced2 = OldIntroduced;
1806 if (MergedDeprecated2.empty())
1807 MergedDeprecated2 = OldDeprecated;
1808 if (MergedObsoleted2.empty())
1809 MergedObsoleted2 = OldObsoleted;
1810
1811 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1812 MergedIntroduced2, MergedDeprecated2,
1813 MergedObsoleted2)) {
1814 Attrs.erase(Attrs.begin() + i);
1815 --e;
1816 continue;
1817 }
1818
1819 MergedIntroduced = MergedIntroduced2;
1820 MergedDeprecated = MergedDeprecated2;
1821 MergedObsoleted = MergedObsoleted2;
1822 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001823 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001824 }
1825
1826 if (FoundAny &&
1827 MergedIntroduced == Introduced &&
1828 MergedDeprecated == Deprecated &&
1829 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001830 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001831
Ted Kremenekb5445722013-04-06 00:34:27 +00001832 // Only create a new attribute if !Override, but we want to do
1833 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001834 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001835 MergedDeprecated, MergedObsoleted) &&
1836 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001837 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1838 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001839 Obsoleted, IsUnavailable, Message,
1840 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001841 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001842 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001843}
1844
Chandler Carruthedc2c642011-07-02 00:01:44 +00001845static void handleAvailabilityAttr(Sema &S, Decl *D,
1846 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001847 if (!checkAttributeNumArgs(S, Attr, 1))
1848 return;
1849 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001850 unsigned Index = Attr.getAttributeSpellingListIndex();
1851
Aaron Ballman00e99962013-08-31 01:11:41 +00001852 IdentifierInfo *II = Platform->Ident;
1853 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1854 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1855 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001856
Rafael Espindolac231fab2013-01-08 21:30:32 +00001857 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1858 if (!ND) {
1859 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1860 return;
1861 }
1862
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001863 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1864 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1865 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001866 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001867 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001868 if (const StringLiteral *SE =
1869 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001870 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001871
Aaron Ballman00e99962013-08-31 01:11:41 +00001872 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001873 Introduced.Version,
1874 Deprecated.Version,
1875 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001876 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001877 /*Override=*/false,
1878 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001879 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001880 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001881}
1882
John McCalld041a9b2013-02-20 01:54:26 +00001883template <class T>
1884static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1885 typename T::VisibilityType value,
1886 unsigned attrSpellingListIndex) {
1887 T *existingAttr = D->getAttr<T>();
1888 if (existingAttr) {
1889 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1890 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001891 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001892 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1893 S.Diag(range.getBegin(), diag::note_previous_attribute);
1894 D->dropAttr<T>();
1895 }
1896 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1897}
1898
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001899VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001900 VisibilityAttr::VisibilityType Vis,
1901 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001902 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1903 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001904}
1905
John McCalld041a9b2013-02-20 01:54:26 +00001906TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1907 TypeVisibilityAttr::VisibilityType Vis,
1908 unsigned AttrSpellingListIndex) {
1909 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1910 AttrSpellingListIndex);
1911}
1912
1913static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1914 bool isTypeVisibility) {
1915 // Visibility attributes don't mean anything on a typedef.
1916 if (isa<TypedefNameDecl>(D)) {
1917 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1918 << Attr.getName();
1919 return;
1920 }
1921
1922 // 'type_visibility' can only go on a type or namespace.
1923 if (isTypeVisibility &&
1924 !(isa<TagDecl>(D) ||
1925 isa<ObjCInterfaceDecl>(D) ||
1926 isa<NamespaceDecl>(D))) {
1927 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1928 << Attr.getName() << ExpectedTypeOrNamespace;
1929 return;
1930 }
1931
Benjamin Kramer70370212013-09-09 15:08:57 +00001932 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001933 StringRef TypeStr;
1934 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001935 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001936 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001937
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001938 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001939 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001940 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001941 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001942 return;
1943 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001944
1945 // Complain about attempts to use protected visibility on targets
1946 // (like Darwin) that don't support it.
1947 if (type == VisibilityAttr::Protected &&
1948 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1949 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1950 type = VisibilityAttr::Default;
1951 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001952
Michael Han99315932013-01-24 16:46:58 +00001953 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001954 clang::Attr *newAttr;
1955 if (isTypeVisibility) {
1956 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1957 (TypeVisibilityAttr::VisibilityType) type,
1958 Index);
1959 } else {
1960 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1961 }
1962 if (newAttr)
1963 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001964}
1965
Chandler Carruthedc2c642011-07-02 00:01:44 +00001966static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1967 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001968 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001969 if (!Attr.isArgIdent(0)) {
1970 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1971 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001972 return;
1973 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001974
Aaron Ballman682ee422013-09-11 19:47:58 +00001975 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1976 ObjCMethodFamilyAttr::FamilyKind F;
1977 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1978 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1979 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001980 return;
1981 }
1982
Alp Toker314cc812014-01-25 16:55:45 +00001983 if (F == ObjCMethodFamilyAttr::OMF_init &&
1984 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001985 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001986 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001987 // Ignore the attribute.
1988 return;
1989 }
1990
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001991 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001992 S.Context, F,
1993 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001994}
1995
Chandler Carruthedc2c642011-07-02 00:01:44 +00001996static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001997 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001998 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001999 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002000 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2001 return;
2002 }
2003 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002004 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2005 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002006 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002007 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2008 return;
2009 }
2010 }
2011 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002012 // It is okay to include this attribute on properties, e.g.:
2013 //
2014 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2015 //
2016 // In this case it follows tradition and suppresses an error in the above
2017 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002018 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002019 }
Michael Han99315932013-01-24 16:46:58 +00002020 D->addAttr(::new (S.Context)
2021 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2022 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002023}
2024
Chandler Carruthedc2c642011-07-02 00:01:44 +00002025static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002026 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002027 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002028 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002029 return;
2030 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002031
Aaron Ballman00e99962013-08-31 01:11:41 +00002032 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002033 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002034 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2035 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2036 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002037 return;
2038 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002039
Michael Han99315932013-01-24 16:46:58 +00002040 D->addAttr(::new (S.Context)
2041 BlocksAttr(Attr.getRange(), S.Context, type,
2042 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002043}
2044
Chandler Carruthedc2c642011-07-02 00:01:44 +00002045static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002046 // check the attribute arguments.
2047 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002048 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2049 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002050 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002051 }
2052
Aaron Ballman18a78382013-11-21 00:28:23 +00002053 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002054 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002055 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002056 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002057 if (E->isTypeDependent() || E->isValueDependent() ||
2058 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002059 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002060 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002061 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002062 return;
2063 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002064
John McCallb46f2872011-09-09 07:56:05 +00002065 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002066 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2067 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002068 return;
2069 }
John McCallb46f2872011-09-09 07:56:05 +00002070
2071 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002072 }
2073
Aaron Ballman18a78382013-11-21 00:28:23 +00002074 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002075 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002076 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002077 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002078 if (E->isTypeDependent() || E->isValueDependent() ||
2079 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002080 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002081 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002082 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002083 return;
2084 }
2085 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002086
John McCallb46f2872011-09-09 07:56:05 +00002087 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002088 // FIXME: This error message could be improved, it would be nice
2089 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002090 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2091 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002092 return;
2093 }
2094 }
2095
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002096 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002097 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002098 if (isa<FunctionNoProtoType>(FT)) {
2099 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2100 return;
2101 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002102
Chris Lattner9363e312009-03-17 23:03:47 +00002103 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002104 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002105 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002106 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002107 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002108 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002109 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002110 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002111 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002112 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2113 if (!BD->isVariadic()) {
2114 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2115 return;
2116 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002117 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002118 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002119 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002120 const FunctionType *FT = Ty->isFunctionPointerType()
2121 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002122 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002123 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002124 int m = Ty->isFunctionPointerType() ? 0 : 1;
2125 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002126 return;
2127 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002128 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002129 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002130 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002131 return;
2132 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002133 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002134 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002135 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 return;
2137 }
Michael Han99315932013-01-24 16:46:58 +00002138 D->addAttr(::new (S.Context)
2139 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2140 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002141}
2142
Chandler Carruthedc2c642011-07-02 00:01:44 +00002143static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002144 if (D->getFunctionType() &&
2145 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002146 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2147 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002148 return;
2149 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002150 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002151 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002152 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2153 << Attr.getName() << 1;
2154 return;
2155 }
2156
Michael Han99315932013-01-24 16:46:58 +00002157 D->addAttr(::new (S.Context)
2158 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2159 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002160}
2161
Chandler Carruthedc2c642011-07-02 00:01:44 +00002162static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002163 // weak_import only applies to variable & function declarations.
2164 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002165 if (!D->canBeWeakImported(isDef)) {
2166 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002167 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2168 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002169 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002170 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002171 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002172 // Nothing to warn about here.
2173 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002174 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002175 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002176
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002177 return;
2178 }
2179
Michael Han99315932013-01-24 16:46:58 +00002180 D->addAttr(::new (S.Context)
2181 WeakImportAttr(Attr.getRange(), S.Context,
2182 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002183}
2184
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002185// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002186template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002187static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002188 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002189 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002190 for (unsigned i = 0; i < 3; ++i) {
2191 const Expr *E = Attr.getArgAsExpr(i);
2192 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002193 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002194 if (WGSize[i] == 0) {
2195 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2196 << Attr.getName() << E->getSourceRange();
2197 return;
2198 }
2199 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002200
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002201 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2202 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2203 Existing->getYDim() == WGSize[1] &&
2204 Existing->getZDim() == WGSize[2]))
2205 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002206
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002207 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2208 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002209 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002210}
2211
Joey Goulyaba589c2013-03-08 09:42:32 +00002212static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002213 if (!Attr.hasParsedType()) {
2214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2215 << Attr.getName() << 1;
2216 return;
2217 }
2218
Craig Topperc3ec1492014-05-26 06:22:03 +00002219 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002220 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2221 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002222
2223 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2224 (ParmType->isBooleanType() ||
2225 !ParmType->isIntegralType(S.getASTContext()))) {
2226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2227 << ParmType;
2228 return;
2229 }
2230
Aaron Ballmana9e05402013-12-02 22:16:55 +00002231 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002232 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002233 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2234 return;
2235 }
2236 }
2237
2238 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002239 ParmTSI,
2240 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002241}
2242
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002243SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002244 StringRef Name,
2245 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002246 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2247 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002248 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002249 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2250 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002251 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002252 }
Michael Han99315932013-01-24 16:46:58 +00002253 return ::new (Context) SectionAttr(Range, Context, Name,
2254 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002255}
2256
Chandler Carruthedc2c642011-07-02 00:01:44 +00002257static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002258 // Make sure that there is a string literal as the sections's single
2259 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002260 StringRef Str;
2261 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002262 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002263 return;
Mike Stump11289f42009-09-09 15:08:12 +00002264
Chris Lattner30ba6742009-08-10 19:03:04 +00002265 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002266 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002267 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002268 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002269 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002270 return;
2271 }
Mike Stump11289f42009-09-09 15:08:12 +00002272
Michael Han99315932013-01-24 16:46:58 +00002273 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002274 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002275 if (NewAttr)
2276 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002277}
2278
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002279
Chandler Carruthedc2c642011-07-02 00:01:44 +00002280static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002281 VarDecl *VD = cast<VarDecl>(D);
2282 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002283 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002284 return;
2285 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002286
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002287 Expr *E = Attr.getArgAsExpr(0);
2288 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002289 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002290 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002291
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002292 // gcc only allows for simple identifiers. Since we support more than gcc, we
2293 // will warn the user.
2294 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2295 if (DRE->hasQualifier())
2296 S.Diag(Loc, diag::warn_cleanup_ext);
2297 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2298 NI = DRE->getNameInfo();
2299 if (!FD) {
2300 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2301 << NI.getName();
2302 return;
2303 }
2304 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2305 if (ULE->hasExplicitTemplateArgs())
2306 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002307 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2308 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002309 if (!FD) {
2310 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2311 << NI.getName();
2312 if (ULE->getType() == S.Context.OverloadTy)
2313 S.NoteAllOverloadCandidates(ULE);
2314 return;
2315 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002316 } else {
2317 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002318 return;
2319 }
2320
Anders Carlssond277d792009-01-31 01:16:18 +00002321 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002322 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2323 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002324 return;
2325 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326
Anders Carlsson723f55d2009-02-07 23:16:50 +00002327 // We're currently more strict than GCC about what function types we accept.
2328 // If this ever proves to be a problem it should be easy to fix.
2329 QualType Ty = S.Context.getPointerType(VD->getType());
2330 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002331 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2332 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002333 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2334 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002335 return;
2336 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002337
Michael Han99315932013-01-24 16:46:58 +00002338 D->addAttr(::new (S.Context)
2339 CleanupAttr(Attr.getRange(), S.Context, FD,
2340 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002341}
2342
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002344/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002345static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002346 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002347 uint64_t Idx;
2348 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002349 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002350
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002351 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002352 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002353
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002354 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2355 if (not_nsstring_type &&
2356 !isCFStringType(Ty, S.Context) &&
2357 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002358 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002359 // FIXME: Should highlight the actual expression that has the wrong type.
2360 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002361 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002362 << IdxExpr->getSourceRange();
2363 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002364 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002365 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002366 if (!isNSStringType(Ty, S.Context) &&
2367 !isCFStringType(Ty, S.Context) &&
2368 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002369 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002370 // FIXME: Should highlight the actual expression that has the wrong type.
2371 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002372 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002373 << IdxExpr->getSourceRange();
2374 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002375 }
2376
Alp Toker601b22c2014-01-21 23:35:24 +00002377 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002378 // because that has corrected for the implicit this parameter, and is zero-
2379 // based. The attribute expects what the user wrote explicitly.
2380 llvm::APSInt Val;
2381 IdxExpr->EvaluateAsInt(Val, S.Context);
2382
Michael Han99315932013-01-24 16:46:58 +00002383 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002384 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002385 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002386}
2387
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002388enum FormatAttrKind {
2389 CFStringFormat,
2390 NSStringFormat,
2391 StrftimeFormat,
2392 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002393 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002394 InvalidFormat
2395};
2396
2397/// getFormatAttrKind - Map from format attribute names to supported format
2398/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002399static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002400 return llvm::StringSwitch<FormatAttrKind>(Format)
2401 // Check for formats that get handled specially.
2402 .Case("NSString", NSStringFormat)
2403 .Case("CFString", CFStringFormat)
2404 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002405
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002406 // Otherwise, check for supported formats.
2407 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2408 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2409 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002410
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002411 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2412 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002413}
2414
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002415/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002416/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002417static void handleInitPriorityAttr(Sema &S, Decl *D,
2418 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002419 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002420 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2421 return;
2422 }
2423
Aaron Ballman4a611152013-11-27 16:34:09 +00002424 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002425 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2426 Attr.setInvalid();
2427 return;
2428 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002429 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002430 if (S.Context.getAsArrayType(T))
2431 T = S.Context.getBaseElementType(T);
2432 if (!T->getAs<RecordType>()) {
2433 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2434 Attr.setInvalid();
2435 return;
2436 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002437
2438 Expr *E = Attr.getArgAsExpr(0);
2439 uint32_t prioritynum;
2440 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002441 Attr.setInvalid();
2442 return;
2443 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002444
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002445 if (prioritynum < 101 || prioritynum > 65535) {
2446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002447 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002448 Attr.setInvalid();
2449 return;
2450 }
Michael Han99315932013-01-24 16:46:58 +00002451 D->addAttr(::new (S.Context)
2452 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2453 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002454}
2455
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002456FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2457 IdentifierInfo *Format, int FormatIdx,
2458 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002459 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002460 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002461 for (auto *F : D->specific_attrs<FormatAttr>()) {
2462 if (F->getType() == Format &&
2463 F->getFormatIdx() == FormatIdx &&
2464 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002465 // If we don't have a valid location for this attribute, adopt the
2466 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002467 if (F->getLocation().isInvalid())
2468 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002469 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002470 }
2471 }
2472
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002473 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2474 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002475}
2476
Mike Stumpd3bb5572009-07-24 19:02:52 +00002477/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002478/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002479static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002480 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002481 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002482 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002483 return;
2484 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002485
Chandler Carruth743682b2010-11-16 08:35:43 +00002486 // In C++ the implicit 'this' function parameter also counts, and they are
2487 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002488 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002489 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002490
Aaron Ballman00e99962013-08-31 01:11:41 +00002491 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2492 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002493
2494 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002495 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002496 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002497 // If we've modified the string name, we need a new identifier for it.
2498 II = &S.Context.Idents.get(Format);
2499 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002500
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002501 // Check for supported formats.
2502 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002503
2504 if (Kind == IgnoredFormat)
2505 return;
2506
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002507 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002508 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002509 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002510 return;
2511 }
2512
2513 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002514 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002515 uint32_t Idx;
2516 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002517 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002518
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002519 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002521 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002522 return;
2523 }
2524
2525 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002526 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002527
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002528 if (HasImplicitThisParam) {
2529 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002530 S.Diag(Attr.getLoc(),
2531 diag::err_format_attribute_implicit_this_format_string)
2532 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002533 return;
2534 }
2535 ArgIdx--;
2536 }
Mike Stump11289f42009-09-09 15:08:12 +00002537
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002538 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002539 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002540
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002541 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002542 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002543 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2544 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002545 return;
2546 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002547 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002548 // FIXME: do we need to check if the type is NSString*? What are the
2549 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002550 if (!isNSStringType(Ty, S.Context)) {
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 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002554 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002555 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002556 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002557 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002558 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002559 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2560 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002561 return;
2562 }
2563
2564 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002565 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002566 uint32_t FirstArg;
2567 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002568 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569
2570 // check if the function is variadic if the 3rd argument non-zero
2571 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002572 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002573 ++NumArgs; // +1 for ...
2574 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002575 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576 return;
2577 }
2578 }
2579
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002580 // strftime requires FirstArg to be 0 because it doesn't read from any
2581 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002582 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002583 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002584 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2585 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002586 return;
2587 }
2588 // if 0 it disables parameter checking (to use with e.g. va_list)
2589 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002591 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592 return;
2593 }
2594
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002595 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002596 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002597 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002598 if (NewAttr)
2599 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002600}
2601
Chandler Carruthedc2c642011-07-02 00:01:44 +00002602static void handleTransparentUnionAttr(Sema &S, Decl *D,
2603 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002604 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002605 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002606 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002607 if (TD && TD->getUnderlyingType()->isUnionType())
2608 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2609 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002610 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002611
2612 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002614 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002615 return;
2616 }
2617
John McCallf937c022011-10-07 06:10:15 +00002618 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002619 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002620 diag::warn_transparent_union_attribute_not_definition);
2621 return;
2622 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002623
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002624 RecordDecl::field_iterator Field = RD->field_begin(),
2625 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002626 if (Field == FieldEnd) {
2627 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2628 return;
2629 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002630
David Blaikie40ed2972012-06-06 20:45:41 +00002631 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002632 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002633 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002634 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002635 diag::warn_transparent_union_attribute_floating)
2636 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002637 return;
2638 }
2639
2640 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2641 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2642 for (; Field != FieldEnd; ++Field) {
2643 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002644 // FIXME: this isn't fully correct; we also need to test whether the
2645 // members of the union would all have the same calling convention as the
2646 // first member of the union. Checking just the size and alignment isn't
2647 // sufficient (consider structs passed on the stack instead of in registers
2648 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002649 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002650 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002651 // Warn if we drop the attribute.
2652 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002653 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002654 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002655 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002656 diag::warn_transparent_union_attribute_field_size_align)
2657 << isSize << Field->getDeclName() << FieldBits;
2658 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002659 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002660 diag::note_transparent_union_first_field_size_align)
2661 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002662 return;
2663 }
2664 }
2665
Michael Han99315932013-01-24 16:46:58 +00002666 RD->addAttr(::new (S.Context)
2667 TransparentUnionAttr(Attr.getRange(), S.Context,
2668 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002669}
2670
Chandler Carruthedc2c642011-07-02 00:01:44 +00002671static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672 // Make sure that there is a string literal as the annotation's single
2673 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002674 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002675 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002676 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002677
2678 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002679 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2680 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002681 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002682 }
Michael Han99315932013-01-24 16:46:58 +00002683
2684 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002685 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002686 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002687}
2688
Chandler Carruthedc2c642011-07-02 00:01:44 +00002689static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002690 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002691 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2693 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002694 return;
2695 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002696
Richard Smith848e1f12013-02-01 08:12:08 +00002697 if (Attr.getNumArgs() == 0) {
2698 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002699 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002700 return;
2701 }
2702
Aaron Ballman00e99962013-08-31 01:11:41 +00002703 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002704 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2705 S.Diag(Attr.getEllipsisLoc(),
2706 diag::err_pack_expansion_without_parameter_packs);
2707 return;
2708 }
2709
2710 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2711 return;
2712
2713 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2714 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002715}
2716
2717void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002718 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002719 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2720 SourceLocation AttrLoc = AttrRange.getBegin();
2721
Richard Smith1dba27c2013-01-29 09:02:09 +00002722 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002723 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002724 // C++11 [dcl.align]p1:
2725 // An alignment-specifier may be applied to a variable or to a class
2726 // data member, but it shall not be applied to a bit-field, a function
2727 // parameter, the formal parameter of a catch clause, or a variable
2728 // declared with the register storage class specifier. An
2729 // alignment-specifier may also be applied to the declaration of a class
2730 // or enumeration type.
2731 // C11 6.7.5/2:
2732 // An alignment attribute shall not be specified in a declaration of
2733 // a typedef, or a bit-field, or a function, or a parameter, or an
2734 // object declared with the register storage-class specifier.
2735 int DiagKind = -1;
2736 if (isa<ParmVarDecl>(D)) {
2737 DiagKind = 0;
2738 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2739 if (VD->getStorageClass() == SC_Register)
2740 DiagKind = 1;
2741 if (VD->isExceptionVariable())
2742 DiagKind = 2;
2743 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2744 if (FD->isBitField())
2745 DiagKind = 3;
2746 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002747 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002748 << (TmpAttr.isC11() ? ExpectedVariableOrField
2749 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002750 return;
2751 }
2752 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002753 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002754 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002755 return;
2756 }
2757 }
2758
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002759 if (E->isTypeDependent() || E->isValueDependent()) {
2760 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002761 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2762 AA->setPackExpansion(IsPackExpansion);
2763 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002764 return;
2765 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002766
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002767 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002768 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002769 ExprResult ICE
2770 = VerifyIntegerConstantExpression(E, &Alignment,
2771 diag::err_aligned_attribute_argument_not_int,
2772 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002773 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002774 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002775
2776 // C++11 [dcl.align]p2:
2777 // -- if the constant expression evaluates to zero, the alignment
2778 // specifier shall have no effect
2779 // C11 6.7.5p6:
2780 // An alignment specification of zero has no effect.
2781 if (!(TmpAttr.isAlignas() && !Alignment) &&
2782 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002783 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2784 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002785 return;
2786 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002787
David Majnemerabecae72014-02-12 20:36:10 +00002788 // Alignment calculations can wrap around if it's greater than 2**28.
2789 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2790 if (Alignment.getZExtValue() > MaxValidAlignment) {
2791 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2792 << E->getSourceRange();
2793 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002794 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002795
Richard Smith44c247f2013-02-22 08:32:16 +00002796 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002797 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002798 AA->setPackExpansion(IsPackExpansion);
2799 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002800}
2801
Michael Hanaf02bbe2013-02-01 01:19:17 +00002802void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002803 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002804 // FIXME: Cache the number on the Attr object if non-dependent?
2805 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002806 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2807 SpellingListIndex);
2808 AA->setPackExpansion(IsPackExpansion);
2809 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002810}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002811
Richard Smith848e1f12013-02-01 08:12:08 +00002812void Sema::CheckAlignasUnderalignment(Decl *D) {
2813 assert(D->hasAttrs() && "no attributes on decl");
2814
2815 QualType Ty;
2816 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2817 Ty = VD->getType();
2818 else
2819 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002820 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002821 return;
2822
2823 // C++11 [dcl.align]p5, C11 6.7.5/4:
2824 // The combined effect of all alignment attributes in a declaration shall
2825 // not specify an alignment that is less strict than the alignment that
2826 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002827 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002828 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002829 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002830 if (I->isAlignmentDependent())
2831 return;
2832 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002833 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002834 Align = std::max(Align, I->getAlignment(Context));
2835 }
2836
2837 if (AlignasAttr && Align) {
2838 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2839 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2840 if (NaturalAlign > RequestedAlign)
2841 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2842 << Ty << (unsigned)NaturalAlign.getQuantity();
2843 }
2844}
2845
David Majnemer2c4e00a2014-01-29 22:07:36 +00002846bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002847 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002848 MSInheritanceAttr::Spelling SemanticSpelling) {
2849 assert(RD->hasDefinition() && "RD has no definition!");
2850
David Majnemer98c9ee22014-02-07 00:43:07 +00002851 // We may not have seen base specifiers or any virtual methods yet. We will
2852 // have to wait until the record is defined to catch any mismatches.
2853 if (!RD->getDefinition()->isCompleteDefinition())
2854 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002855
David Majnemer98c9ee22014-02-07 00:43:07 +00002856 // The unspecified model never matches what a definition could need.
2857 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2858 return false;
2859
David Majnemer4bb09802014-02-10 19:50:15 +00002860 if (BestCase) {
2861 if (RD->calculateInheritanceModel() == SemanticSpelling)
2862 return false;
2863 } else {
2864 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2865 return false;
2866 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002867
2868 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2869 << 0 /*definition*/;
2870 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2871 << RD->getNameAsString();
2872 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002873}
2874
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002875/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002876/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002877///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002878/// Despite what would be logical, the mode attribute is a decl attribute, not a
2879/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2880/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002881static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002882 // This attribute isn't documented, but glibc uses it. It changes
2883 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002884 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002885 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2886 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002887 return;
2888 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002889
Aaron Ballman00e99962013-08-31 01:11:41 +00002890 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2891 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002892
2893 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002894 if (Str.startswith("__") && Str.endswith("__"))
2895 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002896
2897 unsigned DestWidth = 0;
2898 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002899 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002900 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002901 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002902 switch (Str[0]) {
2903 case 'Q': DestWidth = 8; break;
2904 case 'H': DestWidth = 16; break;
2905 case 'S': DestWidth = 32; break;
2906 case 'D': DestWidth = 64; break;
2907 case 'X': DestWidth = 96; break;
2908 case 'T': DestWidth = 128; break;
2909 }
2910 if (Str[1] == 'F') {
2911 IntegerMode = false;
2912 } else if (Str[1] == 'C') {
2913 IntegerMode = false;
2914 ComplexMode = true;
2915 } else if (Str[1] != 'I') {
2916 DestWidth = 0;
2917 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002918 break;
2919 case 4:
2920 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2921 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002922 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002923 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002924 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002925 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002926 break;
2927 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002928 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002929 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002930 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002931 case 11:
2932 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002933 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002934 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002935 }
2936
2937 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002938 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002939 OldTy = TD->getUnderlyingType();
2940 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2941 OldTy = VD->getType();
2942 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002943 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002944 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002945 return;
2946 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002947
John McCall9dd450b2009-09-21 23:43:11 +00002948 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002949 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2950 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002951 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002952 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2953 } else if (ComplexMode) {
2954 if (!OldTy->isComplexType())
2955 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2956 } else {
2957 if (!OldTy->isFloatingType())
2958 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2959 }
2960
Mike Stump87c57ac2009-05-16 07:39:55 +00002961 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2962 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002963 // FIXME: Make sure floating-point mappings are accurate
2964 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002965 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002966 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002967 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002968 }
2969
2970 QualType NewTy;
2971
2972 if (IntegerMode)
2973 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2974 OldTy->isSignedIntegerType());
2975 else
2976 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2977
2978 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002979 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002980 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002981 }
2982
Eli Friedman4735374e2009-03-03 06:41:03 +00002983 if (ComplexMode) {
2984 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002985 }
2986
2987 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002988 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2989 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2990 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002991 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002992
2993 D->addAttr(::new (S.Context)
2994 ModeAttr(Attr.getRange(), S.Context, Name,
2995 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002996}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002997
Chandler Carruthedc2c642011-07-02 00:01:44 +00002998static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002999 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3000 if (!VD->hasGlobalStorage())
3001 S.Diag(Attr.getLoc(),
3002 diag::warn_attribute_requires_functions_or_static_globals)
3003 << Attr.getName();
3004 } else if (!isFunctionOrMethod(D)) {
3005 S.Diag(Attr.getLoc(),
3006 diag::warn_attribute_requires_functions_or_static_globals)
3007 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003008 return;
3009 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003010
Michael Han99315932013-01-24 16:46:58 +00003011 D->addAttr(::new (S.Context)
3012 NoDebugAttr(Attr.getRange(), S.Context,
3013 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003014}
3015
Paul Robinsonf0674352014-03-31 22:29:15 +00003016static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3017 const AttributeList &Attr) {
3018 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3019 return;
3020
3021 D->addAttr(::new (S.Context)
3022 AlwaysInlineAttr(Attr.getRange(), S.Context,
3023 Attr.getAttributeSpellingListIndex()));
3024}
3025
3026static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3027 const AttributeList &Attr) {
3028 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
3029 return;
3030
3031 D->addAttr(::new (S.Context)
3032 OptimizeNoneAttr(Attr.getRange(), S.Context,
3033 Attr.getAttributeSpellingListIndex()));
3034}
3035
Chandler Carruthedc2c642011-07-02 00:01:44 +00003036static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003037 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003038 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003039 SourceRange RTRange = FD->getReturnTypeSourceRange();
3040 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003041 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003042 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3043 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003044 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003045 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003046
Aaron Ballman3aff6332013-12-02 19:30:36 +00003047 D->addAttr(::new (S.Context)
3048 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003049 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003050}
3051
Chandler Carruthedc2c642011-07-02 00:01:44 +00003052static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003053 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003054 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003055 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003056 return;
3057 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003058
Michael Han99315932013-01-24 16:46:58 +00003059 D->addAttr(::new (S.Context)
3060 GNUInlineAttr(Attr.getRange(), S.Context,
3061 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003062}
3063
Chandler Carruthedc2c642011-07-02 00:01:44 +00003064static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003065 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003066
Aaron Ballman02df2e02012-12-09 17:45:41 +00003067 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003068 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003069 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3070 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003071 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003072 return;
3073
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003074 if (!isa<ObjCMethodDecl>(D)) {
3075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3076 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003077 return;
3078 }
3079
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003080 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003081 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003082 D->addAttr(::new (S.Context)
3083 FastCallAttr(Attr.getRange(), S.Context,
3084 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003085 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003086 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003087 D->addAttr(::new (S.Context)
3088 StdCallAttr(Attr.getRange(), S.Context,
3089 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003090 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003091 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003092 D->addAttr(::new (S.Context)
3093 ThisCallAttr(Attr.getRange(), S.Context,
3094 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003095 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003096 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003097 D->addAttr(::new (S.Context)
3098 CDeclAttr(Attr.getRange(), S.Context,
3099 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003100 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003101 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003102 D->addAttr(::new (S.Context)
3103 PascalAttr(Attr.getRange(), S.Context,
3104 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003105 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003106 case AttributeList::AT_MSABI:
3107 D->addAttr(::new (S.Context)
3108 MSABIAttr(Attr.getRange(), S.Context,
3109 Attr.getAttributeSpellingListIndex()));
3110 return;
3111 case AttributeList::AT_SysVABI:
3112 D->addAttr(::new (S.Context)
3113 SysVABIAttr(Attr.getRange(), S.Context,
3114 Attr.getAttributeSpellingListIndex()));
3115 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003116 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003117 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003118 switch (CC) {
3119 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003120 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003121 break;
3122 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003123 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003124 break;
3125 default:
3126 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003127 }
3128
Michael Han99315932013-01-24 16:46:58 +00003129 D->addAttr(::new (S.Context)
3130 PcsAttr(Attr.getRange(), S.Context, PCS,
3131 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003132 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003133 }
Derek Schuffa2020962012-10-16 22:30:41 +00003134 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 PnaclCallAttr(Attr.getRange(), S.Context,
3137 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003138 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003139 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003140 D->addAttr(::new (S.Context)
3141 IntelOclBiccAttr(Attr.getRange(), S.Context,
3142 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003143 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003144
Abramo Bagnara50099372010-04-30 13:10:51 +00003145 default:
3146 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003147 }
3148}
3149
Aaron Ballman02df2e02012-12-09 17:45:41 +00003150bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3151 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003152 if (attr.isInvalid())
3153 return true;
3154
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003155 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003156 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003157 attr.setInvalid();
3158 return true;
3159 }
3160
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003161 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003162 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003163 case AttributeList::AT_CDecl: CC = CC_C; break;
3164 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3165 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3166 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3167 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003168 case AttributeList::AT_MSABI:
3169 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3170 CC_X86_64Win64;
3171 break;
3172 case AttributeList::AT_SysVABI:
3173 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3174 CC_C;
3175 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003176 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003177 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003178 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003179 attr.setInvalid();
3180 return true;
3181 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003182 if (StrRef == "aapcs") {
3183 CC = CC_AAPCS;
3184 break;
3185 } else if (StrRef == "aapcs-vfp") {
3186 CC = CC_AAPCS_VFP;
3187 break;
3188 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003189
3190 attr.setInvalid();
3191 Diag(attr.getLoc(), diag::err_invalid_pcs);
3192 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003193 }
Derek Schuffa2020962012-10-16 22:30:41 +00003194 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003195 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003196 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003197 }
3198
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003199 const TargetInfo &TI = Context.getTargetInfo();
3200 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3201 if (A == TargetInfo::CCCR_Warning) {
3202 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003203
3204 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3205 if (FD)
3206 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3207 TargetInfo::CCMT_NonMember;
3208 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003209 }
3210
John McCall3882ace2011-01-05 12:14:39 +00003211 return false;
3212}
3213
John McCall3882ace2011-01-05 12:14:39 +00003214/// Checks a regparm attribute, returning true if it is ill-formed and
3215/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003216bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3217 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003218 return true;
3219
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003220 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003221 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003222 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003223 }
Eli Friedman7044b762009-03-27 21:06:47 +00003224
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003225 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003226 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003227 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003228 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003229 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003230 }
3231
Douglas Gregore8bbc122011-09-02 00:18:52 +00003232 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003233 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003234 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003235 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003236 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003237 }
3238
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003239 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003240 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003241 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003242 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003243 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003244 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003245 }
3246
John McCall3882ace2011-01-05 12:14:39 +00003247 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003248}
3249
Aaron Ballman66039932013-12-19 00:41:31 +00003250static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3251 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003252 // check the attribute arguments.
3253 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3254 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003255 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3256 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003257 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003258 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003259
Aaron Ballman66039932013-12-19 00:41:31 +00003260 uint32_t MaxThreads, MinBlocks = 0;
3261 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3262 return;
3263 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3264 Attr.getArgAsExpr(1),
3265 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003266 return;
3267
3268 D->addAttr(::new (S.Context)
3269 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3270 MaxThreads, MinBlocks,
3271 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003272}
3273
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003274static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3275 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003276 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003277 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003278 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003279 return;
3280 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003281
3282 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003283 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003284
Aaron Ballman00e99962013-08-31 01:11:41 +00003285 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003286
3287 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3288 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3289 << Attr.getName() << ExpectedFunctionOrMethod;
3290 return;
3291 }
3292
3293 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003294 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3295 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003296 return;
3297
3298 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003299 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3300 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003301 return;
3302
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003303 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003304 if (IsPointer) {
3305 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003306 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003307 if (!BufferTy->isPointerType()) {
3308 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003309 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003310 }
3311 }
3312
Michael Han99315932013-01-24 16:46:58 +00003313 D->addAttr(::new (S.Context)
3314 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3315 ArgumentIdx, TypeTagIdx, IsPointer,
3316 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003317}
3318
3319static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3320 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003321 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003323 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003324 return;
3325 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003326
3327 if (!checkAttributeNumArgs(S, Attr, 1))
3328 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003329
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003330 if (!isa<VarDecl>(D)) {
3331 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3332 << Attr.getName() << ExpectedVariable;
3333 return;
3334 }
3335
Aaron Ballman00e99962013-08-31 01:11:41 +00003336 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003337 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003338 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3339 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003340
Michael Han99315932013-01-24 16:46:58 +00003341 D->addAttr(::new (S.Context)
3342 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003343 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003344 Attr.getLayoutCompatible(),
3345 Attr.getMustBeNull(),
3346 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003347}
3348
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003349//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003350// Checker-specific attribute handlers.
3351//===----------------------------------------------------------------------===//
3352
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003353static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003354 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003355 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003356}
3357
John McCalled433932011-01-25 03:31:58 +00003358static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003359 return type->isDependentType() ||
3360 type->isObjCObjectPointerType() ||
3361 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003362}
3363static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003364 return type->isDependentType() ||
3365 type->isPointerType() ||
3366 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003367}
3368
Chandler Carruthedc2c642011-07-02 00:01:44 +00003369static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003370 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003371 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003372
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003373 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003374 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3375 cf = false;
3376 } else {
3377 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3378 cf = true;
3379 }
3380
3381 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003382 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003383 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003384 return;
3385 }
3386
3387 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003388 param->addAttr(::new (S.Context)
3389 CFConsumedAttr(Attr.getRange(), S.Context,
3390 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003391 else
Michael Han99315932013-01-24 16:46:58 +00003392 param->addAttr(::new (S.Context)
3393 NSConsumedAttr(Attr.getRange(), S.Context,
3394 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003395}
3396
Chandler Carruthedc2c642011-07-02 00:01:44 +00003397static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3398 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003399
John McCalled433932011-01-25 03:31:58 +00003400 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003401
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003402 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003403 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003404 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003405 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003406 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003407 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3408 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003409 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003410 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003411 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003412 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003413 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003414 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003415 return;
3416 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003417
John McCalled433932011-01-25 03:31:58 +00003418 bool typeOK;
3419 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003420 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003421 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003422 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003423 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003424 cf = false;
3425 break;
3426
3427 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003428 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003429 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3430 cf = false;
3431 break;
3432
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003433 case AttributeList::AT_CFReturnsRetained:
3434 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003435 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3436 cf = true;
3437 break;
3438 }
3439
3440 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003441 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003442 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003443 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003444 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003445
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003446 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003447 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003448 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003449 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003450 D->addAttr(::new (S.Context)
3451 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3452 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003453 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003454 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003455 D->addAttr(::new (S.Context)
3456 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3457 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003458 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003459 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003460 D->addAttr(::new (S.Context)
3461 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3462 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003463 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003464 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003465 D->addAttr(::new (S.Context)
3466 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3467 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003468 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003469 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003470 D->addAttr(::new (S.Context)
3471 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3472 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003473 return;
3474 };
3475}
3476
John McCallcf166702011-07-22 08:53:00 +00003477static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3478 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003479 const int EP_ObjCMethod = 1;
3480 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003481
John McCallcf166702011-07-22 08:53:00 +00003482 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003483 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003484 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003485 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003486 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003487 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003488
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003489 if (!resultType->isReferenceType() &&
3490 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003491 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003492 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003493 << attr.getName()
3494 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003495 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003496
3497 // Drop the attribute.
3498 return;
3499 }
3500
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003501 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003502 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3503 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003504}
3505
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003506static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3507 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003508 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003509
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003510 DeclContext *DC = method->getDeclContext();
3511 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3512 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3513 << attr.getName() << 0;
3514 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3515 return;
3516 }
3517 if (method->getMethodFamily() == OMF_dealloc) {
3518 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3519 << attr.getName() << 1;
3520 return;
3521 }
3522
Michael Han99315932013-01-24 16:46:58 +00003523 method->addAttr(::new (S.Context)
3524 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3525 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003526}
3527
Aaron Ballmanfb763042013-12-02 18:05:46 +00003528static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3529 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003530 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003531 return;
John McCall32f5fe12011-09-30 05:12:12 +00003532
Aaron Ballmanfb763042013-12-02 18:05:46 +00003533 D->addAttr(::new (S.Context)
3534 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3535 Attr.getAttributeSpellingListIndex()));
3536}
3537
3538static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3539 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003540 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003541 return;
3542
3543 D->addAttr(::new (S.Context)
3544 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3545 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003546}
3547
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003548static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3549 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003550 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003551
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003552 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003553 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003554 return;
3555 }
3556
3557 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003558 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003559 Attr.getAttributeSpellingListIndex()));
3560}
3561
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003562static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3563 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003564 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3565
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003566 if (!Parm) {
3567 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3568 return;
3569 }
3570
3571 D->addAttr(::new (S.Context)
3572 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3573 Attr.getAttributeSpellingListIndex()));
3574}
3575
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003576static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3577 const AttributeList &Attr) {
3578 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003579 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003580 if (!RelatedClass) {
3581 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3582 return;
3583 }
3584 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003585 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003586 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003587 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003588 D->addAttr(::new (S.Context)
3589 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3590 ClassMethod, InstanceMethod,
3591 Attr.getAttributeSpellingListIndex()));
3592}
3593
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003594static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3595 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003596 ObjCInterfaceDecl *IFace;
3597 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3598 IFace = CatDecl->getClassInterface();
3599 else
3600 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003601 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003602 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003603 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3604 Attr.getAttributeSpellingListIndex()));
3605}
3606
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003607static void handleObjCRuntimeName(Sema &S, Decl *D,
3608 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003609 StringRef MetaDataName;
3610 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3611 return;
3612 D->addAttr(::new (S.Context)
3613 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3614 MetaDataName,
3615 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003616}
3617
Chandler Carruthedc2c642011-07-02 00:01:44 +00003618static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3619 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003620 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003621
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003622 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003623 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003624}
3625
Chandler Carruthedc2c642011-07-02 00:01:44 +00003626static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3627 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003628 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003629 QualType type = vd->getType();
3630
3631 if (!type->isDependentType() &&
3632 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003633 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003634 << type;
3635 return;
3636 }
3637
3638 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3639
3640 // If we have no lifetime yet, check the lifetime we're presumably
3641 // going to infer.
3642 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3643 lifetime = type->getObjCARCImplicitLifetime();
3644
3645 switch (lifetime) {
3646 case Qualifiers::OCL_None:
3647 assert(type->isDependentType() &&
3648 "didn't infer lifetime for non-dependent type?");
3649 break;
3650
3651 case Qualifiers::OCL_Weak: // meaningful
3652 case Qualifiers::OCL_Strong: // meaningful
3653 break;
3654
3655 case Qualifiers::OCL_ExplicitNone:
3656 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003657 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003658 << (lifetime == Qualifiers::OCL_Autoreleasing);
3659 break;
3660 }
3661
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003662 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003663 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3664 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003665}
3666
Francois Picheta83957a2010-12-19 06:50:37 +00003667//===----------------------------------------------------------------------===//
3668// Microsoft specific attribute handlers.
3669//===----------------------------------------------------------------------===//
3670
Chandler Carruthedc2c642011-07-02 00:01:44 +00003671static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003672 if (!S.LangOpts.CPlusPlus) {
3673 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3674 << Attr.getName() << AttributeLangSupport::C;
3675 return;
3676 }
3677
Aaron Ballman60e705e2013-11-24 20:58:02 +00003678 if (!isa<CXXRecordDecl>(D)) {
3679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3680 << Attr.getName() << ExpectedClass;
3681 return;
3682 }
3683
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003684 StringRef StrRef;
3685 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003686 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003687 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003688
David Majnemer89085342013-08-09 08:56:20 +00003689 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3690 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003691 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3692 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003693
Reid Kleckner140c4a72013-05-17 14:04:52 +00003694 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003695 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003696 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003697 return;
3698 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003699
David Majnemer89085342013-08-09 08:56:20 +00003700 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003701 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003702 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003703 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003704 return;
3705 }
David Majnemer89085342013-08-09 08:56:20 +00003706 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003707 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003708 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003709 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003710 }
Francois Picheta83957a2010-12-19 06:50:37 +00003711
David Majnemer89085342013-08-09 08:56:20 +00003712 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3713 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003714}
3715
David Majnemer2c4e00a2014-01-29 22:07:36 +00003716static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3717 if (!S.LangOpts.CPlusPlus) {
3718 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3719 << Attr.getName() << AttributeLangSupport::C;
3720 return;
3721 }
3722 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003723 D, Attr.getRange(), /*BestCase=*/true,
3724 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003725 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3726 if (IA)
3727 D->addAttr(IA);
3728}
3729
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003730static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3731 const AttributeList &Attr) {
3732 VarDecl *VD = cast<VarDecl>(D);
3733 if (!S.Context.getTargetInfo().isTLSSupported()) {
3734 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3735 return;
3736 }
3737 if (VD->getTSCSpec() != TSCS_unspecified) {
3738 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3739 return;
3740 }
3741 if (VD->hasLocalStorage()) {
3742 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3743 return;
3744 }
3745 VD->addAttr(::new (S.Context) ThreadAttr(
3746 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3747}
3748
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003749static void handleARMInterruptAttr(Sema &S, Decl *D,
3750 const AttributeList &Attr) {
3751 // Check the attribute arguments.
3752 if (Attr.getNumArgs() > 1) {
3753 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3754 << Attr.getName() << 1;
3755 return;
3756 }
3757
3758 StringRef Str;
3759 SourceLocation ArgLoc;
3760
3761 if (Attr.getNumArgs() == 0)
3762 Str = "";
3763 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3764 return;
3765
3766 ARMInterruptAttr::InterruptType Kind;
3767 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3768 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3769 << Attr.getName() << Str << ArgLoc;
3770 return;
3771 }
3772
3773 unsigned Index = Attr.getAttributeSpellingListIndex();
3774 D->addAttr(::new (S.Context)
3775 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3776}
3777
3778static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3779 const AttributeList &Attr) {
3780 if (!checkAttributeNumArgs(S, Attr, 1))
3781 return;
3782
3783 if (!Attr.isArgExpr(0)) {
3784 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3785 << AANT_ArgumentIntegerConstant;
3786 return;
3787 }
3788
3789 // FIXME: Check for decl - it should be void ()(void).
3790
3791 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3792 llvm::APSInt NumParams(32);
3793 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3794 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3795 << Attr.getName() << AANT_ArgumentIntegerConstant
3796 << NumParamsExpr->getSourceRange();
3797 return;
3798 }
3799
3800 unsigned Num = NumParams.getLimitedValue(255);
3801 if ((Num & 1) || Num > 30) {
3802 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3803 << Attr.getName() << (int)NumParams.getSExtValue()
3804 << NumParamsExpr->getSourceRange();
3805 return;
3806 }
3807
Aaron Ballman36a53502014-01-16 13:03:14 +00003808 D->addAttr(::new (S.Context)
3809 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3810 Attr.getAttributeSpellingListIndex()));
3811 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003812}
3813
3814static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3815 // Dispatch the interrupt attribute based on the current target.
3816 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3817 handleMSP430InterruptAttr(S, D, Attr);
3818 else
3819 handleARMInterruptAttr(S, D, Attr);
3820}
3821
3822static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3823 const AttributeList& Attr) {
3824 // If we try to apply it to a function pointer, don't warn, but don't
3825 // do anything, either. It doesn't matter anyway, because there's nothing
3826 // special about calling a force_align_arg_pointer function.
3827 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3828 if (VD && VD->getType()->isFunctionPointerType())
3829 return;
3830 // Also don't warn on function pointer typedefs.
3831 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3832 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3833 TD->getUnderlyingType()->isFunctionType()))
3834 return;
3835 // Attribute can only be applied to function types.
3836 if (!isa<FunctionDecl>(D)) {
3837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3838 << Attr.getName() << /* function */0;
3839 return;
3840 }
3841
Aaron Ballman36a53502014-01-16 13:03:14 +00003842 D->addAttr(::new (S.Context)
3843 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3844 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003845}
3846
3847DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3848 unsigned AttrSpellingListIndex) {
3849 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003850 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00003851 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003852 }
3853
3854 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003855 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003856
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003857 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003858}
3859
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003860DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3861 unsigned AttrSpellingListIndex) {
3862 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003863 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003864 D->dropAttr<DLLImportAttr>();
3865 }
3866
3867 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003868 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003869
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003870 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003871}
3872
Hans Wennborge82f19c2014-06-24 23:57:05 +00003873static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00003874 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
3875 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
3876 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
3877 << A.getName();
3878 return;
3879 }
3880
Hans Wennborge82f19c2014-06-24 23:57:05 +00003881 unsigned Index = A.getAttributeSpellingListIndex();
3882 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
3883 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
3884 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003885 if (NewAttr)
3886 D->addAttr(NewAttr);
3887}
3888
David Majnemer2c4e00a2014-01-29 22:07:36 +00003889MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003890Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003891 unsigned AttrSpellingListIndex,
3892 MSInheritanceAttr::Spelling SemanticSpelling) {
3893 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3894 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00003895 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003896 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3897 << 1 /*previous declaration*/;
3898 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3899 D->dropAttr<MSInheritanceAttr>();
3900 }
3901
3902 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3903 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003904 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3905 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003906 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003907 }
3908 } else {
3909 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3910 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3911 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003912 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003913 }
3914 if (RD->getDescribedClassTemplate()) {
3915 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3916 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003917 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003918 }
3919 }
3920
3921 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003922 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003923}
3924
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003925static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3926 // The capability attributes take a single string parameter for the name of
3927 // the capability they represent. The lockable attribute does not take any
3928 // parameters. However, semantically, both attributes represent the same
3929 // concept, and so they use the same semantic attribute. Eventually, the
3930 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003931 //
Alp Toker958027b2014-07-14 19:42:55 +00003932 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00003933 // literal will be considered a "mutex."
3934 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003935 SourceLocation LiteralLoc;
3936 if (Attr.getKind() == AttributeList::AT_Capability &&
3937 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3938 return;
3939
Aaron Ballman6c810072014-03-05 21:47:13 +00003940 // Currently, there are only two names allowed for a capability: role and
3941 // mutex (case insensitive). Diagnose other capability names.
3942 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3943 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3944
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003945 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3946 Attr.getAttributeSpellingListIndex()));
3947}
3948
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003949static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3950 const AttributeList &Attr) {
3951 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3952 Attr.getArgAsExpr(0),
3953 Attr.getAttributeSpellingListIndex()));
3954}
3955
3956static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3957 const AttributeList &Attr) {
3958 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003959 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003960 return;
3961
3962 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3963 S.Context,
3964 Args.data(), Args.size(),
3965 Attr.getAttributeSpellingListIndex()));
3966}
3967
3968static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3969 const AttributeList &Attr) {
3970 SmallVector<Expr*, 2> Args;
3971 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3972 return;
3973
3974 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3975 S.Context,
3976 Attr.getArgAsExpr(0),
3977 Args.data(),
3978 Args.size(),
3979 Attr.getAttributeSpellingListIndex()));
3980}
3981
3982static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3983 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003984 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003985 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003986 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003987
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003988 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
3989 Attr.getRange(), S.Context, Args.data(), Args.size(),
3990 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003991}
3992
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003993static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3994 const AttributeList &Attr) {
3995 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3996 return;
3997
3998 // check that all arguments are lockable objects
3999 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004000 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004001 if (Args.empty())
4002 return;
4003
4004 RequiresCapabilityAttr *RCA = ::new (S.Context)
4005 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4006 Args.size(), Attr.getAttributeSpellingListIndex());
4007
4008 D->addAttr(RCA);
4009}
4010
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004011/// Handles semantic checking for features that are common to all attributes,
4012/// such as checking whether a parameter was properly specified, or the correct
4013/// number of arguments were passed, etc.
4014static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4015 const AttributeList &Attr) {
4016 // Several attributes carry different semantics than the parsing requires, so
4017 // those are opted out of the common handling.
4018 //
4019 // We also bail on unknown and ignored attributes because those are handled
4020 // as part of the target-specific handling logic.
4021 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004022 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004023 return false;
4024
Aaron Ballman3aff6332013-12-02 19:30:36 +00004025 // Check whether the attribute requires specific language extensions to be
4026 // enabled.
4027 if (!Attr.diagnoseLangOpts(S))
4028 return true;
4029
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004030 // If there are no optional arguments, then checking for the argument count
4031 // is trivial.
4032 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4033 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4034 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004035
4036 // Check whether the attribute appertains to the given subject.
4037 if (!Attr.diagnoseAppertainsTo(S, D))
4038 return true;
4039
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004040 return false;
4041}
4042
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004043//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004044// Top Level Sema Entry Points
4045//===----------------------------------------------------------------------===//
4046
Richard Smithf8a75c32013-08-29 00:47:48 +00004047/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4048/// the attribute applies to decls. If the attribute is a type attribute, just
4049/// silently ignore it if a GNU attribute.
4050static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4051 const AttributeList &Attr,
4052 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004053 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004054 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004055
Richard Smithf8a75c32013-08-29 00:47:48 +00004056 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4057 // instead.
4058 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4059 return;
4060
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004061 // Unknown attributes are automatically warned on. Target-specific attributes
4062 // which do not apply to the current target architecture are treated as
4063 // though they were unknown attributes.
4064 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4065 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004066 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4067 ? diag::warn_unhandled_ms_attribute_ignored
4068 : diag::warn_unknown_attribute_ignored)
4069 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004070 return;
4071 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004072
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004073 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4074 return;
4075
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004076 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004077 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004078 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004079 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004080 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004081 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004082 handleInterruptAttr(S, D, Attr);
4083 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004084 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004085 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4086 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004087 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004088 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004089 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004090 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004091 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004092 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4093 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004094 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004095 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4096 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004097 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004098 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4099 break;
4100 case AttributeList::AT_IBOutlet:
4101 handleIBOutlet(S, D, Attr);
4102 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004103 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004104 handleIBOutletCollection(S, D, Attr);
4105 break;
4106 case AttributeList::AT_Alias:
4107 handleAliasAttr(S, D, Attr);
4108 break;
4109 case AttributeList::AT_Aligned:
4110 handleAlignedAttr(S, D, Attr);
4111 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004112 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004113 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004114 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004115 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004116 handleAnalyzerNoReturnAttr(S, D, Attr);
4117 break;
4118 case AttributeList::AT_TLSModel:
4119 handleTLSModelAttr(S, D, Attr);
4120 break;
4121 case AttributeList::AT_Annotate:
4122 handleAnnotateAttr(S, D, Attr);
4123 break;
4124 case AttributeList::AT_Availability:
4125 handleAvailabilityAttr(S, D, Attr);
4126 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004128 handleDependencyAttr(S, scope, D, Attr);
4129 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004130 case AttributeList::AT_Common:
4131 handleCommonAttr(S, D, Attr);
4132 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004133 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004134 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4135 break;
4136 case AttributeList::AT_Constructor:
4137 handleConstructorAttr(S, D, Attr);
4138 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004139 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004140 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4141 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004142 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004143 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004144 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004145 case AttributeList::AT_Destructor:
4146 handleDestructorAttr(S, D, Attr);
4147 break;
4148 case AttributeList::AT_EnableIf:
4149 handleEnableIfAttr(S, D, Attr);
4150 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004151 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004152 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004153 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004154 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004155 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004156 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004157 case AttributeList::AT_OptimizeNone:
4158 handleOptimizeNoneAttr(S, D, Attr);
4159 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004160 case AttributeList::AT_Flatten:
4161 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4162 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004163 case AttributeList::AT_Format:
4164 handleFormatAttr(S, D, Attr);
4165 break;
4166 case AttributeList::AT_FormatArg:
4167 handleFormatArgAttr(S, D, Attr);
4168 break;
4169 case AttributeList::AT_CUDAGlobal:
4170 handleGlobalAttr(S, D, Attr);
4171 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004172 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004173 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4174 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004175 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004176 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4177 break;
4178 case AttributeList::AT_GNUInline:
4179 handleGNUInlineAttr(S, D, Attr);
4180 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004181 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004182 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004183 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004184 case AttributeList::AT_Malloc:
4185 handleMallocAttr(S, D, Attr);
4186 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004187 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004188 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4189 break;
4190 case AttributeList::AT_Mode:
4191 handleModeAttr(S, D, Attr);
4192 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004193 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004194 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4195 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004196 case AttributeList::AT_NoSplitStack:
4197 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4198 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004199 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004200 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4201 handleNonNullAttrParameter(S, PVD, Attr);
4202 else
4203 handleNonNullAttr(S, D, Attr);
4204 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004205 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004206 handleReturnsNonNullAttr(S, D, Attr);
4207 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004208 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004209 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4210 break;
4211 case AttributeList::AT_Ownership:
4212 handleOwnershipAttr(S, D, Attr);
4213 break;
4214 case AttributeList::AT_Cold:
4215 handleColdAttr(S, D, Attr);
4216 break;
4217 case AttributeList::AT_Hot:
4218 handleHotAttr(S, D, Attr);
4219 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004220 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004221 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4222 break;
4223 case AttributeList::AT_NoReturn:
4224 handleNoReturnAttr(S, D, Attr);
4225 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004226 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004227 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4228 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004229 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004230 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4231 break;
4232 case AttributeList::AT_VecReturn:
4233 handleVecReturnAttr(S, D, Attr);
4234 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004235
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004236 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004237 handleObjCOwnershipAttr(S, D, Attr);
4238 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004239 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004240 handleObjCPreciseLifetimeAttr(S, D, Attr);
4241 break;
John McCall31168b02011-06-15 23:02:42 +00004242
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004243 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004244 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4245 break;
John McCallcf166702011-07-22 08:53:00 +00004246
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004247 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004248 handleObjCRequiresSuperAttr(S, D, Attr);
4249 break;
4250
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004251 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004252 handleObjCBridgeAttr(S, scope, D, Attr);
4253 break;
4254
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004255 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004256 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4257 break;
4258
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004259 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004260 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4261 break;
John McCallf1e8b342011-09-29 07:17:38 +00004262
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004263 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004264 handleObjCDesignatedInitializer(S, D, Attr);
4265 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004266
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004267 case AttributeList::AT_ObjCRuntimeName:
4268 handleObjCRuntimeName(S, D, Attr);
4269 break;
4270
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004271 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004272 handleCFAuditedTransferAttr(S, D, Attr);
4273 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004274 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004275 handleCFUnknownTransferAttr(S, D, Attr);
4276 break;
John McCall32f5fe12011-09-30 05:12:12 +00004277
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004278 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004279 case AttributeList::AT_NSConsumed:
4280 handleNSConsumedAttr(S, D, Attr);
4281 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004282 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004283 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4284 break;
John McCalled433932011-01-25 03:31:58 +00004285
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004286 case AttributeList::AT_NSReturnsAutoreleased:
4287 case AttributeList::AT_NSReturnsNotRetained:
4288 case AttributeList::AT_CFReturnsNotRetained:
4289 case AttributeList::AT_NSReturnsRetained:
4290 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004291 handleNSReturnsRetainedAttr(S, D, Attr);
4292 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004293 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004294 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4295 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004296 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004297 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4298 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004299 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004300 handleVecTypeHint(S, D, Attr);
4301 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004302
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004303 case AttributeList::AT_InitPriority:
4304 handleInitPriorityAttr(S, D, Attr);
4305 break;
4306
4307 case AttributeList::AT_Packed:
4308 handlePackedAttr(S, D, Attr);
4309 break;
4310 case AttributeList::AT_Section:
4311 handleSectionAttr(S, D, Attr);
4312 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004313 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004314 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004315 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004316 case AttributeList::AT_ArcWeakrefUnavailable:
4317 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4318 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004319 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004320 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4321 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004322 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004323 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004324 break;
4325 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004326 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4327 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004328 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004329 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4330 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004331 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004332 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4333 break;
4334 case AttributeList::AT_Used:
4335 handleUsedAttr(S, D, Attr);
4336 break;
John McCalld041a9b2013-02-20 01:54:26 +00004337 case AttributeList::AT_Visibility:
4338 handleVisibilityAttr(S, D, Attr, false);
4339 break;
4340 case AttributeList::AT_TypeVisibility:
4341 handleVisibilityAttr(S, D, Attr, true);
4342 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004343 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004344 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4345 break;
4346 case AttributeList::AT_WarnUnusedResult:
4347 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004348 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004349 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004350 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4351 break;
4352 case AttributeList::AT_WeakRef:
4353 handleWeakRefAttr(S, D, Attr);
4354 break;
4355 case AttributeList::AT_WeakImport:
4356 handleWeakImportAttr(S, D, Attr);
4357 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004358 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004359 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004360 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004361 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004362 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4363 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004364 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004365 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004366 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004367 case AttributeList::AT_ObjCNSObject:
4368 handleObjCNSObject(S, D, Attr);
4369 break;
4370 case AttributeList::AT_Blocks:
4371 handleBlocksAttr(S, D, Attr);
4372 break;
4373 case AttributeList::AT_Sentinel:
4374 handleSentinelAttr(S, D, Attr);
4375 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004376 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004377 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4378 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004379 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004380 handleSimpleAttribute<PureAttr>(S, D, Attr);
4381 break;
4382 case AttributeList::AT_Cleanup:
4383 handleCleanupAttr(S, D, Attr);
4384 break;
4385 case AttributeList::AT_NoDebug:
4386 handleNoDebugAttr(S, D, Attr);
4387 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004388 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004389 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4390 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004391 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004392 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4393 break;
4394 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4395 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4396 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004397 case AttributeList::AT_StdCall:
4398 case AttributeList::AT_CDecl:
4399 case AttributeList::AT_FastCall:
4400 case AttributeList::AT_ThisCall:
4401 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004402 case AttributeList::AT_MSABI:
4403 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004404 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004405 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004406 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004407 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004408 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004409 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004410 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4411 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004412 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004413 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4414 break;
John McCall8d32c052012-05-22 21:28:12 +00004415
4416 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004417 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004418 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004419 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004420 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004421 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004422 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004423 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004424 handleMSInheritanceAttr(S, D, Attr);
4425 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004426 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004427 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4428 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004429 case AttributeList::AT_Thread:
4430 handleDeclspecThreadAttr(S, D, Attr);
4431 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004432
4433 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004434 case AttributeList::AT_AssertExclusiveLock:
4435 handleAssertExclusiveLockAttr(S, D, Attr);
4436 break;
4437 case AttributeList::AT_AssertSharedLock:
4438 handleAssertSharedLockAttr(S, D, Attr);
4439 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004440 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004441 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4442 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004443 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004444 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004446 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004447 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4448 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004449 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004450 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004451 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004452 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004453 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004454 break;
4455 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004456 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004457 break;
4458 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004459 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004460 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004461 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004462 handleGuardedByAttr(S, D, Attr);
4463 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004464 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004465 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004466 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004467 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004468 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004470 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004471 handleLockReturnedAttr(S, D, Attr);
4472 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004473 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004474 handleLocksExcludedAttr(S, D, Attr);
4475 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004476 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004477 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004478 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004479 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004480 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004481 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004482 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004483 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004484 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004485
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004486 // Capability analysis attributes.
4487 case AttributeList::AT_Capability:
4488 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004489 handleCapabilityAttr(S, D, Attr);
4490 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004491 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004492 handleRequiresCapabilityAttr(S, D, Attr);
4493 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004494
4495 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004496 handleAssertCapabilityAttr(S, D, Attr);
4497 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004498 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004499 handleAcquireCapabilityAttr(S, D, Attr);
4500 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004501 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004502 handleReleaseCapabilityAttr(S, D, Attr);
4503 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004504 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004505 handleTryAcquireCapabilityAttr(S, D, Attr);
4506 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004507
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004508 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004509 case AttributeList::AT_Consumable:
4510 handleConsumableAttr(S, D, Attr);
4511 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004512 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004513 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4514 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004515 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004516 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4517 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004518 case AttributeList::AT_CallableWhen:
4519 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004520 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004521 case AttributeList::AT_ParamTypestate:
4522 handleParamTypestateAttr(S, D, Attr);
4523 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004524 case AttributeList::AT_ReturnTypestate:
4525 handleReturnTypestateAttr(S, D, Attr);
4526 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004527 case AttributeList::AT_SetTypestate:
4528 handleSetTypestateAttr(S, D, Attr);
4529 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004530 case AttributeList::AT_TestTypestate:
4531 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004532 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004533
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004534 // Type safety attributes.
4535 case AttributeList::AT_ArgumentWithTypeTag:
4536 handleArgumentWithTypeTagAttr(S, D, Attr);
4537 break;
4538 case AttributeList::AT_TypeTagForDatatype:
4539 handleTypeTagForDatatypeAttr(S, D, Attr);
4540 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004541 }
4542}
4543
4544/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4545/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004546void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004547 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004548 bool IncludeCXX11Attributes) {
4549 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004550 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004551
Joey Gouly2cd9db12013-12-13 16:15:28 +00004552 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004553 // GCC accepts
4554 // static int a9 __attribute__((weakref));
4555 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004556 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004557 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4558 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004559 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004560 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004561 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004562
4563 if (!D->hasAttr<OpenCLKernelAttr>()) {
4564 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004565 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4566 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004567 D->setInvalidDecl();
4568 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004569 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4570 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004571 D->setInvalidDecl();
4572 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004573 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4574 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004575 D->setInvalidDecl();
4576 }
4577 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004578}
4579
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004580// Annotation attributes are the only attributes allowed after an access
4581// specifier.
4582bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4583 const AttributeList *AttrList) {
4584 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004585 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004586 handleAnnotateAttr(*this, ASDecl, *l);
4587 } else {
4588 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4589 return true;
4590 }
4591 }
4592
4593 return false;
4594}
4595
John McCall42856de2011-10-01 05:17:03 +00004596/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4597/// contains any decl attributes that we should warn about.
4598static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4599 for ( ; A; A = A->getNext()) {
4600 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004601 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004602 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4603
4604 if (A->getKind() == AttributeList::UnknownAttribute) {
4605 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4606 << A->getName() << A->getRange();
4607 } else {
4608 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4609 << A->getName() << A->getRange();
4610 }
4611 }
4612}
4613
4614/// checkUnusedDeclAttributes - Given a declarator which is not being
4615/// used to build a declaration, complain about any decl attributes
4616/// which might be lying around on it.
4617void Sema::checkUnusedDeclAttributes(Declarator &D) {
4618 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4619 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4620 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4621 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4622}
4623
Ryan Flynn7d470f32009-07-30 03:15:39 +00004624/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004625/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004626NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4627 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004628 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004629 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004630 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004631 FunctionDecl *NewFD;
4632 // FIXME: Missing call to CheckFunctionDeclaration().
4633 // FIXME: Mangling?
4634 // FIXME: Is the qualifier info correct?
4635 // FIXME: Is the DeclContext correct?
4636 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4637 Loc, Loc, DeclarationName(II),
4638 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004639 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004640 FD->hasPrototype(),
4641 false/*isConstexprSpecified*/);
4642 NewD = NewFD;
4643
4644 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004645 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004646
4647 // Fake up parameter variables; they are declared as if this were
4648 // a typedef.
4649 QualType FDTy = FD->getType();
4650 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4651 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004652 for (const auto &AI : FT->param_types()) {
4653 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004654 Param->setScopeInfo(0, Params.size());
4655 Params.push_back(Param);
4656 }
David Blaikie9c70e042011-09-21 18:16:56 +00004657 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004658 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004659 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4660 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004661 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004662 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004663 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004664 if (VD->getQualifier()) {
4665 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004666 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004667 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004668 }
4669 return NewD;
4670}
4671
James Dennett634962f2012-06-14 21:40:34 +00004672/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004673/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004674void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004675 if (W.getUsed()) return; // only do this once
4676 W.setUsed(true);
4677 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4678 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004679 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004680 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4681 W.getLocation()));
4682 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004683 WeakTopLevelDecl.push_back(NewD);
4684 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4685 // to insert Decl at TU scope, sorry.
4686 DeclContext *SavedContext = CurContext;
4687 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004688 NewD->setDeclContext(CurContext);
4689 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004690 PushOnScopeChains(NewD, S);
4691 CurContext = SavedContext;
4692 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004693 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004694 }
4695}
4696
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004697void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4698 // It's valid to "forward-declare" #pragma weak, in which case we
4699 // have to do this.
4700 LoadExternalWeakUndeclaredIdentifiers();
4701 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004702 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004703 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4704 if (VD->isExternC())
4705 ND = VD;
4706 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4707 if (FD->isExternC())
4708 ND = FD;
4709 if (ND) {
4710 if (IdentifierInfo *Id = ND->getIdentifier()) {
4711 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4712 = WeakUndeclaredIdentifiers.find(Id);
4713 if (I != WeakUndeclaredIdentifiers.end()) {
4714 WeakInfo W = I->second;
4715 DeclApplyPragmaWeak(S, ND, W);
4716 WeakUndeclaredIdentifiers[Id] = W;
4717 }
4718 }
4719 }
4720 }
4721}
4722
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004723/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4724/// it, apply them to D. This is a bit tricky because PD can have attributes
4725/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004726void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004727 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004728 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004729 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004730
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004731 // Walk the declarator structure, applying decl attributes that were in a type
4732 // position to the decl itself. This handles cases like:
4733 // int *__attr__(x)** D;
4734 // when X is a decl attribute.
4735 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4736 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004737 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004738
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004739 // Finally, apply any attributes on the decl itself.
4740 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004741 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004742}
John McCall28a6aea2009-11-04 02:18:39 +00004743
John McCall31168b02011-06-15 23:02:42 +00004744/// Is the given declaration allowed to use a forbidden type?
4745static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4746 // Private ivars are always okay. Unfortunately, people don't
4747 // always properly make their ivars private, even in system headers.
4748 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004749 // Function declarations in sys headers will be marked unavailable.
4750 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4751 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004752 return false;
4753
4754 // Require it to be declared in a system header.
4755 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4756}
4757
4758/// Handle a delayed forbidden-type diagnostic.
4759static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4760 Decl *decl) {
4761 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004762 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4763 "this system declaration uses an unsupported type",
4764 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004765 return;
4766 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004767 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004768 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004769 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004770 // kind of forbidden type messages on unavailable functions.
4771 if (FD->hasAttr<UnavailableAttr>() &&
4772 diag.getForbiddenTypeDiagnostic() ==
4773 diag::err_arc_array_param_no_ownership) {
4774 diag.Triggered = true;
4775 return;
4776 }
4777 }
John McCall31168b02011-06-15 23:02:42 +00004778
4779 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4780 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4781 diag.Triggered = true;
4782}
4783
John McCall2ec85372012-05-07 06:16:41 +00004784void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4785 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004786 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004787 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004788
John McCall2ec85372012-05-07 06:16:41 +00004789 // When delaying diagnostics to run in the context of a parsed
4790 // declaration, we only want to actually emit anything if parsing
4791 // succeeds.
4792 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004793
John McCall2ec85372012-05-07 06:16:41 +00004794 // We emit all the active diagnostics in this pool or any of its
4795 // parents. In general, we'll get one pool for the decl spec
4796 // and a child pool for each declarator; in a decl group like:
4797 // deprecated_typedef foo, *bar, baz();
4798 // only the declarator pops will be passed decls. This is correct;
4799 // we really do need to consider delayed diagnostics from the decl spec
4800 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004801 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004802 do {
John McCall6347b682012-05-07 06:16:58 +00004803 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004804 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4805 // This const_cast is a bit lame. Really, Triggered should be mutable.
4806 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004807 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004808 continue;
4809
John McCallc1465822011-02-14 07:13:47 +00004810 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004811 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004812 case DelayedDiagnostic::Unavailable:
4813 // Don't bother giving deprecation/unavailable diagnostics if
4814 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004815 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004816 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004817 break;
4818
4819 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004820 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004821 break;
John McCall31168b02011-06-15 23:02:42 +00004822
4823 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004824 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004825 break;
John McCall86121512010-01-27 03:50:35 +00004826 }
4827 }
John McCall2ec85372012-05-07 06:16:41 +00004828 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004829}
4830
John McCall6347b682012-05-07 06:16:58 +00004831/// Given a set of delayed diagnostics, re-emit them as if they had
4832/// been delayed in the current context instead of in the given pool.
4833/// Essentially, this just moves them to the current pool.
4834void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4835 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4836 assert(curPool && "re-emitting in undelayed context not supported");
4837 curPool->steal(pool);
4838}
4839
John McCall28a6aea2009-11-04 02:18:39 +00004840static bool isDeclDeprecated(Decl *D) {
4841 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004842 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004843 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004844 // A category implicitly has the availability of the interface.
4845 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4846 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004847 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4848 return false;
4849}
4850
Ted Kremenekb79ee572013-12-18 23:30:06 +00004851static bool isDeclUnavailable(Decl *D) {
4852 do {
4853 if (D->isUnavailable())
4854 return true;
4855 // A category implicitly has the availability of the interface.
4856 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4857 return CatD->getClassInterface()->isUnavailable();
4858 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4859 return false;
4860}
4861
Eli Friedman971bfa12012-08-08 21:52:41 +00004862static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004863DoEmitAvailabilityWarning(Sema &S,
4864 DelayedDiagnostic::DDKind K,
4865 Decl *Ctx,
4866 const NamedDecl *D,
4867 StringRef Message,
4868 SourceLocation Loc,
4869 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004870 const ObjCPropertyDecl *ObjCProperty,
4871 bool ObjCPropertyAccess) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004872
4873 // Diagnostics for deprecated or unavailable.
4874 unsigned diag, diag_message, diag_fwdclass_message;
4875
4876 // Matches 'diag::note_property_attribute' options.
4877 unsigned property_note_select;
4878
4879 // Matches diag::note_availability_specified_here.
4880 unsigned available_here_select_kind;
4881
4882 // Don't warn if our current context is deprecated or unavailable.
4883 switch (K) {
4884 case DelayedDiagnostic::Deprecation:
4885 if (isDeclDeprecated(Ctx))
4886 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004887 diag = !ObjCPropertyAccess ? diag::warn_deprecated
4888 : diag::warn_property_method_deprecated;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004889 diag_message = diag::warn_deprecated_message;
4890 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4891 property_note_select = /* deprecated */ 0;
4892 available_here_select_kind = /* deprecated */ 2;
4893 break;
4894
4895 case DelayedDiagnostic::Unavailable:
4896 if (isDeclUnavailable(Ctx))
4897 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004898 diag = !ObjCPropertyAccess ? diag::err_unavailable
4899 : diag::err_property_method_unavailable;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004900 diag_message = diag::err_unavailable_message;
4901 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4902 property_note_select = /* unavailable */ 1;
4903 available_here_select_kind = /* unavailable */ 0;
4904 break;
4905
4906 default:
4907 llvm_unreachable("Neither a deprecation or unavailable kind");
4908 }
4909
Eli Friedman971bfa12012-08-08 21:52:41 +00004910 DeclarationName Name = D->getDeclName();
4911 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004912 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004913 if (ObjCProperty)
4914 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4915 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004916 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004917 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004918 if (ObjCProperty)
4919 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4920 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004921 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004922 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004923 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4924 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004925
4926 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4927 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004928}
4929
Ted Kremenekb79ee572013-12-18 23:30:06 +00004930void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4931 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004932 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004933 DoEmitAvailabilityWarning(*this,
4934 (DelayedDiagnostic::DDKind) DD.Kind,
4935 Ctx,
4936 DD.getDeprecationDecl(),
4937 DD.getDeprecationMessage(),
4938 DD.Loc,
4939 DD.getUnknownObjCClass(),
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004940 DD.getObjCProperty(), false);
John McCall28a6aea2009-11-04 02:18:39 +00004941}
4942
Ted Kremenekb79ee572013-12-18 23:30:06 +00004943void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4944 NamedDecl *D, StringRef Message,
4945 SourceLocation Loc,
4946 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004947 const ObjCPropertyDecl *ObjCProperty,
4948 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00004949 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004950 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004951 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4952 UnknownObjCClass,
4953 ObjCProperty,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004954 Message,
4955 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00004956 return;
4957 }
4958
Ted Kremenekb79ee572013-12-18 23:30:06 +00004959 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4960 DelayedDiagnostic::DDKind K;
4961 switch (AD) {
4962 case AD_Deprecation:
4963 K = DelayedDiagnostic::Deprecation;
4964 break;
4965 case AD_Unavailable:
4966 K = DelayedDiagnostic::Unavailable;
4967 break;
4968 }
4969
4970 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004971 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00004972}