blob: 8837bbdb2bc44ba6d2d776b4f76b08f2630a31d1 [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"
Hal Finkelee90a222014-09-26 05:04:30 +000032#include "llvm/Support/MathExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000034using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000037 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000038 C,
39 Cpp,
40 ObjC
41 };
42}
43
Chris Lattner58418ff2008-06-29 00:16:31 +000044//===----------------------------------------------------------------------===//
45// Helper functions
46//===----------------------------------------------------------------------===//
47
Ted Kremenek527042b2009-08-14 20:49:40 +000048/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000049/// type (function or function-typed variable) or an Objective-C
50/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000051static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000052 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000053}
54
John McCall3882ace2011-01-05 12:14:39 +000055/// Return true if the given decl has a declarator that should have
56/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000058 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
60 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000061}
62
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000063/// hasFunctionProto - Return true if the given decl has a argument
64/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000065/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000067 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000068 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000069 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000070}
71
Alp Toker601b22c2014-01-21 23:35:24 +000072/// getFunctionOrMethodNumParams - Return number of function or method
73/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000074/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000075static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000076 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000077 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000079 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000080 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000081}
82
Alp Toker601b22c2014-01-21 23:35:24 +000083static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000084 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000085 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000087 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000088
Alp Toker03376dc2014-07-07 09:02:20 +000089 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000090}
91
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000092static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
93 if (const auto *FD = dyn_cast<FunctionDecl>(D))
94 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +000095 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000096 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +000097 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000098 return BD->getParamDecl(Idx)->getSourceRange();
99 return SourceRange();
100}
101
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000102static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000103 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000104 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000105 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000106}
107
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000108static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
109 if (const auto *FD = dyn_cast<FunctionDecl>(D))
110 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000111 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000112 return MD->getReturnTypeSourceRange();
113 return SourceRange();
114}
115
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000116static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000117 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000120 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000121 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
122 return BD->isVariadic();
123
124 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000125}
126
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000127static bool isInstanceMethod(const Decl *D) {
128 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000129 return MethodDecl->isInstance();
130 return false;
131}
132
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000134 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000135 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000136 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
John McCall96fa4842010-05-17 21:00:27 +0000138 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
139 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000140 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000141
John McCall96fa4842010-05-17 21:00:27 +0000142 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000143
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000144 // FIXME: Should we walk the chain of classes?
145 return ClsName == &Ctx.Idents.get("NSString") ||
146 ClsName == &Ctx.Idents.get("NSMutableString");
147}
148
Daniel Dunbar980c6692008-09-26 03:32:58 +0000149static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000150 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000151 if (!PT)
152 return false;
153
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000154 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000155 if (!RT)
156 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000157
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000159 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000160 return false;
161
162 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
163}
164
Richard Smithb87c4652013-10-31 21:23:20 +0000165static unsigned getNumAttributeArgs(const AttributeList &Attr) {
166 // FIXME: Include the type in the argument list.
167 return Attr.getNumArgs() + Attr.hasParsedType();
168}
169
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000170template <typename Compare>
171static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
172 unsigned Num, unsigned Diag,
173 Compare Comp) {
174 if (Comp(getNumAttributeArgs(Attr), Num)) {
175 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000176 return false;
177 }
178
179 return true;
180}
181
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000182/// \brief Check if the attribute has exactly as many args as Num. May
183/// output an error.
184static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
185 unsigned Num) {
186 return checkAttributeNumArgsImpl(S, Attr, Num,
187 diag::err_attribute_wrong_number_arguments,
188 std::not_equal_to<unsigned>());
189}
190
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000191/// \brief Check if the attribute has at least as many args as Num. May
192/// output an error.
193static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000194 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000195 return checkAttributeNumArgsImpl(S, Attr, Num,
196 diag::err_attribute_too_few_arguments,
197 std::less<unsigned>());
198}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000199
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000200/// \brief Check if the attribute has at most as many args as Num. May
201/// output an error.
202static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
203 unsigned Num) {
204 return checkAttributeNumArgsImpl(S, Attr, Num,
205 diag::err_attribute_too_many_arguments,
206 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000207}
208
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000209/// \brief If Expr is a valid integer constant, get the value of the integer
210/// expression and return success or failure. May output an error.
211static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
212 const Expr *Expr, uint32_t &Val,
213 unsigned Idx = UINT_MAX) {
214 llvm::APSInt I(32);
215 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
216 !Expr->isIntegerConstantExpr(I, S.Context)) {
217 if (Idx != UINT_MAX)
218 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
219 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
220 << Expr->getSourceRange();
221 else
222 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
223 << Attr.getName() << AANT_ArgumentIntegerConstant
224 << Expr->getSourceRange();
225 return false;
226 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000227
228 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000229 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
230 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000231 return false;
232 }
233
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000234 Val = (uint32_t)I.getZExtValue();
235 return true;
236}
237
Aaron Ballmanfb763042013-12-02 18:05:46 +0000238/// \brief Diagnose mutually exclusive attributes when present on a given
239/// declaration. Returns true if diagnosed.
240template <typename AttrTy>
241static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000242 const AttributeList &Attr) {
243 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000244 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000245 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000246 return true;
247 }
248 return false;
249}
250
Alp Toker601b22c2014-01-21 23:35:24 +0000251/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000252/// instance method D. May output an error.
253///
254/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000255static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
256 const AttributeList &Attr,
257 unsigned AttrArgNum,
258 const Expr *IdxExpr,
259 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000260 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000261
262 // In C++ the implicit 'this' function parameter also counts.
263 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000264 bool HP = hasFunctionProto(D);
265 bool HasImplicitThisParam = isInstanceMethod(D);
266 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000267 unsigned NumParams =
268 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000269
270 llvm::APSInt IdxInt;
271 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
272 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
274 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
275 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000276 return false;
277 }
278
279 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000280 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
282 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000283 return false;
284 }
285 Idx--; // Convert to zero-based.
286 if (HasImplicitThisParam) {
287 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000288 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000289 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000290 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 return false;
292 }
293 --Idx;
294 }
295
296 return true;
297}
298
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000299/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
300/// If not emit an error and return false. If the argument is an identifier it
301/// will emit an error with a fixit hint and treat it as if it was a string
302/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000303bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
304 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000305 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000306 // Look for identifiers. If we have one emit a hint to fix it to a literal.
307 if (Attr.isArgIdent(ArgNum)) {
308 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000309 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000310 << Attr.getName() << AANT_ArgumentString
311 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000312 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000313 Str = Loc->Ident->getName();
314 if (ArgLocation)
315 *ArgLocation = Loc->Loc;
316 return true;
317 }
318
319 // Now check for an actual string literal.
320 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
321 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
322 if (ArgLocation)
323 *ArgLocation = ArgExpr->getLocStart();
324
325 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000326 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000327 << Attr.getName() << AANT_ArgumentString;
328 return false;
329 }
330
331 Str = Literal->getString();
332 return true;
333}
334
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000335/// \brief Applies the given attribute to the Decl without performing any
336/// additional semantic checking.
337template <typename AttrType>
338static void handleSimpleAttribute(Sema &S, Decl *D,
339 const AttributeList &Attr) {
340 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
341 Attr.getAttributeSpellingListIndex()));
342}
343
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000344/// \brief Check if the passed-in expression is of type int or bool.
345static bool isIntOrBool(Expr *Exp) {
346 QualType QT = Exp->getType();
347 return QT->isBooleanType() || QT->isIntegerType();
348}
349
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000350
351// Check to see if the type is a smart pointer of some kind. We assume
352// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000353static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
354 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
355 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000356 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000357 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000358
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000359 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
360 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000361 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362 return false;
363
364 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000365}
366
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000367/// \brief Check if passed in Decl is a pointer type.
368/// Note that this function may produce an error message.
369/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000370static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
371 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000372 const ValueDecl *vd = cast<ValueDecl>(D);
373 QualType QT = vd->getType();
374 if (QT->isAnyPointerType())
375 return true;
376
377 if (const RecordType *RT = QT->getAs<RecordType>()) {
378 // If it's an incomplete type, it could be a smart pointer; skip it.
379 // (We don't want to force template instantiation if we can avoid it,
380 // since that would alter the order in which templates are instantiated.)
381 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000382 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000383
Aaron Ballman553e6812013-12-26 14:54:11 +0000384 if (threadSafetyCheckIsSmartPointer(S, RT))
385 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000386 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000387
388 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000389 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000390 return false;
391}
392
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000393/// \brief Checks that the passed in QualType either is of RecordType or points
394/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000395static const RecordType *getRecordType(QualType QT) {
396 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000397 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000398
399 // Now check if we point to record type.
400 if (const PointerType *PT = QT->getAs<PointerType>())
401 return PT->getPointeeType()->getAs<RecordType>();
402
Craig Topperc3ec1492014-05-26 06:22:03 +0000403 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404}
405
Aaron Ballman76050722014-04-04 15:13:57 +0000406static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000407 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000408
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000409 if (!RT)
410 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000411
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000412 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000413 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000414 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000415
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000416 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000417 // FIXME -- Check the type that the smart pointer points to.
418 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000419 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000420
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000421 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000422 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000423 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000424 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000425
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000426 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000427 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
428 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000429 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
430 void *) {
431 return BS->getType()->getAs<RecordType>()
432 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000433 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000434 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000435 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000436 return false;
437}
438
Aaron Ballman76050722014-04-04 15:13:57 +0000439static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000440 const auto *TD = Ty->getAs<TypedefType>();
441 if (!TD)
442 return false;
443
444 TypedefNameDecl *TN = TD->getDecl();
445 if (!TN)
446 return false;
447
448 return TN->hasAttr<CapabilityAttr>();
449}
450
Aaron Ballman76050722014-04-04 15:13:57 +0000451static bool typeHasCapability(Sema &S, QualType Ty) {
452 if (checkTypedefTypeForCapability(Ty))
453 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000454
Aaron Ballman76050722014-04-04 15:13:57 +0000455 if (checkRecordTypeForCapability(S, Ty))
456 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000457
Aaron Ballman76050722014-04-04 15:13:57 +0000458 return false;
459}
460
461static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
462 // Capability expressions are simple expressions involving the boolean logic
463 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
464 // a DeclRefExpr is found, its type should be checked to determine whether it
465 // is a capability or not.
466
467 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
468 return typeHasCapability(S, E->getType());
469 else if (const auto *E = dyn_cast<CastExpr>(Ex))
470 return isCapabilityExpr(S, E->getSubExpr());
471 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
472 return isCapabilityExpr(S, E->getSubExpr());
473 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
474 if (E->getOpcode() == UO_LNot)
475 return isCapabilityExpr(S, E->getSubExpr());
476 return false;
477 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
478 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
479 return isCapabilityExpr(S, E->getLHS()) &&
480 isCapabilityExpr(S, E->getRHS());
481 return false;
482 }
483
484 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000485}
486
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000487/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
488/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489/// \param Sidx The attribute argument index to start checking with.
490/// \param ParamIdxOk Whether an argument can be indexing into a function
491/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000492static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
493 const AttributeList &Attr,
494 SmallVectorImpl<Expr *> &Args,
495 int Sidx = 0,
496 bool ParamIdxOk = false) {
497 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000498 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000499
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000500 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000502 Args.push_back(ArgExp);
503 continue;
504 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000507 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000508 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000509 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000510 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000511 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000512 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000513 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000514
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000515 // We allow constant strings to be used as a placeholder for expressions
516 // that are not valid C++ syntax, but warn that they are ignored.
517 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
518 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000519 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 continue;
521 }
522
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000523 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000525 // A pointer to member expression of the form &MyClass::mu is treated
526 // specially -- we need to look at the type of the member.
527 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
528 if (UOp->getOpcode() == UO_AddrOf)
529 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
530 if (DRE->getDecl()->isCXXInstanceMember())
531 ArgTy = DRE->getDecl()->getType();
532
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000533 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000534 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000535
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000536 // Now check if we index into a record type function param.
537 if(!RT && ParamIdxOk) {
538 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000539 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
540 if(FD && IL) {
541 unsigned int NumParams = FD->getNumParams();
542 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000543 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
544 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
545 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
547 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000550 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000551 }
552 }
553
Aaron Ballman76050722014-04-04 15:13:57 +0000554 // If the type does not have a capability, see if the components of the
555 // expression have capabilities. This allows for writing C code where the
556 // capability may be on the type, and the expression is a capability
557 // boolean logic expression. Eg) requires_capability(A || B && !C)
558 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
559 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
560 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000561
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000563 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000564}
565
Chris Lattner58418ff2008-06-29 00:16:31 +0000566//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000567// Attribute Implementations
568//===----------------------------------------------------------------------===//
569
Michael Hana9171bc2012-08-03 17:40:43 +0000570static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000571 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000572 if (!threadSafetyCheckIsPointer(S, D, Attr))
573 return;
574
Michael Han99315932013-01-24 16:46:58 +0000575 D->addAttr(::new (S.Context)
576 PtGuardedVarAttr(Attr.getRange(), S.Context,
577 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
581 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000582 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000583 SmallVector<Expr*, 1> Args;
584 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000585 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000586 unsigned Size = Args.size();
587 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000588 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000589
Michael Han3be3b442012-07-23 18:48:41 +0000590 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591
Michael Han3be3b442012-07-23 18:48:41 +0000592 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000593}
594
Michael Han3be3b442012-07-23 18:48:41 +0000595static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000596 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000597 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
598 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000599
Aaron Ballman36a53502014-01-16 13:03:14 +0000600 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
601 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000602}
603
Michael Hana9171bc2012-08-03 17:40:43 +0000604static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000605 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000606 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000607 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
608 return;
609
610 if (!threadSafetyCheckIsPointer(S, D, Attr))
611 return;
612
613 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000614 S.Context, Arg,
615 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000616}
617
Michael Hana9171bc2012-08-03 17:40:43 +0000618static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
619 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000620 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000621 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000622 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000623
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000624 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000625 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000626 if (!QT->isDependentType()) {
627 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000628 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000629 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000630 << Attr.getName();
631 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000632 }
633 }
634
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000635 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000636 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000637 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000638 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000639
Michael Han3be3b442012-07-23 18:48:41 +0000640 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000641}
642
Michael Hana9171bc2012-08-03 17:40:43 +0000643static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000644 const AttributeList &Attr) {
645 SmallVector<Expr*, 1> Args;
646 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
647 return;
648
649 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000650 D->addAttr(::new (S.Context)
651 AcquiredAfterAttr(Attr.getRange(), S.Context,
652 StartArg, Args.size(),
653 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000654}
655
Michael Hana9171bc2012-08-03 17:40:43 +0000656static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000657 const AttributeList &Attr) {
658 SmallVector<Expr*, 1> Args;
659 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
660 return;
661
662 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000663 D->addAttr(::new (S.Context)
664 AcquiredBeforeAttr(Attr.getRange(), S.Context,
665 StartArg, Args.size(),
666 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000667}
668
Michael Hana9171bc2012-08-03 17:40:43 +0000669static bool checkLockFunAttrCommon(Sema &S, Decl *D,
670 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000671 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000672 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000673 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000674 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000675
Michael Han3be3b442012-07-23 18:48:41 +0000676 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000677}
678
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000679static void handleAssertSharedLockAttr(Sema &S, Decl *D,
680 const AttributeList &Attr) {
681 SmallVector<Expr*, 1> Args;
682 if (!checkLockFunAttrCommon(S, D, Attr, Args))
683 return;
684
685 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000686 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000687 D->addAttr(::new (S.Context)
688 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
689 Attr.getAttributeSpellingListIndex()));
690}
691
692static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
693 const AttributeList &Attr) {
694 SmallVector<Expr*, 1> Args;
695 if (!checkLockFunAttrCommon(S, D, Attr, Args))
696 return;
697
698 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000699 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000700 D->addAttr(::new (S.Context)
701 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
702 StartArg, Size,
703 Attr.getAttributeSpellingListIndex()));
704}
705
706
Michael Hana9171bc2012-08-03 17:40:43 +0000707static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
708 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000709 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000710 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000711 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000712
Aaron Ballman00e99962013-08-31 01:11:41 +0000713 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000714 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000715 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000716 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000717 }
718
719 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000720 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000721
Michael Han3be3b442012-07-23 18:48:41 +0000722 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000723}
724
Michael Hana9171bc2012-08-03 17:40:43 +0000725static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000726 const AttributeList &Attr) {
727 SmallVector<Expr*, 2> Args;
728 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
729 return;
730
Michael Han99315932013-01-24 16:46:58 +0000731 D->addAttr(::new (S.Context)
732 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000733 Attr.getArgAsExpr(0),
734 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000735 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000736}
737
Michael Hana9171bc2012-08-03 17:40:43 +0000738static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000739 const AttributeList &Attr) {
740 SmallVector<Expr*, 2> Args;
741 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
742 return;
743
Nico Weber462fd1e2015-01-07 23:50:05 +0000744 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
745 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
746 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000747}
748
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000750 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000751 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000752 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000753 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000754 unsigned Size = Args.size();
755 if (Size == 0)
756 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000757
Michael Han99315932013-01-24 16:46:58 +0000758 D->addAttr(::new (S.Context)
759 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
760 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000761}
762
763static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000764 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000765 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000766 return;
767
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000768 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000769 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000770 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000771 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000772 if (Size == 0)
773 return;
774 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000775
Michael Han99315932013-01-24 16:46:58 +0000776 D->addAttr(::new (S.Context)
777 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
778 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000779}
780
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000781static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
782 Expr *Cond = Attr.getArgAsExpr(0);
783 if (!Cond->isTypeDependent()) {
784 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
785 if (Converted.isInvalid())
786 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000787 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000788 }
789
790 StringRef Msg;
791 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
792 return;
793
794 SmallVector<PartialDiagnosticAt, 8> Diags;
795 if (!Cond->isValueDependent() &&
796 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
797 Diags)) {
798 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
799 for (int I = 0, N = Diags.size(); I != N; ++I)
800 S.Diag(Diags[I].first, Diags[I].second);
801 return;
802 }
803
804 D->addAttr(::new (S.Context)
805 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
806 Attr.getAttributeSpellingListIndex()));
807}
808
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000809static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000810 ConsumableAttr::ConsumedState DefaultState;
811
812 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000813 IdentifierLoc *IL = Attr.getArgAsIdent(0);
814 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
815 DefaultState)) {
816 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
817 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000818 return;
819 }
David Blaikie16f76d22013-09-06 01:28:43 +0000820 } else {
821 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
822 << Attr.getName() << AANT_ArgumentIdentifier;
823 return;
824 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000825
826 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000827 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000828 Attr.getAttributeSpellingListIndex()));
829}
830
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000831
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000832static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
833 const AttributeList &Attr) {
834 ASTContext &CurrContext = S.getASTContext();
835 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
836
837 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
838 if (!RD->hasAttr<ConsumableAttr>()) {
839 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
840 RD->getNameAsString();
841
842 return false;
843 }
844 }
845
846 return true;
847}
848
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000849
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000850static void handleCallableWhenAttr(Sema &S, Decl *D,
851 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000852 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
853 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000854
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000855 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
856 return;
857
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000858 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
859 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
860 CallableWhenAttr::ConsumedState CallableState;
861
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000862 StringRef StateString;
863 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000864 if (Attr.isArgIdent(ArgIndex)) {
865 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
866 StateString = Ident->Ident->getName();
867 Loc = Ident->Loc;
868 } else {
869 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
870 return;
871 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000872
873 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000874 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000875 S.Diag(Loc, diag::warn_attribute_type_not_supported)
876 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000877 return;
878 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000879
880 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000881 }
882
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000883 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000884 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
885 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000886}
887
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000888
DeLesley Hutchins69391772013-10-17 23:23:53 +0000889static void handleParamTypestateAttr(Sema &S, Decl *D,
890 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000891 ParamTypestateAttr::ConsumedState ParamState;
892
893 if (Attr.isArgIdent(0)) {
894 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
895 StringRef StateString = Ident->Ident->getName();
896
897 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
898 ParamState)) {
899 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
900 << Attr.getName() << StateString;
901 return;
902 }
903 } else {
904 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
905 Attr.getName() << AANT_ArgumentIdentifier;
906 return;
907 }
908
909 // FIXME: This check is currently being done in the analysis. It can be
910 // enabled here only after the parser propagates attributes at
911 // template specialization definition, not declaration.
912 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
913 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
914 //
915 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
916 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
917 // ReturnType.getAsString();
918 // return;
919 //}
920
921 D->addAttr(::new (S.Context)
922 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
923 Attr.getAttributeSpellingListIndex()));
924}
925
926
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000927static void handleReturnTypestateAttr(Sema &S, Decl *D,
928 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000929 ReturnTypestateAttr::ConsumedState ReturnState;
930
931 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000932 IdentifierLoc *IL = Attr.getArgAsIdent(0);
933 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
934 ReturnState)) {
935 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
936 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000937 return;
938 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000939 } else {
940 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
941 Attr.getName() << AANT_ArgumentIdentifier;
942 return;
943 }
944
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000945 // FIXME: This check is currently being done in the analysis. It can be
946 // enabled here only after the parser propagates attributes at
947 // template specialization definition, not declaration.
948 //QualType ReturnType;
949 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000950 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
951 // ReturnType = Param->getType();
952 //
953 //} else if (const CXXConstructorDecl *Constructor =
954 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000955 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
956 //
957 //} else {
958 //
959 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
960 //}
961 //
962 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
963 //
964 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
965 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
966 // ReturnType.getAsString();
967 // return;
968 //}
969
970 D->addAttr(::new (S.Context)
971 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
972 Attr.getAttributeSpellingListIndex()));
973}
974
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000975
976static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000977 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
978 return;
979
980 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000981 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000982 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
983 StringRef Param = Ident->Ident->getName();
984 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
985 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
986 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000987 return;
988 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000989 } else {
990 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
991 Attr.getName() << AANT_ArgumentIdentifier;
992 return;
993 }
994
995 D->addAttr(::new (S.Context)
996 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
997 Attr.getAttributeSpellingListIndex()));
998}
999
Chris Wailes9385f9f2013-10-29 20:28:41 +00001000static void handleTestTypestateAttr(Sema &S, Decl *D,
1001 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001002 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1003 return;
1004
Chris Wailes9385f9f2013-10-29 20:28:41 +00001005 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001006 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001007 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1008 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001009 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001010 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1011 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001012 return;
1013 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001014 } else {
1015 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1016 Attr.getName() << AANT_ArgumentIdentifier;
1017 return;
1018 }
1019
1020 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001021 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001022 Attr.getAttributeSpellingListIndex()));
1023}
1024
Chandler Carruthedc2c642011-07-02 00:01:44 +00001025static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1026 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001027 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001028 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001029}
1030
Chandler Carruthedc2c642011-07-02 00:01:44 +00001031static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001032 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001033 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1034 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001035 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001036 // If the alignment is less than or equal to 8 bits, the packed attribute
1037 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001038 if (!FD->getType()->isDependentType() &&
1039 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001040 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001041 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001042 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001043 else
Michael Han99315932013-01-24 16:46:58 +00001044 FD->addAttr(::new (S.Context)
1045 PackedAttr(Attr.getRange(), S.Context,
1046 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001047 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001048 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001049}
1050
Ted Kremenek7fd17232011-09-29 07:02:25 +00001051static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1052 // The IBOutlet/IBOutletCollection attributes only apply to instance
1053 // variables or properties of Objective-C classes. The outlet must also
1054 // have an object reference type.
1055 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1056 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001057 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001058 << Attr.getName() << VD->getType() << 0;
1059 return false;
1060 }
1061 }
1062 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1063 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001064 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001065 << Attr.getName() << PD->getType() << 1;
1066 return false;
1067 }
1068 }
1069 else {
1070 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1071 return false;
1072 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001073
Ted Kremenek7fd17232011-09-29 07:02:25 +00001074 return true;
1075}
1076
Chandler Carruthedc2c642011-07-02 00:01:44 +00001077static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001078 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001079 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001080
Michael Han99315932013-01-24 16:46:58 +00001081 D->addAttr(::new (S.Context)
1082 IBOutletAttr(Attr.getRange(), S.Context,
1083 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001084}
1085
Chandler Carruthedc2c642011-07-02 00:01:44 +00001086static void handleIBOutletCollection(Sema &S, Decl *D,
1087 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001088
1089 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001090 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001091 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1092 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001093 return;
1094 }
1095
Ted Kremenek7fd17232011-09-29 07:02:25 +00001096 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001097 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001098
Richard Smithb1f9a282013-10-31 01:56:18 +00001099 ParsedType PT;
1100
1101 if (Attr.hasParsedType())
1102 PT = Attr.getTypeArg();
1103 else {
1104 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1105 S.getScopeForContext(D->getDeclContext()->getParent()));
1106 if (!PT) {
1107 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1108 return;
1109 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001110 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001111
Craig Topperc3ec1492014-05-26 06:22:03 +00001112 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001113 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1114 if (!QTLoc)
1115 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001116
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001117 // Diagnose use of non-object type in iboutletcollection attribute.
1118 // FIXME. Gnu attribute extension ignores use of builtin types in
1119 // attributes. So, __attribute__((iboutletcollection(char))) will be
1120 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001121 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001122 S.Diag(Attr.getLoc(),
1123 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1124 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001125 return;
1126 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001127
Michael Han99315932013-01-24 16:46:58 +00001128 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001129 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001130 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001131}
1132
Hal Finkelee90a222014-09-26 05:04:30 +00001133bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1134 if (RefOkay) {
1135 if (T->isReferenceType())
1136 return true;
1137 } else {
1138 T = T.getNonReferenceType();
1139 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001140
Hal Finkelee90a222014-09-26 05:04:30 +00001141 // The nonnull attribute, and other similar attributes, can be applied to a
1142 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001143 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001144 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1145 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001146 for (const auto *I : UD->fields()) {
1147 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001148 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1149 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001150 }
1151 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001152 }
1153
1154 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001155}
1156
Ted Kremenek9aedc152014-01-17 06:24:56 +00001157static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001158 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001159 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001160 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001161 if (!S.isValidPointerAttrType(T)) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001162 S.Diag(Attr.getLoc(), isReturnValue
1163 ? diag::warn_attribute_return_pointers_only
1164 : diag::warn_attribute_pointers_only)
Hal Finkelee90a222014-09-26 05:04:30 +00001165 << Attr.getName() << AttrParmRange << TypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001166 return false;
1167 }
1168 return true;
1169}
1170
Chandler Carruthedc2c642011-07-02 00:01:44 +00001171static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001172 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001173 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1174 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001175 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001176 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001177 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001178
1179 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001180 if (Idx < getFunctionOrMethodNumParams(D) &&
1181 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001182 Ex->getSourceRange(),
1183 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001184 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001185
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001186 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001187 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001188
1189 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001190 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1191 // check if the attribute came from a macro expansion or a template
1192 // instantiation.
1193 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1194 S.ActiveTemplateInstantiations.empty()) {
1195 bool AnyPointers = isFunctionOrMethodVariadic(D);
1196 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1197 I != E && !AnyPointers; ++I) {
1198 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001199 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001200 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001201 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001202
Richard Smith588bd9b2014-08-27 04:59:42 +00001203 if (!AnyPointers)
1204 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001205 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001206
Richard Smith588bd9b2014-08-27 04:59:42 +00001207 unsigned *Start = NonNullArgs.data();
1208 unsigned Size = NonNullArgs.size();
1209 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001210 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001211 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001212 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001213}
1214
Jordan Rosec9399072014-02-11 17:27:59 +00001215static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1216 const AttributeList &Attr) {
1217 if (Attr.getNumArgs() > 0) {
1218 if (D->getFunctionType()) {
1219 handleNonNullAttr(S, D, Attr);
1220 } else {
1221 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1222 << D->getSourceRange();
1223 }
1224 return;
1225 }
1226
1227 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001228 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1229 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001230 return;
1231
1232 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001233 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001234 Attr.getAttributeSpellingListIndex()));
1235}
1236
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001237static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1238 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001239 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001240 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1241 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001242 /* isReturnValue */ true))
1243 return;
1244
1245 D->addAttr(::new (S.Context)
1246 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1247 Attr.getAttributeSpellingListIndex()));
1248}
1249
Hal Finkelee90a222014-09-26 05:04:30 +00001250static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1251 const AttributeList &Attr) {
1252 Expr *E = Attr.getArgAsExpr(0),
1253 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1254 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1255 Attr.getAttributeSpellingListIndex());
1256}
1257
1258void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1259 Expr *OE, unsigned SpellingListIndex) {
1260 QualType ResultType = getFunctionOrMethodResultType(D);
1261 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1262
1263 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1264 SourceLocation AttrLoc = AttrRange.getBegin();
1265
1266 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1267 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1268 << &TmpAttr << AttrRange << SR;
1269 return;
1270 }
1271
1272 if (!E->isValueDependent()) {
1273 llvm::APSInt I(64);
1274 if (!E->isIntegerConstantExpr(I, Context)) {
1275 if (OE)
1276 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1277 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1278 << E->getSourceRange();
1279 else
1280 Diag(AttrLoc, diag::err_attribute_argument_type)
1281 << &TmpAttr << AANT_ArgumentIntegerConstant
1282 << E->getSourceRange();
1283 return;
1284 }
1285
1286 if (!I.isPowerOf2()) {
1287 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1288 << E->getSourceRange();
1289 return;
1290 }
1291 }
1292
1293 if (OE) {
1294 if (!OE->isValueDependent()) {
1295 llvm::APSInt I(64);
1296 if (!OE->isIntegerConstantExpr(I, Context)) {
1297 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1298 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1299 << OE->getSourceRange();
1300 return;
1301 }
1302 }
1303 }
1304
1305 D->addAttr(::new (Context)
1306 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1307}
1308
Chandler Carruthedc2c642011-07-02 00:01:44 +00001309static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001310 // This attribute must be applied to a function declaration. The first
1311 // argument to the attribute must be an identifier, the name of the resource,
1312 // for example: malloc. The following arguments must be argument indexes, the
1313 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001314 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001315 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001316 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001317
Aaron Ballman00e99962013-08-31 01:11:41 +00001318 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001319 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001320 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321 return;
1322 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001323
Richard Smith852e9ce2013-11-27 01:46:48 +00001324 // Figure out our Kind.
1325 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001326 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001327 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001328
Richard Smith852e9ce2013-11-27 01:46:48 +00001329 // Check arguments.
1330 switch (K) {
1331 case OwnershipAttr::Takes:
1332 case OwnershipAttr::Holds:
1333 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001334 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1335 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001336 return;
1337 }
1338 break;
1339 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001340 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001341 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1342 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001343 return;
1344 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001345 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001346 }
1347
Richard Smith852e9ce2013-11-27 01:46:48 +00001348 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349
1350 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001351 StringRef ModuleName = Module->getName();
1352 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1353 ModuleName.size() > 4) {
1354 ModuleName = ModuleName.drop_front(2).drop_back(2);
1355 Module = &S.PP.getIdentifierTable().get(ModuleName);
1356 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001357
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001358 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001359 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1360 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001361 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001362 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001363 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001364
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001365 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001366 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001367 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001368 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001369 case OwnershipAttr::Takes:
1370 case OwnershipAttr::Holds:
1371 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1372 Err = 0;
1373 break;
1374 case OwnershipAttr::Returns:
1375 if (!T->isIntegerType())
1376 Err = 1;
1377 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001378 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001379 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001380 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001381 << Ex->getSourceRange();
1382 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384
1385 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001386 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001387 // Cannot have two ownership attributes of different kinds for the same
1388 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001389 if (I->getOwnKind() != K && I->args_end() !=
1390 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001391 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001392 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001393 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001394 } else if (K == OwnershipAttr::Returns &&
1395 I->getOwnKind() == OwnershipAttr::Returns) {
1396 // A returns attribute conflicts with any other returns attribute using
1397 // a different index. Note, diagnostic reporting is 1-based, but stored
1398 // argument indexes are 0-based.
1399 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1400 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1401 << *(I->args_begin()) + 1;
1402 if (I->args_size())
1403 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1404 << (unsigned)Idx + 1 << Ex->getSourceRange();
1405 return;
1406 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001407 }
1408 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001409 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001410 }
1411
1412 unsigned* start = OwnershipArgs.data();
1413 unsigned size = OwnershipArgs.size();
1414 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001415
Michael Han99315932013-01-24 16:46:58 +00001416 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001417 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001418 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001419}
1420
Chandler Carruthedc2c642011-07-02 00:01:44 +00001421static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001422 // Check the attribute arguments.
1423 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1425 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001426 return;
1427 }
1428
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001429 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001430
Rafael Espindolac18086a2010-02-23 22:00:30 +00001431 // gcc rejects
1432 // class c {
1433 // static int a __attribute__((weakref ("v2")));
1434 // static int b() __attribute__((weakref ("f3")));
1435 // };
1436 // and ignores the attributes of
1437 // void f(void) {
1438 // static int a __attribute__((weakref ("v2")));
1439 // }
1440 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001441 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001442 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1444 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001445 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001446 }
1447
1448 // The GCC manual says
1449 //
1450 // At present, a declaration to which `weakref' is attached can only
1451 // be `static'.
1452 //
1453 // It also says
1454 //
1455 // Without a TARGET,
1456 // given as an argument to `weakref' or to `alias', `weakref' is
1457 // equivalent to `weak'.
1458 //
1459 // gcc 4.4.1 will accept
1460 // int a7 __attribute__((weakref));
1461 // as
1462 // int a7 __attribute__((weak));
1463 // This looks like a bug in gcc. We reject that for now. We should revisit
1464 // it if this behaviour is actually used.
1465
Rafael Espindolac18086a2010-02-23 22:00:30 +00001466 // GCC rejects
1467 // static ((alias ("y"), weakref)).
1468 // Should we? How to check that weakref is before or after alias?
1469
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001470 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1471 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1472 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001473 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001474 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001475 // GCC will accept anything as the argument of weakref. Should we
1476 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001477 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1478 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001479
Michael Han99315932013-01-24 16:46:58 +00001480 D->addAttr(::new (S.Context)
1481 WeakRefAttr(Attr.getRange(), S.Context,
1482 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001483}
1484
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001485static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1486 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001487 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001488 return;
1489
Douglas Gregore8bbc122011-09-02 00:18:52 +00001490 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001491 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1492 return;
1493 }
1494
David Majnemer2dc81462015-01-19 09:00:28 +00001495 // Aliases should be on declarations, not definitions.
1496 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1497 if (FD->isThisDeclarationADefinition()) {
1498 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1499 return;
1500 }
1501 } else {
1502 const auto *VD = cast<VarDecl>(D);
1503 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1504 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1505 return;
1506 }
1507 }
1508
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001509 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001510
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001511 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001512 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001513}
1514
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001515static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001516 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001517 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001518
Michael Han99315932013-01-24 16:46:58 +00001519 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1520 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001521}
1522
1523static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001524 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001525 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001526
Michael Han99315932013-01-24 16:46:58 +00001527 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1528 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001529}
1530
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001531static void handleTLSModelAttr(Sema &S, Decl *D,
1532 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001533 StringRef Model;
1534 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001535 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001536 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001537 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001538
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001539 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001540 if (Model != "global-dynamic" && Model != "local-dynamic"
1541 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001542 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001543 return;
1544 }
1545
Michael Han99315932013-01-24 16:46:58 +00001546 D->addAttr(::new (S.Context)
1547 TLSModelAttr(Attr.getRange(), S.Context, Model,
1548 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001549}
1550
Chandler Carruthedc2c642011-07-02 00:01:44 +00001551static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001552 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001553 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001554 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001555 D->addAttr(::new (S.Context)
1556 MallocAttr(Attr.getRange(), S.Context,
1557 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001558 return;
1559 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001560 }
1561
Ted Kremenek08479ae2009-08-15 00:51:46 +00001562 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001563}
1564
Chandler Carruthedc2c642011-07-02 00:01:44 +00001565static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001566 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001567 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1568 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001569 return;
1570 }
1571
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001572 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1573 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001574}
1575
Chandler Carruthedc2c642011-07-02 00:01:44 +00001576static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001577 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001578
1579 if (S.CheckNoReturnAttr(attr)) return;
1580
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001581 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001582 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001583 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001584 return;
1585 }
1586
Michael Han99315932013-01-24 16:46:58 +00001587 D->addAttr(::new (S.Context)
1588 NoReturnAttr(attr.getRange(), S.Context,
1589 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001590}
1591
1592bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001593 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001594 attr.setInvalid();
1595 return true;
1596 }
1597
1598 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001599}
1600
Chandler Carruthedc2c642011-07-02 00:01:44 +00001601static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1602 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001603
1604 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1605 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001606 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1607 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001608 if (!VD || (!VD->getType()->isBlockPointerType() &&
1609 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001610 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001611 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001612 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001613 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001614 return;
1615 }
1616 }
1617
Michael Han99315932013-01-24 16:46:58 +00001618 D->addAttr(::new (S.Context)
1619 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1620 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001621}
1622
John Thompsoncdb847ba2010-08-09 21:53:52 +00001623// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001624static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001625/*
1626 Returning a Vector Class in Registers
1627
Eric Christopherbc638a82010-12-01 22:13:54 +00001628 According to the PPU ABI specifications, a class with a single member of
1629 vector type is returned in memory when used as the return value of a function.
1630 This results in inefficient code when implementing vector classes. To return
1631 the value in a single vector register, add the vecreturn attribute to the
1632 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001633
1634 Example:
1635
1636 struct Vector
1637 {
1638 __vector float xyzw;
1639 } __attribute__((vecreturn));
1640
1641 Vector Add(Vector lhs, Vector rhs)
1642 {
1643 Vector result;
1644 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1645 return result; // This will be returned in a register
1646 }
1647*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001648 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1649 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001650 return;
1651 }
1652
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001653 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001654 int count = 0;
1655
1656 if (!isa<CXXRecordDecl>(record)) {
1657 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1658 return;
1659 }
1660
1661 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1663 return;
1664 }
1665
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001666 for (const auto *I : record->fields()) {
1667 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001668 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1669 return;
1670 }
1671 count++;
1672 }
1673
Michael Han99315932013-01-24 16:46:58 +00001674 D->addAttr(::new (S.Context)
1675 VecReturnAttr(Attr.getRange(), S.Context,
1676 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001677}
1678
Richard Smithe233fbf2013-01-28 22:42:45 +00001679static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1680 const AttributeList &Attr) {
1681 if (isa<ParmVarDecl>(D)) {
1682 // [[carries_dependency]] can only be applied to a parameter if it is a
1683 // parameter of a function declaration or lambda.
1684 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1685 S.Diag(Attr.getLoc(),
1686 diag::err_carries_dependency_param_not_function_decl);
1687 return;
1688 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001689 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001690
1691 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1692 Attr.getRange(), S.Context,
1693 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001694}
1695
Chandler Carruthedc2c642011-07-02 00:01:44 +00001696static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001697 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001698 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001699 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001700 return;
1701 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001702 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001704 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001705 return;
1706 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001707
Michael Han99315932013-01-24 16:46:58 +00001708 D->addAttr(::new (S.Context)
1709 UsedAttr(Attr.getRange(), S.Context,
1710 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001711}
1712
Chandler Carruthedc2c642011-07-02 00:01:44 +00001713static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001714 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001715 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001716 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1717 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001718
Michael Han99315932013-01-24 16:46:58 +00001719 D->addAttr(::new (S.Context)
1720 ConstructorAttr(Attr.getRange(), S.Context, priority,
1721 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001722}
1723
Chandler Carruthedc2c642011-07-02 00:01:44 +00001724static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001725 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001726 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001727 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1728 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001729
Michael Han99315932013-01-24 16:46:58 +00001730 D->addAttr(::new (S.Context)
1731 DestructorAttr(Attr.getRange(), S.Context, priority,
1732 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001733}
1734
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001735template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001736static void handleAttrWithMessage(Sema &S, Decl *D,
1737 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001738 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001739 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001740 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001741 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001742
Michael Han99315932013-01-24 16:46:58 +00001743 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1744 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001745}
1746
Ted Kremenek438f8db2014-02-22 01:06:05 +00001747static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001748 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001749 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001750 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1751 << Attr.getName() << Attr.getRange();
1752 return;
1753 }
1754
Ted Kremenek28eace62013-11-23 01:01:34 +00001755 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001756 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1757 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001758}
1759
Jordy Rose740b0c22012-05-08 03:27:22 +00001760static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1761 IdentifierInfo *Platform,
1762 VersionTuple Introduced,
1763 VersionTuple Deprecated,
1764 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001765 StringRef PlatformName
1766 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1767 if (PlatformName.empty())
1768 PlatformName = Platform->getName();
1769
1770 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1771 // of these steps are needed).
1772 if (!Introduced.empty() && !Deprecated.empty() &&
1773 !(Introduced <= Deprecated)) {
1774 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1775 << 1 << PlatformName << Deprecated.getAsString()
1776 << 0 << Introduced.getAsString();
1777 return true;
1778 }
1779
1780 if (!Introduced.empty() && !Obsoleted.empty() &&
1781 !(Introduced <= Obsoleted)) {
1782 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1783 << 2 << PlatformName << Obsoleted.getAsString()
1784 << 0 << Introduced.getAsString();
1785 return true;
1786 }
1787
1788 if (!Deprecated.empty() && !Obsoleted.empty() &&
1789 !(Deprecated <= Obsoleted)) {
1790 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1791 << 2 << PlatformName << Obsoleted.getAsString()
1792 << 1 << Deprecated.getAsString();
1793 return true;
1794 }
1795
1796 return false;
1797}
1798
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001799/// \brief Check whether the two versions match.
1800///
1801/// If either version tuple is empty, then they are assumed to match. If
1802/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1803static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1804 bool BeforeIsOkay) {
1805 if (X.empty() || Y.empty())
1806 return true;
1807
1808 if (X == Y)
1809 return true;
1810
1811 if (BeforeIsOkay && X < Y)
1812 return true;
1813
1814 return false;
1815}
1816
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001817AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001818 IdentifierInfo *Platform,
1819 VersionTuple Introduced,
1820 VersionTuple Deprecated,
1821 VersionTuple Obsoleted,
1822 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001823 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001824 bool Override,
1825 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 VersionTuple MergedIntroduced = Introduced;
1827 VersionTuple MergedDeprecated = Deprecated;
1828 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001829 bool FoundAny = false;
1830
Rafael Espindolac67f2232012-05-10 02:50:16 +00001831 if (D->hasAttrs()) {
1832 AttrVec &Attrs = D->getAttrs();
1833 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1834 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1835 if (!OldAA) {
1836 ++i;
1837 continue;
1838 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001839
Rafael Espindolac67f2232012-05-10 02:50:16 +00001840 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1841 if (OldPlatform != Platform) {
1842 ++i;
1843 continue;
1844 }
1845
1846 FoundAny = true;
1847 VersionTuple OldIntroduced = OldAA->getIntroduced();
1848 VersionTuple OldDeprecated = OldAA->getDeprecated();
1849 VersionTuple OldObsoleted = OldAA->getObsoleted();
1850 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001851
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001852 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1853 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1854 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1855 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001856 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001857 if (Override) {
1858 int Which = -1;
1859 VersionTuple FirstVersion;
1860 VersionTuple SecondVersion;
1861 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1862 Which = 0;
1863 FirstVersion = OldIntroduced;
1864 SecondVersion = Introduced;
1865 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1866 Which = 1;
1867 FirstVersion = Deprecated;
1868 SecondVersion = OldDeprecated;
1869 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1870 Which = 2;
1871 FirstVersion = Obsoleted;
1872 SecondVersion = OldObsoleted;
1873 }
1874
1875 if (Which == -1) {
1876 Diag(OldAA->getLocation(),
1877 diag::warn_mismatched_availability_override_unavail)
1878 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1879 } else {
1880 Diag(OldAA->getLocation(),
1881 diag::warn_mismatched_availability_override)
1882 << Which
1883 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1884 << FirstVersion.getAsString() << SecondVersion.getAsString();
1885 }
1886 Diag(Range.getBegin(), diag::note_overridden_method);
1887 } else {
1888 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1889 Diag(Range.getBegin(), diag::note_previous_attribute);
1890 }
1891
Rafael Espindolac67f2232012-05-10 02:50:16 +00001892 Attrs.erase(Attrs.begin() + i);
1893 --e;
1894 continue;
1895 }
1896
1897 VersionTuple MergedIntroduced2 = MergedIntroduced;
1898 VersionTuple MergedDeprecated2 = MergedDeprecated;
1899 VersionTuple MergedObsoleted2 = MergedObsoleted;
1900
1901 if (MergedIntroduced2.empty())
1902 MergedIntroduced2 = OldIntroduced;
1903 if (MergedDeprecated2.empty())
1904 MergedDeprecated2 = OldDeprecated;
1905 if (MergedObsoleted2.empty())
1906 MergedObsoleted2 = OldObsoleted;
1907
1908 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1909 MergedIntroduced2, MergedDeprecated2,
1910 MergedObsoleted2)) {
1911 Attrs.erase(Attrs.begin() + i);
1912 --e;
1913 continue;
1914 }
1915
1916 MergedIntroduced = MergedIntroduced2;
1917 MergedDeprecated = MergedDeprecated2;
1918 MergedObsoleted = MergedObsoleted2;
1919 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001920 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001921 }
1922
1923 if (FoundAny &&
1924 MergedIntroduced == Introduced &&
1925 MergedDeprecated == Deprecated &&
1926 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001927 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001928
Ted Kremenekb5445722013-04-06 00:34:27 +00001929 // Only create a new attribute if !Override, but we want to do
1930 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001931 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001932 MergedDeprecated, MergedObsoleted) &&
1933 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001934 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1935 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001936 Obsoleted, IsUnavailable, Message,
1937 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001938 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001939 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001940}
1941
Chandler Carruthedc2c642011-07-02 00:01:44 +00001942static void handleAvailabilityAttr(Sema &S, Decl *D,
1943 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001944 if (!checkAttributeNumArgs(S, Attr, 1))
1945 return;
1946 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001947 unsigned Index = Attr.getAttributeSpellingListIndex();
1948
Aaron Ballman00e99962013-08-31 01:11:41 +00001949 IdentifierInfo *II = Platform->Ident;
1950 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1951 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1952 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001953
Rafael Espindolac231fab2013-01-08 21:30:32 +00001954 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1955 if (!ND) {
1956 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1957 return;
1958 }
1959
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001960 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1961 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1962 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001963 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001964 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001965 if (const StringLiteral *SE =
1966 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001967 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001968
Aaron Ballman00e99962013-08-31 01:11:41 +00001969 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001970 Introduced.Version,
1971 Deprecated.Version,
1972 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001973 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001974 /*Override=*/false,
1975 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001976 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001977 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001978}
1979
John McCalld041a9b2013-02-20 01:54:26 +00001980template <class T>
1981static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1982 typename T::VisibilityType value,
1983 unsigned attrSpellingListIndex) {
1984 T *existingAttr = D->getAttr<T>();
1985 if (existingAttr) {
1986 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1987 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001988 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001989 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1990 S.Diag(range.getBegin(), diag::note_previous_attribute);
1991 D->dropAttr<T>();
1992 }
1993 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1994}
1995
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001996VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001997 VisibilityAttr::VisibilityType Vis,
1998 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001999 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2000 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002001}
2002
John McCalld041a9b2013-02-20 01:54:26 +00002003TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2004 TypeVisibilityAttr::VisibilityType Vis,
2005 unsigned AttrSpellingListIndex) {
2006 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2007 AttrSpellingListIndex);
2008}
2009
2010static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2011 bool isTypeVisibility) {
2012 // Visibility attributes don't mean anything on a typedef.
2013 if (isa<TypedefNameDecl>(D)) {
2014 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2015 << Attr.getName();
2016 return;
2017 }
2018
2019 // 'type_visibility' can only go on a type or namespace.
2020 if (isTypeVisibility &&
2021 !(isa<TagDecl>(D) ||
2022 isa<ObjCInterfaceDecl>(D) ||
2023 isa<NamespaceDecl>(D))) {
2024 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2025 << Attr.getName() << ExpectedTypeOrNamespace;
2026 return;
2027 }
2028
Benjamin Kramer70370212013-09-09 15:08:57 +00002029 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002030 StringRef TypeStr;
2031 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002032 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002033 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002034
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002035 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002036 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002037 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002038 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002039 return;
2040 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002041
2042 // Complain about attempts to use protected visibility on targets
2043 // (like Darwin) that don't support it.
2044 if (type == VisibilityAttr::Protected &&
2045 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2046 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2047 type = VisibilityAttr::Default;
2048 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002049
Michael Han99315932013-01-24 16:46:58 +00002050 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002051 clang::Attr *newAttr;
2052 if (isTypeVisibility) {
2053 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2054 (TypeVisibilityAttr::VisibilityType) type,
2055 Index);
2056 } else {
2057 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2058 }
2059 if (newAttr)
2060 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002061}
2062
Chandler Carruthedc2c642011-07-02 00:01:44 +00002063static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2064 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002065 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002066 if (!Attr.isArgIdent(0)) {
2067 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2068 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002069 return;
2070 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002071
Aaron Ballman682ee422013-09-11 19:47:58 +00002072 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2073 ObjCMethodFamilyAttr::FamilyKind F;
2074 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2075 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2076 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002077 return;
2078 }
2079
Alp Toker314cc812014-01-25 16:55:45 +00002080 if (F == ObjCMethodFamilyAttr::OMF_init &&
2081 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002082 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002083 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002084 // Ignore the attribute.
2085 return;
2086 }
2087
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002088 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002089 S.Context, F,
2090 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002091}
2092
Chandler Carruthedc2c642011-07-02 00:01:44 +00002093static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002094 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002095 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002096 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002097 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2098 return;
2099 }
2100 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002101 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2102 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002103 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002104 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2105 return;
2106 }
2107 }
2108 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002109 // It is okay to include this attribute on properties, e.g.:
2110 //
2111 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2112 //
2113 // In this case it follows tradition and suppresses an error in the above
2114 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002115 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002116 }
Michael Han99315932013-01-24 16:46:58 +00002117 D->addAttr(::new (S.Context)
2118 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2119 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002120}
2121
Chandler Carruthedc2c642011-07-02 00:01:44 +00002122static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002123 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002124 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002125 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002126 return;
2127 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002128
Aaron Ballman00e99962013-08-31 01:11:41 +00002129 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002130 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002131 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2132 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2133 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002134 return;
2135 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002136
Michael Han99315932013-01-24 16:46:58 +00002137 D->addAttr(::new (S.Context)
2138 BlocksAttr(Attr.getRange(), S.Context, type,
2139 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002140}
2141
Chandler Carruthedc2c642011-07-02 00:01:44 +00002142static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002143 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002144 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002145 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002146 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002147 if (E->isTypeDependent() || E->isValueDependent() ||
2148 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002150 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002151 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002152 return;
2153 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002154
John McCallb46f2872011-09-09 07:56:05 +00002155 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002156 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2157 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002158 return;
2159 }
John McCallb46f2872011-09-09 07:56:05 +00002160
2161 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002162 }
2163
Aaron Ballman18a78382013-11-21 00:28:23 +00002164 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002165 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002166 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002167 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002168 if (E->isTypeDependent() || E->isValueDependent() ||
2169 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002170 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002171 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002172 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002173 return;
2174 }
2175 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002176
John McCallb46f2872011-09-09 07:56:05 +00002177 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002178 // FIXME: This error message could be improved, it would be nice
2179 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002180 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2181 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002182 return;
2183 }
2184 }
2185
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002186 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002187 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002188 if (isa<FunctionNoProtoType>(FT)) {
2189 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2190 return;
2191 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002192
Chris Lattner9363e312009-03-17 23:03:47 +00002193 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002194 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002195 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002196 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002197 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002198 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002199 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002200 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002201 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002202 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2203 if (!BD->isVariadic()) {
2204 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2205 return;
2206 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002207 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002208 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002209 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002210 const FunctionType *FT = Ty->isFunctionPointerType()
2211 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002212 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002213 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002214 int m = Ty->isFunctionPointerType() ? 0 : 1;
2215 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002216 return;
2217 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002218 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002219 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002220 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002221 return;
2222 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002223 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002225 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002226 return;
2227 }
Michael Han99315932013-01-24 16:46:58 +00002228 D->addAttr(::new (S.Context)
2229 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2230 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002231}
2232
Chandler Carruthedc2c642011-07-02 00:01:44 +00002233static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002234 if (D->getFunctionType() &&
2235 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002236 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2237 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002238 return;
2239 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002240 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002241 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002242 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2243 << Attr.getName() << 1;
2244 return;
2245 }
2246
Michael Han99315932013-01-24 16:46:58 +00002247 D->addAttr(::new (S.Context)
2248 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2249 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002250}
2251
Chandler Carruthedc2c642011-07-02 00:01:44 +00002252static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002253 // weak_import only applies to variable & function declarations.
2254 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002255 if (!D->canBeWeakImported(isDef)) {
2256 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002257 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2258 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002259 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002260 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002261 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002262 // Nothing to warn about here.
2263 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002264 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002265 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002266
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002267 return;
2268 }
2269
Michael Han99315932013-01-24 16:46:58 +00002270 D->addAttr(::new (S.Context)
2271 WeakImportAttr(Attr.getRange(), S.Context,
2272 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002273}
2274
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002275// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002276template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002277static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002278 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002279 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002280 for (unsigned i = 0; i < 3; ++i) {
2281 const Expr *E = Attr.getArgAsExpr(i);
2282 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002283 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002284 if (WGSize[i] == 0) {
2285 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2286 << Attr.getName() << E->getSourceRange();
2287 return;
2288 }
2289 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002290
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002291 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2292 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2293 Existing->getYDim() == WGSize[1] &&
2294 Existing->getZDim() == WGSize[2]))
2295 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002296
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002297 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2298 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002299 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002300}
2301
Joey Goulyaba589c2013-03-08 09:42:32 +00002302static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002303 if (!Attr.hasParsedType()) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2305 << Attr.getName() << 1;
2306 return;
2307 }
2308
Craig Topperc3ec1492014-05-26 06:22:03 +00002309 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002310 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2311 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002312
2313 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2314 (ParmType->isBooleanType() ||
2315 !ParmType->isIntegralType(S.getASTContext()))) {
2316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2317 << ParmType;
2318 return;
2319 }
2320
Aaron Ballmana9e05402013-12-02 22:16:55 +00002321 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002322 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002323 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2324 return;
2325 }
2326 }
2327
2328 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002329 ParmTSI,
2330 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002331}
2332
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002333SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002334 StringRef Name,
2335 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002336 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2337 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002338 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002339 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2340 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002341 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002342 }
Michael Han99315932013-01-24 16:46:58 +00002343 return ::new (Context) SectionAttr(Range, Context, Name,
2344 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002345}
2346
Chandler Carruthedc2c642011-07-02 00:01:44 +00002347static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002348 // Make sure that there is a string literal as the sections's single
2349 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002350 StringRef Str;
2351 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002352 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002353 return;
Mike Stump11289f42009-09-09 15:08:12 +00002354
Chris Lattner30ba6742009-08-10 19:03:04 +00002355 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002356 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002357 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002358 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002359 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002360 return;
2361 }
Mike Stump11289f42009-09-09 15:08:12 +00002362
Michael Han99315932013-01-24 16:46:58 +00002363 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002364 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002365 if (NewAttr)
2366 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002367}
2368
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002369
Chandler Carruthedc2c642011-07-02 00:01:44 +00002370static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002371 VarDecl *VD = cast<VarDecl>(D);
2372 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002373 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002374 return;
2375 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002376
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002377 Expr *E = Attr.getArgAsExpr(0);
2378 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002379 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002380 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002381
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002382 // gcc only allows for simple identifiers. Since we support more than gcc, we
2383 // will warn the user.
2384 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2385 if (DRE->hasQualifier())
2386 S.Diag(Loc, diag::warn_cleanup_ext);
2387 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2388 NI = DRE->getNameInfo();
2389 if (!FD) {
2390 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2391 << NI.getName();
2392 return;
2393 }
2394 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2395 if (ULE->hasExplicitTemplateArgs())
2396 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002397 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2398 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002399 if (!FD) {
2400 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2401 << NI.getName();
2402 if (ULE->getType() == S.Context.OverloadTy)
2403 S.NoteAllOverloadCandidates(ULE);
2404 return;
2405 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002406 } else {
2407 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002408 return;
2409 }
2410
Anders Carlssond277d792009-01-31 01:16:18 +00002411 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002412 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2413 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002414 return;
2415 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002416
Anders Carlsson723f55d2009-02-07 23:16:50 +00002417 // We're currently more strict than GCC about what function types we accept.
2418 // If this ever proves to be a problem it should be easy to fix.
2419 QualType Ty = S.Context.getPointerType(VD->getType());
2420 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002421 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2422 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002423 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2424 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002425 return;
2426 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002427
Michael Han99315932013-01-24 16:46:58 +00002428 D->addAttr(::new (S.Context)
2429 CleanupAttr(Attr.getRange(), S.Context, FD,
2430 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002431}
2432
Mike Stumpd3bb5572009-07-24 19:02:52 +00002433/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002434/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002435static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002436 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002437 uint64_t Idx;
2438 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002439 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002440
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002441 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002442 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002443
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002444 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2445 if (not_nsstring_type &&
2446 !isCFStringType(Ty, S.Context) &&
2447 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002448 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002449 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002450 << (not_nsstring_type ? "a string type" : "an NSString")
2451 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002452 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002453 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002454 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002455 if (!isNSStringType(Ty, S.Context) &&
2456 !isCFStringType(Ty, S.Context) &&
2457 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002458 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002459 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002460 << (not_nsstring_type ? "string type" : "NSString")
2461 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002462 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002463 }
2464
Alp Toker601b22c2014-01-21 23:35:24 +00002465 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002466 // because that has corrected for the implicit this parameter, and is zero-
2467 // based. The attribute expects what the user wrote explicitly.
2468 llvm::APSInt Val;
2469 IdxExpr->EvaluateAsInt(Val, S.Context);
2470
Michael Han99315932013-01-24 16:46:58 +00002471 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002472 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002473 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002474}
2475
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002476enum FormatAttrKind {
2477 CFStringFormat,
2478 NSStringFormat,
2479 StrftimeFormat,
2480 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002481 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002482 InvalidFormat
2483};
2484
2485/// getFormatAttrKind - Map from format attribute names to supported format
2486/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002487static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002488 return llvm::StringSwitch<FormatAttrKind>(Format)
2489 // Check for formats that get handled specially.
2490 .Case("NSString", NSStringFormat)
2491 .Case("CFString", CFStringFormat)
2492 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002493
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002494 // Otherwise, check for supported formats.
2495 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2496 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2497 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002498
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002499 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2500 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002501}
2502
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002503/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002504/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002505static void handleInitPriorityAttr(Sema &S, Decl *D,
2506 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002507 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002508 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2509 return;
2510 }
2511
Aaron Ballman4a611152013-11-27 16:34:09 +00002512 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002513 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2514 Attr.setInvalid();
2515 return;
2516 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002517 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002518 if (S.Context.getAsArrayType(T))
2519 T = S.Context.getBaseElementType(T);
2520 if (!T->getAs<RecordType>()) {
2521 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2522 Attr.setInvalid();
2523 return;
2524 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002525
2526 Expr *E = Attr.getArgAsExpr(0);
2527 uint32_t prioritynum;
2528 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002529 Attr.setInvalid();
2530 return;
2531 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002532
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002533 if (prioritynum < 101 || prioritynum > 65535) {
2534 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002535 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002536 Attr.setInvalid();
2537 return;
2538 }
Michael Han99315932013-01-24 16:46:58 +00002539 D->addAttr(::new (S.Context)
2540 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2541 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002542}
2543
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002544FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2545 IdentifierInfo *Format, int FormatIdx,
2546 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002547 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002548 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002549 for (auto *F : D->specific_attrs<FormatAttr>()) {
2550 if (F->getType() == Format &&
2551 F->getFormatIdx() == FormatIdx &&
2552 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002553 // If we don't have a valid location for this attribute, adopt the
2554 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002555 if (F->getLocation().isInvalid())
2556 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002557 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002558 }
2559 }
2560
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002561 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2562 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002563}
2564
Mike Stumpd3bb5572009-07-24 19:02:52 +00002565/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002566/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002567static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002568 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002570 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002571 return;
2572 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002573
Chandler Carruth743682b2010-11-16 08:35:43 +00002574 // In C++ the implicit 'this' function parameter also counts, and they are
2575 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002576 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002577 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002578
Aaron Ballman00e99962013-08-31 01:11:41 +00002579 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2580 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002581
2582 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002583 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002584 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002585 // If we've modified the string name, we need a new identifier for it.
2586 II = &S.Context.Idents.get(Format);
2587 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002588
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002589 // Check for supported formats.
2590 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002591
2592 if (Kind == IgnoredFormat)
2593 return;
2594
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002595 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002596 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002597 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598 return;
2599 }
2600
2601 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002602 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002603 uint32_t Idx;
2604 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002605 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002607 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002608 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002609 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610 return;
2611 }
2612
2613 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002614 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002615
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002616 if (HasImplicitThisParam) {
2617 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002618 S.Diag(Attr.getLoc(),
2619 diag::err_format_attribute_implicit_this_format_string)
2620 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002621 return;
2622 }
2623 ArgIdx--;
2624 }
Mike Stump11289f42009-09-09 15:08:12 +00002625
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002626 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002627 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002629 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002630 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002631 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002632 << "a CFString" << IdxExpr->getSourceRange()
2633 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002634 return;
2635 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002636 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002637 // FIXME: do we need to check if the type is NSString*? What are the
2638 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002639 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002640 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002641 << "an NSString" << IdxExpr->getSourceRange()
2642 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002643 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002644 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002645 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002646 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002647 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002648 << "a string type" << IdxExpr->getSourceRange()
2649 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650 return;
2651 }
2652
2653 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002654 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002655 uint32_t FirstArg;
2656 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002658
2659 // check if the function is variadic if the 3rd argument non-zero
2660 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002661 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662 ++NumArgs; // +1 for ...
2663 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002664 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002665 return;
2666 }
2667 }
2668
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002669 // strftime requires FirstArg to be 0 because it doesn't read from any
2670 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002671 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002673 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2674 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675 return;
2676 }
2677 // if 0 it disables parameter checking (to use with e.g. va_list)
2678 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002679 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002680 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002681 return;
2682 }
2683
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002684 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002685 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002686 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002687 if (NewAttr)
2688 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002689}
2690
Chandler Carruthedc2c642011-07-02 00:01:44 +00002691static void handleTransparentUnionAttr(Sema &S, Decl *D,
2692 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002693 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002694 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002695 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002696 if (TD && TD->getUnderlyingType()->isUnionType())
2697 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2698 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002699 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002700
2701 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002702 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002703 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704 return;
2705 }
2706
John McCallf937c022011-10-07 06:10:15 +00002707 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002708 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002709 diag::warn_transparent_union_attribute_not_definition);
2710 return;
2711 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002712
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002713 RecordDecl::field_iterator Field = RD->field_begin(),
2714 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002715 if (Field == FieldEnd) {
2716 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2717 return;
2718 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002719
David Blaikie40ed2972012-06-06 20:45:41 +00002720 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002721 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002722 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002723 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002724 diag::warn_transparent_union_attribute_floating)
2725 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002726 return;
2727 }
2728
2729 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2730 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2731 for (; Field != FieldEnd; ++Field) {
2732 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002733 // FIXME: this isn't fully correct; we also need to test whether the
2734 // members of the union would all have the same calling convention as the
2735 // first member of the union. Checking just the size and alignment isn't
2736 // sufficient (consider structs passed on the stack instead of in registers
2737 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002738 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002739 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002740 // Warn if we drop the attribute.
2741 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002742 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002743 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002744 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002745 diag::warn_transparent_union_attribute_field_size_align)
2746 << isSize << Field->getDeclName() << FieldBits;
2747 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002748 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002749 diag::note_transparent_union_first_field_size_align)
2750 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002751 return;
2752 }
2753 }
2754
Michael Han99315932013-01-24 16:46:58 +00002755 RD->addAttr(::new (S.Context)
2756 TransparentUnionAttr(Attr.getRange(), S.Context,
2757 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002758}
2759
Chandler Carruthedc2c642011-07-02 00:01:44 +00002760static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002761 // Make sure that there is a string literal as the annotation's single
2762 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002763 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002764 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002766
2767 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002768 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2769 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002770 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002771 }
Michael Han99315932013-01-24 16:46:58 +00002772
2773 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002774 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002775 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002776}
2777
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002778static void handleAlignValueAttr(Sema &S, Decl *D,
2779 const AttributeList &Attr) {
2780 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2781 Attr.getAttributeSpellingListIndex());
2782}
2783
2784void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2785 unsigned SpellingListIndex) {
2786 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2787 SourceLocation AttrLoc = AttrRange.getBegin();
2788
2789 QualType T;
2790 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2791 T = TD->getUnderlyingType();
2792 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2793 T = VD->getType();
2794 else
2795 llvm_unreachable("Unknown decl type for align_value");
2796
2797 if (!T->isDependentType() && !T->isAnyPointerType() &&
2798 !T->isReferenceType() && !T->isMemberPointerType()) {
2799 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2800 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2801 return;
2802 }
2803
2804 if (!E->isValueDependent()) {
2805 llvm::APSInt Alignment(32);
2806 ExprResult ICE
2807 = VerifyIntegerConstantExpression(E, &Alignment,
2808 diag::err_align_value_attribute_argument_not_int,
2809 /*AllowFold*/ false);
2810 if (ICE.isInvalid())
2811 return;
2812
2813 if (!Alignment.isPowerOf2()) {
2814 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2815 << E->getSourceRange();
2816 return;
2817 }
2818
2819 D->addAttr(::new (Context)
2820 AlignValueAttr(AttrRange, Context, ICE.get(),
2821 SpellingListIndex));
2822 return;
2823 }
2824
2825 // Save dependent expressions in the AST to be instantiated.
2826 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2827 return;
2828}
2829
Chandler Carruthedc2c642011-07-02 00:01:44 +00002830static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002831 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002832 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2834 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002835 return;
2836 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002837
Richard Smith848e1f12013-02-01 08:12:08 +00002838 if (Attr.getNumArgs() == 0) {
2839 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002840 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002841 return;
2842 }
2843
Aaron Ballman00e99962013-08-31 01:11:41 +00002844 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002845 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2846 S.Diag(Attr.getEllipsisLoc(),
2847 diag::err_pack_expansion_without_parameter_packs);
2848 return;
2849 }
2850
2851 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2852 return;
2853
2854 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2855 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002856}
2857
2858void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002859 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002860 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2861 SourceLocation AttrLoc = AttrRange.getBegin();
2862
Richard Smith1dba27c2013-01-29 09:02:09 +00002863 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002864 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002865 // C++11 [dcl.align]p1:
2866 // An alignment-specifier may be applied to a variable or to a class
2867 // data member, but it shall not be applied to a bit-field, a function
2868 // parameter, the formal parameter of a catch clause, or a variable
2869 // declared with the register storage class specifier. An
2870 // alignment-specifier may also be applied to the declaration of a class
2871 // or enumeration type.
2872 // C11 6.7.5/2:
2873 // An alignment attribute shall not be specified in a declaration of
2874 // a typedef, or a bit-field, or a function, or a parameter, or an
2875 // object declared with the register storage-class specifier.
2876 int DiagKind = -1;
2877 if (isa<ParmVarDecl>(D)) {
2878 DiagKind = 0;
2879 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2880 if (VD->getStorageClass() == SC_Register)
2881 DiagKind = 1;
2882 if (VD->isExceptionVariable())
2883 DiagKind = 2;
2884 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2885 if (FD->isBitField())
2886 DiagKind = 3;
2887 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002888 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002889 << (TmpAttr.isC11() ? ExpectedVariableOrField
2890 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002891 return;
2892 }
2893 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002894 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002895 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002896 return;
2897 }
2898 }
2899
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002900 if (E->isTypeDependent() || E->isValueDependent()) {
2901 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002902 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2903 AA->setPackExpansion(IsPackExpansion);
2904 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002905 return;
2906 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002907
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002908 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002909 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002910 ExprResult ICE
2911 = VerifyIntegerConstantExpression(E, &Alignment,
2912 diag::err_aligned_attribute_argument_not_int,
2913 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002914 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002915 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002916
2917 // C++11 [dcl.align]p2:
2918 // -- if the constant expression evaluates to zero, the alignment
2919 // specifier shall have no effect
2920 // C11 6.7.5p6:
2921 // An alignment specification of zero has no effect.
2922 if (!(TmpAttr.isAlignas() && !Alignment) &&
2923 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Hal Finkelbcc06082014-09-07 22:58:14 +00002924 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002925 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002926 return;
2927 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002928
David Majnemerabecae72014-02-12 20:36:10 +00002929 // Alignment calculations can wrap around if it's greater than 2**28.
2930 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2931 if (Alignment.getZExtValue() > MaxValidAlignment) {
2932 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2933 << E->getSourceRange();
2934 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002935 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002936
Richard Smith44c247f2013-02-22 08:32:16 +00002937 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002938 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002939 AA->setPackExpansion(IsPackExpansion);
2940 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002941}
2942
Michael Hanaf02bbe2013-02-01 01:19:17 +00002943void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002944 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002945 // FIXME: Cache the number on the Attr object if non-dependent?
2946 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002947 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2948 SpellingListIndex);
2949 AA->setPackExpansion(IsPackExpansion);
2950 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002951}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002952
Richard Smith848e1f12013-02-01 08:12:08 +00002953void Sema::CheckAlignasUnderalignment(Decl *D) {
2954 assert(D->hasAttrs() && "no attributes on decl");
2955
2956 QualType Ty;
2957 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2958 Ty = VD->getType();
2959 else
2960 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002961 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002962 return;
2963
2964 // C++11 [dcl.align]p5, C11 6.7.5/4:
2965 // The combined effect of all alignment attributes in a declaration shall
2966 // not specify an alignment that is less strict than the alignment that
2967 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002968 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002969 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002970 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002971 if (I->isAlignmentDependent())
2972 return;
2973 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002974 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002975 Align = std::max(Align, I->getAlignment(Context));
2976 }
2977
2978 if (AlignasAttr && Align) {
2979 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2980 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2981 if (NaturalAlign > RequestedAlign)
2982 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2983 << Ty << (unsigned)NaturalAlign.getQuantity();
2984 }
2985}
2986
David Majnemer2c4e00a2014-01-29 22:07:36 +00002987bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002988 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002989 MSInheritanceAttr::Spelling SemanticSpelling) {
2990 assert(RD->hasDefinition() && "RD has no definition!");
2991
David Majnemer98c9ee22014-02-07 00:43:07 +00002992 // We may not have seen base specifiers or any virtual methods yet. We will
2993 // have to wait until the record is defined to catch any mismatches.
2994 if (!RD->getDefinition()->isCompleteDefinition())
2995 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002996
David Majnemer98c9ee22014-02-07 00:43:07 +00002997 // The unspecified model never matches what a definition could need.
2998 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2999 return false;
3000
David Majnemer4bb09802014-02-10 19:50:15 +00003001 if (BestCase) {
3002 if (RD->calculateInheritanceModel() == SemanticSpelling)
3003 return false;
3004 } else {
3005 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3006 return false;
3007 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003008
3009 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3010 << 0 /*definition*/;
3011 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3012 << RD->getNameAsString();
3013 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003014}
3015
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003016/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003017/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003018///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003019/// Despite what would be logical, the mode attribute is a decl attribute, not a
3020/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3021/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003022static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003023 // This attribute isn't documented, but glibc uses it. It changes
3024 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003025 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3027 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003028 return;
3029 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003030
Aaron Ballman00e99962013-08-31 01:11:41 +00003031 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3032 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003033
3034 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003035 if (Str.startswith("__") && Str.endswith("__"))
3036 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003037
3038 unsigned DestWidth = 0;
3039 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003040 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003041 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003042 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003043 switch (Str[0]) {
3044 case 'Q': DestWidth = 8; break;
3045 case 'H': DestWidth = 16; break;
3046 case 'S': DestWidth = 32; break;
3047 case 'D': DestWidth = 64; break;
3048 case 'X': DestWidth = 96; break;
3049 case 'T': DestWidth = 128; break;
3050 }
3051 if (Str[1] == 'F') {
3052 IntegerMode = false;
3053 } else if (Str[1] == 'C') {
3054 IntegerMode = false;
3055 ComplexMode = true;
3056 } else if (Str[1] != 'I') {
3057 DestWidth = 0;
3058 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003059 break;
3060 case 4:
3061 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3062 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003063 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003064 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003065 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003066 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003067 break;
3068 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003069 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003070 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003071 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003072 case 11:
3073 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003074 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003075 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003076 }
3077
3078 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003079 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003080 OldTy = TD->getUnderlyingType();
3081 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3082 OldTy = VD->getType();
3083 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003084 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003085 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003086 return;
3087 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003088
John McCall9dd450b2009-09-21 23:43:11 +00003089 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003090 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3091 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003092 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003093 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3094 } else if (ComplexMode) {
3095 if (!OldTy->isComplexType())
3096 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3097 } else {
3098 if (!OldTy->isFloatingType())
3099 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3100 }
3101
Mike Stump87c57ac2009-05-16 07:39:55 +00003102 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3103 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003104 // FIXME: Make sure floating-point mappings are accurate
3105 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003106 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003107 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003108 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003109 }
3110
3111 QualType NewTy;
3112
3113 if (IntegerMode)
3114 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3115 OldTy->isSignedIntegerType());
3116 else
3117 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3118
3119 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003120 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003121 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003122 }
3123
Eli Friedman4735374e2009-03-03 06:41:03 +00003124 if (ComplexMode) {
3125 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003126 }
3127
3128 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003129 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3130 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3131 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003132 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003133
3134 D->addAttr(::new (S.Context)
3135 ModeAttr(Attr.getRange(), S.Context, Name,
3136 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003137}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003138
Chandler Carruthedc2c642011-07-02 00:01:44 +00003139static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003140 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3141 if (!VD->hasGlobalStorage())
3142 S.Diag(Attr.getLoc(),
3143 diag::warn_attribute_requires_functions_or_static_globals)
3144 << Attr.getName();
3145 } else if (!isFunctionOrMethod(D)) {
3146 S.Diag(Attr.getLoc(),
3147 diag::warn_attribute_requires_functions_or_static_globals)
3148 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003149 return;
3150 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003151
Michael Han99315932013-01-24 16:46:58 +00003152 D->addAttr(::new (S.Context)
3153 NoDebugAttr(Attr.getRange(), S.Context,
3154 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003155}
3156
Paul Robinson30e41fb2014-12-15 18:57:28 +00003157AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003158 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003159 unsigned AttrSpellingListIndex) {
3160 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003161 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003162 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3163 return nullptr;
3164 }
3165
3166 if (D->hasAttr<AlwaysInlineAttr>())
3167 return nullptr;
3168
3169 return ::new (Context) AlwaysInlineAttr(Range, Context,
3170 AttrSpellingListIndex);
3171}
3172
3173MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3174 unsigned AttrSpellingListIndex) {
3175 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3176 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3177 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3178 return nullptr;
3179 }
3180
3181 if (D->hasAttr<MinSizeAttr>())
3182 return nullptr;
3183
3184 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3185}
3186
3187OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3188 unsigned AttrSpellingListIndex) {
3189 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3190 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3191 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3192 D->dropAttr<AlwaysInlineAttr>();
3193 }
3194 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3195 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3196 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3197 D->dropAttr<MinSizeAttr>();
3198 }
3199
3200 if (D->hasAttr<OptimizeNoneAttr>())
3201 return nullptr;
3202
3203 return ::new (Context) OptimizeNoneAttr(Range, Context,
3204 AttrSpellingListIndex);
3205}
3206
Paul Robinsonf0674352014-03-31 22:29:15 +00003207static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3208 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003209 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3210 D, Attr.getRange(), Attr.getName(),
3211 Attr.getAttributeSpellingListIndex()))
3212 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003213}
3214
Paul Robinson080b1f32015-01-13 18:34:56 +00003215static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3216 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3217 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3218 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003219}
3220
Paul Robinsonf0674352014-03-31 22:29:15 +00003221static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3222 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003223 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3224 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3225 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003226}
3227
Chandler Carruthedc2c642011-07-02 00:01:44 +00003228static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003229 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003230 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003231 SourceRange RTRange = FD->getReturnTypeSourceRange();
3232 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003233 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003234 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3235 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003236 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003237 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003238
Aaron Ballman3aff6332013-12-02 19:30:36 +00003239 D->addAttr(::new (S.Context)
3240 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003241 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003242}
3243
Chandler Carruthedc2c642011-07-02 00:01:44 +00003244static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003245 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003246 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003247 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003248 return;
3249 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003250
Michael Han99315932013-01-24 16:46:58 +00003251 D->addAttr(::new (S.Context)
3252 GNUInlineAttr(Attr.getRange(), S.Context,
3253 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003254}
3255
Chandler Carruthedc2c642011-07-02 00:01:44 +00003256static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003257 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003258
Aaron Ballman02df2e02012-12-09 17:45:41 +00003259 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003260 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003261 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3262 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003263 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003264 return;
3265
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003266 if (!isa<ObjCMethodDecl>(D)) {
3267 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3268 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003269 return;
3270 }
3271
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003272 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003273 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003274 D->addAttr(::new (S.Context)
3275 FastCallAttr(Attr.getRange(), S.Context,
3276 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003277 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003278 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003279 D->addAttr(::new (S.Context)
3280 StdCallAttr(Attr.getRange(), S.Context,
3281 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003282 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003283 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003284 D->addAttr(::new (S.Context)
3285 ThisCallAttr(Attr.getRange(), S.Context,
3286 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003287 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003288 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003289 D->addAttr(::new (S.Context)
3290 CDeclAttr(Attr.getRange(), S.Context,
3291 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003292 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003293 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003294 D->addAttr(::new (S.Context)
3295 PascalAttr(Attr.getRange(), S.Context,
3296 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003297 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003298 case AttributeList::AT_VectorCall:
3299 D->addAttr(::new (S.Context)
3300 VectorCallAttr(Attr.getRange(), S.Context,
3301 Attr.getAttributeSpellingListIndex()));
3302 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003303 case AttributeList::AT_MSABI:
3304 D->addAttr(::new (S.Context)
3305 MSABIAttr(Attr.getRange(), S.Context,
3306 Attr.getAttributeSpellingListIndex()));
3307 return;
3308 case AttributeList::AT_SysVABI:
3309 D->addAttr(::new (S.Context)
3310 SysVABIAttr(Attr.getRange(), S.Context,
3311 Attr.getAttributeSpellingListIndex()));
3312 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003313 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003314 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003315 switch (CC) {
3316 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003317 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003318 break;
3319 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003320 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003321 break;
3322 default:
3323 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003324 }
3325
Michael Han99315932013-01-24 16:46:58 +00003326 D->addAttr(::new (S.Context)
3327 PcsAttr(Attr.getRange(), S.Context, PCS,
3328 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003329 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003330 }
Derek Schuffa2020962012-10-16 22:30:41 +00003331 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003332 D->addAttr(::new (S.Context)
3333 PnaclCallAttr(Attr.getRange(), S.Context,
3334 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003335 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003336 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003337 D->addAttr(::new (S.Context)
3338 IntelOclBiccAttr(Attr.getRange(), S.Context,
3339 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003340 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003341
Abramo Bagnara50099372010-04-30 13:10:51 +00003342 default:
3343 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003344 }
3345}
3346
Aaron Ballman02df2e02012-12-09 17:45:41 +00003347bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3348 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003349 if (attr.isInvalid())
3350 return true;
3351
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003352 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003353 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003354 attr.setInvalid();
3355 return true;
3356 }
3357
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003358 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003359 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003360 case AttributeList::AT_CDecl: CC = CC_C; break;
3361 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3362 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3363 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3364 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003365 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003366 case AttributeList::AT_MSABI:
3367 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3368 CC_X86_64Win64;
3369 break;
3370 case AttributeList::AT_SysVABI:
3371 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3372 CC_C;
3373 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003374 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003375 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003376 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003377 attr.setInvalid();
3378 return true;
3379 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003380 if (StrRef == "aapcs") {
3381 CC = CC_AAPCS;
3382 break;
3383 } else if (StrRef == "aapcs-vfp") {
3384 CC = CC_AAPCS_VFP;
3385 break;
3386 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003387
3388 attr.setInvalid();
3389 Diag(attr.getLoc(), diag::err_invalid_pcs);
3390 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003391 }
Derek Schuffa2020962012-10-16 22:30:41 +00003392 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003393 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003394 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003395 }
3396
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003397 const TargetInfo &TI = Context.getTargetInfo();
3398 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3399 if (A == TargetInfo::CCCR_Warning) {
3400 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003401
3402 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3403 if (FD)
3404 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3405 TargetInfo::CCMT_NonMember;
3406 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003407 }
3408
John McCall3882ace2011-01-05 12:14:39 +00003409 return false;
3410}
3411
John McCall3882ace2011-01-05 12:14:39 +00003412/// Checks a regparm attribute, returning true if it is ill-formed and
3413/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003414bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3415 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003416 return true;
3417
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003418 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003419 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003420 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003421 }
Eli Friedman7044b762009-03-27 21:06:47 +00003422
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003423 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003424 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003425 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003426 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003427 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003428 }
3429
Douglas Gregore8bbc122011-09-02 00:18:52 +00003430 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003432 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003433 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003434 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003435 }
3436
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003437 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003438 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003439 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003440 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003441 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003442 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003443 }
3444
John McCall3882ace2011-01-05 12:14:39 +00003445 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003446}
3447
Aaron Ballman66039932013-12-19 00:41:31 +00003448static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3449 const AttributeList &Attr) {
Aaron Ballman66039932013-12-19 00:41:31 +00003450 uint32_t MaxThreads, MinBlocks = 0;
3451 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3452 return;
3453 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3454 Attr.getArgAsExpr(1),
3455 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003456 return;
3457
3458 D->addAttr(::new (S.Context)
3459 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3460 MaxThreads, MinBlocks,
3461 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003462}
3463
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003464static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3465 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003466 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003468 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003469 return;
3470 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003471
3472 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003473 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003474
Aaron Ballman00e99962013-08-31 01:11:41 +00003475 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003476
3477 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3479 << Attr.getName() << ExpectedFunctionOrMethod;
3480 return;
3481 }
3482
3483 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003484 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3485 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003486 return;
3487
3488 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003489 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3490 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003491 return;
3492
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003493 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003494 if (IsPointer) {
3495 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003496 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003497 if (!BufferTy->isPointerType()) {
3498 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003499 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003500 }
3501 }
3502
Michael Han99315932013-01-24 16:46:58 +00003503 D->addAttr(::new (S.Context)
3504 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3505 ArgumentIdx, TypeTagIdx, IsPointer,
3506 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003507}
3508
3509static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3510 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003511 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003512 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003513 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003514 return;
3515 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003516
3517 if (!checkAttributeNumArgs(S, Attr, 1))
3518 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003519
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003520 if (!isa<VarDecl>(D)) {
3521 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3522 << Attr.getName() << ExpectedVariable;
3523 return;
3524 }
3525
Aaron Ballman00e99962013-08-31 01:11:41 +00003526 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003527 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003528 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3529 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003530
Michael Han99315932013-01-24 16:46:58 +00003531 D->addAttr(::new (S.Context)
3532 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003533 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003534 Attr.getLayoutCompatible(),
3535 Attr.getMustBeNull(),
3536 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003537}
3538
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003539//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003540// Checker-specific attribute handlers.
3541//===----------------------------------------------------------------------===//
3542
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003543static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003544 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003545 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003546}
3547
John McCalled433932011-01-25 03:31:58 +00003548static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003549 return type->isDependentType() ||
3550 type->isObjCObjectPointerType() ||
3551 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003552}
3553static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003554 return type->isDependentType() ||
3555 type->isPointerType() ||
3556 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003557}
3558
Chandler Carruthedc2c642011-07-02 00:01:44 +00003559static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003560 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003561 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003562
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003563 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003564 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3565 cf = false;
3566 } else {
3567 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3568 cf = true;
3569 }
3570
3571 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003572 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003573 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003574 return;
3575 }
3576
3577 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003578 param->addAttr(::new (S.Context)
3579 CFConsumedAttr(Attr.getRange(), S.Context,
3580 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003581 else
Michael Han99315932013-01-24 16:46:58 +00003582 param->addAttr(::new (S.Context)
3583 NSConsumedAttr(Attr.getRange(), S.Context,
3584 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003585}
3586
Chandler Carruthedc2c642011-07-02 00:01:44 +00003587static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3588 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003589
John McCalled433932011-01-25 03:31:58 +00003590 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003591
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003592 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003593 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003594 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003595 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003596 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003597 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3598 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003599 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003600 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003601 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003602 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003603 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003604 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003605 return;
3606 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003607
John McCalled433932011-01-25 03:31:58 +00003608 bool typeOK;
3609 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003610 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003611 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003612 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003613 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003614 cf = false;
3615 break;
3616
3617 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003618 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003619 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3620 cf = false;
3621 break;
3622
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003623 case AttributeList::AT_CFReturnsRetained:
3624 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003625 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3626 cf = true;
3627 break;
3628 }
3629
3630 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003631 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003632 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003633 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003634 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003635
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003636 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003637 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003638 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003639 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00003640 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
3641 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003642 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003643 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003644 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
3645 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003646 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003647 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003648 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
3649 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003650 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003651 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003652 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
3653 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003654 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003655 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003656 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
3657 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003658 return;
3659 };
3660}
3661
John McCallcf166702011-07-22 08:53:00 +00003662static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3663 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003664 const int EP_ObjCMethod = 1;
3665 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003666
John McCallcf166702011-07-22 08:53:00 +00003667 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003668 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003669 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003670 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003671 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003672 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003673
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003674 if (!resultType->isReferenceType() &&
3675 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003676 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003677 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003678 << attr.getName()
3679 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003680 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003681
3682 // Drop the attribute.
3683 return;
3684 }
3685
Nico Weber462fd1e2015-01-07 23:50:05 +00003686 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
3687 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003688}
3689
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003690static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3691 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003692 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003693
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003694 DeclContext *DC = method->getDeclContext();
3695 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3696 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3697 << attr.getName() << 0;
3698 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3699 return;
3700 }
3701 if (method->getMethodFamily() == OMF_dealloc) {
3702 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3703 << attr.getName() << 1;
3704 return;
3705 }
3706
Michael Han99315932013-01-24 16:46:58 +00003707 method->addAttr(::new (S.Context)
3708 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3709 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003710}
3711
Aaron Ballmanfb763042013-12-02 18:05:46 +00003712static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3713 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003714 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003715 return;
John McCall32f5fe12011-09-30 05:12:12 +00003716
Aaron Ballmanfb763042013-12-02 18:05:46 +00003717 D->addAttr(::new (S.Context)
3718 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3719 Attr.getAttributeSpellingListIndex()));
3720}
3721
3722static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3723 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003724 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003725 return;
3726
3727 D->addAttr(::new (S.Context)
3728 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3729 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003730}
3731
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003732static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3733 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003734 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003735
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003736 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003737 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003738 return;
3739 }
3740
3741 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003742 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003743 Attr.getAttributeSpellingListIndex()));
3744}
3745
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003746static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3747 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003748 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3749
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003750 if (!Parm) {
3751 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3752 return;
3753 }
3754
3755 D->addAttr(::new (S.Context)
3756 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3757 Attr.getAttributeSpellingListIndex()));
3758}
3759
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003760static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3761 const AttributeList &Attr) {
3762 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003763 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003764 if (!RelatedClass) {
3765 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3766 return;
3767 }
3768 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003769 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003770 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003771 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003772 D->addAttr(::new (S.Context)
3773 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3774 ClassMethod, InstanceMethod,
3775 Attr.getAttributeSpellingListIndex()));
3776}
3777
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003778static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3779 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003780 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00003781 if (ObjCCategoryDecl *CatDecl =
3782 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003783 IFace = CatDecl->getClassInterface();
3784 else
3785 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003786 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003787 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003788 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3789 Attr.getAttributeSpellingListIndex()));
3790}
3791
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003792static void handleObjCRuntimeName(Sema &S, Decl *D,
3793 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003794 StringRef MetaDataName;
3795 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3796 return;
3797 D->addAttr(::new (S.Context)
3798 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3799 MetaDataName,
3800 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003801}
3802
Chandler Carruthedc2c642011-07-02 00:01:44 +00003803static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3804 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003805 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003806
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003807 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003808 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003809}
3810
Chandler Carruthedc2c642011-07-02 00:01:44 +00003811static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3812 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003813 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003814 QualType type = vd->getType();
3815
3816 if (!type->isDependentType() &&
3817 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003818 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003819 << type;
3820 return;
3821 }
3822
3823 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3824
3825 // If we have no lifetime yet, check the lifetime we're presumably
3826 // going to infer.
3827 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3828 lifetime = type->getObjCARCImplicitLifetime();
3829
3830 switch (lifetime) {
3831 case Qualifiers::OCL_None:
3832 assert(type->isDependentType() &&
3833 "didn't infer lifetime for non-dependent type?");
3834 break;
3835
3836 case Qualifiers::OCL_Weak: // meaningful
3837 case Qualifiers::OCL_Strong: // meaningful
3838 break;
3839
3840 case Qualifiers::OCL_ExplicitNone:
3841 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003842 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003843 << (lifetime == Qualifiers::OCL_Autoreleasing);
3844 break;
3845 }
3846
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003847 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003848 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003850}
3851
Francois Picheta83957a2010-12-19 06:50:37 +00003852//===----------------------------------------------------------------------===//
3853// Microsoft specific attribute handlers.
3854//===----------------------------------------------------------------------===//
3855
Chandler Carruthedc2c642011-07-02 00:01:44 +00003856static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003857 if (!S.LangOpts.CPlusPlus) {
3858 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3859 << Attr.getName() << AttributeLangSupport::C;
3860 return;
3861 }
3862
Aaron Ballman60e705e2013-11-24 20:58:02 +00003863 if (!isa<CXXRecordDecl>(D)) {
3864 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3865 << Attr.getName() << ExpectedClass;
3866 return;
3867 }
3868
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003869 StringRef StrRef;
3870 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003871 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003872 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003873
David Majnemer89085342013-08-09 08:56:20 +00003874 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3875 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003876 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3877 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003878
Reid Kleckner140c4a72013-05-17 14:04:52 +00003879 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003880 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003881 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003882 return;
3883 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003884
David Majnemer89085342013-08-09 08:56:20 +00003885 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003886 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003887 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003888 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003889 return;
3890 }
David Majnemer89085342013-08-09 08:56:20 +00003891 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003892 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003893 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003894 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003895 }
Francois Picheta83957a2010-12-19 06:50:37 +00003896
David Majnemer89085342013-08-09 08:56:20 +00003897 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3898 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003899}
3900
David Majnemer2c4e00a2014-01-29 22:07:36 +00003901static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3902 if (!S.LangOpts.CPlusPlus) {
3903 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3904 << Attr.getName() << AttributeLangSupport::C;
3905 return;
3906 }
3907 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003908 D, Attr.getRange(), /*BestCase=*/true,
3909 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003910 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3911 if (IA)
3912 D->addAttr(IA);
3913}
3914
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003915static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3916 const AttributeList &Attr) {
3917 VarDecl *VD = cast<VarDecl>(D);
3918 if (!S.Context.getTargetInfo().isTLSSupported()) {
3919 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3920 return;
3921 }
3922 if (VD->getTSCSpec() != TSCS_unspecified) {
3923 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3924 return;
3925 }
3926 if (VD->hasLocalStorage()) {
3927 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3928 return;
3929 }
3930 VD->addAttr(::new (S.Context) ThreadAttr(
3931 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3932}
3933
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003934static void handleARMInterruptAttr(Sema &S, Decl *D,
3935 const AttributeList &Attr) {
3936 // Check the attribute arguments.
3937 if (Attr.getNumArgs() > 1) {
3938 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3939 << Attr.getName() << 1;
3940 return;
3941 }
3942
3943 StringRef Str;
3944 SourceLocation ArgLoc;
3945
3946 if (Attr.getNumArgs() == 0)
3947 Str = "";
3948 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3949 return;
3950
3951 ARMInterruptAttr::InterruptType Kind;
3952 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3953 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3954 << Attr.getName() << Str << ArgLoc;
3955 return;
3956 }
3957
3958 unsigned Index = Attr.getAttributeSpellingListIndex();
3959 D->addAttr(::new (S.Context)
3960 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3961}
3962
3963static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3964 const AttributeList &Attr) {
3965 if (!checkAttributeNumArgs(S, Attr, 1))
3966 return;
3967
3968 if (!Attr.isArgExpr(0)) {
3969 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3970 << AANT_ArgumentIntegerConstant;
3971 return;
3972 }
3973
3974 // FIXME: Check for decl - it should be void ()(void).
3975
3976 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3977 llvm::APSInt NumParams(32);
3978 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3979 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3980 << Attr.getName() << AANT_ArgumentIntegerConstant
3981 << NumParamsExpr->getSourceRange();
3982 return;
3983 }
3984
3985 unsigned Num = NumParams.getLimitedValue(255);
3986 if ((Num & 1) || Num > 30) {
3987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3988 << Attr.getName() << (int)NumParams.getSExtValue()
3989 << NumParamsExpr->getSourceRange();
3990 return;
3991 }
3992
Aaron Ballman36a53502014-01-16 13:03:14 +00003993 D->addAttr(::new (S.Context)
3994 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3995 Attr.getAttributeSpellingListIndex()));
3996 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003997}
3998
3999static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4000 // Dispatch the interrupt attribute based on the current target.
4001 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4002 handleMSP430InterruptAttr(S, D, Attr);
4003 else
4004 handleARMInterruptAttr(S, D, Attr);
4005}
4006
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004007static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4008 const AttributeList &Attr) {
4009 uint32_t NumRegs;
4010 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4011 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4012 return;
4013
4014 D->addAttr(::new (S.Context)
4015 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4016 NumRegs,
4017 Attr.getAttributeSpellingListIndex()));
4018}
4019
4020static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4021 const AttributeList &Attr) {
4022 uint32_t NumRegs;
4023 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4024 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4025 return;
4026
4027 D->addAttr(::new (S.Context)
4028 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4029 NumRegs,
4030 Attr.getAttributeSpellingListIndex()));
4031}
4032
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004033static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4034 const AttributeList& Attr) {
4035 // If we try to apply it to a function pointer, don't warn, but don't
4036 // do anything, either. It doesn't matter anyway, because there's nothing
4037 // special about calling a force_align_arg_pointer function.
4038 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4039 if (VD && VD->getType()->isFunctionPointerType())
4040 return;
4041 // Also don't warn on function pointer typedefs.
4042 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4043 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4044 TD->getUnderlyingType()->isFunctionType()))
4045 return;
4046 // Attribute can only be applied to function types.
4047 if (!isa<FunctionDecl>(D)) {
4048 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4049 << Attr.getName() << /* function */0;
4050 return;
4051 }
4052
Aaron Ballman36a53502014-01-16 13:03:14 +00004053 D->addAttr(::new (S.Context)
4054 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4055 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004056}
4057
4058DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4059 unsigned AttrSpellingListIndex) {
4060 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004061 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004062 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004063 }
4064
4065 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004066 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004067
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004068 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004069}
4070
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004071DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4072 unsigned AttrSpellingListIndex) {
4073 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004074 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004075 D->dropAttr<DLLImportAttr>();
4076 }
4077
4078 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004079 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004080
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004081 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004082}
4083
Hans Wennborge82f19c2014-06-24 23:57:05 +00004084static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004085 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4086 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4087 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4088 << A.getName();
4089 return;
4090 }
4091
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004092 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4093 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4094 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4095 // MinGW doesn't allow dllimport on inline functions.
4096 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4097 << A.getName();
4098 return;
4099 }
4100 }
4101
Hans Wennborge82f19c2014-06-24 23:57:05 +00004102 unsigned Index = A.getAttributeSpellingListIndex();
4103 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4104 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4105 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004106 if (NewAttr)
4107 D->addAttr(NewAttr);
4108}
4109
David Majnemer2c4e00a2014-01-29 22:07:36 +00004110MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004111Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004112 unsigned AttrSpellingListIndex,
4113 MSInheritanceAttr::Spelling SemanticSpelling) {
4114 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4115 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004116 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004117 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4118 << 1 /*previous declaration*/;
4119 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4120 D->dropAttr<MSInheritanceAttr>();
4121 }
4122
4123 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4124 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004125 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4126 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004127 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004128 }
4129 } else {
4130 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4131 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4132 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004133 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004134 }
4135 if (RD->getDescribedClassTemplate()) {
4136 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4137 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004138 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004139 }
4140 }
4141
4142 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004143 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004144}
4145
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004146static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4147 // The capability attributes take a single string parameter for the name of
4148 // the capability they represent. The lockable attribute does not take any
4149 // parameters. However, semantically, both attributes represent the same
4150 // concept, and so they use the same semantic attribute. Eventually, the
4151 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004152 //
Alp Toker958027b2014-07-14 19:42:55 +00004153 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004154 // literal will be considered a "mutex."
4155 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004156 SourceLocation LiteralLoc;
4157 if (Attr.getKind() == AttributeList::AT_Capability &&
4158 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4159 return;
4160
Aaron Ballman6c810072014-03-05 21:47:13 +00004161 // Currently, there are only two names allowed for a capability: role and
4162 // mutex (case insensitive). Diagnose other capability names.
4163 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4164 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4165
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004166 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4167 Attr.getAttributeSpellingListIndex()));
4168}
4169
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004170static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4171 const AttributeList &Attr) {
4172 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4173 Attr.getArgAsExpr(0),
4174 Attr.getAttributeSpellingListIndex()));
4175}
4176
4177static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4178 const AttributeList &Attr) {
4179 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004180 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004181 return;
4182
4183 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4184 S.Context,
4185 Args.data(), Args.size(),
4186 Attr.getAttributeSpellingListIndex()));
4187}
4188
4189static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4190 const AttributeList &Attr) {
4191 SmallVector<Expr*, 2> Args;
4192 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4193 return;
4194
4195 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4196 S.Context,
4197 Attr.getArgAsExpr(0),
4198 Args.data(),
4199 Args.size(),
4200 Attr.getAttributeSpellingListIndex()));
4201}
4202
4203static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4204 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004205 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004206 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004207 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004208
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004209 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4210 Attr.getRange(), S.Context, Args.data(), Args.size(),
4211 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004212}
4213
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004214static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4215 const AttributeList &Attr) {
4216 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4217 return;
4218
4219 // check that all arguments are lockable objects
4220 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004221 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004222 if (Args.empty())
4223 return;
4224
4225 RequiresCapabilityAttr *RCA = ::new (S.Context)
4226 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4227 Args.size(), Attr.getAttributeSpellingListIndex());
4228
4229 D->addAttr(RCA);
4230}
4231
Aaron Ballman43f40102014-11-14 22:34:56 +00004232static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4233 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4234 if (NSD->isAnonymousNamespace()) {
4235 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4236 // Do not want to attach the attribute to the namespace because that will
4237 // cause confusing diagnostic reports for uses of declarations within the
4238 // namespace.
4239 return;
4240 }
4241 }
4242 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4243}
4244
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004245/// Handles semantic checking for features that are common to all attributes,
4246/// such as checking whether a parameter was properly specified, or the correct
4247/// number of arguments were passed, etc.
4248static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4249 const AttributeList &Attr) {
4250 // Several attributes carry different semantics than the parsing requires, so
4251 // those are opted out of the common handling.
4252 //
4253 // We also bail on unknown and ignored attributes because those are handled
4254 // as part of the target-specific handling logic.
4255 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004256 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004257 return false;
4258
Aaron Ballman3aff6332013-12-02 19:30:36 +00004259 // Check whether the attribute requires specific language extensions to be
4260 // enabled.
4261 if (!Attr.diagnoseLangOpts(S))
4262 return true;
4263
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004264 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4265 // If there are no optional arguments, then checking for the argument count
4266 // is trivial.
4267 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4268 return true;
4269 } else {
4270 // There are optional arguments, so checking is slightly more involved.
4271 if (Attr.getMinArgs() &&
4272 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4273 return true;
4274 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4275 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4276 return true;
4277 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004278
4279 // Check whether the attribute appertains to the given subject.
4280 if (!Attr.diagnoseAppertainsTo(S, D))
4281 return true;
4282
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004283 return false;
4284}
4285
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004286//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004287// Top Level Sema Entry Points
4288//===----------------------------------------------------------------------===//
4289
Richard Smithf8a75c32013-08-29 00:47:48 +00004290/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4291/// the attribute applies to decls. If the attribute is a type attribute, just
4292/// silently ignore it if a GNU attribute.
4293static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4294 const AttributeList &Attr,
4295 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004296 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004297 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004298
Richard Smithf8a75c32013-08-29 00:47:48 +00004299 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4300 // instead.
4301 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4302 return;
4303
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004304 // Unknown attributes are automatically warned on. Target-specific attributes
4305 // which do not apply to the current target architecture are treated as
4306 // though they were unknown attributes.
4307 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4308 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004309 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4310 ? diag::warn_unhandled_ms_attribute_ignored
4311 : diag::warn_unknown_attribute_ignored)
4312 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004313 return;
4314 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004315
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004316 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4317 return;
4318
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004319 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004320 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004321 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004322 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004323 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004324 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004325 handleInterruptAttr(S, D, Attr);
4326 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004327 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004328 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4329 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004330 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004331 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004332 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004333 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004334 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004335 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4336 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004337 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004338 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4339 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004340 case AttributeList::AT_AMDGPUNumVGPR:
4341 handleAMDGPUNumVGPRAttr(S, D, Attr);
4342 break;
4343 case AttributeList::AT_AMDGPUNumSGPR:
4344 handleAMDGPUNumSGPRAttr(S, D, Attr);
4345 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004346 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004347 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4348 break;
4349 case AttributeList::AT_IBOutlet:
4350 handleIBOutlet(S, D, Attr);
4351 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004352 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004353 handleIBOutletCollection(S, D, Attr);
4354 break;
4355 case AttributeList::AT_Alias:
4356 handleAliasAttr(S, D, Attr);
4357 break;
4358 case AttributeList::AT_Aligned:
4359 handleAlignedAttr(S, D, Attr);
4360 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004361 case AttributeList::AT_AlignValue:
4362 handleAlignValueAttr(S, D, Attr);
4363 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004364 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004365 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004366 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004367 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004368 handleAnalyzerNoReturnAttr(S, D, Attr);
4369 break;
4370 case AttributeList::AT_TLSModel:
4371 handleTLSModelAttr(S, D, Attr);
4372 break;
4373 case AttributeList::AT_Annotate:
4374 handleAnnotateAttr(S, D, Attr);
4375 break;
4376 case AttributeList::AT_Availability:
4377 handleAvailabilityAttr(S, D, Attr);
4378 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004379 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004380 handleDependencyAttr(S, scope, D, Attr);
4381 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004382 case AttributeList::AT_Common:
4383 handleCommonAttr(S, D, Attr);
4384 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004385 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004386 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4387 break;
4388 case AttributeList::AT_Constructor:
4389 handleConstructorAttr(S, D, Attr);
4390 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004391 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004392 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4393 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004394 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004395 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004396 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004397 case AttributeList::AT_Destructor:
4398 handleDestructorAttr(S, D, Attr);
4399 break;
4400 case AttributeList::AT_EnableIf:
4401 handleEnableIfAttr(S, D, Attr);
4402 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004403 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004404 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004405 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004406 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004407 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004408 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004409 case AttributeList::AT_OptimizeNone:
4410 handleOptimizeNoneAttr(S, D, Attr);
4411 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004412 case AttributeList::AT_FlagEnum:
4413 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4414 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004415 case AttributeList::AT_Flatten:
4416 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4417 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004418 case AttributeList::AT_Format:
4419 handleFormatAttr(S, D, Attr);
4420 break;
4421 case AttributeList::AT_FormatArg:
4422 handleFormatArgAttr(S, D, Attr);
4423 break;
4424 case AttributeList::AT_CUDAGlobal:
4425 handleGlobalAttr(S, D, Attr);
4426 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004427 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004428 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4429 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004430 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004431 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4432 break;
4433 case AttributeList::AT_GNUInline:
4434 handleGNUInlineAttr(S, D, Attr);
4435 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004436 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004437 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004438 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004439 case AttributeList::AT_Malloc:
4440 handleMallocAttr(S, D, Attr);
4441 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004442 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004443 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4444 break;
4445 case AttributeList::AT_Mode:
4446 handleModeAttr(S, D, Attr);
4447 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004448 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004449 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4450 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004451 case AttributeList::AT_NoSplitStack:
4452 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4453 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004454 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004455 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4456 handleNonNullAttrParameter(S, PVD, Attr);
4457 else
4458 handleNonNullAttr(S, D, Attr);
4459 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004460 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004461 handleReturnsNonNullAttr(S, D, Attr);
4462 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004463 case AttributeList::AT_AssumeAligned:
4464 handleAssumeAlignedAttr(S, D, Attr);
4465 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004466 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004467 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4468 break;
4469 case AttributeList::AT_Ownership:
4470 handleOwnershipAttr(S, D, Attr);
4471 break;
4472 case AttributeList::AT_Cold:
4473 handleColdAttr(S, D, Attr);
4474 break;
4475 case AttributeList::AT_Hot:
4476 handleHotAttr(S, D, Attr);
4477 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004478 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004479 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4480 break;
4481 case AttributeList::AT_NoReturn:
4482 handleNoReturnAttr(S, D, Attr);
4483 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004484 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004485 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4486 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004487 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004488 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4489 break;
4490 case AttributeList::AT_VecReturn:
4491 handleVecReturnAttr(S, D, Attr);
4492 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004493
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004494 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004495 handleObjCOwnershipAttr(S, D, Attr);
4496 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004497 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004498 handleObjCPreciseLifetimeAttr(S, D, Attr);
4499 break;
John McCall31168b02011-06-15 23:02:42 +00004500
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004501 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004502 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4503 break;
John McCallcf166702011-07-22 08:53:00 +00004504
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004505 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004506 handleObjCRequiresSuperAttr(S, D, Attr);
4507 break;
4508
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004509 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004510 handleObjCBridgeAttr(S, scope, D, Attr);
4511 break;
4512
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004513 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004514 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4515 break;
4516
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004517 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004518 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4519 break;
John McCallf1e8b342011-09-29 07:17:38 +00004520
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004521 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004522 handleObjCDesignatedInitializer(S, D, Attr);
4523 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004524
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004525 case AttributeList::AT_ObjCRuntimeName:
4526 handleObjCRuntimeName(S, D, Attr);
4527 break;
4528
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004529 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004530 handleCFAuditedTransferAttr(S, D, Attr);
4531 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004532 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004533 handleCFUnknownTransferAttr(S, D, Attr);
4534 break;
John McCall32f5fe12011-09-30 05:12:12 +00004535
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004536 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004537 case AttributeList::AT_NSConsumed:
4538 handleNSConsumedAttr(S, D, Attr);
4539 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004540 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004541 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4542 break;
John McCalled433932011-01-25 03:31:58 +00004543
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004544 case AttributeList::AT_NSReturnsAutoreleased:
4545 case AttributeList::AT_NSReturnsNotRetained:
4546 case AttributeList::AT_CFReturnsNotRetained:
4547 case AttributeList::AT_NSReturnsRetained:
4548 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004549 handleNSReturnsRetainedAttr(S, D, Attr);
4550 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004551 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004552 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4553 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004554 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004555 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4556 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004557 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004558 handleVecTypeHint(S, D, Attr);
4559 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004560
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004561 case AttributeList::AT_InitPriority:
4562 handleInitPriorityAttr(S, D, Attr);
4563 break;
4564
4565 case AttributeList::AT_Packed:
4566 handlePackedAttr(S, D, Attr);
4567 break;
4568 case AttributeList::AT_Section:
4569 handleSectionAttr(S, D, Attr);
4570 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004571 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004572 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004573 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004574 case AttributeList::AT_ArcWeakrefUnavailable:
4575 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4576 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004577 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004578 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4579 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004580 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004581 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004582 break;
4583 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004584 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4585 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004586 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004587 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4588 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004589 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004590 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4591 break;
4592 case AttributeList::AT_Used:
4593 handleUsedAttr(S, D, Attr);
4594 break;
John McCalld041a9b2013-02-20 01:54:26 +00004595 case AttributeList::AT_Visibility:
4596 handleVisibilityAttr(S, D, Attr, false);
4597 break;
4598 case AttributeList::AT_TypeVisibility:
4599 handleVisibilityAttr(S, D, Attr, true);
4600 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004601 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004602 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4603 break;
4604 case AttributeList::AT_WarnUnusedResult:
4605 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004606 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004607 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004608 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4609 break;
4610 case AttributeList::AT_WeakRef:
4611 handleWeakRefAttr(S, D, Attr);
4612 break;
4613 case AttributeList::AT_WeakImport:
4614 handleWeakImportAttr(S, D, Attr);
4615 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004616 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004617 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004618 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004619 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004620 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4621 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004622 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004623 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004624 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004625 case AttributeList::AT_ObjCNSObject:
4626 handleObjCNSObject(S, D, Attr);
4627 break;
4628 case AttributeList::AT_Blocks:
4629 handleBlocksAttr(S, D, Attr);
4630 break;
4631 case AttributeList::AT_Sentinel:
4632 handleSentinelAttr(S, D, Attr);
4633 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004634 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004635 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4636 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004637 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004638 handleSimpleAttribute<PureAttr>(S, D, Attr);
4639 break;
4640 case AttributeList::AT_Cleanup:
4641 handleCleanupAttr(S, D, Attr);
4642 break;
4643 case AttributeList::AT_NoDebug:
4644 handleNoDebugAttr(S, D, Attr);
4645 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004646 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004647 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4648 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004649 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004650 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4651 break;
4652 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4653 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4654 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004655 case AttributeList::AT_StdCall:
4656 case AttributeList::AT_CDecl:
4657 case AttributeList::AT_FastCall:
4658 case AttributeList::AT_ThisCall:
4659 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004660 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00004661 case AttributeList::AT_MSABI:
4662 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004663 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004664 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004665 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004666 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004667 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004668 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004669 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4670 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004671 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004672 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4673 break;
John McCall8d32c052012-05-22 21:28:12 +00004674
4675 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004676 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004677 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004678 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004680 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004681 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004682 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004683 handleMSInheritanceAttr(S, D, Attr);
4684 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004685 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004686 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4687 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004688 case AttributeList::AT_Thread:
4689 handleDeclspecThreadAttr(S, D, Attr);
4690 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004691
4692 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004693 case AttributeList::AT_AssertExclusiveLock:
4694 handleAssertExclusiveLockAttr(S, D, Attr);
4695 break;
4696 case AttributeList::AT_AssertSharedLock:
4697 handleAssertSharedLockAttr(S, D, Attr);
4698 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004699 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004700 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4701 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004702 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004703 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004704 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004705 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004706 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4707 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004708 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004709 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004710 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004711 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004712 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004713 break;
4714 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004715 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004716 break;
4717 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004718 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004719 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004720 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004721 handleGuardedByAttr(S, D, Attr);
4722 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004723 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004724 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004725 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004726 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004727 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004728 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004729 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004730 handleLockReturnedAttr(S, D, Attr);
4731 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004732 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004733 handleLocksExcludedAttr(S, D, Attr);
4734 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004735 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004736 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004737 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004738 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004739 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004740 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004741 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004742 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004743 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004744
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004745 // Capability analysis attributes.
4746 case AttributeList::AT_Capability:
4747 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004748 handleCapabilityAttr(S, D, Attr);
4749 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004750 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004751 handleRequiresCapabilityAttr(S, D, Attr);
4752 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004753
4754 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004755 handleAssertCapabilityAttr(S, D, Attr);
4756 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004757 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004758 handleAcquireCapabilityAttr(S, D, Attr);
4759 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004760 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004761 handleReleaseCapabilityAttr(S, D, Attr);
4762 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004763 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004764 handleTryAcquireCapabilityAttr(S, D, Attr);
4765 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004766
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004767 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004768 case AttributeList::AT_Consumable:
4769 handleConsumableAttr(S, D, Attr);
4770 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004771 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004772 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4773 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004774 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004775 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4776 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004777 case AttributeList::AT_CallableWhen:
4778 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004779 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004780 case AttributeList::AT_ParamTypestate:
4781 handleParamTypestateAttr(S, D, Attr);
4782 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004783 case AttributeList::AT_ReturnTypestate:
4784 handleReturnTypestateAttr(S, D, Attr);
4785 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004786 case AttributeList::AT_SetTypestate:
4787 handleSetTypestateAttr(S, D, Attr);
4788 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004789 case AttributeList::AT_TestTypestate:
4790 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004791 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004792
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004793 // Type safety attributes.
4794 case AttributeList::AT_ArgumentWithTypeTag:
4795 handleArgumentWithTypeTagAttr(S, D, Attr);
4796 break;
4797 case AttributeList::AT_TypeTagForDatatype:
4798 handleTypeTagForDatatypeAttr(S, D, Attr);
4799 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004800 }
4801}
4802
4803/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4804/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004805void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004806 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004807 bool IncludeCXX11Attributes) {
4808 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004809 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004810
Joey Gouly2cd9db12013-12-13 16:15:28 +00004811 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004812 // GCC accepts
4813 // static int a9 __attribute__((weakref));
4814 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004815 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004816 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4817 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004818 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004819 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004820 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004821
Aaron Ballmanbe243a72014-12-04 22:45:31 +00004822 // FIXME: We should be able to handle this in TableGen as well. It would be
4823 // good to have a way to specify "these attributes must appear as a group",
4824 // for these. Additionally, it would be good to have a way to specify "these
4825 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00004826 if (!D->hasAttr<OpenCLKernelAttr>()) {
4827 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004828 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004829 // FIXME: This emits a different error message than
4830 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004831 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004832 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00004833 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00004834 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004835 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00004836 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00004837 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004838 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004839 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
4840 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
4841 << A << ExpectedKernelFunction;
4842 D->setInvalidDecl();
4843 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
4844 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
4845 << A << ExpectedKernelFunction;
4846 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00004847 }
4848 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004849}
4850
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004851// Annotation attributes are the only attributes allowed after an access
4852// specifier.
4853bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4854 const AttributeList *AttrList) {
4855 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004856 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00004857 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004858 } else {
4859 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4860 return true;
4861 }
4862 }
4863
4864 return false;
4865}
4866
John McCall42856de2011-10-01 05:17:03 +00004867/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4868/// contains any decl attributes that we should warn about.
4869static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4870 for ( ; A; A = A->getNext()) {
4871 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004872 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004873 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4874
4875 if (A->getKind() == AttributeList::UnknownAttribute) {
4876 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4877 << A->getName() << A->getRange();
4878 } else {
4879 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4880 << A->getName() << A->getRange();
4881 }
4882 }
4883}
4884
4885/// checkUnusedDeclAttributes - Given a declarator which is not being
4886/// used to build a declaration, complain about any decl attributes
4887/// which might be lying around on it.
4888void Sema::checkUnusedDeclAttributes(Declarator &D) {
4889 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4890 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4891 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4892 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4893}
4894
Ryan Flynn7d470f32009-07-30 03:15:39 +00004895/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004896/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004897NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4898 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004899 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004900 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004901 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004902 FunctionDecl *NewFD;
4903 // FIXME: Missing call to CheckFunctionDeclaration().
4904 // FIXME: Mangling?
4905 // FIXME: Is the qualifier info correct?
4906 // FIXME: Is the DeclContext correct?
4907 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4908 Loc, Loc, DeclarationName(II),
4909 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004910 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004911 FD->hasPrototype(),
4912 false/*isConstexprSpecified*/);
4913 NewD = NewFD;
4914
4915 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004916 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004917
4918 // Fake up parameter variables; they are declared as if this were
4919 // a typedef.
4920 QualType FDTy = FD->getType();
4921 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4922 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004923 for (const auto &AI : FT->param_types()) {
4924 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004925 Param->setScopeInfo(0, Params.size());
4926 Params.push_back(Param);
4927 }
David Blaikie9c70e042011-09-21 18:16:56 +00004928 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004929 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004930 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4931 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004932 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004933 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004934 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004935 if (VD->getQualifier()) {
4936 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004937 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004938 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004939 }
4940 return NewD;
4941}
4942
James Dennett634962f2012-06-14 21:40:34 +00004943/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004944/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004945void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004946 if (W.getUsed()) return; // only do this once
4947 W.setUsed(true);
4948 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4949 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004950 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004951 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4952 W.getLocation()));
4953 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004954 WeakTopLevelDecl.push_back(NewD);
4955 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4956 // to insert Decl at TU scope, sorry.
4957 DeclContext *SavedContext = CurContext;
4958 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004959 NewD->setDeclContext(CurContext);
4960 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004961 PushOnScopeChains(NewD, S);
4962 CurContext = SavedContext;
4963 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004964 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004965 }
4966}
4967
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004968void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4969 // It's valid to "forward-declare" #pragma weak, in which case we
4970 // have to do this.
4971 LoadExternalWeakUndeclaredIdentifiers();
4972 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004973 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004974 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4975 if (VD->isExternC())
4976 ND = VD;
4977 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4978 if (FD->isExternC())
4979 ND = FD;
4980 if (ND) {
4981 if (IdentifierInfo *Id = ND->getIdentifier()) {
4982 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4983 = WeakUndeclaredIdentifiers.find(Id);
4984 if (I != WeakUndeclaredIdentifiers.end()) {
4985 WeakInfo W = I->second;
4986 DeclApplyPragmaWeak(S, ND, W);
4987 WeakUndeclaredIdentifiers[Id] = W;
4988 }
4989 }
4990 }
4991 }
4992}
4993
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004994/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4995/// it, apply them to D. This is a bit tricky because PD can have attributes
4996/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004997void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004998 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004999 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005000 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005001
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005002 // Walk the declarator structure, applying decl attributes that were in a type
5003 // position to the decl itself. This handles cases like:
5004 // int *__attr__(x)** D;
5005 // when X is a decl attribute.
5006 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5007 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005008 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005009
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005010 // Finally, apply any attributes on the decl itself.
5011 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005012 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005013}
John McCall28a6aea2009-11-04 02:18:39 +00005014
John McCall31168b02011-06-15 23:02:42 +00005015/// Is the given declaration allowed to use a forbidden type?
5016static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5017 // Private ivars are always okay. Unfortunately, people don't
5018 // always properly make their ivars private, even in system headers.
5019 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005020 // Function declarations in sys headers will be marked unavailable.
5021 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5022 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005023 return false;
5024
5025 // Require it to be declared in a system header.
5026 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5027}
5028
5029/// Handle a delayed forbidden-type diagnostic.
5030static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5031 Decl *decl) {
5032 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00005033 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
5034 "this system declaration uses an unsupported type",
5035 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005036 return;
5037 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005038 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005039 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005040 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005041 // kind of forbidden type messages on unavailable functions.
5042 if (FD->hasAttr<UnavailableAttr>() &&
5043 diag.getForbiddenTypeDiagnostic() ==
5044 diag::err_arc_array_param_no_ownership) {
5045 diag.Triggered = true;
5046 return;
5047 }
5048 }
John McCall31168b02011-06-15 23:02:42 +00005049
5050 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5051 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5052 diag.Triggered = true;
5053}
5054
Aaron Ballmanfb237522014-10-15 15:37:51 +00005055
5056static bool isDeclDeprecated(Decl *D) {
5057 do {
5058 if (D->isDeprecated())
5059 return true;
5060 // A category implicitly has the availability of the interface.
5061 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5062 return CatD->getClassInterface()->isDeprecated();
5063 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5064 return false;
5065}
5066
5067static bool isDeclUnavailable(Decl *D) {
5068 do {
5069 if (D->isUnavailable())
5070 return true;
5071 // A category implicitly has the availability of the interface.
5072 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5073 return CatD->getClassInterface()->isUnavailable();
5074 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5075 return false;
5076}
5077
5078static void DoEmitAvailabilityWarning(Sema &S, DelayedDiagnostic::DDKind K,
5079 Decl *Ctx, const NamedDecl *D,
5080 StringRef Message, SourceLocation Loc,
5081 const ObjCInterfaceDecl *UnknownObjCClass,
5082 const ObjCPropertyDecl *ObjCProperty,
5083 bool ObjCPropertyAccess) {
5084 // Diagnostics for deprecated or unavailable.
5085 unsigned diag, diag_message, diag_fwdclass_message;
5086
5087 // Matches 'diag::note_property_attribute' options.
5088 unsigned property_note_select;
5089
5090 // Matches diag::note_availability_specified_here.
5091 unsigned available_here_select_kind;
5092
5093 // Don't warn if our current context is deprecated or unavailable.
5094 switch (K) {
5095 case DelayedDiagnostic::Deprecation:
5096 if (isDeclDeprecated(Ctx))
5097 return;
5098 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5099 : diag::warn_property_method_deprecated;
5100 diag_message = diag::warn_deprecated_message;
5101 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5102 property_note_select = /* deprecated */ 0;
5103 available_here_select_kind = /* deprecated */ 2;
5104 break;
5105
5106 case DelayedDiagnostic::Unavailable:
5107 if (isDeclUnavailable(Ctx))
5108 return;
5109 diag = !ObjCPropertyAccess ? diag::err_unavailable
5110 : diag::err_property_method_unavailable;
5111 diag_message = diag::err_unavailable_message;
5112 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5113 property_note_select = /* unavailable */ 1;
5114 available_here_select_kind = /* unavailable */ 0;
5115 break;
5116
5117 default:
5118 llvm_unreachable("Neither a deprecation or unavailable kind");
5119 }
5120
Aaron Ballmanfb237522014-10-15 15:37:51 +00005121 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005122 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005123 if (ObjCProperty)
5124 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5125 << ObjCProperty->getDeclName() << property_note_select;
5126 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005127 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005128 if (ObjCProperty)
5129 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5130 << ObjCProperty->getDeclName() << property_note_select;
5131 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005132 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005133 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5134 }
5135
5136 S.Diag(D->getLocation(), diag::note_availability_specified_here)
5137 << D << available_here_select_kind;
5138}
5139
5140static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5141 Decl *Ctx) {
5142 DD.Triggered = true;
5143 DoEmitAvailabilityWarning(S, (DelayedDiagnostic::DDKind)DD.Kind, Ctx,
5144 DD.getDeprecationDecl(), DD.getDeprecationMessage(),
5145 DD.Loc, DD.getUnknownObjCClass(),
5146 DD.getObjCProperty(), false);
5147}
5148
John McCall2ec85372012-05-07 06:16:41 +00005149void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5150 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005151 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005152 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005153
John McCall2ec85372012-05-07 06:16:41 +00005154 // When delaying diagnostics to run in the context of a parsed
5155 // declaration, we only want to actually emit anything if parsing
5156 // succeeds.
5157 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005158
John McCall2ec85372012-05-07 06:16:41 +00005159 // We emit all the active diagnostics in this pool or any of its
5160 // parents. In general, we'll get one pool for the decl spec
5161 // and a child pool for each declarator; in a decl group like:
5162 // deprecated_typedef foo, *bar, baz();
5163 // only the declarator pops will be passed decls. This is correct;
5164 // we really do need to consider delayed diagnostics from the decl spec
5165 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005166 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005167 do {
John McCall6347b682012-05-07 06:16:58 +00005168 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005169 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5170 // This const_cast is a bit lame. Really, Triggered should be mutable.
5171 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005172 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005173 continue;
5174
John McCallc1465822011-02-14 07:13:47 +00005175 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005176 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005177 case DelayedDiagnostic::Unavailable:
5178 // Don't bother giving deprecation/unavailable diagnostics if
5179 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005180 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005181 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005182 break;
5183
5184 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005185 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005186 break;
John McCall31168b02011-06-15 23:02:42 +00005187
5188 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005189 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005190 break;
John McCall86121512010-01-27 03:50:35 +00005191 }
5192 }
John McCall2ec85372012-05-07 06:16:41 +00005193 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005194}
5195
John McCall6347b682012-05-07 06:16:58 +00005196/// Given a set of delayed diagnostics, re-emit them as if they had
5197/// been delayed in the current context instead of in the given pool.
5198/// Essentially, this just moves them to the current pool.
5199void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5200 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5201 assert(curPool && "re-emitting in undelayed context not supported");
5202 curPool->steal(pool);
5203}
5204
Ted Kremenekb79ee572013-12-18 23:30:06 +00005205void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5206 NamedDecl *D, StringRef Message,
5207 SourceLocation Loc,
5208 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005209 const ObjCPropertyDecl *ObjCProperty,
5210 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005211 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005212 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005213 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5214 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5215 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005216 return;
5217 }
5218
Ted Kremenekb79ee572013-12-18 23:30:06 +00005219 Decl *Ctx = cast<Decl>(getCurLexicalContext());
5220 DelayedDiagnostic::DDKind K;
5221 switch (AD) {
5222 case AD_Deprecation:
5223 K = DelayedDiagnostic::Deprecation;
5224 break;
5225 case AD_Unavailable:
5226 K = DelayedDiagnostic::Unavailable;
5227 break;
5228 }
5229
5230 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005231 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005232}