blob: ff4192e91d3a80611458f99894ebb634f6d0ba90 [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
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000151template <typename Compare>
152static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
153 unsigned Num, unsigned Diag,
154 Compare Comp) {
155 if (Comp(getNumAttributeArgs(Attr), Num)) {
156 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000157 return false;
158 }
159
160 return true;
161}
162
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000163/// \brief Check if the attribute has exactly as many args as Num. May
164/// output an error.
165static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
166 unsigned Num) {
167 return checkAttributeNumArgsImpl(S, Attr, Num,
168 diag::err_attribute_wrong_number_arguments,
169 std::not_equal_to<unsigned>());
170}
171
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000172/// \brief Check if the attribute has at least as many args as Num. May
173/// output an error.
174static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000175 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000176 return checkAttributeNumArgsImpl(S, Attr, Num,
177 diag::err_attribute_too_few_arguments,
178 std::less<unsigned>());
179}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000180
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000181/// \brief Check if the attribute has at most as many args as Num. May
182/// output an error.
183static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
184 unsigned Num) {
185 return checkAttributeNumArgsImpl(S, Attr, Num,
186 diag::err_attribute_too_many_arguments,
187 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000188}
189
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000190/// \brief If Expr is a valid integer constant, get the value of the integer
191/// expression and return success or failure. May output an error.
192static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
193 const Expr *Expr, uint32_t &Val,
194 unsigned Idx = UINT_MAX) {
195 llvm::APSInt I(32);
196 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
197 !Expr->isIntegerConstantExpr(I, S.Context)) {
198 if (Idx != UINT_MAX)
199 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
200 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
201 << Expr->getSourceRange();
202 else
203 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
204 << Attr.getName() << AANT_ArgumentIntegerConstant
205 << Expr->getSourceRange();
206 return false;
207 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000208
209 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000210 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
211 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000212 return false;
213 }
214
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000215 Val = (uint32_t)I.getZExtValue();
216 return true;
217}
218
Aaron Ballmanfb763042013-12-02 18:05:46 +0000219/// \brief Diagnose mutually exclusive attributes when present on a given
220/// declaration. Returns true if diagnosed.
221template <typename AttrTy>
222static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000223 const AttributeList &Attr) {
224 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000225 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000226 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000227 return true;
228 }
229 return false;
230}
231
Alp Toker601b22c2014-01-21 23:35:24 +0000232/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000233/// instance method D. May output an error.
234///
235/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000236static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
237 const AttributeList &Attr,
238 unsigned AttrArgNum,
239 const Expr *IdxExpr,
240 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000241 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000242
243 // In C++ the implicit 'this' function parameter also counts.
244 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000245 bool HP = hasFunctionProto(D);
246 bool HasImplicitThisParam = isInstanceMethod(D);
247 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000248 unsigned NumParams =
249 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000250
251 llvm::APSInt IdxInt;
252 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
253 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000254 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
255 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
256 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000257 return false;
258 }
259
260 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000261 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000262 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
263 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000264 return false;
265 }
266 Idx--; // Convert to zero-based.
267 if (HasImplicitThisParam) {
268 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000269 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000270 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000271 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000272 return false;
273 }
274 --Idx;
275 }
276
277 return true;
278}
279
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000280/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
281/// If not emit an error and return false. If the argument is an identifier it
282/// will emit an error with a fixit hint and treat it as if it was a string
283/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000284bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
285 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000286 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000287 // Look for identifiers. If we have one emit a hint to fix it to a literal.
288 if (Attr.isArgIdent(ArgNum)) {
289 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000290 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000291 << Attr.getName() << AANT_ArgumentString
292 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000293 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000294 Str = Loc->Ident->getName();
295 if (ArgLocation)
296 *ArgLocation = Loc->Loc;
297 return true;
298 }
299
300 // Now check for an actual string literal.
301 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
302 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
303 if (ArgLocation)
304 *ArgLocation = ArgExpr->getLocStart();
305
306 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000307 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000308 << Attr.getName() << AANT_ArgumentString;
309 return false;
310 }
311
312 Str = Literal->getString();
313 return true;
314}
315
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000316/// \brief Applies the given attribute to the Decl without performing any
317/// additional semantic checking.
318template <typename AttrType>
319static void handleSimpleAttribute(Sema &S, Decl *D,
320 const AttributeList &Attr) {
321 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
322 Attr.getAttributeSpellingListIndex()));
323}
324
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000325/// \brief Check if the passed-in expression is of type int or bool.
326static bool isIntOrBool(Expr *Exp) {
327 QualType QT = Exp->getType();
328 return QT->isBooleanType() || QT->isIntegerType();
329}
330
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000331
332// Check to see if the type is a smart pointer of some kind. We assume
333// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000334static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
335 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
336 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000337 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000338 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000339
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000340 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
341 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000342 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000343 return false;
344
345 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000346}
347
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000348/// \brief Check if passed in Decl is a pointer type.
349/// Note that this function may produce an error message.
350/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000351static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
352 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000353 const ValueDecl *vd = cast<ValueDecl>(D);
354 QualType QT = vd->getType();
355 if (QT->isAnyPointerType())
356 return true;
357
358 if (const RecordType *RT = QT->getAs<RecordType>()) {
359 // If it's an incomplete type, it could be a smart pointer; skip it.
360 // (We don't want to force template instantiation if we can avoid it,
361 // since that would alter the order in which templates are instantiated.)
362 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000363 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000364
Aaron Ballman553e6812013-12-26 14:54:11 +0000365 if (threadSafetyCheckIsSmartPointer(S, RT))
366 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000367 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000368
369 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000370 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000371 return false;
372}
373
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000374/// \brief Checks that the passed in QualType either is of RecordType or points
375/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000376static const RecordType *getRecordType(QualType QT) {
377 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000378 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000379
380 // Now check if we point to record type.
381 if (const PointerType *PT = QT->getAs<PointerType>())
382 return PT->getPointeeType()->getAs<RecordType>();
383
Craig Topperc3ec1492014-05-26 06:22:03 +0000384 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000385}
386
Aaron Ballman76050722014-04-04 15:13:57 +0000387static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000388 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000389
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000390 if (!RT)
391 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000392
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000393 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000394 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000395 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000396
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000397 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000398 // FIXME -- Check the type that the smart pointer points to.
399 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000400 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000401
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000402 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000403 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000404 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000405 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000406
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000407 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000408 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
409 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000410 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
411 void *) {
412 return BS->getType()->getAs<RecordType>()
413 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000414 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000415 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000416 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000417 return false;
418}
419
Aaron Ballman76050722014-04-04 15:13:57 +0000420static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000421 const auto *TD = Ty->getAs<TypedefType>();
422 if (!TD)
423 return false;
424
425 TypedefNameDecl *TN = TD->getDecl();
426 if (!TN)
427 return false;
428
429 return TN->hasAttr<CapabilityAttr>();
430}
431
Aaron Ballman76050722014-04-04 15:13:57 +0000432static bool typeHasCapability(Sema &S, QualType Ty) {
433 if (checkTypedefTypeForCapability(Ty))
434 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000435
Aaron Ballman76050722014-04-04 15:13:57 +0000436 if (checkRecordTypeForCapability(S, Ty))
437 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000438
Aaron Ballman76050722014-04-04 15:13:57 +0000439 return false;
440}
441
442static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
443 // Capability expressions are simple expressions involving the boolean logic
444 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
445 // a DeclRefExpr is found, its type should be checked to determine whether it
446 // is a capability or not.
447
448 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
449 return typeHasCapability(S, E->getType());
450 else if (const auto *E = dyn_cast<CastExpr>(Ex))
451 return isCapabilityExpr(S, E->getSubExpr());
452 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
453 return isCapabilityExpr(S, E->getSubExpr());
454 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
455 if (E->getOpcode() == UO_LNot)
456 return isCapabilityExpr(S, E->getSubExpr());
457 return false;
458 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
459 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
460 return isCapabilityExpr(S, E->getLHS()) &&
461 isCapabilityExpr(S, E->getRHS());
462 return false;
463 }
464
465 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000466}
467
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000468/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
469/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000470/// \param Sidx The attribute argument index to start checking with.
471/// \param ParamIdxOk Whether an argument can be indexing into a function
472/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000473static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
474 const AttributeList &Attr,
475 SmallVectorImpl<Expr *> &Args,
476 int Sidx = 0,
477 bool ParamIdxOk = false) {
478 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000479 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000480
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000481 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000482 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000483 Args.push_back(ArgExp);
484 continue;
485 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000486
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000487 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000488 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000489 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000490 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000491 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000492 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000493 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000494 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000495
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000496 // We allow constant strings to be used as a placeholder for expressions
497 // that are not valid C++ syntax, but warn that they are ignored.
498 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
499 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000500 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501 continue;
502 }
503
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506 // A pointer to member expression of the form &MyClass::mu is treated
507 // specially -- we need to look at the type of the member.
508 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
509 if (UOp->getOpcode() == UO_AddrOf)
510 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
511 if (DRE->getDecl()->isCXXInstanceMember())
512 ArgTy = DRE->getDecl()->getType();
513
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000514 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000517 // Now check if we index into a record type function param.
518 if(!RT && ParamIdxOk) {
519 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000520 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
521 if(FD && IL) {
522 unsigned int NumParams = FD->getNumParams();
523 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000524 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
525 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
526 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
528 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000529 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000531 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000532 }
533 }
534
Aaron Ballman76050722014-04-04 15:13:57 +0000535 // If the type does not have a capability, see if the components of the
536 // expression have capabilities. This allows for writing C code where the
537 // capability may be on the type, and the expression is a capability
538 // boolean logic expression. Eg) requires_capability(A || B && !C)
539 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
540 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
541 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000542
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000543 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000544 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545}
546
Chris Lattner58418ff2008-06-29 00:16:31 +0000547//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000548// Attribute Implementations
549//===----------------------------------------------------------------------===//
550
Michael Hana9171bc2012-08-03 17:40:43 +0000551static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000552 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000553 if (!threadSafetyCheckIsPointer(S, D, Attr))
554 return;
555
Michael Han99315932013-01-24 16:46:58 +0000556 D->addAttr(::new (S.Context)
557 PtGuardedVarAttr(Attr.getRange(), S.Context,
558 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000559}
560
Michael Hana9171bc2012-08-03 17:40:43 +0000561static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
562 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000563 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000564 SmallVector<Expr*, 1> Args;
565 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000566 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000567 unsigned Size = Args.size();
568 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000569 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000570
Michael Han3be3b442012-07-23 18:48:41 +0000571 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000572
Michael Han3be3b442012-07-23 18:48:41 +0000573 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000574}
575
Michael Han3be3b442012-07-23 18:48:41 +0000576static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000577 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000578 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
579 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000580
Aaron Ballman36a53502014-01-16 13:03:14 +0000581 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
582 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000583}
584
Michael Hana9171bc2012-08-03 17:40:43 +0000585static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000586 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000587 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000588 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
589 return;
590
591 if (!threadSafetyCheckIsPointer(S, D, Attr))
592 return;
593
594 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000595 S.Context, Arg,
596 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000597}
598
Michael Hana9171bc2012-08-03 17:40:43 +0000599static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
600 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000601 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000602 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000603 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000604
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000605 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000606 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000607 if (!QT->isDependentType()) {
608 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000609 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000610 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000611 << Attr.getName();
612 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000613 }
614 }
615
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000616 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000617 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000618 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000619 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000620
Michael Han3be3b442012-07-23 18:48:41 +0000621 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000625 const AttributeList &Attr) {
626 SmallVector<Expr*, 1> Args;
627 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
628 return;
629
630 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000631 D->addAttr(::new (S.Context)
632 AcquiredAfterAttr(Attr.getRange(), S.Context,
633 StartArg, Args.size(),
634 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000635}
636
Michael Hana9171bc2012-08-03 17:40:43 +0000637static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000638 const AttributeList &Attr) {
639 SmallVector<Expr*, 1> Args;
640 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
641 return;
642
643 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000644 D->addAttr(::new (S.Context)
645 AcquiredBeforeAttr(Attr.getRange(), S.Context,
646 StartArg, Args.size(),
647 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000648}
649
Michael Hana9171bc2012-08-03 17:40:43 +0000650static bool checkLockFunAttrCommon(Sema &S, Decl *D,
651 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000652 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000653 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000654 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000655 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000656
Michael Han3be3b442012-07-23 18:48:41 +0000657 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000658}
659
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000660static void handleAssertSharedLockAttr(Sema &S, Decl *D,
661 const AttributeList &Attr) {
662 SmallVector<Expr*, 1> Args;
663 if (!checkLockFunAttrCommon(S, D, Attr, Args))
664 return;
665
666 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000667 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000668 D->addAttr(::new (S.Context)
669 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
670 Attr.getAttributeSpellingListIndex()));
671}
672
673static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
674 const AttributeList &Attr) {
675 SmallVector<Expr*, 1> Args;
676 if (!checkLockFunAttrCommon(S, D, Attr, Args))
677 return;
678
679 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000680 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000681 D->addAttr(::new (S.Context)
682 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
683 StartArg, Size,
684 Attr.getAttributeSpellingListIndex()));
685}
686
687
Michael Hana9171bc2012-08-03 17:40:43 +0000688static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
689 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000690 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000691 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000692 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000693
Aaron Ballman00e99962013-08-31 01:11:41 +0000694 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000696 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000697 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 }
699
700 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000701 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000702
Michael Han3be3b442012-07-23 18:48:41 +0000703 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000704}
705
Michael Hana9171bc2012-08-03 17:40:43 +0000706static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000707 const AttributeList &Attr) {
708 SmallVector<Expr*, 2> Args;
709 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
710 return;
711
Michael Han99315932013-01-24 16:46:58 +0000712 D->addAttr(::new (S.Context)
713 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000714 Attr.getArgAsExpr(0),
715 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000716 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000717}
718
Michael Hana9171bc2012-08-03 17:40:43 +0000719static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000720 const AttributeList &Attr) {
721 SmallVector<Expr*, 2> Args;
722 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
723 return;
724
Michael Han99315932013-01-24 16:46:58 +0000725 D->addAttr(::new (S.Context)
726 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000727 Attr.getArgAsExpr(0),
728 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000729 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000730}
731
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000732static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000733 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000734 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000735 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000736 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000737 unsigned Size = Args.size();
738 if (Size == 0)
739 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000740
Michael Han99315932013-01-24 16:46:58 +0000741 D->addAttr(::new (S.Context)
742 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
743 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000744}
745
746static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000747 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000748 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749 return;
750
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000751 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000752 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000753 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000754 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000755 if (Size == 0)
756 return;
757 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000758
Michael Han99315932013-01-24 16:46:58 +0000759 D->addAttr(::new (S.Context)
760 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
761 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000762}
763
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000764static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
765 Expr *Cond = Attr.getArgAsExpr(0);
766 if (!Cond->isTypeDependent()) {
767 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
768 if (Converted.isInvalid())
769 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000770 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000771 }
772
773 StringRef Msg;
774 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
775 return;
776
777 SmallVector<PartialDiagnosticAt, 8> Diags;
778 if (!Cond->isValueDependent() &&
779 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
780 Diags)) {
781 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
782 for (int I = 0, N = Diags.size(); I != N; ++I)
783 S.Diag(Diags[I].first, Diags[I].second);
784 return;
785 }
786
787 D->addAttr(::new (S.Context)
788 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
789 Attr.getAttributeSpellingListIndex()));
790}
791
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000792static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000793 ConsumableAttr::ConsumedState DefaultState;
794
795 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000796 IdentifierLoc *IL = Attr.getArgAsIdent(0);
797 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
798 DefaultState)) {
799 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
800 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000801 return;
802 }
David Blaikie16f76d22013-09-06 01:28:43 +0000803 } else {
804 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
805 << Attr.getName() << AANT_ArgumentIdentifier;
806 return;
807 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000808
809 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000810 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000811 Attr.getAttributeSpellingListIndex()));
812}
813
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000814
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000815static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
816 const AttributeList &Attr) {
817 ASTContext &CurrContext = S.getASTContext();
818 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
819
820 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
821 if (!RD->hasAttr<ConsumableAttr>()) {
822 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
823 RD->getNameAsString();
824
825 return false;
826 }
827 }
828
829 return true;
830}
831
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000832
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000833static void handleCallableWhenAttr(Sema &S, Decl *D,
834 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000835 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
836 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000837
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000838 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
839 return;
840
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000841 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
842 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
843 CallableWhenAttr::ConsumedState CallableState;
844
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000845 StringRef StateString;
846 SourceLocation Loc;
847 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
848 return;
849
850 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000851 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000852 S.Diag(Loc, diag::warn_attribute_type_not_supported)
853 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000854 return;
855 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000856
857 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000858 }
859
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000860 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000861 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
862 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000863}
864
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000865
DeLesley Hutchins69391772013-10-17 23:23:53 +0000866static void handleParamTypestateAttr(Sema &S, Decl *D,
867 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000868 ParamTypestateAttr::ConsumedState ParamState;
869
870 if (Attr.isArgIdent(0)) {
871 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
872 StringRef StateString = Ident->Ident->getName();
873
874 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
875 ParamState)) {
876 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
877 << Attr.getName() << StateString;
878 return;
879 }
880 } else {
881 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
882 Attr.getName() << AANT_ArgumentIdentifier;
883 return;
884 }
885
886 // FIXME: This check is currently being done in the analysis. It can be
887 // enabled here only after the parser propagates attributes at
888 // template specialization definition, not declaration.
889 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
890 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
891 //
892 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
893 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
894 // ReturnType.getAsString();
895 // return;
896 //}
897
898 D->addAttr(::new (S.Context)
899 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
900 Attr.getAttributeSpellingListIndex()));
901}
902
903
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000904static void handleReturnTypestateAttr(Sema &S, Decl *D,
905 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000906 ReturnTypestateAttr::ConsumedState ReturnState;
907
908 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000909 IdentifierLoc *IL = Attr.getArgAsIdent(0);
910 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
911 ReturnState)) {
912 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
913 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000914 return;
915 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000916 } else {
917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
918 Attr.getName() << AANT_ArgumentIdentifier;
919 return;
920 }
921
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000922 // FIXME: This check is currently being done in the analysis. It can be
923 // enabled here only after the parser propagates attributes at
924 // template specialization definition, not declaration.
925 //QualType ReturnType;
926 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000927 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
928 // ReturnType = Param->getType();
929 //
930 //} else if (const CXXConstructorDecl *Constructor =
931 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000932 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
933 //
934 //} else {
935 //
936 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
937 //}
938 //
939 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
940 //
941 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
942 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
943 // ReturnType.getAsString();
944 // return;
945 //}
946
947 D->addAttr(::new (S.Context)
948 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
949 Attr.getAttributeSpellingListIndex()));
950}
951
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000952
953static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000954 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
955 return;
956
957 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000958 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000959 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
960 StringRef Param = Ident->Ident->getName();
961 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
962 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
963 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000964 return;
965 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000966 } else {
967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
968 Attr.getName() << AANT_ArgumentIdentifier;
969 return;
970 }
971
972 D->addAttr(::new (S.Context)
973 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
974 Attr.getAttributeSpellingListIndex()));
975}
976
Chris Wailes9385f9f2013-10-29 20:28:41 +0000977static void handleTestTypestateAttr(Sema &S, Decl *D,
978 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000979 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
980 return;
981
Chris Wailes9385f9f2013-10-29 20:28:41 +0000982 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000984 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
985 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +0000986 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000987 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
988 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000989 return;
990 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000991 } else {
992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
993 Attr.getName() << AANT_ArgumentIdentifier;
994 return;
995 }
996
997 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +0000998 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000999 Attr.getAttributeSpellingListIndex()));
1000}
1001
Chandler Carruthedc2c642011-07-02 00:01:44 +00001002static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1003 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001004 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001005 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001006}
1007
Chandler Carruthedc2c642011-07-02 00:01:44 +00001008static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001009 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001010 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1011 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001012 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001013 // If the alignment is less than or equal to 8 bits, the packed attribute
1014 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001015 if (!FD->getType()->isDependentType() &&
1016 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001017 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001018 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001019 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001020 else
Michael Han99315932013-01-24 16:46:58 +00001021 FD->addAttr(::new (S.Context)
1022 PackedAttr(Attr.getRange(), S.Context,
1023 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001024 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001026}
1027
Ted Kremenek7fd17232011-09-29 07:02:25 +00001028static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1029 // The IBOutlet/IBOutletCollection attributes only apply to instance
1030 // variables or properties of Objective-C classes. The outlet must also
1031 // have an object reference type.
1032 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1033 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001034 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001035 << Attr.getName() << VD->getType() << 0;
1036 return false;
1037 }
1038 }
1039 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1040 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001041 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001042 << Attr.getName() << PD->getType() << 1;
1043 return false;
1044 }
1045 }
1046 else {
1047 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1048 return false;
1049 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001050
Ted Kremenek7fd17232011-09-29 07:02:25 +00001051 return true;
1052}
1053
Chandler Carruthedc2c642011-07-02 00:01:44 +00001054static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001055 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001056 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001057
Michael Han99315932013-01-24 16:46:58 +00001058 D->addAttr(::new (S.Context)
1059 IBOutletAttr(Attr.getRange(), S.Context,
1060 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001061}
1062
Chandler Carruthedc2c642011-07-02 00:01:44 +00001063static void handleIBOutletCollection(Sema &S, Decl *D,
1064 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001065
1066 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001067 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001068 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1069 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001070 return;
1071 }
1072
Ted Kremenek7fd17232011-09-29 07:02:25 +00001073 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001074 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001075
Richard Smithb1f9a282013-10-31 01:56:18 +00001076 ParsedType PT;
1077
1078 if (Attr.hasParsedType())
1079 PT = Attr.getTypeArg();
1080 else {
1081 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1082 S.getScopeForContext(D->getDeclContext()->getParent()));
1083 if (!PT) {
1084 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1085 return;
1086 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001087 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001088
Craig Topperc3ec1492014-05-26 06:22:03 +00001089 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001090 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1091 if (!QTLoc)
1092 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001093
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001094 // Diagnose use of non-object type in iboutletcollection attribute.
1095 // FIXME. Gnu attribute extension ignores use of builtin types in
1096 // attributes. So, __attribute__((iboutletcollection(char))) will be
1097 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001098 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001099 S.Diag(Attr.getLoc(),
1100 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1101 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001102 return;
1103 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001104
Michael Han99315932013-01-24 16:46:58 +00001105 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001106 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001107 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001108}
1109
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001110static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001111 if (const RecordType *UT = T->getAsUnionType())
1112 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1113 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001114 for (const auto *I : UD->fields()) {
1115 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001116 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1117 T = QT;
1118 return;
1119 }
1120 }
1121 }
1122}
1123
Ted Kremenek9aedc152014-01-17 06:24:56 +00001124static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001125 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001126 T = T.getNonReferenceType();
1127 possibleTransparentUnionPointerType(T);
1128
1129 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001130 S.Diag(Attr.getLoc(),
1131 isReturnValue ? diag::warn_attribute_return_pointers_only
1132 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001133 << Attr.getName() << R;
1134 return false;
1135 }
1136 return true;
1137}
1138
Chandler Carruthedc2c642011-07-02 00:01:44 +00001139static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001140 SmallVector<unsigned, 8> NonNullArgs;
1141 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001142 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001143 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001144 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001145 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001146
1147 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001148 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001149 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1150 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001151 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001152
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001153 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155
1156 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1157 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001158 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001159 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1160 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001161 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001162 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001163 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001165
Ted Kremenek22813f42010-10-21 18:49:36 +00001166 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001167 if (NonNullArgs.empty()) {
1168 // Warn the trivial case only if attribute is not coming from a
1169 // macro instantiation.
1170 if (Attr.getLoc().isFileID())
1171 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001172 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001173 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001174 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001175
Nick Lewyckye1121512013-01-24 01:12:16 +00001176 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001177 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001178 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001179 D->addAttr(::new (S.Context)
1180 NonNullAttr(Attr.getRange(), S.Context, start, size,
1181 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001182}
1183
Jordan Rosec9399072014-02-11 17:27:59 +00001184static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1185 const AttributeList &Attr) {
1186 if (Attr.getNumArgs() > 0) {
1187 if (D->getFunctionType()) {
1188 handleNonNullAttr(S, D, Attr);
1189 } else {
1190 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1191 << D->getSourceRange();
1192 }
1193 return;
1194 }
1195
1196 // Is the argument a pointer type?
1197 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1198 return;
1199
1200 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001201 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001202 Attr.getAttributeSpellingListIndex()));
1203}
1204
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001205static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1206 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001207 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001208 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1209 /* isReturnValue */ true))
1210 return;
1211
1212 D->addAttr(::new (S.Context)
1213 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1214 Attr.getAttributeSpellingListIndex()));
1215}
1216
Chandler Carruthedc2c642011-07-02 00:01:44 +00001217static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001218 // This attribute must be applied to a function declaration. The first
1219 // argument to the attribute must be an identifier, the name of the resource,
1220 // for example: malloc. The following arguments must be argument indexes, the
1221 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001222 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001223 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001224 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001225
Aaron Ballman00e99962013-08-31 01:11:41 +00001226 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001227 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001228 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001229 return;
1230 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001231
Richard Smith852e9ce2013-11-27 01:46:48 +00001232 // Figure out our Kind.
1233 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001234 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001235 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001236
Richard Smith852e9ce2013-11-27 01:46:48 +00001237 // Check arguments.
1238 switch (K) {
1239 case OwnershipAttr::Takes:
1240 case OwnershipAttr::Holds:
1241 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001242 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1243 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001244 return;
1245 }
1246 break;
1247 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001248 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001249 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1250 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251 return;
1252 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001253 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001254 }
1255
Richard Smith852e9ce2013-11-27 01:46:48 +00001256 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001257
1258 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001259 StringRef ModuleName = Module->getName();
1260 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1261 ModuleName.size() > 4) {
1262 ModuleName = ModuleName.drop_front(2).drop_back(2);
1263 Module = &S.PP.getIdentifierTable().get(ModuleName);
1264 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001265
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001266 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001267 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1268 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001269 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001270 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001271 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001272
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001273 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001274 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001275 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001276 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001277 case OwnershipAttr::Takes:
1278 case OwnershipAttr::Holds:
1279 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1280 Err = 0;
1281 break;
1282 case OwnershipAttr::Returns:
1283 if (!T->isIntegerType())
1284 Err = 1;
1285 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001286 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001287 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001288 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001289 << Ex->getSourceRange();
1290 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001291 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001292
1293 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001294 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001295 // Cannot have two ownership attributes of different kinds for the same
1296 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001297 if (I->getOwnKind() != K && I->args_end() !=
1298 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001299 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001300 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001301 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001302 } else if (K == OwnershipAttr::Returns &&
1303 I->getOwnKind() == OwnershipAttr::Returns) {
1304 // A returns attribute conflicts with any other returns attribute using
1305 // a different index. Note, diagnostic reporting is 1-based, but stored
1306 // argument indexes are 0-based.
1307 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1308 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1309 << *(I->args_begin()) + 1;
1310 if (I->args_size())
1311 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1312 << (unsigned)Idx + 1 << Ex->getSourceRange();
1313 return;
1314 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001315 }
1316 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001317 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001318 }
1319
1320 unsigned* start = OwnershipArgs.data();
1321 unsigned size = OwnershipArgs.size();
1322 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001323
Michael Han99315932013-01-24 16:46:58 +00001324 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001325 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001326 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001327}
1328
Chandler Carruthedc2c642011-07-02 00:01:44 +00001329static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001330 // Check the attribute arguments.
1331 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1333 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001334 return;
1335 }
1336
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001337 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001338
Rafael Espindolac18086a2010-02-23 22:00:30 +00001339 // gcc rejects
1340 // class c {
1341 // static int a __attribute__((weakref ("v2")));
1342 // static int b() __attribute__((weakref ("f3")));
1343 // };
1344 // and ignores the attributes of
1345 // void f(void) {
1346 // static int a __attribute__((weakref ("v2")));
1347 // }
1348 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001349 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001350 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1352 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001353 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001354 }
1355
1356 // The GCC manual says
1357 //
1358 // At present, a declaration to which `weakref' is attached can only
1359 // be `static'.
1360 //
1361 // It also says
1362 //
1363 // Without a TARGET,
1364 // given as an argument to `weakref' or to `alias', `weakref' is
1365 // equivalent to `weak'.
1366 //
1367 // gcc 4.4.1 will accept
1368 // int a7 __attribute__((weakref));
1369 // as
1370 // int a7 __attribute__((weak));
1371 // This looks like a bug in gcc. We reject that for now. We should revisit
1372 // it if this behaviour is actually used.
1373
Rafael Espindolac18086a2010-02-23 22:00:30 +00001374 // GCC rejects
1375 // static ((alias ("y"), weakref)).
1376 // Should we? How to check that weakref is before or after alias?
1377
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001378 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1379 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1380 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001381 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001382 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001383 // GCC will accept anything as the argument of weakref. Should we
1384 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001385 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1386 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001387
Michael Han99315932013-01-24 16:46:58 +00001388 D->addAttr(::new (S.Context)
1389 WeakRefAttr(Attr.getRange(), S.Context,
1390 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001391}
1392
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001393static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1394 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001395 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001396 return;
1397
Douglas Gregore8bbc122011-09-02 00:18:52 +00001398 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001399 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1400 return;
1401 }
1402
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001403 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001404
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001405 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001406 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001407}
1408
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001409static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001410 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001411 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001412
Michael Han99315932013-01-24 16:46:58 +00001413 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1414 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001415}
1416
1417static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001418 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001419 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001420
Michael Han99315932013-01-24 16:46:58 +00001421 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1422 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001423}
1424
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001425static void handleTLSModelAttr(Sema &S, Decl *D,
1426 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001427 StringRef Model;
1428 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001429 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001430 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001431 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001432
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001433 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001434 if (Model != "global-dynamic" && Model != "local-dynamic"
1435 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001436 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001437 return;
1438 }
1439
Michael Han99315932013-01-24 16:46:58 +00001440 D->addAttr(::new (S.Context)
1441 TLSModelAttr(Attr.getRange(), S.Context, Model,
1442 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001443}
1444
Chandler Carruthedc2c642011-07-02 00:01:44 +00001445static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001446 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001447 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001448 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001449 D->addAttr(::new (S.Context)
1450 MallocAttr(Attr.getRange(), S.Context,
1451 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001452 return;
1453 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001454 }
1455
Ted Kremenek08479ae2009-08-15 00:51:46 +00001456 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001457}
1458
Chandler Carruthedc2c642011-07-02 00:01:44 +00001459static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001460 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001461 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1462 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001463 return;
1464 }
1465
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001466 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1467 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001468}
1469
Chandler Carruthedc2c642011-07-02 00:01:44 +00001470static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001471 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001472
1473 if (S.CheckNoReturnAttr(attr)) return;
1474
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001475 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001476 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001477 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001478 return;
1479 }
1480
Michael Han99315932013-01-24 16:46:58 +00001481 D->addAttr(::new (S.Context)
1482 NoReturnAttr(attr.getRange(), S.Context,
1483 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001484}
1485
1486bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001487 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001488 attr.setInvalid();
1489 return true;
1490 }
1491
1492 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001493}
1494
Chandler Carruthedc2c642011-07-02 00:01:44 +00001495static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1496 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001497
1498 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1499 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001500 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1501 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001502 if (!VD || (!VD->getType()->isBlockPointerType() &&
1503 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001504 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001505 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001506 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001507 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001508 return;
1509 }
1510 }
1511
Michael Han99315932013-01-24 16:46:58 +00001512 D->addAttr(::new (S.Context)
1513 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1514 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001515}
1516
John Thompsoncdb847ba2010-08-09 21:53:52 +00001517// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001518static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001519/*
1520 Returning a Vector Class in Registers
1521
Eric Christopherbc638a82010-12-01 22:13:54 +00001522 According to the PPU ABI specifications, a class with a single member of
1523 vector type is returned in memory when used as the return value of a function.
1524 This results in inefficient code when implementing vector classes. To return
1525 the value in a single vector register, add the vecreturn attribute to the
1526 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001527
1528 Example:
1529
1530 struct Vector
1531 {
1532 __vector float xyzw;
1533 } __attribute__((vecreturn));
1534
1535 Vector Add(Vector lhs, Vector rhs)
1536 {
1537 Vector result;
1538 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1539 return result; // This will be returned in a register
1540 }
1541*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001542 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1543 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001544 return;
1545 }
1546
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001547 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001548 int count = 0;
1549
1550 if (!isa<CXXRecordDecl>(record)) {
1551 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1552 return;
1553 }
1554
1555 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1556 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1557 return;
1558 }
1559
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001560 for (const auto *I : record->fields()) {
1561 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001562 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1563 return;
1564 }
1565 count++;
1566 }
1567
Michael Han99315932013-01-24 16:46:58 +00001568 D->addAttr(::new (S.Context)
1569 VecReturnAttr(Attr.getRange(), S.Context,
1570 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001571}
1572
Richard Smithe233fbf2013-01-28 22:42:45 +00001573static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1574 const AttributeList &Attr) {
1575 if (isa<ParmVarDecl>(D)) {
1576 // [[carries_dependency]] can only be applied to a parameter if it is a
1577 // parameter of a function declaration or lambda.
1578 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1579 S.Diag(Attr.getLoc(),
1580 diag::err_carries_dependency_param_not_function_decl);
1581 return;
1582 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001583 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001584
1585 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1586 Attr.getRange(), S.Context,
1587 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001588}
1589
Chandler Carruthedc2c642011-07-02 00:01:44 +00001590static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001591 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001592 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001593 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001594 return;
1595 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001596 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001598 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001599 return;
1600 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001601
Michael Han99315932013-01-24 16:46:58 +00001602 D->addAttr(::new (S.Context)
1603 UsedAttr(Attr.getRange(), S.Context,
1604 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001605}
1606
Chandler Carruthedc2c642011-07-02 00:01:44 +00001607static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001608 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001609 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001610 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1611 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001612
Michael Han99315932013-01-24 16:46:58 +00001613 D->addAttr(::new (S.Context)
1614 ConstructorAttr(Attr.getRange(), S.Context, priority,
1615 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001616}
1617
Chandler Carruthedc2c642011-07-02 00:01:44 +00001618static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001619 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001620 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001621 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1622 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001623
Michael Han99315932013-01-24 16:46:58 +00001624 D->addAttr(::new (S.Context)
1625 DestructorAttr(Attr.getRange(), S.Context, priority,
1626 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001627}
1628
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001629template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001630static void handleAttrWithMessage(Sema &S, Decl *D,
1631 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001632 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001633 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001634 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001635 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1638 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001639}
1640
Ted Kremenek438f8db2014-02-22 01:06:05 +00001641static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001642 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001643 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001644 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1645 << Attr.getName() << Attr.getRange();
1646 return;
1647 }
1648
Ted Kremenek28eace62013-11-23 01:01:34 +00001649 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001650 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1651 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001652}
1653
Jordy Rose740b0c22012-05-08 03:27:22 +00001654static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1655 IdentifierInfo *Platform,
1656 VersionTuple Introduced,
1657 VersionTuple Deprecated,
1658 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001659 StringRef PlatformName
1660 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1661 if (PlatformName.empty())
1662 PlatformName = Platform->getName();
1663
1664 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1665 // of these steps are needed).
1666 if (!Introduced.empty() && !Deprecated.empty() &&
1667 !(Introduced <= Deprecated)) {
1668 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1669 << 1 << PlatformName << Deprecated.getAsString()
1670 << 0 << Introduced.getAsString();
1671 return true;
1672 }
1673
1674 if (!Introduced.empty() && !Obsoleted.empty() &&
1675 !(Introduced <= Obsoleted)) {
1676 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1677 << 2 << PlatformName << Obsoleted.getAsString()
1678 << 0 << Introduced.getAsString();
1679 return true;
1680 }
1681
1682 if (!Deprecated.empty() && !Obsoleted.empty() &&
1683 !(Deprecated <= Obsoleted)) {
1684 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1685 << 2 << PlatformName << Obsoleted.getAsString()
1686 << 1 << Deprecated.getAsString();
1687 return true;
1688 }
1689
1690 return false;
1691}
1692
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001693/// \brief Check whether the two versions match.
1694///
1695/// If either version tuple is empty, then they are assumed to match. If
1696/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1697static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1698 bool BeforeIsOkay) {
1699 if (X.empty() || Y.empty())
1700 return true;
1701
1702 if (X == Y)
1703 return true;
1704
1705 if (BeforeIsOkay && X < Y)
1706 return true;
1707
1708 return false;
1709}
1710
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001711AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001712 IdentifierInfo *Platform,
1713 VersionTuple Introduced,
1714 VersionTuple Deprecated,
1715 VersionTuple Obsoleted,
1716 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001717 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001718 bool Override,
1719 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001720 VersionTuple MergedIntroduced = Introduced;
1721 VersionTuple MergedDeprecated = Deprecated;
1722 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001723 bool FoundAny = false;
1724
Rafael Espindolac67f2232012-05-10 02:50:16 +00001725 if (D->hasAttrs()) {
1726 AttrVec &Attrs = D->getAttrs();
1727 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1728 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1729 if (!OldAA) {
1730 ++i;
1731 continue;
1732 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001733
Rafael Espindolac67f2232012-05-10 02:50:16 +00001734 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1735 if (OldPlatform != Platform) {
1736 ++i;
1737 continue;
1738 }
1739
1740 FoundAny = true;
1741 VersionTuple OldIntroduced = OldAA->getIntroduced();
1742 VersionTuple OldDeprecated = OldAA->getDeprecated();
1743 VersionTuple OldObsoleted = OldAA->getObsoleted();
1744 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001745
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001746 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1747 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1748 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1749 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001750 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001751 if (Override) {
1752 int Which = -1;
1753 VersionTuple FirstVersion;
1754 VersionTuple SecondVersion;
1755 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1756 Which = 0;
1757 FirstVersion = OldIntroduced;
1758 SecondVersion = Introduced;
1759 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1760 Which = 1;
1761 FirstVersion = Deprecated;
1762 SecondVersion = OldDeprecated;
1763 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1764 Which = 2;
1765 FirstVersion = Obsoleted;
1766 SecondVersion = OldObsoleted;
1767 }
1768
1769 if (Which == -1) {
1770 Diag(OldAA->getLocation(),
1771 diag::warn_mismatched_availability_override_unavail)
1772 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1773 } else {
1774 Diag(OldAA->getLocation(),
1775 diag::warn_mismatched_availability_override)
1776 << Which
1777 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1778 << FirstVersion.getAsString() << SecondVersion.getAsString();
1779 }
1780 Diag(Range.getBegin(), diag::note_overridden_method);
1781 } else {
1782 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1783 Diag(Range.getBegin(), diag::note_previous_attribute);
1784 }
1785
Rafael Espindolac67f2232012-05-10 02:50:16 +00001786 Attrs.erase(Attrs.begin() + i);
1787 --e;
1788 continue;
1789 }
1790
1791 VersionTuple MergedIntroduced2 = MergedIntroduced;
1792 VersionTuple MergedDeprecated2 = MergedDeprecated;
1793 VersionTuple MergedObsoleted2 = MergedObsoleted;
1794
1795 if (MergedIntroduced2.empty())
1796 MergedIntroduced2 = OldIntroduced;
1797 if (MergedDeprecated2.empty())
1798 MergedDeprecated2 = OldDeprecated;
1799 if (MergedObsoleted2.empty())
1800 MergedObsoleted2 = OldObsoleted;
1801
1802 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1803 MergedIntroduced2, MergedDeprecated2,
1804 MergedObsoleted2)) {
1805 Attrs.erase(Attrs.begin() + i);
1806 --e;
1807 continue;
1808 }
1809
1810 MergedIntroduced = MergedIntroduced2;
1811 MergedDeprecated = MergedDeprecated2;
1812 MergedObsoleted = MergedObsoleted2;
1813 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001814 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001815 }
1816
1817 if (FoundAny &&
1818 MergedIntroduced == Introduced &&
1819 MergedDeprecated == Deprecated &&
1820 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001821 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001822
Ted Kremenekb5445722013-04-06 00:34:27 +00001823 // Only create a new attribute if !Override, but we want to do
1824 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001825 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001826 MergedDeprecated, MergedObsoleted) &&
1827 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001828 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1829 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001830 Obsoleted, IsUnavailable, Message,
1831 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001832 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001833 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001834}
1835
Chandler Carruthedc2c642011-07-02 00:01:44 +00001836static void handleAvailabilityAttr(Sema &S, Decl *D,
1837 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001838 if (!checkAttributeNumArgs(S, Attr, 1))
1839 return;
1840 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001841 unsigned Index = Attr.getAttributeSpellingListIndex();
1842
Aaron Ballman00e99962013-08-31 01:11:41 +00001843 IdentifierInfo *II = Platform->Ident;
1844 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1845 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1846 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001847
Rafael Espindolac231fab2013-01-08 21:30:32 +00001848 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1849 if (!ND) {
1850 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1851 return;
1852 }
1853
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001854 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1855 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1856 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001857 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001858 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001859 if (const StringLiteral *SE =
1860 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001861 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001862
Aaron Ballman00e99962013-08-31 01:11:41 +00001863 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001864 Introduced.Version,
1865 Deprecated.Version,
1866 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001867 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001868 /*Override=*/false,
1869 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001870 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001871 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001872}
1873
John McCalld041a9b2013-02-20 01:54:26 +00001874template <class T>
1875static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1876 typename T::VisibilityType value,
1877 unsigned attrSpellingListIndex) {
1878 T *existingAttr = D->getAttr<T>();
1879 if (existingAttr) {
1880 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1881 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001882 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001883 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1884 S.Diag(range.getBegin(), diag::note_previous_attribute);
1885 D->dropAttr<T>();
1886 }
1887 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1888}
1889
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001890VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001891 VisibilityAttr::VisibilityType Vis,
1892 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001893 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1894 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001895}
1896
John McCalld041a9b2013-02-20 01:54:26 +00001897TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1898 TypeVisibilityAttr::VisibilityType Vis,
1899 unsigned AttrSpellingListIndex) {
1900 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1901 AttrSpellingListIndex);
1902}
1903
1904static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1905 bool isTypeVisibility) {
1906 // Visibility attributes don't mean anything on a typedef.
1907 if (isa<TypedefNameDecl>(D)) {
1908 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1909 << Attr.getName();
1910 return;
1911 }
1912
1913 // 'type_visibility' can only go on a type or namespace.
1914 if (isTypeVisibility &&
1915 !(isa<TagDecl>(D) ||
1916 isa<ObjCInterfaceDecl>(D) ||
1917 isa<NamespaceDecl>(D))) {
1918 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1919 << Attr.getName() << ExpectedTypeOrNamespace;
1920 return;
1921 }
1922
Benjamin Kramer70370212013-09-09 15:08:57 +00001923 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001924 StringRef TypeStr;
1925 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001926 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001927 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001928
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001929 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001930 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001931 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001932 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001933 return;
1934 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001935
1936 // Complain about attempts to use protected visibility on targets
1937 // (like Darwin) that don't support it.
1938 if (type == VisibilityAttr::Protected &&
1939 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1940 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1941 type = VisibilityAttr::Default;
1942 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001943
Michael Han99315932013-01-24 16:46:58 +00001944 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001945 clang::Attr *newAttr;
1946 if (isTypeVisibility) {
1947 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1948 (TypeVisibilityAttr::VisibilityType) type,
1949 Index);
1950 } else {
1951 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1952 }
1953 if (newAttr)
1954 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001955}
1956
Chandler Carruthedc2c642011-07-02 00:01:44 +00001957static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1958 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001959 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001960 if (!Attr.isArgIdent(0)) {
1961 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1962 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001963 return;
1964 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001965
Aaron Ballman682ee422013-09-11 19:47:58 +00001966 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1967 ObjCMethodFamilyAttr::FamilyKind F;
1968 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1969 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1970 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001971 return;
1972 }
1973
Alp Toker314cc812014-01-25 16:55:45 +00001974 if (F == ObjCMethodFamilyAttr::OMF_init &&
1975 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001976 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001977 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001978 // Ignore the attribute.
1979 return;
1980 }
1981
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001982 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001983 S.Context, F,
1984 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001985}
1986
Chandler Carruthedc2c642011-07-02 00:01:44 +00001987static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001988 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001989 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001990 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001991 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1992 return;
1993 }
1994 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001995 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1996 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001997 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00001998 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
1999 return;
2000 }
2001 }
2002 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002003 // It is okay to include this attribute on properties, e.g.:
2004 //
2005 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2006 //
2007 // In this case it follows tradition and suppresses an error in the above
2008 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002009 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002010 }
Michael Han99315932013-01-24 16:46:58 +00002011 D->addAttr(::new (S.Context)
2012 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2013 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002014}
2015
Chandler Carruthedc2c642011-07-02 00:01:44 +00002016static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002017 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002018 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002019 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002020 return;
2021 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002022
Aaron Ballman00e99962013-08-31 01:11:41 +00002023 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002024 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002025 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2026 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2027 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002028 return;
2029 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002030
Michael Han99315932013-01-24 16:46:58 +00002031 D->addAttr(::new (S.Context)
2032 BlocksAttr(Attr.getRange(), S.Context, type,
2033 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002034}
2035
Chandler Carruthedc2c642011-07-02 00:01:44 +00002036static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002037 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002038 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002039 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002040 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002041 if (E->isTypeDependent() || E->isValueDependent() ||
2042 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002044 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002045 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002046 return;
2047 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002048
John McCallb46f2872011-09-09 07:56:05 +00002049 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002050 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2051 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002052 return;
2053 }
John McCallb46f2872011-09-09 07:56:05 +00002054
2055 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002056 }
2057
Aaron Ballman18a78382013-11-21 00:28:23 +00002058 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002059 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002060 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002061 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002062 if (E->isTypeDependent() || E->isValueDependent() ||
2063 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002065 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002066 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002067 return;
2068 }
2069 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002070
John McCallb46f2872011-09-09 07:56:05 +00002071 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002072 // FIXME: This error message could be improved, it would be nice
2073 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002074 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2075 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 return;
2077 }
2078 }
2079
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002080 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002081 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002082 if (isa<FunctionNoProtoType>(FT)) {
2083 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2084 return;
2085 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002086
Chris Lattner9363e312009-03-17 23:03:47 +00002087 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002088 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002089 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002091 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002092 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002093 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002094 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002095 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002096 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2097 if (!BD->isVariadic()) {
2098 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2099 return;
2100 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002101 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002102 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002103 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002104 const FunctionType *FT = Ty->isFunctionPointerType()
2105 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002106 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002107 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002108 int m = Ty->isFunctionPointerType() ? 0 : 1;
2109 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002110 return;
2111 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002112 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002113 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002114 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002115 return;
2116 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002117 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002118 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002119 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002120 return;
2121 }
Michael Han99315932013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context)
2123 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2124 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002125}
2126
Chandler Carruthedc2c642011-07-02 00:01:44 +00002127static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002128 if (D->getFunctionType() &&
2129 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002130 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2131 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002132 return;
2133 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002134 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002135 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002136 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2137 << Attr.getName() << 1;
2138 return;
2139 }
2140
Michael Han99315932013-01-24 16:46:58 +00002141 D->addAttr(::new (S.Context)
2142 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2143 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002144}
2145
Chandler Carruthedc2c642011-07-02 00:01:44 +00002146static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002147 // weak_import only applies to variable & function declarations.
2148 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002149 if (!D->canBeWeakImported(isDef)) {
2150 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002151 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2152 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002153 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002154 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002155 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002156 // Nothing to warn about here.
2157 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002158 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002159 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002160
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002161 return;
2162 }
2163
Michael Han99315932013-01-24 16:46:58 +00002164 D->addAttr(::new (S.Context)
2165 WeakImportAttr(Attr.getRange(), S.Context,
2166 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002167}
2168
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002169// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002170template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002171static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002172 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002173 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002174 for (unsigned i = 0; i < 3; ++i) {
2175 const Expr *E = Attr.getArgAsExpr(i);
2176 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002177 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002178 if (WGSize[i] == 0) {
2179 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2180 << Attr.getName() << E->getSourceRange();
2181 return;
2182 }
2183 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002184
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002185 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2186 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2187 Existing->getYDim() == WGSize[1] &&
2188 Existing->getZDim() == WGSize[2]))
2189 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002190
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002191 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2192 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002193 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002194}
2195
Joey Goulyaba589c2013-03-08 09:42:32 +00002196static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002197 if (!Attr.hasParsedType()) {
2198 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2199 << Attr.getName() << 1;
2200 return;
2201 }
2202
Craig Topperc3ec1492014-05-26 06:22:03 +00002203 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002204 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2205 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002206
2207 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2208 (ParmType->isBooleanType() ||
2209 !ParmType->isIntegralType(S.getASTContext()))) {
2210 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2211 << ParmType;
2212 return;
2213 }
2214
Aaron Ballmana9e05402013-12-02 22:16:55 +00002215 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002216 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002217 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2218 return;
2219 }
2220 }
2221
2222 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002223 ParmTSI,
2224 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002225}
2226
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002227SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002228 StringRef Name,
2229 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002230 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2231 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002232 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002233 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2234 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002235 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002236 }
Michael Han99315932013-01-24 16:46:58 +00002237 return ::new (Context) SectionAttr(Range, Context, Name,
2238 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002239}
2240
Chandler Carruthedc2c642011-07-02 00:01:44 +00002241static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002242 // Make sure that there is a string literal as the sections's single
2243 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002244 StringRef Str;
2245 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002246 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002247 return;
Mike Stump11289f42009-09-09 15:08:12 +00002248
Chris Lattner30ba6742009-08-10 19:03:04 +00002249 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002250 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002251 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002252 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002253 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002254 return;
2255 }
Mike Stump11289f42009-09-09 15:08:12 +00002256
Michael Han99315932013-01-24 16:46:58 +00002257 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002258 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002259 if (NewAttr)
2260 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002261}
2262
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002263
Chandler Carruthedc2c642011-07-02 00:01:44 +00002264static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002265 VarDecl *VD = cast<VarDecl>(D);
2266 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002267 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002268 return;
2269 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002270
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002271 Expr *E = Attr.getArgAsExpr(0);
2272 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002273 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002274 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002275
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002276 // gcc only allows for simple identifiers. Since we support more than gcc, we
2277 // will warn the user.
2278 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2279 if (DRE->hasQualifier())
2280 S.Diag(Loc, diag::warn_cleanup_ext);
2281 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2282 NI = DRE->getNameInfo();
2283 if (!FD) {
2284 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2285 << NI.getName();
2286 return;
2287 }
2288 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2289 if (ULE->hasExplicitTemplateArgs())
2290 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002291 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2292 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002293 if (!FD) {
2294 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2295 << NI.getName();
2296 if (ULE->getType() == S.Context.OverloadTy)
2297 S.NoteAllOverloadCandidates(ULE);
2298 return;
2299 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002300 } else {
2301 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002302 return;
2303 }
2304
Anders Carlssond277d792009-01-31 01:16:18 +00002305 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002306 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2307 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002308 return;
2309 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002310
Anders Carlsson723f55d2009-02-07 23:16:50 +00002311 // We're currently more strict than GCC about what function types we accept.
2312 // If this ever proves to be a problem it should be easy to fix.
2313 QualType Ty = S.Context.getPointerType(VD->getType());
2314 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002315 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2316 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002317 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2318 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002319 return;
2320 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002321
Michael Han99315932013-01-24 16:46:58 +00002322 D->addAttr(::new (S.Context)
2323 CleanupAttr(Attr.getRange(), S.Context, FD,
2324 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002325}
2326
Mike Stumpd3bb5572009-07-24 19:02:52 +00002327/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002328/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002329static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002330 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002331 uint64_t Idx;
2332 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002333 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002334
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002335 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002336 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002337
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002338 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2339 if (not_nsstring_type &&
2340 !isCFStringType(Ty, S.Context) &&
2341 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002342 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002343 // FIXME: Should highlight the actual expression that has the wrong type.
2344 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002345 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002346 << IdxExpr->getSourceRange();
2347 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002348 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002349 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002350 if (!isNSStringType(Ty, S.Context) &&
2351 !isCFStringType(Ty, S.Context) &&
2352 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002353 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002354 // FIXME: Should highlight the actual expression that has the wrong type.
2355 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002356 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002357 << IdxExpr->getSourceRange();
2358 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002359 }
2360
Alp Toker601b22c2014-01-21 23:35:24 +00002361 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002362 // because that has corrected for the implicit this parameter, and is zero-
2363 // based. The attribute expects what the user wrote explicitly.
2364 llvm::APSInt Val;
2365 IdxExpr->EvaluateAsInt(Val, S.Context);
2366
Michael Han99315932013-01-24 16:46:58 +00002367 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002368 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002369 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002370}
2371
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002372enum FormatAttrKind {
2373 CFStringFormat,
2374 NSStringFormat,
2375 StrftimeFormat,
2376 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002377 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002378 InvalidFormat
2379};
2380
2381/// getFormatAttrKind - Map from format attribute names to supported format
2382/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002383static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002384 return llvm::StringSwitch<FormatAttrKind>(Format)
2385 // Check for formats that get handled specially.
2386 .Case("NSString", NSStringFormat)
2387 .Case("CFString", CFStringFormat)
2388 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002389
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002390 // Otherwise, check for supported formats.
2391 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2392 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2393 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002394
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002395 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2396 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002397}
2398
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002399/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002400/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002401static void handleInitPriorityAttr(Sema &S, Decl *D,
2402 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002403 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002404 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2405 return;
2406 }
2407
Aaron Ballman4a611152013-11-27 16:34:09 +00002408 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002409 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2410 Attr.setInvalid();
2411 return;
2412 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002413 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002414 if (S.Context.getAsArrayType(T))
2415 T = S.Context.getBaseElementType(T);
2416 if (!T->getAs<RecordType>()) {
2417 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2418 Attr.setInvalid();
2419 return;
2420 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002421
2422 Expr *E = Attr.getArgAsExpr(0);
2423 uint32_t prioritynum;
2424 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002425 Attr.setInvalid();
2426 return;
2427 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002428
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002429 if (prioritynum < 101 || prioritynum > 65535) {
2430 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002431 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002432 Attr.setInvalid();
2433 return;
2434 }
Michael Han99315932013-01-24 16:46:58 +00002435 D->addAttr(::new (S.Context)
2436 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2437 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002438}
2439
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002440FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2441 IdentifierInfo *Format, int FormatIdx,
2442 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002443 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002444 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002445 for (auto *F : D->specific_attrs<FormatAttr>()) {
2446 if (F->getType() == Format &&
2447 F->getFormatIdx() == FormatIdx &&
2448 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002449 // If we don't have a valid location for this attribute, adopt the
2450 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002451 if (F->getLocation().isInvalid())
2452 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002453 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002454 }
2455 }
2456
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002457 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2458 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002459}
2460
Mike Stumpd3bb5572009-07-24 19:02:52 +00002461/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002462/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002463static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002464 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002466 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002467 return;
2468 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002469
Chandler Carruth743682b2010-11-16 08:35:43 +00002470 // In C++ the implicit 'this' function parameter also counts, and they are
2471 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002472 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002473 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002474
Aaron Ballman00e99962013-08-31 01:11:41 +00002475 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2476 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002477
2478 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002479 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002480 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002481 // If we've modified the string name, we need a new identifier for it.
2482 II = &S.Context.Idents.get(Format);
2483 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002484
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002485 // Check for supported formats.
2486 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002487
2488 if (Kind == IgnoredFormat)
2489 return;
2490
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002491 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002492 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002493 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002494 return;
2495 }
2496
2497 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002498 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002499 uint32_t Idx;
2500 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002501 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002502
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002503 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002504 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002505 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002506 return;
2507 }
2508
2509 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002510 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002511
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002512 if (HasImplicitThisParam) {
2513 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002514 S.Diag(Attr.getLoc(),
2515 diag::err_format_attribute_implicit_this_format_string)
2516 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002517 return;
2518 }
2519 ArgIdx--;
2520 }
Mike Stump11289f42009-09-09 15:08:12 +00002521
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002522 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002523 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002524
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002525 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002526 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002527 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2528 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002529 return;
2530 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002531 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002532 // FIXME: do we need to check if the type is NSString*? What are the
2533 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002534 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002535 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002536 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2537 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002538 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002539 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002540 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002541 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002542 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002543 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2544 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002545 return;
2546 }
2547
2548 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002549 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002550 uint32_t FirstArg;
2551 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002552 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002553
2554 // check if the function is variadic if the 3rd argument non-zero
2555 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002556 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002557 ++NumArgs; // +1 for ...
2558 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002559 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002560 return;
2561 }
2562 }
2563
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002564 // strftime requires FirstArg to be 0 because it doesn't read from any
2565 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002566 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002568 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2569 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002570 return;
2571 }
2572 // if 0 it disables parameter checking (to use with e.g. va_list)
2573 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002574 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002575 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002576 return;
2577 }
2578
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002579 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002580 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002581 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002582 if (NewAttr)
2583 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002584}
2585
Chandler Carruthedc2c642011-07-02 00:01:44 +00002586static void handleTransparentUnionAttr(Sema &S, Decl *D,
2587 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002588 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002589 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002590 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002591 if (TD && TD->getUnderlyingType()->isUnionType())
2592 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2593 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002594 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002595
2596 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002598 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002599 return;
2600 }
2601
John McCallf937c022011-10-07 06:10:15 +00002602 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002603 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002604 diag::warn_transparent_union_attribute_not_definition);
2605 return;
2606 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002607
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002608 RecordDecl::field_iterator Field = RD->field_begin(),
2609 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002610 if (Field == FieldEnd) {
2611 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2612 return;
2613 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002614
David Blaikie40ed2972012-06-06 20:45:41 +00002615 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002616 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002617 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002618 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002619 diag::warn_transparent_union_attribute_floating)
2620 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002621 return;
2622 }
2623
2624 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2625 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2626 for (; Field != FieldEnd; ++Field) {
2627 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002628 // FIXME: this isn't fully correct; we also need to test whether the
2629 // members of the union would all have the same calling convention as the
2630 // first member of the union. Checking just the size and alignment isn't
2631 // sufficient (consider structs passed on the stack instead of in registers
2632 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002633 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002634 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002635 // Warn if we drop the attribute.
2636 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002637 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002638 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002639 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002640 diag::warn_transparent_union_attribute_field_size_align)
2641 << isSize << Field->getDeclName() << FieldBits;
2642 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002643 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002644 diag::note_transparent_union_first_field_size_align)
2645 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002646 return;
2647 }
2648 }
2649
Michael Han99315932013-01-24 16:46:58 +00002650 RD->addAttr(::new (S.Context)
2651 TransparentUnionAttr(Attr.getRange(), S.Context,
2652 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653}
2654
Chandler Carruthedc2c642011-07-02 00:01:44 +00002655static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002656 // Make sure that there is a string literal as the annotation's single
2657 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002658 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002659 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002660 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002661
2662 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002663 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2664 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002665 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002666 }
Michael Han99315932013-01-24 16:46:58 +00002667
2668 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002669 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002670 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002671}
2672
Chandler Carruthedc2c642011-07-02 00:01:44 +00002673static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002674 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002675 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002676 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2677 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002678 return;
2679 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002680
Richard Smith848e1f12013-02-01 08:12:08 +00002681 if (Attr.getNumArgs() == 0) {
2682 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002683 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002684 return;
2685 }
2686
Aaron Ballman00e99962013-08-31 01:11:41 +00002687 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002688 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2689 S.Diag(Attr.getEllipsisLoc(),
2690 diag::err_pack_expansion_without_parameter_packs);
2691 return;
2692 }
2693
2694 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2695 return;
2696
2697 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2698 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002699}
2700
2701void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002702 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002703 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2704 SourceLocation AttrLoc = AttrRange.getBegin();
2705
Richard Smith1dba27c2013-01-29 09:02:09 +00002706 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002707 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002708 // C++11 [dcl.align]p1:
2709 // An alignment-specifier may be applied to a variable or to a class
2710 // data member, but it shall not be applied to a bit-field, a function
2711 // parameter, the formal parameter of a catch clause, or a variable
2712 // declared with the register storage class specifier. An
2713 // alignment-specifier may also be applied to the declaration of a class
2714 // or enumeration type.
2715 // C11 6.7.5/2:
2716 // An alignment attribute shall not be specified in a declaration of
2717 // a typedef, or a bit-field, or a function, or a parameter, or an
2718 // object declared with the register storage-class specifier.
2719 int DiagKind = -1;
2720 if (isa<ParmVarDecl>(D)) {
2721 DiagKind = 0;
2722 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2723 if (VD->getStorageClass() == SC_Register)
2724 DiagKind = 1;
2725 if (VD->isExceptionVariable())
2726 DiagKind = 2;
2727 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2728 if (FD->isBitField())
2729 DiagKind = 3;
2730 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002731 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002732 << (TmpAttr.isC11() ? ExpectedVariableOrField
2733 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002734 return;
2735 }
2736 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002737 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002738 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002739 return;
2740 }
2741 }
2742
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002743 if (E->isTypeDependent() || E->isValueDependent()) {
2744 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002745 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2746 AA->setPackExpansion(IsPackExpansion);
2747 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002748 return;
2749 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002750
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002751 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002752 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002753 ExprResult ICE
2754 = VerifyIntegerConstantExpression(E, &Alignment,
2755 diag::err_aligned_attribute_argument_not_int,
2756 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002757 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002758 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002759
2760 // C++11 [dcl.align]p2:
2761 // -- if the constant expression evaluates to zero, the alignment
2762 // specifier shall have no effect
2763 // C11 6.7.5p6:
2764 // An alignment specification of zero has no effect.
2765 if (!(TmpAttr.isAlignas() && !Alignment) &&
2766 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002767 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2768 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002769 return;
2770 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002771
David Majnemerabecae72014-02-12 20:36:10 +00002772 // Alignment calculations can wrap around if it's greater than 2**28.
2773 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2774 if (Alignment.getZExtValue() > MaxValidAlignment) {
2775 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2776 << E->getSourceRange();
2777 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002778 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002779
Richard Smith44c247f2013-02-22 08:32:16 +00002780 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002781 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002782 AA->setPackExpansion(IsPackExpansion);
2783 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002784}
2785
Michael Hanaf02bbe2013-02-01 01:19:17 +00002786void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002787 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002788 // FIXME: Cache the number on the Attr object if non-dependent?
2789 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002790 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2791 SpellingListIndex);
2792 AA->setPackExpansion(IsPackExpansion);
2793 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002794}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002795
Richard Smith848e1f12013-02-01 08:12:08 +00002796void Sema::CheckAlignasUnderalignment(Decl *D) {
2797 assert(D->hasAttrs() && "no attributes on decl");
2798
2799 QualType Ty;
2800 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2801 Ty = VD->getType();
2802 else
2803 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002804 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002805 return;
2806
2807 // C++11 [dcl.align]p5, C11 6.7.5/4:
2808 // The combined effect of all alignment attributes in a declaration shall
2809 // not specify an alignment that is less strict than the alignment that
2810 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002811 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002812 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002813 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002814 if (I->isAlignmentDependent())
2815 return;
2816 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002817 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002818 Align = std::max(Align, I->getAlignment(Context));
2819 }
2820
2821 if (AlignasAttr && Align) {
2822 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2823 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2824 if (NaturalAlign > RequestedAlign)
2825 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2826 << Ty << (unsigned)NaturalAlign.getQuantity();
2827 }
2828}
2829
David Majnemer2c4e00a2014-01-29 22:07:36 +00002830bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002831 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002832 MSInheritanceAttr::Spelling SemanticSpelling) {
2833 assert(RD->hasDefinition() && "RD has no definition!");
2834
David Majnemer98c9ee22014-02-07 00:43:07 +00002835 // We may not have seen base specifiers or any virtual methods yet. We will
2836 // have to wait until the record is defined to catch any mismatches.
2837 if (!RD->getDefinition()->isCompleteDefinition())
2838 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002839
David Majnemer98c9ee22014-02-07 00:43:07 +00002840 // The unspecified model never matches what a definition could need.
2841 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2842 return false;
2843
David Majnemer4bb09802014-02-10 19:50:15 +00002844 if (BestCase) {
2845 if (RD->calculateInheritanceModel() == SemanticSpelling)
2846 return false;
2847 } else {
2848 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2849 return false;
2850 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002851
2852 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2853 << 0 /*definition*/;
2854 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2855 << RD->getNameAsString();
2856 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002857}
2858
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002859/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002860/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002861///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002862/// Despite what would be logical, the mode attribute is a decl attribute, not a
2863/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2864/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002865static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002866 // This attribute isn't documented, but glibc uses it. It changes
2867 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002868 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002869 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2870 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002871 return;
2872 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002873
Aaron Ballman00e99962013-08-31 01:11:41 +00002874 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2875 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002876
2877 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002878 if (Str.startswith("__") && Str.endswith("__"))
2879 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002880
2881 unsigned DestWidth = 0;
2882 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002883 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002884 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002885 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002886 switch (Str[0]) {
2887 case 'Q': DestWidth = 8; break;
2888 case 'H': DestWidth = 16; break;
2889 case 'S': DestWidth = 32; break;
2890 case 'D': DestWidth = 64; break;
2891 case 'X': DestWidth = 96; break;
2892 case 'T': DestWidth = 128; break;
2893 }
2894 if (Str[1] == 'F') {
2895 IntegerMode = false;
2896 } else if (Str[1] == 'C') {
2897 IntegerMode = false;
2898 ComplexMode = true;
2899 } else if (Str[1] != 'I') {
2900 DestWidth = 0;
2901 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002902 break;
2903 case 4:
2904 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2905 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002906 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002907 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002908 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002909 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002910 break;
2911 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002912 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002913 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002914 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002915 case 11:
2916 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002917 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002918 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002919 }
2920
2921 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002922 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002923 OldTy = TD->getUnderlyingType();
2924 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2925 OldTy = VD->getType();
2926 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002927 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002928 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002929 return;
2930 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002931
John McCall9dd450b2009-09-21 23:43:11 +00002932 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002933 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2934 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002935 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002936 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2937 } else if (ComplexMode) {
2938 if (!OldTy->isComplexType())
2939 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2940 } else {
2941 if (!OldTy->isFloatingType())
2942 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2943 }
2944
Mike Stump87c57ac2009-05-16 07:39:55 +00002945 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2946 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002947 // FIXME: Make sure floating-point mappings are accurate
2948 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002949 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002950 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002951 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002952 }
2953
2954 QualType NewTy;
2955
2956 if (IntegerMode)
2957 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2958 OldTy->isSignedIntegerType());
2959 else
2960 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2961
2962 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002963 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002964 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002965 }
2966
Eli Friedman4735374e2009-03-03 06:41:03 +00002967 if (ComplexMode) {
2968 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002969 }
2970
2971 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002972 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2973 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2974 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002975 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002976
2977 D->addAttr(::new (S.Context)
2978 ModeAttr(Attr.getRange(), S.Context, Name,
2979 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002980}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002981
Chandler Carruthedc2c642011-07-02 00:01:44 +00002982static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002983 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2984 if (!VD->hasGlobalStorage())
2985 S.Diag(Attr.getLoc(),
2986 diag::warn_attribute_requires_functions_or_static_globals)
2987 << Attr.getName();
2988 } else if (!isFunctionOrMethod(D)) {
2989 S.Diag(Attr.getLoc(),
2990 diag::warn_attribute_requires_functions_or_static_globals)
2991 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00002992 return;
2993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002994
Michael Han99315932013-01-24 16:46:58 +00002995 D->addAttr(::new (S.Context)
2996 NoDebugAttr(Attr.getRange(), S.Context,
2997 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00002998}
2999
Paul Robinsonf0674352014-03-31 22:29:15 +00003000static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3001 const AttributeList &Attr) {
3002 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3003 return;
3004
3005 D->addAttr(::new (S.Context)
3006 AlwaysInlineAttr(Attr.getRange(), S.Context,
3007 Attr.getAttributeSpellingListIndex()));
3008}
3009
3010static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3011 const AttributeList &Attr) {
3012 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
3013 return;
3014
3015 D->addAttr(::new (S.Context)
3016 OptimizeNoneAttr(Attr.getRange(), S.Context,
3017 Attr.getAttributeSpellingListIndex()));
3018}
3019
Chandler Carruthedc2c642011-07-02 00:01:44 +00003020static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003021 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003022 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003023 SourceRange RTRange = FD->getReturnTypeSourceRange();
3024 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003025 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003026 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3027 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003028 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003029 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003030
Aaron Ballman3aff6332013-12-02 19:30:36 +00003031 D->addAttr(::new (S.Context)
3032 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003033 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003034}
3035
Chandler Carruthedc2c642011-07-02 00:01:44 +00003036static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003037 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003038 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003039 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003040 return;
3041 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003042
Michael Han99315932013-01-24 16:46:58 +00003043 D->addAttr(::new (S.Context)
3044 GNUInlineAttr(Attr.getRange(), S.Context,
3045 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003046}
3047
Chandler Carruthedc2c642011-07-02 00:01:44 +00003048static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003049 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003050
Aaron Ballman02df2e02012-12-09 17:45:41 +00003051 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003052 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003053 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3054 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003055 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003056 return;
3057
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003058 if (!isa<ObjCMethodDecl>(D)) {
3059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3060 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003061 return;
3062 }
3063
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003064 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003065 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003066 D->addAttr(::new (S.Context)
3067 FastCallAttr(Attr.getRange(), S.Context,
3068 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003069 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003070 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003071 D->addAttr(::new (S.Context)
3072 StdCallAttr(Attr.getRange(), S.Context,
3073 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003074 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003075 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003076 D->addAttr(::new (S.Context)
3077 ThisCallAttr(Attr.getRange(), S.Context,
3078 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003079 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003080 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003081 D->addAttr(::new (S.Context)
3082 CDeclAttr(Attr.getRange(), S.Context,
3083 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003084 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003085 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003086 D->addAttr(::new (S.Context)
3087 PascalAttr(Attr.getRange(), S.Context,
3088 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003089 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003090 case AttributeList::AT_MSABI:
3091 D->addAttr(::new (S.Context)
3092 MSABIAttr(Attr.getRange(), S.Context,
3093 Attr.getAttributeSpellingListIndex()));
3094 return;
3095 case AttributeList::AT_SysVABI:
3096 D->addAttr(::new (S.Context)
3097 SysVABIAttr(Attr.getRange(), S.Context,
3098 Attr.getAttributeSpellingListIndex()));
3099 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003100 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003101 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003102 switch (CC) {
3103 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003104 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003105 break;
3106 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003107 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003108 break;
3109 default:
3110 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003111 }
3112
Michael Han99315932013-01-24 16:46:58 +00003113 D->addAttr(::new (S.Context)
3114 PcsAttr(Attr.getRange(), S.Context, PCS,
3115 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003116 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003117 }
Derek Schuffa2020962012-10-16 22:30:41 +00003118 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003119 D->addAttr(::new (S.Context)
3120 PnaclCallAttr(Attr.getRange(), S.Context,
3121 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003122 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003123 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003124 D->addAttr(::new (S.Context)
3125 IntelOclBiccAttr(Attr.getRange(), S.Context,
3126 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003127 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003128
Abramo Bagnara50099372010-04-30 13:10:51 +00003129 default:
3130 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003131 }
3132}
3133
Aaron Ballman02df2e02012-12-09 17:45:41 +00003134bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3135 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003136 if (attr.isInvalid())
3137 return true;
3138
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003139 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003140 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003141 attr.setInvalid();
3142 return true;
3143 }
3144
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003145 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003146 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003147 case AttributeList::AT_CDecl: CC = CC_C; break;
3148 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3149 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3150 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3151 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003152 case AttributeList::AT_MSABI:
3153 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3154 CC_X86_64Win64;
3155 break;
3156 case AttributeList::AT_SysVABI:
3157 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3158 CC_C;
3159 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003160 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003161 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003162 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003163 attr.setInvalid();
3164 return true;
3165 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003166 if (StrRef == "aapcs") {
3167 CC = CC_AAPCS;
3168 break;
3169 } else if (StrRef == "aapcs-vfp") {
3170 CC = CC_AAPCS_VFP;
3171 break;
3172 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003173
3174 attr.setInvalid();
3175 Diag(attr.getLoc(), diag::err_invalid_pcs);
3176 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003177 }
Derek Schuffa2020962012-10-16 22:30:41 +00003178 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003179 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003180 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003181 }
3182
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003183 const TargetInfo &TI = Context.getTargetInfo();
3184 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3185 if (A == TargetInfo::CCCR_Warning) {
3186 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003187
3188 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3189 if (FD)
3190 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3191 TargetInfo::CCMT_NonMember;
3192 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003193 }
3194
John McCall3882ace2011-01-05 12:14:39 +00003195 return false;
3196}
3197
John McCall3882ace2011-01-05 12:14:39 +00003198/// Checks a regparm attribute, returning true if it is ill-formed and
3199/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003200bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3201 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003202 return true;
3203
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003204 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003205 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003206 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003207 }
Eli Friedman7044b762009-03-27 21:06:47 +00003208
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003209 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003210 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003211 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003212 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003213 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003214 }
3215
Douglas Gregore8bbc122011-09-02 00:18:52 +00003216 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003217 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003218 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003219 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003220 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003221 }
3222
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003223 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003224 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003225 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003226 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003227 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003228 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003229 }
3230
John McCall3882ace2011-01-05 12:14:39 +00003231 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003232}
3233
Aaron Ballman66039932013-12-19 00:41:31 +00003234static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3235 const AttributeList &Attr) {
Aaron Ballman66039932013-12-19 00:41:31 +00003236 uint32_t MaxThreads, MinBlocks = 0;
3237 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3238 return;
3239 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3240 Attr.getArgAsExpr(1),
3241 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003242 return;
3243
3244 D->addAttr(::new (S.Context)
3245 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3246 MaxThreads, MinBlocks,
3247 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003248}
3249
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003250static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3251 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003252 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003254 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003255 return;
3256 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003257
3258 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003259 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003260
Aaron Ballman00e99962013-08-31 01:11:41 +00003261 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003262
3263 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3265 << Attr.getName() << ExpectedFunctionOrMethod;
3266 return;
3267 }
3268
3269 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003270 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3271 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003272 return;
3273
3274 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003275 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3276 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003277 return;
3278
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003279 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003280 if (IsPointer) {
3281 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003282 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003283 if (!BufferTy->isPointerType()) {
3284 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003285 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003286 }
3287 }
3288
Michael Han99315932013-01-24 16:46:58 +00003289 D->addAttr(::new (S.Context)
3290 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3291 ArgumentIdx, TypeTagIdx, IsPointer,
3292 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003293}
3294
3295static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3296 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003297 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003298 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003299 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003300 return;
3301 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003302
3303 if (!checkAttributeNumArgs(S, Attr, 1))
3304 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003305
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003306 if (!isa<VarDecl>(D)) {
3307 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3308 << Attr.getName() << ExpectedVariable;
3309 return;
3310 }
3311
Aaron Ballman00e99962013-08-31 01:11:41 +00003312 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003313 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003314 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3315 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003316
Michael Han99315932013-01-24 16:46:58 +00003317 D->addAttr(::new (S.Context)
3318 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003319 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003320 Attr.getLayoutCompatible(),
3321 Attr.getMustBeNull(),
3322 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323}
3324
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003325//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003326// Checker-specific attribute handlers.
3327//===----------------------------------------------------------------------===//
3328
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003329static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003330 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003331 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003332}
3333
John McCalled433932011-01-25 03:31:58 +00003334static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003335 return type->isDependentType() ||
3336 type->isObjCObjectPointerType() ||
3337 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003338}
3339static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003340 return type->isDependentType() ||
3341 type->isPointerType() ||
3342 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003343}
3344
Chandler Carruthedc2c642011-07-02 00:01:44 +00003345static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003346 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003347 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003348
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003349 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003350 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3351 cf = false;
3352 } else {
3353 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3354 cf = true;
3355 }
3356
3357 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003358 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003359 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003360 return;
3361 }
3362
3363 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003364 param->addAttr(::new (S.Context)
3365 CFConsumedAttr(Attr.getRange(), S.Context,
3366 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003367 else
Michael Han99315932013-01-24 16:46:58 +00003368 param->addAttr(::new (S.Context)
3369 NSConsumedAttr(Attr.getRange(), S.Context,
3370 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003371}
3372
Chandler Carruthedc2c642011-07-02 00:01:44 +00003373static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3374 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003375
John McCalled433932011-01-25 03:31:58 +00003376 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003377
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003378 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003379 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003380 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003381 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003382 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003383 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3384 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003385 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003386 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003387 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003388 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003389 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003390 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003391 return;
3392 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003393
John McCalled433932011-01-25 03:31:58 +00003394 bool typeOK;
3395 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003396 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003397 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003398 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003399 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003400 cf = false;
3401 break;
3402
3403 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003404 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003405 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3406 cf = false;
3407 break;
3408
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003409 case AttributeList::AT_CFReturnsRetained:
3410 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003411 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3412 cf = true;
3413 break;
3414 }
3415
3416 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003417 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003418 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003419 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003420 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003421
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003422 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003423 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003424 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003425 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003426 D->addAttr(::new (S.Context)
3427 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3428 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003429 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003430 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003431 D->addAttr(::new (S.Context)
3432 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003434 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003435 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003436 D->addAttr(::new (S.Context)
3437 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3438 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003439 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003440 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003441 D->addAttr(::new (S.Context)
3442 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3443 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003444 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003445 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003446 D->addAttr(::new (S.Context)
3447 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3448 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003449 return;
3450 };
3451}
3452
John McCallcf166702011-07-22 08:53:00 +00003453static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3454 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003455 const int EP_ObjCMethod = 1;
3456 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003457
John McCallcf166702011-07-22 08:53:00 +00003458 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003459 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003460 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003461 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003462 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003463 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003464
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003465 if (!resultType->isReferenceType() &&
3466 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003467 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003468 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003469 << attr.getName()
3470 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003471 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003472
3473 // Drop the attribute.
3474 return;
3475 }
3476
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003477 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003478 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3479 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003480}
3481
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003482static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3483 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003484 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003485
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003486 DeclContext *DC = method->getDeclContext();
3487 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3488 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3489 << attr.getName() << 0;
3490 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3491 return;
3492 }
3493 if (method->getMethodFamily() == OMF_dealloc) {
3494 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3495 << attr.getName() << 1;
3496 return;
3497 }
3498
Michael Han99315932013-01-24 16:46:58 +00003499 method->addAttr(::new (S.Context)
3500 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3501 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003502}
3503
Aaron Ballmanfb763042013-12-02 18:05:46 +00003504static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3505 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003506 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003507 return;
John McCall32f5fe12011-09-30 05:12:12 +00003508
Aaron Ballmanfb763042013-12-02 18:05:46 +00003509 D->addAttr(::new (S.Context)
3510 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3511 Attr.getAttributeSpellingListIndex()));
3512}
3513
3514static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3515 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003516 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003517 return;
3518
3519 D->addAttr(::new (S.Context)
3520 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3521 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003522}
3523
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003524static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3525 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003526 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003527
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003528 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003529 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003530 return;
3531 }
3532
3533 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003534 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003535 Attr.getAttributeSpellingListIndex()));
3536}
3537
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003538static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3539 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003540 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3541
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003542 if (!Parm) {
3543 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3544 return;
3545 }
3546
3547 D->addAttr(::new (S.Context)
3548 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3549 Attr.getAttributeSpellingListIndex()));
3550}
3551
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003552static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3553 const AttributeList &Attr) {
3554 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003555 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003556 if (!RelatedClass) {
3557 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3558 return;
3559 }
3560 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003561 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003562 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003563 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003564 D->addAttr(::new (S.Context)
3565 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3566 ClassMethod, InstanceMethod,
3567 Attr.getAttributeSpellingListIndex()));
3568}
3569
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003570static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3571 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003572 ObjCInterfaceDecl *IFace;
3573 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3574 IFace = CatDecl->getClassInterface();
3575 else
3576 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003577 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003578 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003579 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3580 Attr.getAttributeSpellingListIndex()));
3581}
3582
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003583static void handleObjCRuntimeName(Sema &S, Decl *D,
3584 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003585 StringRef MetaDataName;
3586 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3587 return;
3588 D->addAttr(::new (S.Context)
3589 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3590 MetaDataName,
3591 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003592}
3593
Chandler Carruthedc2c642011-07-02 00:01:44 +00003594static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3595 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003596 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003597
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003598 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003599 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003600}
3601
Chandler Carruthedc2c642011-07-02 00:01:44 +00003602static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3603 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003604 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003605 QualType type = vd->getType();
3606
3607 if (!type->isDependentType() &&
3608 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003609 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003610 << type;
3611 return;
3612 }
3613
3614 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3615
3616 // If we have no lifetime yet, check the lifetime we're presumably
3617 // going to infer.
3618 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3619 lifetime = type->getObjCARCImplicitLifetime();
3620
3621 switch (lifetime) {
3622 case Qualifiers::OCL_None:
3623 assert(type->isDependentType() &&
3624 "didn't infer lifetime for non-dependent type?");
3625 break;
3626
3627 case Qualifiers::OCL_Weak: // meaningful
3628 case Qualifiers::OCL_Strong: // meaningful
3629 break;
3630
3631 case Qualifiers::OCL_ExplicitNone:
3632 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003633 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003634 << (lifetime == Qualifiers::OCL_Autoreleasing);
3635 break;
3636 }
3637
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003638 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003639 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3640 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003641}
3642
Francois Picheta83957a2010-12-19 06:50:37 +00003643//===----------------------------------------------------------------------===//
3644// Microsoft specific attribute handlers.
3645//===----------------------------------------------------------------------===//
3646
Chandler Carruthedc2c642011-07-02 00:01:44 +00003647static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003648 if (!S.LangOpts.CPlusPlus) {
3649 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3650 << Attr.getName() << AttributeLangSupport::C;
3651 return;
3652 }
3653
Aaron Ballman60e705e2013-11-24 20:58:02 +00003654 if (!isa<CXXRecordDecl>(D)) {
3655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3656 << Attr.getName() << ExpectedClass;
3657 return;
3658 }
3659
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003660 StringRef StrRef;
3661 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003662 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003663 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003664
David Majnemer89085342013-08-09 08:56:20 +00003665 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3666 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003667 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3668 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003669
Reid Kleckner140c4a72013-05-17 14:04:52 +00003670 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003671 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003672 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003673 return;
3674 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003675
David Majnemer89085342013-08-09 08:56:20 +00003676 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003677 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003678 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003679 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003680 return;
3681 }
David Majnemer89085342013-08-09 08:56:20 +00003682 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003683 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003684 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003685 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003686 }
Francois Picheta83957a2010-12-19 06:50:37 +00003687
David Majnemer89085342013-08-09 08:56:20 +00003688 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3689 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003690}
3691
David Majnemer2c4e00a2014-01-29 22:07:36 +00003692static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3693 if (!S.LangOpts.CPlusPlus) {
3694 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3695 << Attr.getName() << AttributeLangSupport::C;
3696 return;
3697 }
3698 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003699 D, Attr.getRange(), /*BestCase=*/true,
3700 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003701 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3702 if (IA)
3703 D->addAttr(IA);
3704}
3705
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003706static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3707 const AttributeList &Attr) {
3708 VarDecl *VD = cast<VarDecl>(D);
3709 if (!S.Context.getTargetInfo().isTLSSupported()) {
3710 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3711 return;
3712 }
3713 if (VD->getTSCSpec() != TSCS_unspecified) {
3714 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3715 return;
3716 }
3717 if (VD->hasLocalStorage()) {
3718 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3719 return;
3720 }
3721 VD->addAttr(::new (S.Context) ThreadAttr(
3722 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3723}
3724
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003725static void handleARMInterruptAttr(Sema &S, Decl *D,
3726 const AttributeList &Attr) {
3727 // Check the attribute arguments.
3728 if (Attr.getNumArgs() > 1) {
3729 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3730 << Attr.getName() << 1;
3731 return;
3732 }
3733
3734 StringRef Str;
3735 SourceLocation ArgLoc;
3736
3737 if (Attr.getNumArgs() == 0)
3738 Str = "";
3739 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3740 return;
3741
3742 ARMInterruptAttr::InterruptType Kind;
3743 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3744 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3745 << Attr.getName() << Str << ArgLoc;
3746 return;
3747 }
3748
3749 unsigned Index = Attr.getAttributeSpellingListIndex();
3750 D->addAttr(::new (S.Context)
3751 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3752}
3753
3754static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3755 const AttributeList &Attr) {
3756 if (!checkAttributeNumArgs(S, Attr, 1))
3757 return;
3758
3759 if (!Attr.isArgExpr(0)) {
3760 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3761 << AANT_ArgumentIntegerConstant;
3762 return;
3763 }
3764
3765 // FIXME: Check for decl - it should be void ()(void).
3766
3767 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3768 llvm::APSInt NumParams(32);
3769 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3771 << Attr.getName() << AANT_ArgumentIntegerConstant
3772 << NumParamsExpr->getSourceRange();
3773 return;
3774 }
3775
3776 unsigned Num = NumParams.getLimitedValue(255);
3777 if ((Num & 1) || Num > 30) {
3778 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3779 << Attr.getName() << (int)NumParams.getSExtValue()
3780 << NumParamsExpr->getSourceRange();
3781 return;
3782 }
3783
Aaron Ballman36a53502014-01-16 13:03:14 +00003784 D->addAttr(::new (S.Context)
3785 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3786 Attr.getAttributeSpellingListIndex()));
3787 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003788}
3789
3790static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3791 // Dispatch the interrupt attribute based on the current target.
3792 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3793 handleMSP430InterruptAttr(S, D, Attr);
3794 else
3795 handleARMInterruptAttr(S, D, Attr);
3796}
3797
3798static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3799 const AttributeList& Attr) {
3800 // If we try to apply it to a function pointer, don't warn, but don't
3801 // do anything, either. It doesn't matter anyway, because there's nothing
3802 // special about calling a force_align_arg_pointer function.
3803 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3804 if (VD && VD->getType()->isFunctionPointerType())
3805 return;
3806 // Also don't warn on function pointer typedefs.
3807 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3808 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3809 TD->getUnderlyingType()->isFunctionType()))
3810 return;
3811 // Attribute can only be applied to function types.
3812 if (!isa<FunctionDecl>(D)) {
3813 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3814 << Attr.getName() << /* function */0;
3815 return;
3816 }
3817
Aaron Ballman36a53502014-01-16 13:03:14 +00003818 D->addAttr(::new (S.Context)
3819 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3820 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003821}
3822
3823DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3824 unsigned AttrSpellingListIndex) {
3825 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003826 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00003827 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003828 }
3829
3830 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003831 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003832
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003833 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003834}
3835
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003836DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3837 unsigned AttrSpellingListIndex) {
3838 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003839 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003840 D->dropAttr<DLLImportAttr>();
3841 }
3842
3843 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003844 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003845
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003846 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003847}
3848
Hans Wennborge82f19c2014-06-24 23:57:05 +00003849static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00003850 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
3851 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
3852 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
3853 << A.getName();
3854 return;
3855 }
3856
Hans Wennborge82f19c2014-06-24 23:57:05 +00003857 unsigned Index = A.getAttributeSpellingListIndex();
3858 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
3859 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
3860 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003861 if (NewAttr)
3862 D->addAttr(NewAttr);
3863}
3864
David Majnemer2c4e00a2014-01-29 22:07:36 +00003865MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003866Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003867 unsigned AttrSpellingListIndex,
3868 MSInheritanceAttr::Spelling SemanticSpelling) {
3869 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3870 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00003871 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003872 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3873 << 1 /*previous declaration*/;
3874 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3875 D->dropAttr<MSInheritanceAttr>();
3876 }
3877
3878 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3879 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003880 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3881 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003882 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003883 }
3884 } else {
3885 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3886 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3887 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003888 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003889 }
3890 if (RD->getDescribedClassTemplate()) {
3891 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3892 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003893 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003894 }
3895 }
3896
3897 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003898 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003899}
3900
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003901static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3902 // The capability attributes take a single string parameter for the name of
3903 // the capability they represent. The lockable attribute does not take any
3904 // parameters. However, semantically, both attributes represent the same
3905 // concept, and so they use the same semantic attribute. Eventually, the
3906 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003907 //
Alp Toker958027b2014-07-14 19:42:55 +00003908 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00003909 // literal will be considered a "mutex."
3910 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003911 SourceLocation LiteralLoc;
3912 if (Attr.getKind() == AttributeList::AT_Capability &&
3913 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3914 return;
3915
Aaron Ballman6c810072014-03-05 21:47:13 +00003916 // Currently, there are only two names allowed for a capability: role and
3917 // mutex (case insensitive). Diagnose other capability names.
3918 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3919 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3920
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003921 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3922 Attr.getAttributeSpellingListIndex()));
3923}
3924
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003925static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3926 const AttributeList &Attr) {
3927 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3928 Attr.getArgAsExpr(0),
3929 Attr.getAttributeSpellingListIndex()));
3930}
3931
3932static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3933 const AttributeList &Attr) {
3934 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003935 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003936 return;
3937
3938 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3939 S.Context,
3940 Args.data(), Args.size(),
3941 Attr.getAttributeSpellingListIndex()));
3942}
3943
3944static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3945 const AttributeList &Attr) {
3946 SmallVector<Expr*, 2> Args;
3947 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3948 return;
3949
3950 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3951 S.Context,
3952 Attr.getArgAsExpr(0),
3953 Args.data(),
3954 Args.size(),
3955 Attr.getAttributeSpellingListIndex()));
3956}
3957
3958static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3959 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003960 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003961 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003962 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003963
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003964 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
3965 Attr.getRange(), S.Context, Args.data(), Args.size(),
3966 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003967}
3968
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003969static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3970 const AttributeList &Attr) {
3971 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3972 return;
3973
3974 // check that all arguments are lockable objects
3975 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003976 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003977 if (Args.empty())
3978 return;
3979
3980 RequiresCapabilityAttr *RCA = ::new (S.Context)
3981 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
3982 Args.size(), Attr.getAttributeSpellingListIndex());
3983
3984 D->addAttr(RCA);
3985}
3986
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003987/// Handles semantic checking for features that are common to all attributes,
3988/// such as checking whether a parameter was properly specified, or the correct
3989/// number of arguments were passed, etc.
3990static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3991 const AttributeList &Attr) {
3992 // Several attributes carry different semantics than the parsing requires, so
3993 // those are opted out of the common handling.
3994 //
3995 // We also bail on unknown and ignored attributes because those are handled
3996 // as part of the target-specific handling logic.
3997 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003998 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003999 return false;
4000
Aaron Ballman3aff6332013-12-02 19:30:36 +00004001 // Check whether the attribute requires specific language extensions to be
4002 // enabled.
4003 if (!Attr.diagnoseLangOpts(S))
4004 return true;
4005
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004006 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4007 // If there are no optional arguments, then checking for the argument count
4008 // is trivial.
4009 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4010 return true;
4011 } else {
4012 // There are optional arguments, so checking is slightly more involved.
4013 if (Attr.getMinArgs() &&
4014 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4015 return true;
4016 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4017 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4018 return true;
4019 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004020
4021 // Check whether the attribute appertains to the given subject.
4022 if (!Attr.diagnoseAppertainsTo(S, D))
4023 return true;
4024
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004025 return false;
4026}
4027
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004028//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004029// Top Level Sema Entry Points
4030//===----------------------------------------------------------------------===//
4031
Richard Smithf8a75c32013-08-29 00:47:48 +00004032/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4033/// the attribute applies to decls. If the attribute is a type attribute, just
4034/// silently ignore it if a GNU attribute.
4035static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4036 const AttributeList &Attr,
4037 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004038 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004039 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004040
Richard Smithf8a75c32013-08-29 00:47:48 +00004041 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4042 // instead.
4043 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4044 return;
4045
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004046 // Unknown attributes are automatically warned on. Target-specific attributes
4047 // which do not apply to the current target architecture are treated as
4048 // though they were unknown attributes.
4049 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4050 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004051 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4052 ? diag::warn_unhandled_ms_attribute_ignored
4053 : diag::warn_unknown_attribute_ignored)
4054 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004055 return;
4056 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004057
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004058 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4059 return;
4060
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004061 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004062 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004063 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004064 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004065 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004066 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004067 handleInterruptAttr(S, D, Attr);
4068 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004069 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004070 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4071 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004072 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004073 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004074 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004075 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004076 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004077 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4078 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004079 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004080 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4081 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004082 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004083 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4084 break;
4085 case AttributeList::AT_IBOutlet:
4086 handleIBOutlet(S, D, Attr);
4087 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004088 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004089 handleIBOutletCollection(S, D, Attr);
4090 break;
4091 case AttributeList::AT_Alias:
4092 handleAliasAttr(S, D, Attr);
4093 break;
4094 case AttributeList::AT_Aligned:
4095 handleAlignedAttr(S, D, Attr);
4096 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004097 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004098 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004099 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004100 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004101 handleAnalyzerNoReturnAttr(S, D, Attr);
4102 break;
4103 case AttributeList::AT_TLSModel:
4104 handleTLSModelAttr(S, D, Attr);
4105 break;
4106 case AttributeList::AT_Annotate:
4107 handleAnnotateAttr(S, D, Attr);
4108 break;
4109 case AttributeList::AT_Availability:
4110 handleAvailabilityAttr(S, D, Attr);
4111 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004112 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004113 handleDependencyAttr(S, scope, D, Attr);
4114 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004115 case AttributeList::AT_Common:
4116 handleCommonAttr(S, D, Attr);
4117 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004118 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004119 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4120 break;
4121 case AttributeList::AT_Constructor:
4122 handleConstructorAttr(S, D, Attr);
4123 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004124 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004125 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4126 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004128 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004129 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004130 case AttributeList::AT_Destructor:
4131 handleDestructorAttr(S, D, Attr);
4132 break;
4133 case AttributeList::AT_EnableIf:
4134 handleEnableIfAttr(S, D, Attr);
4135 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004136 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004137 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004138 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004139 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004140 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004141 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004142 case AttributeList::AT_OptimizeNone:
4143 handleOptimizeNoneAttr(S, D, Attr);
4144 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004145 case AttributeList::AT_Flatten:
4146 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4147 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004148 case AttributeList::AT_Format:
4149 handleFormatAttr(S, D, Attr);
4150 break;
4151 case AttributeList::AT_FormatArg:
4152 handleFormatArgAttr(S, D, Attr);
4153 break;
4154 case AttributeList::AT_CUDAGlobal:
4155 handleGlobalAttr(S, D, Attr);
4156 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004157 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004158 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4159 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004160 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004161 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4162 break;
4163 case AttributeList::AT_GNUInline:
4164 handleGNUInlineAttr(S, D, Attr);
4165 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004166 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004167 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004168 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004169 case AttributeList::AT_Malloc:
4170 handleMallocAttr(S, D, Attr);
4171 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004172 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004173 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4174 break;
4175 case AttributeList::AT_Mode:
4176 handleModeAttr(S, D, Attr);
4177 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004178 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004179 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4180 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004181 case AttributeList::AT_NoSplitStack:
4182 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4183 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004184 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004185 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4186 handleNonNullAttrParameter(S, PVD, Attr);
4187 else
4188 handleNonNullAttr(S, D, Attr);
4189 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004190 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004191 handleReturnsNonNullAttr(S, D, Attr);
4192 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004193 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004194 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4195 break;
4196 case AttributeList::AT_Ownership:
4197 handleOwnershipAttr(S, D, Attr);
4198 break;
4199 case AttributeList::AT_Cold:
4200 handleColdAttr(S, D, Attr);
4201 break;
4202 case AttributeList::AT_Hot:
4203 handleHotAttr(S, D, Attr);
4204 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004205 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004206 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4207 break;
4208 case AttributeList::AT_NoReturn:
4209 handleNoReturnAttr(S, D, Attr);
4210 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004211 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004212 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4213 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004214 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004215 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4216 break;
4217 case AttributeList::AT_VecReturn:
4218 handleVecReturnAttr(S, D, Attr);
4219 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004220
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004221 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004222 handleObjCOwnershipAttr(S, D, Attr);
4223 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004225 handleObjCPreciseLifetimeAttr(S, D, Attr);
4226 break;
John McCall31168b02011-06-15 23:02:42 +00004227
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004229 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4230 break;
John McCallcf166702011-07-22 08:53:00 +00004231
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004232 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004233 handleObjCRequiresSuperAttr(S, D, Attr);
4234 break;
4235
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004236 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004237 handleObjCBridgeAttr(S, scope, D, Attr);
4238 break;
4239
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004240 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004241 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4242 break;
4243
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004244 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004245 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4246 break;
John McCallf1e8b342011-09-29 07:17:38 +00004247
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004248 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004249 handleObjCDesignatedInitializer(S, D, Attr);
4250 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004251
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004252 case AttributeList::AT_ObjCRuntimeName:
4253 handleObjCRuntimeName(S, D, Attr);
4254 break;
4255
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004256 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004257 handleCFAuditedTransferAttr(S, D, Attr);
4258 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004259 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004260 handleCFUnknownTransferAttr(S, D, Attr);
4261 break;
John McCall32f5fe12011-09-30 05:12:12 +00004262
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004263 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004264 case AttributeList::AT_NSConsumed:
4265 handleNSConsumedAttr(S, D, Attr);
4266 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004267 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004268 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4269 break;
John McCalled433932011-01-25 03:31:58 +00004270
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004271 case AttributeList::AT_NSReturnsAutoreleased:
4272 case AttributeList::AT_NSReturnsNotRetained:
4273 case AttributeList::AT_CFReturnsNotRetained:
4274 case AttributeList::AT_NSReturnsRetained:
4275 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004276 handleNSReturnsRetainedAttr(S, D, Attr);
4277 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004278 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004279 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4280 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004281 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004282 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4283 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004284 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004285 handleVecTypeHint(S, D, Attr);
4286 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004287
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004288 case AttributeList::AT_InitPriority:
4289 handleInitPriorityAttr(S, D, Attr);
4290 break;
4291
4292 case AttributeList::AT_Packed:
4293 handlePackedAttr(S, D, Attr);
4294 break;
4295 case AttributeList::AT_Section:
4296 handleSectionAttr(S, D, Attr);
4297 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004298 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004299 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004300 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004301 case AttributeList::AT_ArcWeakrefUnavailable:
4302 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4303 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004304 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004305 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4306 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004307 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004308 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004309 break;
4310 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004311 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4312 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004313 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004314 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4315 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004316 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004317 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4318 break;
4319 case AttributeList::AT_Used:
4320 handleUsedAttr(S, D, Attr);
4321 break;
John McCalld041a9b2013-02-20 01:54:26 +00004322 case AttributeList::AT_Visibility:
4323 handleVisibilityAttr(S, D, Attr, false);
4324 break;
4325 case AttributeList::AT_TypeVisibility:
4326 handleVisibilityAttr(S, D, Attr, true);
4327 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004328 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004329 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4330 break;
4331 case AttributeList::AT_WarnUnusedResult:
4332 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004333 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004334 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004335 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4336 break;
4337 case AttributeList::AT_WeakRef:
4338 handleWeakRefAttr(S, D, Attr);
4339 break;
4340 case AttributeList::AT_WeakImport:
4341 handleWeakImportAttr(S, D, Attr);
4342 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004343 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004344 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004345 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004346 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004347 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4348 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004349 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004350 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004351 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004352 case AttributeList::AT_ObjCNSObject:
4353 handleObjCNSObject(S, D, Attr);
4354 break;
4355 case AttributeList::AT_Blocks:
4356 handleBlocksAttr(S, D, Attr);
4357 break;
4358 case AttributeList::AT_Sentinel:
4359 handleSentinelAttr(S, D, Attr);
4360 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004361 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004362 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4363 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004364 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004365 handleSimpleAttribute<PureAttr>(S, D, Attr);
4366 break;
4367 case AttributeList::AT_Cleanup:
4368 handleCleanupAttr(S, D, Attr);
4369 break;
4370 case AttributeList::AT_NoDebug:
4371 handleNoDebugAttr(S, D, Attr);
4372 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004373 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004374 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4375 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004376 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004377 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4378 break;
4379 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4380 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4381 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004382 case AttributeList::AT_StdCall:
4383 case AttributeList::AT_CDecl:
4384 case AttributeList::AT_FastCall:
4385 case AttributeList::AT_ThisCall:
4386 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004387 case AttributeList::AT_MSABI:
4388 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004389 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004390 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004391 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004392 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004393 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004394 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004395 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4396 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004397 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004398 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4399 break;
John McCall8d32c052012-05-22 21:28:12 +00004400
4401 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004402 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004403 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004404 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004405 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004406 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004407 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004408 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004409 handleMSInheritanceAttr(S, D, Attr);
4410 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004411 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004412 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4413 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004414 case AttributeList::AT_Thread:
4415 handleDeclspecThreadAttr(S, D, Attr);
4416 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004417
4418 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004419 case AttributeList::AT_AssertExclusiveLock:
4420 handleAssertExclusiveLockAttr(S, D, Attr);
4421 break;
4422 case AttributeList::AT_AssertSharedLock:
4423 handleAssertSharedLockAttr(S, D, Attr);
4424 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004425 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004426 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4427 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004428 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004429 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004430 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004431 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004432 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4433 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004434 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004435 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004436 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004437 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004438 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004439 break;
4440 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004441 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004442 break;
4443 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004444 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004446 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004447 handleGuardedByAttr(S, D, Attr);
4448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004450 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004451 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004452 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004453 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004454 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004455 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004456 handleLockReturnedAttr(S, D, Attr);
4457 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004458 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004459 handleLocksExcludedAttr(S, D, Attr);
4460 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004461 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004462 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004463 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004464 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004465 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004466 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004467 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004468 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004469 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004470
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004471 // Capability analysis attributes.
4472 case AttributeList::AT_Capability:
4473 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004474 handleCapabilityAttr(S, D, Attr);
4475 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004476 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004477 handleRequiresCapabilityAttr(S, D, Attr);
4478 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004479
4480 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004481 handleAssertCapabilityAttr(S, D, Attr);
4482 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004483 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004484 handleAcquireCapabilityAttr(S, D, Attr);
4485 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004486 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004487 handleReleaseCapabilityAttr(S, D, Attr);
4488 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004489 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004490 handleTryAcquireCapabilityAttr(S, D, Attr);
4491 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004492
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004493 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004494 case AttributeList::AT_Consumable:
4495 handleConsumableAttr(S, D, Attr);
4496 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004497 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004498 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4499 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004500 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004501 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4502 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004503 case AttributeList::AT_CallableWhen:
4504 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004505 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004506 case AttributeList::AT_ParamTypestate:
4507 handleParamTypestateAttr(S, D, Attr);
4508 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004509 case AttributeList::AT_ReturnTypestate:
4510 handleReturnTypestateAttr(S, D, Attr);
4511 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004512 case AttributeList::AT_SetTypestate:
4513 handleSetTypestateAttr(S, D, Attr);
4514 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004515 case AttributeList::AT_TestTypestate:
4516 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004517 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004518
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004519 // Type safety attributes.
4520 case AttributeList::AT_ArgumentWithTypeTag:
4521 handleArgumentWithTypeTagAttr(S, D, Attr);
4522 break;
4523 case AttributeList::AT_TypeTagForDatatype:
4524 handleTypeTagForDatatypeAttr(S, D, Attr);
4525 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004526 }
4527}
4528
4529/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4530/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004531void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004532 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004533 bool IncludeCXX11Attributes) {
4534 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004535 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004536
Joey Gouly2cd9db12013-12-13 16:15:28 +00004537 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004538 // GCC accepts
4539 // static int a9 __attribute__((weakref));
4540 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004541 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004542 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4543 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004544 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004545 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004546 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004547
4548 if (!D->hasAttr<OpenCLKernelAttr>()) {
4549 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004550 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4551 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004552 D->setInvalidDecl();
4553 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004554 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4555 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004556 D->setInvalidDecl();
4557 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004558 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4559 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004560 D->setInvalidDecl();
4561 }
4562 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004563}
4564
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004565// Annotation attributes are the only attributes allowed after an access
4566// specifier.
4567bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4568 const AttributeList *AttrList) {
4569 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004570 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004571 handleAnnotateAttr(*this, ASDecl, *l);
4572 } else {
4573 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4574 return true;
4575 }
4576 }
4577
4578 return false;
4579}
4580
John McCall42856de2011-10-01 05:17:03 +00004581/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4582/// contains any decl attributes that we should warn about.
4583static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4584 for ( ; A; A = A->getNext()) {
4585 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004586 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004587 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4588
4589 if (A->getKind() == AttributeList::UnknownAttribute) {
4590 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4591 << A->getName() << A->getRange();
4592 } else {
4593 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4594 << A->getName() << A->getRange();
4595 }
4596 }
4597}
4598
4599/// checkUnusedDeclAttributes - Given a declarator which is not being
4600/// used to build a declaration, complain about any decl attributes
4601/// which might be lying around on it.
4602void Sema::checkUnusedDeclAttributes(Declarator &D) {
4603 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4604 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4605 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4606 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4607}
4608
Ryan Flynn7d470f32009-07-30 03:15:39 +00004609/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004610/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004611NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4612 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004613 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004614 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004615 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004616 FunctionDecl *NewFD;
4617 // FIXME: Missing call to CheckFunctionDeclaration().
4618 // FIXME: Mangling?
4619 // FIXME: Is the qualifier info correct?
4620 // FIXME: Is the DeclContext correct?
4621 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4622 Loc, Loc, DeclarationName(II),
4623 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004624 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004625 FD->hasPrototype(),
4626 false/*isConstexprSpecified*/);
4627 NewD = NewFD;
4628
4629 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004630 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004631
4632 // Fake up parameter variables; they are declared as if this were
4633 // a typedef.
4634 QualType FDTy = FD->getType();
4635 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4636 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004637 for (const auto &AI : FT->param_types()) {
4638 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004639 Param->setScopeInfo(0, Params.size());
4640 Params.push_back(Param);
4641 }
David Blaikie9c70e042011-09-21 18:16:56 +00004642 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004643 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004644 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4645 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004646 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004647 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004648 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004649 if (VD->getQualifier()) {
4650 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004651 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004652 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004653 }
4654 return NewD;
4655}
4656
James Dennett634962f2012-06-14 21:40:34 +00004657/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004658/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004659void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004660 if (W.getUsed()) return; // only do this once
4661 W.setUsed(true);
4662 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4663 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004664 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004665 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4666 W.getLocation()));
4667 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004668 WeakTopLevelDecl.push_back(NewD);
4669 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4670 // to insert Decl at TU scope, sorry.
4671 DeclContext *SavedContext = CurContext;
4672 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004673 NewD->setDeclContext(CurContext);
4674 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004675 PushOnScopeChains(NewD, S);
4676 CurContext = SavedContext;
4677 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004678 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004679 }
4680}
4681
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004682void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4683 // It's valid to "forward-declare" #pragma weak, in which case we
4684 // have to do this.
4685 LoadExternalWeakUndeclaredIdentifiers();
4686 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004687 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004688 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4689 if (VD->isExternC())
4690 ND = VD;
4691 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4692 if (FD->isExternC())
4693 ND = FD;
4694 if (ND) {
4695 if (IdentifierInfo *Id = ND->getIdentifier()) {
4696 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4697 = WeakUndeclaredIdentifiers.find(Id);
4698 if (I != WeakUndeclaredIdentifiers.end()) {
4699 WeakInfo W = I->second;
4700 DeclApplyPragmaWeak(S, ND, W);
4701 WeakUndeclaredIdentifiers[Id] = W;
4702 }
4703 }
4704 }
4705 }
4706}
4707
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004708/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4709/// it, apply them to D. This is a bit tricky because PD can have attributes
4710/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004711void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004712 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004713 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004714 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004715
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004716 // Walk the declarator structure, applying decl attributes that were in a type
4717 // position to the decl itself. This handles cases like:
4718 // int *__attr__(x)** D;
4719 // when X is a decl attribute.
4720 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4721 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004722 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004723
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004724 // Finally, apply any attributes on the decl itself.
4725 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004726 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004727}
John McCall28a6aea2009-11-04 02:18:39 +00004728
John McCall31168b02011-06-15 23:02:42 +00004729/// Is the given declaration allowed to use a forbidden type?
4730static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4731 // Private ivars are always okay. Unfortunately, people don't
4732 // always properly make their ivars private, even in system headers.
4733 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004734 // Function declarations in sys headers will be marked unavailable.
4735 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4736 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004737 return false;
4738
4739 // Require it to be declared in a system header.
4740 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4741}
4742
4743/// Handle a delayed forbidden-type diagnostic.
4744static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4745 Decl *decl) {
4746 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004747 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4748 "this system declaration uses an unsupported type",
4749 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004750 return;
4751 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004752 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004753 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004754 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004755 // kind of forbidden type messages on unavailable functions.
4756 if (FD->hasAttr<UnavailableAttr>() &&
4757 diag.getForbiddenTypeDiagnostic() ==
4758 diag::err_arc_array_param_no_ownership) {
4759 diag.Triggered = true;
4760 return;
4761 }
4762 }
John McCall31168b02011-06-15 23:02:42 +00004763
4764 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4765 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4766 diag.Triggered = true;
4767}
4768
John McCall2ec85372012-05-07 06:16:41 +00004769void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4770 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004771 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004772 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004773
John McCall2ec85372012-05-07 06:16:41 +00004774 // When delaying diagnostics to run in the context of a parsed
4775 // declaration, we only want to actually emit anything if parsing
4776 // succeeds.
4777 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004778
John McCall2ec85372012-05-07 06:16:41 +00004779 // We emit all the active diagnostics in this pool or any of its
4780 // parents. In general, we'll get one pool for the decl spec
4781 // and a child pool for each declarator; in a decl group like:
4782 // deprecated_typedef foo, *bar, baz();
4783 // only the declarator pops will be passed decls. This is correct;
4784 // we really do need to consider delayed diagnostics from the decl spec
4785 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004786 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004787 do {
John McCall6347b682012-05-07 06:16:58 +00004788 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004789 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4790 // This const_cast is a bit lame. Really, Triggered should be mutable.
4791 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004792 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004793 continue;
4794
John McCallc1465822011-02-14 07:13:47 +00004795 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004796 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004797 case DelayedDiagnostic::Unavailable:
4798 // Don't bother giving deprecation/unavailable diagnostics if
4799 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004800 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004801 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004802 break;
4803
4804 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004805 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004806 break;
John McCall31168b02011-06-15 23:02:42 +00004807
4808 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004809 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004810 break;
John McCall86121512010-01-27 03:50:35 +00004811 }
4812 }
John McCall2ec85372012-05-07 06:16:41 +00004813 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004814}
4815
John McCall6347b682012-05-07 06:16:58 +00004816/// Given a set of delayed diagnostics, re-emit them as if they had
4817/// been delayed in the current context instead of in the given pool.
4818/// Essentially, this just moves them to the current pool.
4819void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4820 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4821 assert(curPool && "re-emitting in undelayed context not supported");
4822 curPool->steal(pool);
4823}
4824
John McCall28a6aea2009-11-04 02:18:39 +00004825static bool isDeclDeprecated(Decl *D) {
4826 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004827 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004828 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004829 // A category implicitly has the availability of the interface.
4830 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4831 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004832 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4833 return false;
4834}
4835
Ted Kremenekb79ee572013-12-18 23:30:06 +00004836static bool isDeclUnavailable(Decl *D) {
4837 do {
4838 if (D->isUnavailable())
4839 return true;
4840 // A category implicitly has the availability of the interface.
4841 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4842 return CatD->getClassInterface()->isUnavailable();
4843 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4844 return false;
4845}
4846
Eli Friedman971bfa12012-08-08 21:52:41 +00004847static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004848DoEmitAvailabilityWarning(Sema &S,
4849 DelayedDiagnostic::DDKind K,
4850 Decl *Ctx,
4851 const NamedDecl *D,
4852 StringRef Message,
4853 SourceLocation Loc,
4854 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004855 const ObjCPropertyDecl *ObjCProperty,
4856 bool ObjCPropertyAccess) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004857
4858 // Diagnostics for deprecated or unavailable.
4859 unsigned diag, diag_message, diag_fwdclass_message;
4860
4861 // Matches 'diag::note_property_attribute' options.
4862 unsigned property_note_select;
4863
4864 // Matches diag::note_availability_specified_here.
4865 unsigned available_here_select_kind;
4866
4867 // Don't warn if our current context is deprecated or unavailable.
4868 switch (K) {
4869 case DelayedDiagnostic::Deprecation:
4870 if (isDeclDeprecated(Ctx))
4871 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004872 diag = !ObjCPropertyAccess ? diag::warn_deprecated
4873 : diag::warn_property_method_deprecated;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004874 diag_message = diag::warn_deprecated_message;
4875 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4876 property_note_select = /* deprecated */ 0;
4877 available_here_select_kind = /* deprecated */ 2;
4878 break;
4879
4880 case DelayedDiagnostic::Unavailable:
4881 if (isDeclUnavailable(Ctx))
4882 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004883 diag = !ObjCPropertyAccess ? diag::err_unavailable
4884 : diag::err_property_method_unavailable;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004885 diag_message = diag::err_unavailable_message;
4886 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4887 property_note_select = /* unavailable */ 1;
4888 available_here_select_kind = /* unavailable */ 0;
4889 break;
4890
4891 default:
4892 llvm_unreachable("Neither a deprecation or unavailable kind");
4893 }
4894
Eli Friedman971bfa12012-08-08 21:52:41 +00004895 DeclarationName Name = D->getDeclName();
4896 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004897 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004898 if (ObjCProperty)
4899 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4900 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004901 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004902 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004903 if (ObjCProperty)
4904 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4905 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004906 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004907 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004908 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4909 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004910
4911 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4912 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004913}
4914
Ted Kremenekb79ee572013-12-18 23:30:06 +00004915void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4916 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004917 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004918 DoEmitAvailabilityWarning(*this,
4919 (DelayedDiagnostic::DDKind) DD.Kind,
4920 Ctx,
4921 DD.getDeprecationDecl(),
4922 DD.getDeprecationMessage(),
4923 DD.Loc,
4924 DD.getUnknownObjCClass(),
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004925 DD.getObjCProperty(), false);
John McCall28a6aea2009-11-04 02:18:39 +00004926}
4927
Ted Kremenekb79ee572013-12-18 23:30:06 +00004928void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4929 NamedDecl *D, StringRef Message,
4930 SourceLocation Loc,
4931 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004932 const ObjCPropertyDecl *ObjCProperty,
4933 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00004934 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004935 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004936 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4937 UnknownObjCClass,
4938 ObjCProperty,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004939 Message,
4940 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00004941 return;
4942 }
4943
Ted Kremenekb79ee572013-12-18 23:30:06 +00004944 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4945 DelayedDiagnostic::DDKind K;
4946 switch (AD) {
4947 case AD_Deprecation:
4948 K = DelayedDiagnostic::Deprecation;
4949 break;
4950 case AD_Unavailable:
4951 K = DelayedDiagnostic::Unavailable;
4952 break;
4953 }
4954
4955 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004956 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00004957}