blob: 17a849ea7af09d3615cf914fb3f1f45cfbaab03c [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
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001495 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001497 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001498 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001499}
1500
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001501static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001502 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001503 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001504
Michael Han99315932013-01-24 16:46:58 +00001505 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1506 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001507}
1508
1509static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001510 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001511 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001512
Michael Han99315932013-01-24 16:46:58 +00001513 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1514 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001515}
1516
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001517static void handleTLSModelAttr(Sema &S, Decl *D,
1518 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001519 StringRef Model;
1520 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001521 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001522 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001523 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001524
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001525 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001526 if (Model != "global-dynamic" && Model != "local-dynamic"
1527 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001528 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001529 return;
1530 }
1531
Michael Han99315932013-01-24 16:46:58 +00001532 D->addAttr(::new (S.Context)
1533 TLSModelAttr(Attr.getRange(), S.Context, Model,
1534 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001535}
1536
Chandler Carruthedc2c642011-07-02 00:01:44 +00001537static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001538 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001539 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001540 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001541 D->addAttr(::new (S.Context)
1542 MallocAttr(Attr.getRange(), S.Context,
1543 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001544 return;
1545 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001546 }
1547
Ted Kremenek08479ae2009-08-15 00:51:46 +00001548 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001549}
1550
Chandler Carruthedc2c642011-07-02 00:01:44 +00001551static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001552 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001553 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1554 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001555 return;
1556 }
1557
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001558 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1559 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001560}
1561
Chandler Carruthedc2c642011-07-02 00:01:44 +00001562static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001563 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001564
1565 if (S.CheckNoReturnAttr(attr)) return;
1566
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001567 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001568 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001569 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001570 return;
1571 }
1572
Michael Han99315932013-01-24 16:46:58 +00001573 D->addAttr(::new (S.Context)
1574 NoReturnAttr(attr.getRange(), S.Context,
1575 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001576}
1577
1578bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001579 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001580 attr.setInvalid();
1581 return true;
1582 }
1583
1584 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001585}
1586
Chandler Carruthedc2c642011-07-02 00:01:44 +00001587static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1588 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001589
1590 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1591 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001592 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1593 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001594 if (!VD || (!VD->getType()->isBlockPointerType() &&
1595 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001596 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001597 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001598 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001599 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001600 return;
1601 }
1602 }
1603
Michael Han99315932013-01-24 16:46:58 +00001604 D->addAttr(::new (S.Context)
1605 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1606 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001607}
1608
John Thompsoncdb847ba2010-08-09 21:53:52 +00001609// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001610static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001611/*
1612 Returning a Vector Class in Registers
1613
Eric Christopherbc638a82010-12-01 22:13:54 +00001614 According to the PPU ABI specifications, a class with a single member of
1615 vector type is returned in memory when used as the return value of a function.
1616 This results in inefficient code when implementing vector classes. To return
1617 the value in a single vector register, add the vecreturn attribute to the
1618 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001619
1620 Example:
1621
1622 struct Vector
1623 {
1624 __vector float xyzw;
1625 } __attribute__((vecreturn));
1626
1627 Vector Add(Vector lhs, Vector rhs)
1628 {
1629 Vector result;
1630 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1631 return result; // This will be returned in a register
1632 }
1633*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001634 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1635 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001636 return;
1637 }
1638
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001639 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001640 int count = 0;
1641
1642 if (!isa<CXXRecordDecl>(record)) {
1643 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1644 return;
1645 }
1646
1647 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1648 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1649 return;
1650 }
1651
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001652 for (const auto *I : record->fields()) {
1653 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001654 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1655 return;
1656 }
1657 count++;
1658 }
1659
Michael Han99315932013-01-24 16:46:58 +00001660 D->addAttr(::new (S.Context)
1661 VecReturnAttr(Attr.getRange(), S.Context,
1662 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001663}
1664
Richard Smithe233fbf2013-01-28 22:42:45 +00001665static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1666 const AttributeList &Attr) {
1667 if (isa<ParmVarDecl>(D)) {
1668 // [[carries_dependency]] can only be applied to a parameter if it is a
1669 // parameter of a function declaration or lambda.
1670 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1671 S.Diag(Attr.getLoc(),
1672 diag::err_carries_dependency_param_not_function_decl);
1673 return;
1674 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001675 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001676
1677 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1678 Attr.getRange(), S.Context,
1679 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001680}
1681
Chandler Carruthedc2c642011-07-02 00:01:44 +00001682static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001683 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001684 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001685 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001686 return;
1687 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001688 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001690 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001691 return;
1692 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001693
Michael Han99315932013-01-24 16:46:58 +00001694 D->addAttr(::new (S.Context)
1695 UsedAttr(Attr.getRange(), S.Context,
1696 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001697}
1698
Chandler Carruthedc2c642011-07-02 00:01:44 +00001699static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001700 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001701 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001702 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1703 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001704
Michael Han99315932013-01-24 16:46:58 +00001705 D->addAttr(::new (S.Context)
1706 ConstructorAttr(Attr.getRange(), S.Context, priority,
1707 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001708}
1709
Chandler Carruthedc2c642011-07-02 00:01:44 +00001710static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001711 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001712 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001713 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1714 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Michael Han99315932013-01-24 16:46:58 +00001716 D->addAttr(::new (S.Context)
1717 DestructorAttr(Attr.getRange(), S.Context, priority,
1718 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001719}
1720
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001721template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001722static void handleAttrWithMessage(Sema &S, Decl *D,
1723 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001724 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001725 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001726 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001727 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001728
Michael Han99315932013-01-24 16:46:58 +00001729 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1730 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001731}
1732
Ted Kremenek438f8db2014-02-22 01:06:05 +00001733static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001734 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001735 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001736 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1737 << Attr.getName() << Attr.getRange();
1738 return;
1739 }
1740
Ted Kremenek28eace62013-11-23 01:01:34 +00001741 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001742 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1743 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001744}
1745
Jordy Rose740b0c22012-05-08 03:27:22 +00001746static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1747 IdentifierInfo *Platform,
1748 VersionTuple Introduced,
1749 VersionTuple Deprecated,
1750 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001751 StringRef PlatformName
1752 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1753 if (PlatformName.empty())
1754 PlatformName = Platform->getName();
1755
1756 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1757 // of these steps are needed).
1758 if (!Introduced.empty() && !Deprecated.empty() &&
1759 !(Introduced <= Deprecated)) {
1760 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1761 << 1 << PlatformName << Deprecated.getAsString()
1762 << 0 << Introduced.getAsString();
1763 return true;
1764 }
1765
1766 if (!Introduced.empty() && !Obsoleted.empty() &&
1767 !(Introduced <= Obsoleted)) {
1768 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1769 << 2 << PlatformName << Obsoleted.getAsString()
1770 << 0 << Introduced.getAsString();
1771 return true;
1772 }
1773
1774 if (!Deprecated.empty() && !Obsoleted.empty() &&
1775 !(Deprecated <= Obsoleted)) {
1776 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1777 << 2 << PlatformName << Obsoleted.getAsString()
1778 << 1 << Deprecated.getAsString();
1779 return true;
1780 }
1781
1782 return false;
1783}
1784
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001785/// \brief Check whether the two versions match.
1786///
1787/// If either version tuple is empty, then they are assumed to match. If
1788/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1789static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1790 bool BeforeIsOkay) {
1791 if (X.empty() || Y.empty())
1792 return true;
1793
1794 if (X == Y)
1795 return true;
1796
1797 if (BeforeIsOkay && X < Y)
1798 return true;
1799
1800 return false;
1801}
1802
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001803AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001804 IdentifierInfo *Platform,
1805 VersionTuple Introduced,
1806 VersionTuple Deprecated,
1807 VersionTuple Obsoleted,
1808 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001809 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001810 bool Override,
1811 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001812 VersionTuple MergedIntroduced = Introduced;
1813 VersionTuple MergedDeprecated = Deprecated;
1814 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001815 bool FoundAny = false;
1816
Rafael Espindolac67f2232012-05-10 02:50:16 +00001817 if (D->hasAttrs()) {
1818 AttrVec &Attrs = D->getAttrs();
1819 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1820 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1821 if (!OldAA) {
1822 ++i;
1823 continue;
1824 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001825
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1827 if (OldPlatform != Platform) {
1828 ++i;
1829 continue;
1830 }
1831
1832 FoundAny = true;
1833 VersionTuple OldIntroduced = OldAA->getIntroduced();
1834 VersionTuple OldDeprecated = OldAA->getDeprecated();
1835 VersionTuple OldObsoleted = OldAA->getObsoleted();
1836 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001837
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001838 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1839 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1840 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1841 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001842 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001843 if (Override) {
1844 int Which = -1;
1845 VersionTuple FirstVersion;
1846 VersionTuple SecondVersion;
1847 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1848 Which = 0;
1849 FirstVersion = OldIntroduced;
1850 SecondVersion = Introduced;
1851 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1852 Which = 1;
1853 FirstVersion = Deprecated;
1854 SecondVersion = OldDeprecated;
1855 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1856 Which = 2;
1857 FirstVersion = Obsoleted;
1858 SecondVersion = OldObsoleted;
1859 }
1860
1861 if (Which == -1) {
1862 Diag(OldAA->getLocation(),
1863 diag::warn_mismatched_availability_override_unavail)
1864 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1865 } else {
1866 Diag(OldAA->getLocation(),
1867 diag::warn_mismatched_availability_override)
1868 << Which
1869 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1870 << FirstVersion.getAsString() << SecondVersion.getAsString();
1871 }
1872 Diag(Range.getBegin(), diag::note_overridden_method);
1873 } else {
1874 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1875 Diag(Range.getBegin(), diag::note_previous_attribute);
1876 }
1877
Rafael Espindolac67f2232012-05-10 02:50:16 +00001878 Attrs.erase(Attrs.begin() + i);
1879 --e;
1880 continue;
1881 }
1882
1883 VersionTuple MergedIntroduced2 = MergedIntroduced;
1884 VersionTuple MergedDeprecated2 = MergedDeprecated;
1885 VersionTuple MergedObsoleted2 = MergedObsoleted;
1886
1887 if (MergedIntroduced2.empty())
1888 MergedIntroduced2 = OldIntroduced;
1889 if (MergedDeprecated2.empty())
1890 MergedDeprecated2 = OldDeprecated;
1891 if (MergedObsoleted2.empty())
1892 MergedObsoleted2 = OldObsoleted;
1893
1894 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1895 MergedIntroduced2, MergedDeprecated2,
1896 MergedObsoleted2)) {
1897 Attrs.erase(Attrs.begin() + i);
1898 --e;
1899 continue;
1900 }
1901
1902 MergedIntroduced = MergedIntroduced2;
1903 MergedDeprecated = MergedDeprecated2;
1904 MergedObsoleted = MergedObsoleted2;
1905 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001906 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907 }
1908
1909 if (FoundAny &&
1910 MergedIntroduced == Introduced &&
1911 MergedDeprecated == Deprecated &&
1912 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001913 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001914
Ted Kremenekb5445722013-04-06 00:34:27 +00001915 // Only create a new attribute if !Override, but we want to do
1916 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001917 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001918 MergedDeprecated, MergedObsoleted) &&
1919 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001920 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1921 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001922 Obsoleted, IsUnavailable, Message,
1923 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001925 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001926}
1927
Chandler Carruthedc2c642011-07-02 00:01:44 +00001928static void handleAvailabilityAttr(Sema &S, Decl *D,
1929 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001930 if (!checkAttributeNumArgs(S, Attr, 1))
1931 return;
1932 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001933 unsigned Index = Attr.getAttributeSpellingListIndex();
1934
Aaron Ballman00e99962013-08-31 01:11:41 +00001935 IdentifierInfo *II = Platform->Ident;
1936 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1937 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1938 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001939
Rafael Espindolac231fab2013-01-08 21:30:32 +00001940 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1941 if (!ND) {
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1943 return;
1944 }
1945
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001946 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1947 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1948 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001949 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001950 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001951 if (const StringLiteral *SE =
1952 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001953 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001954
Aaron Ballman00e99962013-08-31 01:11:41 +00001955 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001956 Introduced.Version,
1957 Deprecated.Version,
1958 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001959 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001960 /*Override=*/false,
1961 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001962 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001963 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001964}
1965
John McCalld041a9b2013-02-20 01:54:26 +00001966template <class T>
1967static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1968 typename T::VisibilityType value,
1969 unsigned attrSpellingListIndex) {
1970 T *existingAttr = D->getAttr<T>();
1971 if (existingAttr) {
1972 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1973 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001974 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001975 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1976 S.Diag(range.getBegin(), diag::note_previous_attribute);
1977 D->dropAttr<T>();
1978 }
1979 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1980}
1981
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001982VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001983 VisibilityAttr::VisibilityType Vis,
1984 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001985 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1986 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001987}
1988
John McCalld041a9b2013-02-20 01:54:26 +00001989TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1990 TypeVisibilityAttr::VisibilityType Vis,
1991 unsigned AttrSpellingListIndex) {
1992 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1993 AttrSpellingListIndex);
1994}
1995
1996static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1997 bool isTypeVisibility) {
1998 // Visibility attributes don't mean anything on a typedef.
1999 if (isa<TypedefNameDecl>(D)) {
2000 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2001 << Attr.getName();
2002 return;
2003 }
2004
2005 // 'type_visibility' can only go on a type or namespace.
2006 if (isTypeVisibility &&
2007 !(isa<TagDecl>(D) ||
2008 isa<ObjCInterfaceDecl>(D) ||
2009 isa<NamespaceDecl>(D))) {
2010 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2011 << Attr.getName() << ExpectedTypeOrNamespace;
2012 return;
2013 }
2014
Benjamin Kramer70370212013-09-09 15:08:57 +00002015 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002016 StringRef TypeStr;
2017 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002018 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002019 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002020
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002021 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002022 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002023 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002025 return;
2026 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002027
2028 // Complain about attempts to use protected visibility on targets
2029 // (like Darwin) that don't support it.
2030 if (type == VisibilityAttr::Protected &&
2031 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2032 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2033 type = VisibilityAttr::Default;
2034 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002035
Michael Han99315932013-01-24 16:46:58 +00002036 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002037 clang::Attr *newAttr;
2038 if (isTypeVisibility) {
2039 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2040 (TypeVisibilityAttr::VisibilityType) type,
2041 Index);
2042 } else {
2043 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2044 }
2045 if (newAttr)
2046 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002047}
2048
Chandler Carruthedc2c642011-07-02 00:01:44 +00002049static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2050 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002051 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002052 if (!Attr.isArgIdent(0)) {
2053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2054 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002055 return;
2056 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002057
Aaron Ballman682ee422013-09-11 19:47:58 +00002058 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2059 ObjCMethodFamilyAttr::FamilyKind F;
2060 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2061 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2062 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002063 return;
2064 }
2065
Alp Toker314cc812014-01-25 16:55:45 +00002066 if (F == ObjCMethodFamilyAttr::OMF_init &&
2067 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002068 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002069 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002070 // Ignore the attribute.
2071 return;
2072 }
2073
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002074 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002075 S.Context, F,
2076 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002077}
2078
Chandler Carruthedc2c642011-07-02 00:01:44 +00002079static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002080 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002081 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002082 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002083 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2084 return;
2085 }
2086 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002087 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2088 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002089 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002090 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2091 return;
2092 }
2093 }
2094 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002095 // It is okay to include this attribute on properties, e.g.:
2096 //
2097 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2098 //
2099 // In this case it follows tradition and suppresses an error in the above
2100 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002101 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002102 }
Michael Han99315932013-01-24 16:46:58 +00002103 D->addAttr(::new (S.Context)
2104 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2105 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002106}
2107
Chandler Carruthedc2c642011-07-02 00:01:44 +00002108static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002109 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002111 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002112 return;
2113 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002114
Aaron Ballman00e99962013-08-31 01:11:41 +00002115 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002116 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002117 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2118 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2119 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002120 return;
2121 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002122
Michael Han99315932013-01-24 16:46:58 +00002123 D->addAttr(::new (S.Context)
2124 BlocksAttr(Attr.getRange(), S.Context, type,
2125 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002126}
2127
Chandler Carruthedc2c642011-07-02 00:01:44 +00002128static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002129 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002130 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002131 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002133 if (E->isTypeDependent() || E->isValueDependent() ||
2134 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002136 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002137 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002138 return;
2139 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002140
John McCallb46f2872011-09-09 07:56:05 +00002141 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002142 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2143 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002144 return;
2145 }
John McCallb46f2872011-09-09 07:56:05 +00002146
2147 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002148 }
2149
Aaron Ballman18a78382013-11-21 00:28:23 +00002150 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002151 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002152 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002153 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002154 if (E->isTypeDependent() || E->isValueDependent() ||
2155 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002156 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002157 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002158 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002159 return;
2160 }
2161 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002162
John McCallb46f2872011-09-09 07:56:05 +00002163 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002164 // FIXME: This error message could be improved, it would be nice
2165 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002166 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2167 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002168 return;
2169 }
2170 }
2171
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002172 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002173 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002174 if (isa<FunctionNoProtoType>(FT)) {
2175 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2176 return;
2177 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002178
Chris Lattner9363e312009-03-17 23:03:47 +00002179 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002180 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002181 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002182 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002183 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002184 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002185 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002186 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002187 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002188 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2189 if (!BD->isVariadic()) {
2190 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2191 return;
2192 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002193 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002194 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002195 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002196 const FunctionType *FT = Ty->isFunctionPointerType()
2197 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002198 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002199 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002200 int m = Ty->isFunctionPointerType() ? 0 : 1;
2201 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002202 return;
2203 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002204 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002205 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002206 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002207 return;
2208 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002209 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002210 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002211 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002212 return;
2213 }
Michael Han99315932013-01-24 16:46:58 +00002214 D->addAttr(::new (S.Context)
2215 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2216 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002217}
2218
Chandler Carruthedc2c642011-07-02 00:01:44 +00002219static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002220 if (D->getFunctionType() &&
2221 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002222 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2223 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002224 return;
2225 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002226 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002227 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002228 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2229 << Attr.getName() << 1;
2230 return;
2231 }
2232
Michael Han99315932013-01-24 16:46:58 +00002233 D->addAttr(::new (S.Context)
2234 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2235 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002236}
2237
Chandler Carruthedc2c642011-07-02 00:01:44 +00002238static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002239 // weak_import only applies to variable & function declarations.
2240 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002241 if (!D->canBeWeakImported(isDef)) {
2242 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002243 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2244 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002245 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002246 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002247 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002248 // Nothing to warn about here.
2249 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002251 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002252
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002253 return;
2254 }
2255
Michael Han99315932013-01-24 16:46:58 +00002256 D->addAttr(::new (S.Context)
2257 WeakImportAttr(Attr.getRange(), S.Context,
2258 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002259}
2260
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002261// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002262template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002263static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002264 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002265 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002266 for (unsigned i = 0; i < 3; ++i) {
2267 const Expr *E = Attr.getArgAsExpr(i);
2268 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002269 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002270 if (WGSize[i] == 0) {
2271 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2272 << Attr.getName() << E->getSourceRange();
2273 return;
2274 }
2275 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002276
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002277 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2278 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2279 Existing->getYDim() == WGSize[1] &&
2280 Existing->getZDim() == WGSize[2]))
2281 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002282
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002283 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2284 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002285 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002286}
2287
Joey Goulyaba589c2013-03-08 09:42:32 +00002288static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002289 if (!Attr.hasParsedType()) {
2290 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2291 << Attr.getName() << 1;
2292 return;
2293 }
2294
Craig Topperc3ec1492014-05-26 06:22:03 +00002295 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002296 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2297 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002298
2299 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2300 (ParmType->isBooleanType() ||
2301 !ParmType->isIntegralType(S.getASTContext()))) {
2302 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2303 << ParmType;
2304 return;
2305 }
2306
Aaron Ballmana9e05402013-12-02 22:16:55 +00002307 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002308 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002309 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2310 return;
2311 }
2312 }
2313
2314 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002315 ParmTSI,
2316 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002317}
2318
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002319SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002320 StringRef Name,
2321 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002322 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2323 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002324 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002325 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2326 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002327 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002328 }
Michael Han99315932013-01-24 16:46:58 +00002329 return ::new (Context) SectionAttr(Range, Context, Name,
2330 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002331}
2332
Chandler Carruthedc2c642011-07-02 00:01:44 +00002333static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002334 // Make sure that there is a string literal as the sections's single
2335 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002336 StringRef Str;
2337 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002338 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002339 return;
Mike Stump11289f42009-09-09 15:08:12 +00002340
Chris Lattner30ba6742009-08-10 19:03:04 +00002341 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002342 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002343 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002344 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002345 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002346 return;
2347 }
Mike Stump11289f42009-09-09 15:08:12 +00002348
Michael Han99315932013-01-24 16:46:58 +00002349 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002350 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002351 if (NewAttr)
2352 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002353}
2354
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002355
Chandler Carruthedc2c642011-07-02 00:01:44 +00002356static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002357 VarDecl *VD = cast<VarDecl>(D);
2358 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002359 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002360 return;
2361 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002362
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002363 Expr *E = Attr.getArgAsExpr(0);
2364 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002365 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002366 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002367
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002368 // gcc only allows for simple identifiers. Since we support more than gcc, we
2369 // will warn the user.
2370 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2371 if (DRE->hasQualifier())
2372 S.Diag(Loc, diag::warn_cleanup_ext);
2373 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2374 NI = DRE->getNameInfo();
2375 if (!FD) {
2376 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2377 << NI.getName();
2378 return;
2379 }
2380 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2381 if (ULE->hasExplicitTemplateArgs())
2382 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002383 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2384 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002385 if (!FD) {
2386 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2387 << NI.getName();
2388 if (ULE->getType() == S.Context.OverloadTy)
2389 S.NoteAllOverloadCandidates(ULE);
2390 return;
2391 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002392 } else {
2393 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002394 return;
2395 }
2396
Anders Carlssond277d792009-01-31 01:16:18 +00002397 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002398 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2399 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002400 return;
2401 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002402
Anders Carlsson723f55d2009-02-07 23:16:50 +00002403 // We're currently more strict than GCC about what function types we accept.
2404 // If this ever proves to be a problem it should be easy to fix.
2405 QualType Ty = S.Context.getPointerType(VD->getType());
2406 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002407 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2408 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002409 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2410 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002411 return;
2412 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002413
Michael Han99315932013-01-24 16:46:58 +00002414 D->addAttr(::new (S.Context)
2415 CleanupAttr(Attr.getRange(), S.Context, FD,
2416 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002417}
2418
Mike Stumpd3bb5572009-07-24 19:02:52 +00002419/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002420/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002421static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002422 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002423 uint64_t Idx;
2424 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002425 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002426
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002427 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002428 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002429
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002430 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2431 if (not_nsstring_type &&
2432 !isCFStringType(Ty, S.Context) &&
2433 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002434 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002435 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002436 << (not_nsstring_type ? "a string type" : "an NSString")
2437 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002438 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002439 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002440 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002441 if (!isNSStringType(Ty, S.Context) &&
2442 !isCFStringType(Ty, S.Context) &&
2443 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002444 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002445 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002446 << (not_nsstring_type ? "string type" : "NSString")
2447 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002448 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002449 }
2450
Alp Toker601b22c2014-01-21 23:35:24 +00002451 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002452 // because that has corrected for the implicit this parameter, and is zero-
2453 // based. The attribute expects what the user wrote explicitly.
2454 llvm::APSInt Val;
2455 IdxExpr->EvaluateAsInt(Val, S.Context);
2456
Michael Han99315932013-01-24 16:46:58 +00002457 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002458 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002459 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002460}
2461
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002462enum FormatAttrKind {
2463 CFStringFormat,
2464 NSStringFormat,
2465 StrftimeFormat,
2466 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002467 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002468 InvalidFormat
2469};
2470
2471/// getFormatAttrKind - Map from format attribute names to supported format
2472/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002473static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002474 return llvm::StringSwitch<FormatAttrKind>(Format)
2475 // Check for formats that get handled specially.
2476 .Case("NSString", NSStringFormat)
2477 .Case("CFString", CFStringFormat)
2478 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002479
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002480 // Otherwise, check for supported formats.
2481 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2482 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2483 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002484
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002485 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2486 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002487}
2488
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002489/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002490/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002491static void handleInitPriorityAttr(Sema &S, Decl *D,
2492 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002493 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002494 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2495 return;
2496 }
2497
Aaron Ballman4a611152013-11-27 16:34:09 +00002498 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002499 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2500 Attr.setInvalid();
2501 return;
2502 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002503 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002504 if (S.Context.getAsArrayType(T))
2505 T = S.Context.getBaseElementType(T);
2506 if (!T->getAs<RecordType>()) {
2507 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2508 Attr.setInvalid();
2509 return;
2510 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002511
2512 Expr *E = Attr.getArgAsExpr(0);
2513 uint32_t prioritynum;
2514 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002515 Attr.setInvalid();
2516 return;
2517 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002518
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002519 if (prioritynum < 101 || prioritynum > 65535) {
2520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002521 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002522 Attr.setInvalid();
2523 return;
2524 }
Michael Han99315932013-01-24 16:46:58 +00002525 D->addAttr(::new (S.Context)
2526 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2527 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002528}
2529
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002530FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2531 IdentifierInfo *Format, int FormatIdx,
2532 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002533 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002534 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002535 for (auto *F : D->specific_attrs<FormatAttr>()) {
2536 if (F->getType() == Format &&
2537 F->getFormatIdx() == FormatIdx &&
2538 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002539 // If we don't have a valid location for this attribute, adopt the
2540 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002541 if (F->getLocation().isInvalid())
2542 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002543 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002544 }
2545 }
2546
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002547 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2548 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002549}
2550
Mike Stumpd3bb5572009-07-24 19:02:52 +00002551/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002552/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002553static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002554 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002555 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002556 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002557 return;
2558 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559
Chandler Carruth743682b2010-11-16 08:35:43 +00002560 // In C++ the implicit 'this' function parameter also counts, and they are
2561 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002562 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002563 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002564
Aaron Ballman00e99962013-08-31 01:11:41 +00002565 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2566 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567
2568 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002569 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002570 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002571 // If we've modified the string name, we need a new identifier for it.
2572 II = &S.Context.Idents.get(Format);
2573 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002574
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002575 // Check for supported formats.
2576 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002577
2578 if (Kind == IgnoredFormat)
2579 return;
2580
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002581 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002582 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002583 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002584 return;
2585 }
2586
2587 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002588 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002589 uint32_t Idx;
2590 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002593 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002594 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002595 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002596 return;
2597 }
2598
2599 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002600 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002601
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002602 if (HasImplicitThisParam) {
2603 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002604 S.Diag(Attr.getLoc(),
2605 diag::err_format_attribute_implicit_this_format_string)
2606 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002607 return;
2608 }
2609 ArgIdx--;
2610 }
Mike Stump11289f42009-09-09 15:08:12 +00002611
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002612 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002613 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002615 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002616 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002617 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002618 << "a CFString" << IdxExpr->getSourceRange()
2619 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002620 return;
2621 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002622 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002623 // FIXME: do we need to check if the type is NSString*? What are the
2624 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002625 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002626 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002627 << "an NSString" << IdxExpr->getSourceRange()
2628 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002629 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002630 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002632 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002633 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002634 << "a string type" << IdxExpr->getSourceRange()
2635 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002636 return;
2637 }
2638
2639 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002640 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002641 uint32_t FirstArg;
2642 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002643 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644
2645 // check if the function is variadic if the 3rd argument non-zero
2646 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002647 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002648 ++NumArgs; // +1 for ...
2649 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002650 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002651 return;
2652 }
2653 }
2654
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002655 // strftime requires FirstArg to be 0 because it doesn't read from any
2656 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002657 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002658 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002659 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2660 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002661 return;
2662 }
2663 // if 0 it disables parameter checking (to use with e.g. va_list)
2664 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002666 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002667 return;
2668 }
2669
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002670 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002671 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002672 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002673 if (NewAttr)
2674 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675}
2676
Chandler Carruthedc2c642011-07-02 00:01:44 +00002677static void handleTransparentUnionAttr(Sema &S, Decl *D,
2678 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002679 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002680 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002681 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002682 if (TD && TD->getUnderlyingType()->isUnionType())
2683 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2684 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002685 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002686
2687 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002689 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002690 return;
2691 }
2692
John McCallf937c022011-10-07 06:10:15 +00002693 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002694 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002695 diag::warn_transparent_union_attribute_not_definition);
2696 return;
2697 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002698
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002699 RecordDecl::field_iterator Field = RD->field_begin(),
2700 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002701 if (Field == FieldEnd) {
2702 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2703 return;
2704 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002705
David Blaikie40ed2972012-06-06 20:45:41 +00002706 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002707 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002708 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002709 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002710 diag::warn_transparent_union_attribute_floating)
2711 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002712 return;
2713 }
2714
2715 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2716 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2717 for (; Field != FieldEnd; ++Field) {
2718 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002719 // FIXME: this isn't fully correct; we also need to test whether the
2720 // members of the union would all have the same calling convention as the
2721 // first member of the union. Checking just the size and alignment isn't
2722 // sufficient (consider structs passed on the stack instead of in registers
2723 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002724 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002725 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002726 // Warn if we drop the attribute.
2727 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002728 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002729 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002730 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 diag::warn_transparent_union_attribute_field_size_align)
2732 << isSize << Field->getDeclName() << FieldBits;
2733 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002734 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002735 diag::note_transparent_union_first_field_size_align)
2736 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002737 return;
2738 }
2739 }
2740
Michael Han99315932013-01-24 16:46:58 +00002741 RD->addAttr(::new (S.Context)
2742 TransparentUnionAttr(Attr.getRange(), S.Context,
2743 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002744}
2745
Chandler Carruthedc2c642011-07-02 00:01:44 +00002746static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747 // Make sure that there is a string literal as the annotation's single
2748 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002749 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002750 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002751 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002752
2753 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002754 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2755 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002756 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002757 }
Michael Han99315932013-01-24 16:46:58 +00002758
2759 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002760 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002761 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002762}
2763
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002764static void handleAlignValueAttr(Sema &S, Decl *D,
2765 const AttributeList &Attr) {
2766 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2767 Attr.getAttributeSpellingListIndex());
2768}
2769
2770void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2771 unsigned SpellingListIndex) {
2772 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2773 SourceLocation AttrLoc = AttrRange.getBegin();
2774
2775 QualType T;
2776 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2777 T = TD->getUnderlyingType();
2778 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2779 T = VD->getType();
2780 else
2781 llvm_unreachable("Unknown decl type for align_value");
2782
2783 if (!T->isDependentType() && !T->isAnyPointerType() &&
2784 !T->isReferenceType() && !T->isMemberPointerType()) {
2785 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2786 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2787 return;
2788 }
2789
2790 if (!E->isValueDependent()) {
2791 llvm::APSInt Alignment(32);
2792 ExprResult ICE
2793 = VerifyIntegerConstantExpression(E, &Alignment,
2794 diag::err_align_value_attribute_argument_not_int,
2795 /*AllowFold*/ false);
2796 if (ICE.isInvalid())
2797 return;
2798
2799 if (!Alignment.isPowerOf2()) {
2800 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2801 << E->getSourceRange();
2802 return;
2803 }
2804
2805 D->addAttr(::new (Context)
2806 AlignValueAttr(AttrRange, Context, ICE.get(),
2807 SpellingListIndex));
2808 return;
2809 }
2810
2811 // Save dependent expressions in the AST to be instantiated.
2812 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2813 return;
2814}
2815
Chandler Carruthedc2c642011-07-02 00:01:44 +00002816static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002817 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002818 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002819 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2820 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002821 return;
2822 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002823
Richard Smith848e1f12013-02-01 08:12:08 +00002824 if (Attr.getNumArgs() == 0) {
2825 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002826 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002827 return;
2828 }
2829
Aaron Ballman00e99962013-08-31 01:11:41 +00002830 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002831 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2832 S.Diag(Attr.getEllipsisLoc(),
2833 diag::err_pack_expansion_without_parameter_packs);
2834 return;
2835 }
2836
2837 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2838 return;
2839
2840 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2841 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002842}
2843
2844void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002845 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002846 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2847 SourceLocation AttrLoc = AttrRange.getBegin();
2848
Richard Smith1dba27c2013-01-29 09:02:09 +00002849 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002850 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002851 // C++11 [dcl.align]p1:
2852 // An alignment-specifier may be applied to a variable or to a class
2853 // data member, but it shall not be applied to a bit-field, a function
2854 // parameter, the formal parameter of a catch clause, or a variable
2855 // declared with the register storage class specifier. An
2856 // alignment-specifier may also be applied to the declaration of a class
2857 // or enumeration type.
2858 // C11 6.7.5/2:
2859 // An alignment attribute shall not be specified in a declaration of
2860 // a typedef, or a bit-field, or a function, or a parameter, or an
2861 // object declared with the register storage-class specifier.
2862 int DiagKind = -1;
2863 if (isa<ParmVarDecl>(D)) {
2864 DiagKind = 0;
2865 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2866 if (VD->getStorageClass() == SC_Register)
2867 DiagKind = 1;
2868 if (VD->isExceptionVariable())
2869 DiagKind = 2;
2870 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2871 if (FD->isBitField())
2872 DiagKind = 3;
2873 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002874 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002875 << (TmpAttr.isC11() ? ExpectedVariableOrField
2876 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002877 return;
2878 }
2879 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002880 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002881 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002882 return;
2883 }
2884 }
2885
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002886 if (E->isTypeDependent() || E->isValueDependent()) {
2887 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002888 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2889 AA->setPackExpansion(IsPackExpansion);
2890 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002891 return;
2892 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002893
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002894 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002895 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002896 ExprResult ICE
2897 = VerifyIntegerConstantExpression(E, &Alignment,
2898 diag::err_aligned_attribute_argument_not_int,
2899 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002900 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002901 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002902
2903 // C++11 [dcl.align]p2:
2904 // -- if the constant expression evaluates to zero, the alignment
2905 // specifier shall have no effect
2906 // C11 6.7.5p6:
2907 // An alignment specification of zero has no effect.
2908 if (!(TmpAttr.isAlignas() && !Alignment) &&
2909 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Hal Finkelbcc06082014-09-07 22:58:14 +00002910 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002911 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002912 return;
2913 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002914
David Majnemerabecae72014-02-12 20:36:10 +00002915 // Alignment calculations can wrap around if it's greater than 2**28.
2916 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2917 if (Alignment.getZExtValue() > MaxValidAlignment) {
2918 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2919 << E->getSourceRange();
2920 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002921 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002922
Richard Smith44c247f2013-02-22 08:32:16 +00002923 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002924 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002925 AA->setPackExpansion(IsPackExpansion);
2926 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002927}
2928
Michael Hanaf02bbe2013-02-01 01:19:17 +00002929void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002930 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002931 // FIXME: Cache the number on the Attr object if non-dependent?
2932 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002933 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2934 SpellingListIndex);
2935 AA->setPackExpansion(IsPackExpansion);
2936 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002937}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002938
Richard Smith848e1f12013-02-01 08:12:08 +00002939void Sema::CheckAlignasUnderalignment(Decl *D) {
2940 assert(D->hasAttrs() && "no attributes on decl");
2941
2942 QualType Ty;
2943 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2944 Ty = VD->getType();
2945 else
2946 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002947 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002948 return;
2949
2950 // C++11 [dcl.align]p5, C11 6.7.5/4:
2951 // The combined effect of all alignment attributes in a declaration shall
2952 // not specify an alignment that is less strict than the alignment that
2953 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002954 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002955 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002956 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002957 if (I->isAlignmentDependent())
2958 return;
2959 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002960 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002961 Align = std::max(Align, I->getAlignment(Context));
2962 }
2963
2964 if (AlignasAttr && Align) {
2965 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2966 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2967 if (NaturalAlign > RequestedAlign)
2968 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2969 << Ty << (unsigned)NaturalAlign.getQuantity();
2970 }
2971}
2972
David Majnemer2c4e00a2014-01-29 22:07:36 +00002973bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002974 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002975 MSInheritanceAttr::Spelling SemanticSpelling) {
2976 assert(RD->hasDefinition() && "RD has no definition!");
2977
David Majnemer98c9ee22014-02-07 00:43:07 +00002978 // We may not have seen base specifiers or any virtual methods yet. We will
2979 // have to wait until the record is defined to catch any mismatches.
2980 if (!RD->getDefinition()->isCompleteDefinition())
2981 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002982
David Majnemer98c9ee22014-02-07 00:43:07 +00002983 // The unspecified model never matches what a definition could need.
2984 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2985 return false;
2986
David Majnemer4bb09802014-02-10 19:50:15 +00002987 if (BestCase) {
2988 if (RD->calculateInheritanceModel() == SemanticSpelling)
2989 return false;
2990 } else {
2991 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2992 return false;
2993 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002994
2995 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2996 << 0 /*definition*/;
2997 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2998 << RD->getNameAsString();
2999 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003000}
3001
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003002/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003003/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003004///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003005/// Despite what would be logical, the mode attribute is a decl attribute, not a
3006/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3007/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003008static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003009 // This attribute isn't documented, but glibc uses it. It changes
3010 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003011 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003012 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3013 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003014 return;
3015 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003016
Aaron Ballman00e99962013-08-31 01:11:41 +00003017 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3018 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003019
3020 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003021 if (Str.startswith("__") && Str.endswith("__"))
3022 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003023
3024 unsigned DestWidth = 0;
3025 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003026 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003027 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003028 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003029 switch (Str[0]) {
3030 case 'Q': DestWidth = 8; break;
3031 case 'H': DestWidth = 16; break;
3032 case 'S': DestWidth = 32; break;
3033 case 'D': DestWidth = 64; break;
3034 case 'X': DestWidth = 96; break;
3035 case 'T': DestWidth = 128; break;
3036 }
3037 if (Str[1] == 'F') {
3038 IntegerMode = false;
3039 } else if (Str[1] == 'C') {
3040 IntegerMode = false;
3041 ComplexMode = true;
3042 } else if (Str[1] != 'I') {
3043 DestWidth = 0;
3044 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003045 break;
3046 case 4:
3047 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3048 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003049 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003050 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003051 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003052 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003053 break;
3054 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003055 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003056 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003057 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003058 case 11:
3059 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003060 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003061 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003062 }
3063
3064 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003065 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003066 OldTy = TD->getUnderlyingType();
3067 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3068 OldTy = VD->getType();
3069 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003070 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003071 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003072 return;
3073 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003074
John McCall9dd450b2009-09-21 23:43:11 +00003075 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003076 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3077 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003078 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003079 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3080 } else if (ComplexMode) {
3081 if (!OldTy->isComplexType())
3082 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3083 } else {
3084 if (!OldTy->isFloatingType())
3085 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3086 }
3087
Mike Stump87c57ac2009-05-16 07:39:55 +00003088 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3089 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003090 // FIXME: Make sure floating-point mappings are accurate
3091 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003092 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003093 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003094 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003095 }
3096
3097 QualType NewTy;
3098
3099 if (IntegerMode)
3100 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3101 OldTy->isSignedIntegerType());
3102 else
3103 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3104
3105 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003106 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003107 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003108 }
3109
Eli Friedman4735374e2009-03-03 06:41:03 +00003110 if (ComplexMode) {
3111 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003112 }
3113
3114 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003115 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3116 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3117 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003118 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003119
3120 D->addAttr(::new (S.Context)
3121 ModeAttr(Attr.getRange(), S.Context, Name,
3122 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003123}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003124
Chandler Carruthedc2c642011-07-02 00:01:44 +00003125static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003126 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3127 if (!VD->hasGlobalStorage())
3128 S.Diag(Attr.getLoc(),
3129 diag::warn_attribute_requires_functions_or_static_globals)
3130 << Attr.getName();
3131 } else if (!isFunctionOrMethod(D)) {
3132 S.Diag(Attr.getLoc(),
3133 diag::warn_attribute_requires_functions_or_static_globals)
3134 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003135 return;
3136 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003137
Michael Han99315932013-01-24 16:46:58 +00003138 D->addAttr(::new (S.Context)
3139 NoDebugAttr(Attr.getRange(), S.Context,
3140 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003141}
3142
Paul Robinson30e41fb2014-12-15 18:57:28 +00003143AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003144 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003145 unsigned AttrSpellingListIndex) {
3146 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003147 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003148 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3149 return nullptr;
3150 }
3151
3152 if (D->hasAttr<AlwaysInlineAttr>())
3153 return nullptr;
3154
3155 return ::new (Context) AlwaysInlineAttr(Range, Context,
3156 AttrSpellingListIndex);
3157}
3158
3159MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3160 unsigned AttrSpellingListIndex) {
3161 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3162 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3163 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3164 return nullptr;
3165 }
3166
3167 if (D->hasAttr<MinSizeAttr>())
3168 return nullptr;
3169
3170 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3171}
3172
3173OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3174 unsigned AttrSpellingListIndex) {
3175 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3176 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3177 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3178 D->dropAttr<AlwaysInlineAttr>();
3179 }
3180 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3181 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3182 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3183 D->dropAttr<MinSizeAttr>();
3184 }
3185
3186 if (D->hasAttr<OptimizeNoneAttr>())
3187 return nullptr;
3188
3189 return ::new (Context) OptimizeNoneAttr(Range, Context,
3190 AttrSpellingListIndex);
3191}
3192
Paul Robinsonf0674352014-03-31 22:29:15 +00003193static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3194 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003195 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3196 D, Attr.getRange(), Attr.getName(),
3197 Attr.getAttributeSpellingListIndex()))
3198 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003199}
3200
Paul Robinson080b1f32015-01-13 18:34:56 +00003201static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3202 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3203 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3204 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003205}
3206
Paul Robinsonf0674352014-03-31 22:29:15 +00003207static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3208 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003209 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3210 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3211 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003212}
3213
Chandler Carruthedc2c642011-07-02 00:01:44 +00003214static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003215 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003216 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003217 SourceRange RTRange = FD->getReturnTypeSourceRange();
3218 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003219 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003220 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3221 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003222 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003223 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003224
Aaron Ballman3aff6332013-12-02 19:30:36 +00003225 D->addAttr(::new (S.Context)
3226 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003227 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003228}
3229
Chandler Carruthedc2c642011-07-02 00:01:44 +00003230static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003231 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003232 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003233 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003234 return;
3235 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003236
Michael Han99315932013-01-24 16:46:58 +00003237 D->addAttr(::new (S.Context)
3238 GNUInlineAttr(Attr.getRange(), S.Context,
3239 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003240}
3241
Chandler Carruthedc2c642011-07-02 00:01:44 +00003242static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003243 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003244
Aaron Ballman02df2e02012-12-09 17:45:41 +00003245 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003246 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003247 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3248 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003249 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003250 return;
3251
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003252 if (!isa<ObjCMethodDecl>(D)) {
3253 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3254 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003255 return;
3256 }
3257
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003258 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003259 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003260 D->addAttr(::new (S.Context)
3261 FastCallAttr(Attr.getRange(), S.Context,
3262 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003263 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003264 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003265 D->addAttr(::new (S.Context)
3266 StdCallAttr(Attr.getRange(), S.Context,
3267 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003268 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003269 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003270 D->addAttr(::new (S.Context)
3271 ThisCallAttr(Attr.getRange(), S.Context,
3272 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003273 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003274 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003275 D->addAttr(::new (S.Context)
3276 CDeclAttr(Attr.getRange(), S.Context,
3277 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003278 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003279 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003280 D->addAttr(::new (S.Context)
3281 PascalAttr(Attr.getRange(), S.Context,
3282 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003283 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003284 case AttributeList::AT_VectorCall:
3285 D->addAttr(::new (S.Context)
3286 VectorCallAttr(Attr.getRange(), S.Context,
3287 Attr.getAttributeSpellingListIndex()));
3288 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003289 case AttributeList::AT_MSABI:
3290 D->addAttr(::new (S.Context)
3291 MSABIAttr(Attr.getRange(), S.Context,
3292 Attr.getAttributeSpellingListIndex()));
3293 return;
3294 case AttributeList::AT_SysVABI:
3295 D->addAttr(::new (S.Context)
3296 SysVABIAttr(Attr.getRange(), S.Context,
3297 Attr.getAttributeSpellingListIndex()));
3298 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003299 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003300 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003301 switch (CC) {
3302 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003303 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003304 break;
3305 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003306 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003307 break;
3308 default:
3309 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003310 }
3311
Michael Han99315932013-01-24 16:46:58 +00003312 D->addAttr(::new (S.Context)
3313 PcsAttr(Attr.getRange(), S.Context, PCS,
3314 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003315 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003316 }
Derek Schuffa2020962012-10-16 22:30:41 +00003317 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003318 D->addAttr(::new (S.Context)
3319 PnaclCallAttr(Attr.getRange(), S.Context,
3320 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003321 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003322 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003323 D->addAttr(::new (S.Context)
3324 IntelOclBiccAttr(Attr.getRange(), S.Context,
3325 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003326 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003327
Abramo Bagnara50099372010-04-30 13:10:51 +00003328 default:
3329 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003330 }
3331}
3332
Aaron Ballman02df2e02012-12-09 17:45:41 +00003333bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3334 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003335 if (attr.isInvalid())
3336 return true;
3337
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003338 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003339 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003340 attr.setInvalid();
3341 return true;
3342 }
3343
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003344 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003345 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003346 case AttributeList::AT_CDecl: CC = CC_C; break;
3347 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3348 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3349 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3350 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003351 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003352 case AttributeList::AT_MSABI:
3353 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3354 CC_X86_64Win64;
3355 break;
3356 case AttributeList::AT_SysVABI:
3357 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3358 CC_C;
3359 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003360 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003361 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003362 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003363 attr.setInvalid();
3364 return true;
3365 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003366 if (StrRef == "aapcs") {
3367 CC = CC_AAPCS;
3368 break;
3369 } else if (StrRef == "aapcs-vfp") {
3370 CC = CC_AAPCS_VFP;
3371 break;
3372 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003373
3374 attr.setInvalid();
3375 Diag(attr.getLoc(), diag::err_invalid_pcs);
3376 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003377 }
Derek Schuffa2020962012-10-16 22:30:41 +00003378 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003379 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003380 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003381 }
3382
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003383 const TargetInfo &TI = Context.getTargetInfo();
3384 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3385 if (A == TargetInfo::CCCR_Warning) {
3386 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003387
3388 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3389 if (FD)
3390 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3391 TargetInfo::CCMT_NonMember;
3392 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003393 }
3394
John McCall3882ace2011-01-05 12:14:39 +00003395 return false;
3396}
3397
John McCall3882ace2011-01-05 12:14:39 +00003398/// Checks a regparm attribute, returning true if it is ill-formed and
3399/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003400bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3401 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003402 return true;
3403
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003404 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003405 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003406 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003407 }
Eli Friedman7044b762009-03-27 21:06:47 +00003408
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003409 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003410 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003411 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003412 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003413 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003414 }
3415
Douglas Gregore8bbc122011-09-02 00:18:52 +00003416 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003417 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003418 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003419 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003420 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003421 }
3422
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003423 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003424 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003425 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003426 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003427 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003428 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003429 }
3430
John McCall3882ace2011-01-05 12:14:39 +00003431 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003432}
3433
Aaron Ballman66039932013-12-19 00:41:31 +00003434static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3435 const AttributeList &Attr) {
Aaron Ballman66039932013-12-19 00:41:31 +00003436 uint32_t MaxThreads, MinBlocks = 0;
3437 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3438 return;
3439 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3440 Attr.getArgAsExpr(1),
3441 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003442 return;
3443
3444 D->addAttr(::new (S.Context)
3445 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3446 MaxThreads, MinBlocks,
3447 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003448}
3449
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003450static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3451 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003452 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003453 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003454 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003455 return;
3456 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003457
3458 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003459 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003460
Aaron Ballman00e99962013-08-31 01:11:41 +00003461 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003462
3463 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3464 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3465 << Attr.getName() << ExpectedFunctionOrMethod;
3466 return;
3467 }
3468
3469 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003470 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3471 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003472 return;
3473
3474 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003475 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3476 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003477 return;
3478
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003479 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003480 if (IsPointer) {
3481 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003482 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003483 if (!BufferTy->isPointerType()) {
3484 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003485 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003486 }
3487 }
3488
Michael Han99315932013-01-24 16:46:58 +00003489 D->addAttr(::new (S.Context)
3490 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3491 ArgumentIdx, TypeTagIdx, IsPointer,
3492 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003493}
3494
3495static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3496 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003497 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003499 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003500 return;
3501 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003502
3503 if (!checkAttributeNumArgs(S, Attr, 1))
3504 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003505
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003506 if (!isa<VarDecl>(D)) {
3507 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3508 << Attr.getName() << ExpectedVariable;
3509 return;
3510 }
3511
Aaron Ballman00e99962013-08-31 01:11:41 +00003512 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003513 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003514 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3515 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003516
Michael Han99315932013-01-24 16:46:58 +00003517 D->addAttr(::new (S.Context)
3518 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003519 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003520 Attr.getLayoutCompatible(),
3521 Attr.getMustBeNull(),
3522 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003523}
3524
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003525//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003526// Checker-specific attribute handlers.
3527//===----------------------------------------------------------------------===//
3528
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003529static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003530 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003531 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003532}
3533
John McCalled433932011-01-25 03:31:58 +00003534static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003535 return type->isDependentType() ||
3536 type->isObjCObjectPointerType() ||
3537 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003538}
3539static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003540 return type->isDependentType() ||
3541 type->isPointerType() ||
3542 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003543}
3544
Chandler Carruthedc2c642011-07-02 00:01:44 +00003545static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003546 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003547 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003548
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003549 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003550 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3551 cf = false;
3552 } else {
3553 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3554 cf = true;
3555 }
3556
3557 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003558 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003559 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003560 return;
3561 }
3562
3563 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003564 param->addAttr(::new (S.Context)
3565 CFConsumedAttr(Attr.getRange(), S.Context,
3566 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003567 else
Michael Han99315932013-01-24 16:46:58 +00003568 param->addAttr(::new (S.Context)
3569 NSConsumedAttr(Attr.getRange(), S.Context,
3570 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003571}
3572
Chandler Carruthedc2c642011-07-02 00:01:44 +00003573static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3574 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003575
John McCalled433932011-01-25 03:31:58 +00003576 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003577
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003578 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003579 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003580 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003581 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003582 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003583 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3584 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003585 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003586 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003587 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003588 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003589 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003590 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003591 return;
3592 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003593
John McCalled433932011-01-25 03:31:58 +00003594 bool typeOK;
3595 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003596 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003597 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003598 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003599 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003600 cf = false;
3601 break;
3602
3603 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003604 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003605 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3606 cf = false;
3607 break;
3608
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003609 case AttributeList::AT_CFReturnsRetained:
3610 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003611 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3612 cf = true;
3613 break;
3614 }
3615
3616 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003617 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003618 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003619 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003620 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003621
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003622 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003623 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003624 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003625 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00003626 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
3627 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003628 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003629 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003630 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
3631 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003632 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003633 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003634 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
3635 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003636 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003637 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003638 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
3639 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003640 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003641 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003642 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
3643 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003644 return;
3645 };
3646}
3647
John McCallcf166702011-07-22 08:53:00 +00003648static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3649 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003650 const int EP_ObjCMethod = 1;
3651 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003652
John McCallcf166702011-07-22 08:53:00 +00003653 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003654 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003655 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003656 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003657 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003658 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003659
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003660 if (!resultType->isReferenceType() &&
3661 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003662 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003663 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003664 << attr.getName()
3665 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003666 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003667
3668 // Drop the attribute.
3669 return;
3670 }
3671
Nico Weber462fd1e2015-01-07 23:50:05 +00003672 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
3673 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003674}
3675
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003676static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3677 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003678 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003679
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003680 DeclContext *DC = method->getDeclContext();
3681 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3682 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3683 << attr.getName() << 0;
3684 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3685 return;
3686 }
3687 if (method->getMethodFamily() == OMF_dealloc) {
3688 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3689 << attr.getName() << 1;
3690 return;
3691 }
3692
Michael Han99315932013-01-24 16:46:58 +00003693 method->addAttr(::new (S.Context)
3694 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3695 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003696}
3697
Aaron Ballmanfb763042013-12-02 18:05:46 +00003698static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3699 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003700 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003701 return;
John McCall32f5fe12011-09-30 05:12:12 +00003702
Aaron Ballmanfb763042013-12-02 18:05:46 +00003703 D->addAttr(::new (S.Context)
3704 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3705 Attr.getAttributeSpellingListIndex()));
3706}
3707
3708static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3709 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003710 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003711 return;
3712
3713 D->addAttr(::new (S.Context)
3714 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3715 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003716}
3717
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003718static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3719 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003720 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003721
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003722 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003723 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003724 return;
3725 }
3726
3727 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003728 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003729 Attr.getAttributeSpellingListIndex()));
3730}
3731
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003732static void handleObjCBridgeMutableAttr(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;
3735
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003736 if (!Parm) {
3737 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3738 return;
3739 }
3740
3741 D->addAttr(::new (S.Context)
3742 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3743 Attr.getAttributeSpellingListIndex()));
3744}
3745
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003746static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3747 const AttributeList &Attr) {
3748 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003749 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003750 if (!RelatedClass) {
3751 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3752 return;
3753 }
3754 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003755 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003756 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003757 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003758 D->addAttr(::new (S.Context)
3759 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3760 ClassMethod, InstanceMethod,
3761 Attr.getAttributeSpellingListIndex()));
3762}
3763
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003764static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3765 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003766 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00003767 if (ObjCCategoryDecl *CatDecl =
3768 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003769 IFace = CatDecl->getClassInterface();
3770 else
3771 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003772 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003773 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003774 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3775 Attr.getAttributeSpellingListIndex()));
3776}
3777
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003778static void handleObjCRuntimeName(Sema &S, Decl *D,
3779 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003780 StringRef MetaDataName;
3781 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3782 return;
3783 D->addAttr(::new (S.Context)
3784 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3785 MetaDataName,
3786 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003787}
3788
Chandler Carruthedc2c642011-07-02 00:01:44 +00003789static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3790 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003791 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003792
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003793 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003794 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003795}
3796
Chandler Carruthedc2c642011-07-02 00:01:44 +00003797static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3798 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003799 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003800 QualType type = vd->getType();
3801
3802 if (!type->isDependentType() &&
3803 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003804 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003805 << type;
3806 return;
3807 }
3808
3809 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3810
3811 // If we have no lifetime yet, check the lifetime we're presumably
3812 // going to infer.
3813 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3814 lifetime = type->getObjCARCImplicitLifetime();
3815
3816 switch (lifetime) {
3817 case Qualifiers::OCL_None:
3818 assert(type->isDependentType() &&
3819 "didn't infer lifetime for non-dependent type?");
3820 break;
3821
3822 case Qualifiers::OCL_Weak: // meaningful
3823 case Qualifiers::OCL_Strong: // meaningful
3824 break;
3825
3826 case Qualifiers::OCL_ExplicitNone:
3827 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003828 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003829 << (lifetime == Qualifiers::OCL_Autoreleasing);
3830 break;
3831 }
3832
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003833 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003834 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3835 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003836}
3837
Francois Picheta83957a2010-12-19 06:50:37 +00003838//===----------------------------------------------------------------------===//
3839// Microsoft specific attribute handlers.
3840//===----------------------------------------------------------------------===//
3841
Chandler Carruthedc2c642011-07-02 00:01:44 +00003842static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003843 if (!S.LangOpts.CPlusPlus) {
3844 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3845 << Attr.getName() << AttributeLangSupport::C;
3846 return;
3847 }
3848
Aaron Ballman60e705e2013-11-24 20:58:02 +00003849 if (!isa<CXXRecordDecl>(D)) {
3850 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3851 << Attr.getName() << ExpectedClass;
3852 return;
3853 }
3854
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003855 StringRef StrRef;
3856 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003857 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003858 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003859
David Majnemer89085342013-08-09 08:56:20 +00003860 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3861 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003862 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3863 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003864
Reid Kleckner140c4a72013-05-17 14:04:52 +00003865 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003866 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003867 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003868 return;
3869 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003870
David Majnemer89085342013-08-09 08:56:20 +00003871 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003872 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003873 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003874 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003875 return;
3876 }
David Majnemer89085342013-08-09 08:56:20 +00003877 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003878 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003879 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003880 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003881 }
Francois Picheta83957a2010-12-19 06:50:37 +00003882
David Majnemer89085342013-08-09 08:56:20 +00003883 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3884 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003885}
3886
David Majnemer2c4e00a2014-01-29 22:07:36 +00003887static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3888 if (!S.LangOpts.CPlusPlus) {
3889 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3890 << Attr.getName() << AttributeLangSupport::C;
3891 return;
3892 }
3893 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003894 D, Attr.getRange(), /*BestCase=*/true,
3895 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003896 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3897 if (IA)
3898 D->addAttr(IA);
3899}
3900
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003901static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3902 const AttributeList &Attr) {
3903 VarDecl *VD = cast<VarDecl>(D);
3904 if (!S.Context.getTargetInfo().isTLSSupported()) {
3905 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3906 return;
3907 }
3908 if (VD->getTSCSpec() != TSCS_unspecified) {
3909 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3910 return;
3911 }
3912 if (VD->hasLocalStorage()) {
3913 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3914 return;
3915 }
3916 VD->addAttr(::new (S.Context) ThreadAttr(
3917 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3918}
3919
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003920static void handleARMInterruptAttr(Sema &S, Decl *D,
3921 const AttributeList &Attr) {
3922 // Check the attribute arguments.
3923 if (Attr.getNumArgs() > 1) {
3924 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3925 << Attr.getName() << 1;
3926 return;
3927 }
3928
3929 StringRef Str;
3930 SourceLocation ArgLoc;
3931
3932 if (Attr.getNumArgs() == 0)
3933 Str = "";
3934 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3935 return;
3936
3937 ARMInterruptAttr::InterruptType Kind;
3938 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3939 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3940 << Attr.getName() << Str << ArgLoc;
3941 return;
3942 }
3943
3944 unsigned Index = Attr.getAttributeSpellingListIndex();
3945 D->addAttr(::new (S.Context)
3946 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3947}
3948
3949static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3950 const AttributeList &Attr) {
3951 if (!checkAttributeNumArgs(S, Attr, 1))
3952 return;
3953
3954 if (!Attr.isArgExpr(0)) {
3955 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3956 << AANT_ArgumentIntegerConstant;
3957 return;
3958 }
3959
3960 // FIXME: Check for decl - it should be void ()(void).
3961
3962 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3963 llvm::APSInt NumParams(32);
3964 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3965 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3966 << Attr.getName() << AANT_ArgumentIntegerConstant
3967 << NumParamsExpr->getSourceRange();
3968 return;
3969 }
3970
3971 unsigned Num = NumParams.getLimitedValue(255);
3972 if ((Num & 1) || Num > 30) {
3973 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3974 << Attr.getName() << (int)NumParams.getSExtValue()
3975 << NumParamsExpr->getSourceRange();
3976 return;
3977 }
3978
Aaron Ballman36a53502014-01-16 13:03:14 +00003979 D->addAttr(::new (S.Context)
3980 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3981 Attr.getAttributeSpellingListIndex()));
3982 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003983}
3984
3985static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3986 // Dispatch the interrupt attribute based on the current target.
3987 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3988 handleMSP430InterruptAttr(S, D, Attr);
3989 else
3990 handleARMInterruptAttr(S, D, Attr);
3991}
3992
Matt Arsenault43fae6c2014-12-04 20:38:18 +00003993static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
3994 const AttributeList &Attr) {
3995 uint32_t NumRegs;
3996 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3997 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
3998 return;
3999
4000 D->addAttr(::new (S.Context)
4001 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4002 NumRegs,
4003 Attr.getAttributeSpellingListIndex()));
4004}
4005
4006static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4007 const AttributeList &Attr) {
4008 uint32_t NumRegs;
4009 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4010 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4011 return;
4012
4013 D->addAttr(::new (S.Context)
4014 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4015 NumRegs,
4016 Attr.getAttributeSpellingListIndex()));
4017}
4018
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004019static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4020 const AttributeList& Attr) {
4021 // If we try to apply it to a function pointer, don't warn, but don't
4022 // do anything, either. It doesn't matter anyway, because there's nothing
4023 // special about calling a force_align_arg_pointer function.
4024 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4025 if (VD && VD->getType()->isFunctionPointerType())
4026 return;
4027 // Also don't warn on function pointer typedefs.
4028 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4029 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4030 TD->getUnderlyingType()->isFunctionType()))
4031 return;
4032 // Attribute can only be applied to function types.
4033 if (!isa<FunctionDecl>(D)) {
4034 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4035 << Attr.getName() << /* function */0;
4036 return;
4037 }
4038
Aaron Ballman36a53502014-01-16 13:03:14 +00004039 D->addAttr(::new (S.Context)
4040 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4041 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004042}
4043
4044DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4045 unsigned AttrSpellingListIndex) {
4046 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004047 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004048 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004049 }
4050
4051 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004052 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004053
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004054 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004055}
4056
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004057DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4058 unsigned AttrSpellingListIndex) {
4059 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004060 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004061 D->dropAttr<DLLImportAttr>();
4062 }
4063
4064 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004065 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004066
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004067 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004068}
4069
Hans Wennborge82f19c2014-06-24 23:57:05 +00004070static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004071 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4072 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4073 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4074 << A.getName();
4075 return;
4076 }
4077
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004078 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4079 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4080 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4081 // MinGW doesn't allow dllimport on inline functions.
4082 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4083 << A.getName();
4084 return;
4085 }
4086 }
4087
Hans Wennborge82f19c2014-06-24 23:57:05 +00004088 unsigned Index = A.getAttributeSpellingListIndex();
4089 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4090 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4091 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004092 if (NewAttr)
4093 D->addAttr(NewAttr);
4094}
4095
David Majnemer2c4e00a2014-01-29 22:07:36 +00004096MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004097Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004098 unsigned AttrSpellingListIndex,
4099 MSInheritanceAttr::Spelling SemanticSpelling) {
4100 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4101 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004102 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004103 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4104 << 1 /*previous declaration*/;
4105 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4106 D->dropAttr<MSInheritanceAttr>();
4107 }
4108
4109 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4110 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004111 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4112 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004113 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004114 }
4115 } else {
4116 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4117 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4118 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004119 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004120 }
4121 if (RD->getDescribedClassTemplate()) {
4122 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4123 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004124 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004125 }
4126 }
4127
4128 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004129 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004130}
4131
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004132static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4133 // The capability attributes take a single string parameter for the name of
4134 // the capability they represent. The lockable attribute does not take any
4135 // parameters. However, semantically, both attributes represent the same
4136 // concept, and so they use the same semantic attribute. Eventually, the
4137 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004138 //
Alp Toker958027b2014-07-14 19:42:55 +00004139 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004140 // literal will be considered a "mutex."
4141 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004142 SourceLocation LiteralLoc;
4143 if (Attr.getKind() == AttributeList::AT_Capability &&
4144 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4145 return;
4146
Aaron Ballman6c810072014-03-05 21:47:13 +00004147 // Currently, there are only two names allowed for a capability: role and
4148 // mutex (case insensitive). Diagnose other capability names.
4149 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4150 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4151
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004152 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4153 Attr.getAttributeSpellingListIndex()));
4154}
4155
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004156static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4157 const AttributeList &Attr) {
4158 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4159 Attr.getArgAsExpr(0),
4160 Attr.getAttributeSpellingListIndex()));
4161}
4162
4163static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4164 const AttributeList &Attr) {
4165 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004166 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004167 return;
4168
4169 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4170 S.Context,
4171 Args.data(), Args.size(),
4172 Attr.getAttributeSpellingListIndex()));
4173}
4174
4175static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4176 const AttributeList &Attr) {
4177 SmallVector<Expr*, 2> Args;
4178 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4179 return;
4180
4181 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4182 S.Context,
4183 Attr.getArgAsExpr(0),
4184 Args.data(),
4185 Args.size(),
4186 Attr.getAttributeSpellingListIndex()));
4187}
4188
4189static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4190 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004191 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004192 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004193 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004194
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004195 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4196 Attr.getRange(), S.Context, Args.data(), Args.size(),
4197 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004198}
4199
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004200static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4201 const AttributeList &Attr) {
4202 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4203 return;
4204
4205 // check that all arguments are lockable objects
4206 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004207 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004208 if (Args.empty())
4209 return;
4210
4211 RequiresCapabilityAttr *RCA = ::new (S.Context)
4212 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4213 Args.size(), Attr.getAttributeSpellingListIndex());
4214
4215 D->addAttr(RCA);
4216}
4217
Aaron Ballman43f40102014-11-14 22:34:56 +00004218static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4219 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4220 if (NSD->isAnonymousNamespace()) {
4221 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4222 // Do not want to attach the attribute to the namespace because that will
4223 // cause confusing diagnostic reports for uses of declarations within the
4224 // namespace.
4225 return;
4226 }
4227 }
4228 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4229}
4230
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004231/// Handles semantic checking for features that are common to all attributes,
4232/// such as checking whether a parameter was properly specified, or the correct
4233/// number of arguments were passed, etc.
4234static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4235 const AttributeList &Attr) {
4236 // Several attributes carry different semantics than the parsing requires, so
4237 // those are opted out of the common handling.
4238 //
4239 // We also bail on unknown and ignored attributes because those are handled
4240 // as part of the target-specific handling logic.
4241 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004242 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004243 return false;
4244
Aaron Ballman3aff6332013-12-02 19:30:36 +00004245 // Check whether the attribute requires specific language extensions to be
4246 // enabled.
4247 if (!Attr.diagnoseLangOpts(S))
4248 return true;
4249
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004250 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4251 // If there are no optional arguments, then checking for the argument count
4252 // is trivial.
4253 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4254 return true;
4255 } else {
4256 // There are optional arguments, so checking is slightly more involved.
4257 if (Attr.getMinArgs() &&
4258 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4259 return true;
4260 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4261 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4262 return true;
4263 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004264
4265 // Check whether the attribute appertains to the given subject.
4266 if (!Attr.diagnoseAppertainsTo(S, D))
4267 return true;
4268
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004269 return false;
4270}
4271
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004272//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004273// Top Level Sema Entry Points
4274//===----------------------------------------------------------------------===//
4275
Richard Smithf8a75c32013-08-29 00:47:48 +00004276/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4277/// the attribute applies to decls. If the attribute is a type attribute, just
4278/// silently ignore it if a GNU attribute.
4279static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4280 const AttributeList &Attr,
4281 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004282 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004283 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004284
Richard Smithf8a75c32013-08-29 00:47:48 +00004285 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4286 // instead.
4287 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4288 return;
4289
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004290 // Unknown attributes are automatically warned on. Target-specific attributes
4291 // which do not apply to the current target architecture are treated as
4292 // though they were unknown attributes.
4293 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4294 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004295 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4296 ? diag::warn_unhandled_ms_attribute_ignored
4297 : diag::warn_unknown_attribute_ignored)
4298 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004299 return;
4300 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004301
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004302 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4303 return;
4304
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004305 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004306 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004307 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004308 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004309 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004310 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004311 handleInterruptAttr(S, D, Attr);
4312 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004313 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004314 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4315 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004316 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004317 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004318 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004319 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004320 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004321 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4322 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004323 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004324 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4325 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004326 case AttributeList::AT_AMDGPUNumVGPR:
4327 handleAMDGPUNumVGPRAttr(S, D, Attr);
4328 break;
4329 case AttributeList::AT_AMDGPUNumSGPR:
4330 handleAMDGPUNumSGPRAttr(S, D, Attr);
4331 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004332 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004333 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4334 break;
4335 case AttributeList::AT_IBOutlet:
4336 handleIBOutlet(S, D, Attr);
4337 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004338 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004339 handleIBOutletCollection(S, D, Attr);
4340 break;
4341 case AttributeList::AT_Alias:
4342 handleAliasAttr(S, D, Attr);
4343 break;
4344 case AttributeList::AT_Aligned:
4345 handleAlignedAttr(S, D, Attr);
4346 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004347 case AttributeList::AT_AlignValue:
4348 handleAlignValueAttr(S, D, Attr);
4349 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004350 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004351 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004352 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004353 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004354 handleAnalyzerNoReturnAttr(S, D, Attr);
4355 break;
4356 case AttributeList::AT_TLSModel:
4357 handleTLSModelAttr(S, D, Attr);
4358 break;
4359 case AttributeList::AT_Annotate:
4360 handleAnnotateAttr(S, D, Attr);
4361 break;
4362 case AttributeList::AT_Availability:
4363 handleAvailabilityAttr(S, D, Attr);
4364 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004365 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004366 handleDependencyAttr(S, scope, D, Attr);
4367 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004368 case AttributeList::AT_Common:
4369 handleCommonAttr(S, D, Attr);
4370 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004371 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004372 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4373 break;
4374 case AttributeList::AT_Constructor:
4375 handleConstructorAttr(S, D, Attr);
4376 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004377 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004378 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4379 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004380 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004381 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004382 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004383 case AttributeList::AT_Destructor:
4384 handleDestructorAttr(S, D, Attr);
4385 break;
4386 case AttributeList::AT_EnableIf:
4387 handleEnableIfAttr(S, D, Attr);
4388 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004389 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004390 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004391 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004392 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004393 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004394 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004395 case AttributeList::AT_OptimizeNone:
4396 handleOptimizeNoneAttr(S, D, Attr);
4397 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004398 case AttributeList::AT_FlagEnum:
4399 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4400 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004401 case AttributeList::AT_Flatten:
4402 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4403 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004404 case AttributeList::AT_Format:
4405 handleFormatAttr(S, D, Attr);
4406 break;
4407 case AttributeList::AT_FormatArg:
4408 handleFormatArgAttr(S, D, Attr);
4409 break;
4410 case AttributeList::AT_CUDAGlobal:
4411 handleGlobalAttr(S, D, Attr);
4412 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004413 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004414 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4415 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004416 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004417 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4418 break;
4419 case AttributeList::AT_GNUInline:
4420 handleGNUInlineAttr(S, D, Attr);
4421 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004422 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004423 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004424 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004425 case AttributeList::AT_Malloc:
4426 handleMallocAttr(S, D, Attr);
4427 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004428 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004429 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4430 break;
4431 case AttributeList::AT_Mode:
4432 handleModeAttr(S, D, Attr);
4433 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004434 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004435 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4436 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004437 case AttributeList::AT_NoSplitStack:
4438 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4439 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004440 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004441 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4442 handleNonNullAttrParameter(S, PVD, Attr);
4443 else
4444 handleNonNullAttr(S, D, Attr);
4445 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004446 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004447 handleReturnsNonNullAttr(S, D, Attr);
4448 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004449 case AttributeList::AT_AssumeAligned:
4450 handleAssumeAlignedAttr(S, D, Attr);
4451 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004452 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004453 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4454 break;
4455 case AttributeList::AT_Ownership:
4456 handleOwnershipAttr(S, D, Attr);
4457 break;
4458 case AttributeList::AT_Cold:
4459 handleColdAttr(S, D, Attr);
4460 break;
4461 case AttributeList::AT_Hot:
4462 handleHotAttr(S, D, Attr);
4463 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004464 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004465 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4466 break;
4467 case AttributeList::AT_NoReturn:
4468 handleNoReturnAttr(S, D, Attr);
4469 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004470 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004471 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4472 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004473 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004474 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4475 break;
4476 case AttributeList::AT_VecReturn:
4477 handleVecReturnAttr(S, D, Attr);
4478 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004479
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004480 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004481 handleObjCOwnershipAttr(S, D, Attr);
4482 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004483 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004484 handleObjCPreciseLifetimeAttr(S, D, Attr);
4485 break;
John McCall31168b02011-06-15 23:02:42 +00004486
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004487 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004488 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4489 break;
John McCallcf166702011-07-22 08:53:00 +00004490
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004491 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004492 handleObjCRequiresSuperAttr(S, D, Attr);
4493 break;
4494
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004495 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004496 handleObjCBridgeAttr(S, scope, D, Attr);
4497 break;
4498
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004499 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004500 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4501 break;
4502
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004503 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004504 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4505 break;
John McCallf1e8b342011-09-29 07:17:38 +00004506
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004507 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004508 handleObjCDesignatedInitializer(S, D, Attr);
4509 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004510
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004511 case AttributeList::AT_ObjCRuntimeName:
4512 handleObjCRuntimeName(S, D, Attr);
4513 break;
4514
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004515 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004516 handleCFAuditedTransferAttr(S, D, Attr);
4517 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004518 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004519 handleCFUnknownTransferAttr(S, D, Attr);
4520 break;
John McCall32f5fe12011-09-30 05:12:12 +00004521
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004522 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004523 case AttributeList::AT_NSConsumed:
4524 handleNSConsumedAttr(S, D, Attr);
4525 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004526 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004527 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4528 break;
John McCalled433932011-01-25 03:31:58 +00004529
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004530 case AttributeList::AT_NSReturnsAutoreleased:
4531 case AttributeList::AT_NSReturnsNotRetained:
4532 case AttributeList::AT_CFReturnsNotRetained:
4533 case AttributeList::AT_NSReturnsRetained:
4534 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004535 handleNSReturnsRetainedAttr(S, D, Attr);
4536 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004537 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004538 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4539 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004540 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004541 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4542 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004543 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004544 handleVecTypeHint(S, D, Attr);
4545 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004546
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004547 case AttributeList::AT_InitPriority:
4548 handleInitPriorityAttr(S, D, Attr);
4549 break;
4550
4551 case AttributeList::AT_Packed:
4552 handlePackedAttr(S, D, Attr);
4553 break;
4554 case AttributeList::AT_Section:
4555 handleSectionAttr(S, D, Attr);
4556 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004557 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004558 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004559 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004560 case AttributeList::AT_ArcWeakrefUnavailable:
4561 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4562 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004563 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004564 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4565 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004566 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004567 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004568 break;
4569 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004570 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4571 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004572 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004573 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4574 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004575 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004576 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4577 break;
4578 case AttributeList::AT_Used:
4579 handleUsedAttr(S, D, Attr);
4580 break;
John McCalld041a9b2013-02-20 01:54:26 +00004581 case AttributeList::AT_Visibility:
4582 handleVisibilityAttr(S, D, Attr, false);
4583 break;
4584 case AttributeList::AT_TypeVisibility:
4585 handleVisibilityAttr(S, D, Attr, true);
4586 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004587 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004588 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4589 break;
4590 case AttributeList::AT_WarnUnusedResult:
4591 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004592 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004593 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004594 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4595 break;
4596 case AttributeList::AT_WeakRef:
4597 handleWeakRefAttr(S, D, Attr);
4598 break;
4599 case AttributeList::AT_WeakImport:
4600 handleWeakImportAttr(S, D, Attr);
4601 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004602 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004603 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004604 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004605 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004606 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4607 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004608 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004609 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004610 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004611 case AttributeList::AT_ObjCNSObject:
4612 handleObjCNSObject(S, D, Attr);
4613 break;
4614 case AttributeList::AT_Blocks:
4615 handleBlocksAttr(S, D, Attr);
4616 break;
4617 case AttributeList::AT_Sentinel:
4618 handleSentinelAttr(S, D, Attr);
4619 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004620 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004621 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4622 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004623 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004624 handleSimpleAttribute<PureAttr>(S, D, Attr);
4625 break;
4626 case AttributeList::AT_Cleanup:
4627 handleCleanupAttr(S, D, Attr);
4628 break;
4629 case AttributeList::AT_NoDebug:
4630 handleNoDebugAttr(S, D, Attr);
4631 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004632 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004633 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4634 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004635 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004636 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4637 break;
4638 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4639 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4640 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004641 case AttributeList::AT_StdCall:
4642 case AttributeList::AT_CDecl:
4643 case AttributeList::AT_FastCall:
4644 case AttributeList::AT_ThisCall:
4645 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004646 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00004647 case AttributeList::AT_MSABI:
4648 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004649 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004650 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004651 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004652 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004653 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004654 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004655 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4656 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004657 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004658 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4659 break;
John McCall8d32c052012-05-22 21:28:12 +00004660
4661 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004662 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004663 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004664 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004665 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004666 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004667 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004668 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004669 handleMSInheritanceAttr(S, D, Attr);
4670 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004671 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004672 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4673 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004674 case AttributeList::AT_Thread:
4675 handleDeclspecThreadAttr(S, D, Attr);
4676 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004677
4678 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004679 case AttributeList::AT_AssertExclusiveLock:
4680 handleAssertExclusiveLockAttr(S, D, Attr);
4681 break;
4682 case AttributeList::AT_AssertSharedLock:
4683 handleAssertSharedLockAttr(S, D, Attr);
4684 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004685 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004686 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4687 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004688 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004689 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004690 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004692 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4693 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004694 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004695 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004696 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004697 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004698 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004699 break;
4700 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004701 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004702 break;
4703 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004704 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004705 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004706 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004707 handleGuardedByAttr(S, D, Attr);
4708 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004709 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004710 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004711 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004712 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004713 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004714 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004715 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004716 handleLockReturnedAttr(S, D, Attr);
4717 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004718 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004719 handleLocksExcludedAttr(S, D, Attr);
4720 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004721 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004722 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004723 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004724 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004725 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004726 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004727 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004728 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004729 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004730
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004731 // Capability analysis attributes.
4732 case AttributeList::AT_Capability:
4733 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004734 handleCapabilityAttr(S, D, Attr);
4735 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004736 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004737 handleRequiresCapabilityAttr(S, D, Attr);
4738 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004739
4740 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004741 handleAssertCapabilityAttr(S, D, Attr);
4742 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004743 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004744 handleAcquireCapabilityAttr(S, D, Attr);
4745 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004746 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004747 handleReleaseCapabilityAttr(S, D, Attr);
4748 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004749 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004750 handleTryAcquireCapabilityAttr(S, D, Attr);
4751 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004752
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004753 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004754 case AttributeList::AT_Consumable:
4755 handleConsumableAttr(S, D, Attr);
4756 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004757 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004758 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4759 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004760 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004761 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4762 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004763 case AttributeList::AT_CallableWhen:
4764 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004765 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004766 case AttributeList::AT_ParamTypestate:
4767 handleParamTypestateAttr(S, D, Attr);
4768 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004769 case AttributeList::AT_ReturnTypestate:
4770 handleReturnTypestateAttr(S, D, Attr);
4771 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004772 case AttributeList::AT_SetTypestate:
4773 handleSetTypestateAttr(S, D, Attr);
4774 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004775 case AttributeList::AT_TestTypestate:
4776 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004777 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004778
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004779 // Type safety attributes.
4780 case AttributeList::AT_ArgumentWithTypeTag:
4781 handleArgumentWithTypeTagAttr(S, D, Attr);
4782 break;
4783 case AttributeList::AT_TypeTagForDatatype:
4784 handleTypeTagForDatatypeAttr(S, D, Attr);
4785 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004786 }
4787}
4788
4789/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4790/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004791void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004792 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004793 bool IncludeCXX11Attributes) {
4794 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004795 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004796
Joey Gouly2cd9db12013-12-13 16:15:28 +00004797 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004798 // GCC accepts
4799 // static int a9 __attribute__((weakref));
4800 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004801 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004802 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4803 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004804 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004805 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004806 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004807
Aaron Ballmanbe243a72014-12-04 22:45:31 +00004808 // FIXME: We should be able to handle this in TableGen as well. It would be
4809 // good to have a way to specify "these attributes must appear as a group",
4810 // for these. Additionally, it would be good to have a way to specify "these
4811 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00004812 if (!D->hasAttr<OpenCLKernelAttr>()) {
4813 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004814 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004815 // FIXME: This emits a different error message than
4816 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004817 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004818 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00004819 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00004820 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004821 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00004822 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00004823 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004824 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004825 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
4826 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
4827 << A << ExpectedKernelFunction;
4828 D->setInvalidDecl();
4829 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
4830 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
4831 << A << ExpectedKernelFunction;
4832 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00004833 }
4834 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004835}
4836
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004837// Annotation attributes are the only attributes allowed after an access
4838// specifier.
4839bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4840 const AttributeList *AttrList) {
4841 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004842 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00004843 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004844 } else {
4845 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4846 return true;
4847 }
4848 }
4849
4850 return false;
4851}
4852
John McCall42856de2011-10-01 05:17:03 +00004853/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4854/// contains any decl attributes that we should warn about.
4855static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4856 for ( ; A; A = A->getNext()) {
4857 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004858 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004859 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4860
4861 if (A->getKind() == AttributeList::UnknownAttribute) {
4862 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4863 << A->getName() << A->getRange();
4864 } else {
4865 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4866 << A->getName() << A->getRange();
4867 }
4868 }
4869}
4870
4871/// checkUnusedDeclAttributes - Given a declarator which is not being
4872/// used to build a declaration, complain about any decl attributes
4873/// which might be lying around on it.
4874void Sema::checkUnusedDeclAttributes(Declarator &D) {
4875 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4876 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4877 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4878 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4879}
4880
Ryan Flynn7d470f32009-07-30 03:15:39 +00004881/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004882/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004883NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4884 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004885 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004886 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004887 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004888 FunctionDecl *NewFD;
4889 // FIXME: Missing call to CheckFunctionDeclaration().
4890 // FIXME: Mangling?
4891 // FIXME: Is the qualifier info correct?
4892 // FIXME: Is the DeclContext correct?
4893 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4894 Loc, Loc, DeclarationName(II),
4895 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004896 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004897 FD->hasPrototype(),
4898 false/*isConstexprSpecified*/);
4899 NewD = NewFD;
4900
4901 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004902 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004903
4904 // Fake up parameter variables; they are declared as if this were
4905 // a typedef.
4906 QualType FDTy = FD->getType();
4907 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4908 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004909 for (const auto &AI : FT->param_types()) {
4910 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004911 Param->setScopeInfo(0, Params.size());
4912 Params.push_back(Param);
4913 }
David Blaikie9c70e042011-09-21 18:16:56 +00004914 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004915 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004916 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4917 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004918 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004919 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004920 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004921 if (VD->getQualifier()) {
4922 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004923 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004924 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004925 }
4926 return NewD;
4927}
4928
James Dennett634962f2012-06-14 21:40:34 +00004929/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004930/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004931void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004932 if (W.getUsed()) return; // only do this once
4933 W.setUsed(true);
4934 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4935 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004936 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004937 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4938 W.getLocation()));
4939 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004940 WeakTopLevelDecl.push_back(NewD);
4941 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4942 // to insert Decl at TU scope, sorry.
4943 DeclContext *SavedContext = CurContext;
4944 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004945 NewD->setDeclContext(CurContext);
4946 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004947 PushOnScopeChains(NewD, S);
4948 CurContext = SavedContext;
4949 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004950 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004951 }
4952}
4953
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004954void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4955 // It's valid to "forward-declare" #pragma weak, in which case we
4956 // have to do this.
4957 LoadExternalWeakUndeclaredIdentifiers();
4958 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004959 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004960 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4961 if (VD->isExternC())
4962 ND = VD;
4963 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4964 if (FD->isExternC())
4965 ND = FD;
4966 if (ND) {
4967 if (IdentifierInfo *Id = ND->getIdentifier()) {
4968 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4969 = WeakUndeclaredIdentifiers.find(Id);
4970 if (I != WeakUndeclaredIdentifiers.end()) {
4971 WeakInfo W = I->second;
4972 DeclApplyPragmaWeak(S, ND, W);
4973 WeakUndeclaredIdentifiers[Id] = W;
4974 }
4975 }
4976 }
4977 }
4978}
4979
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004980/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4981/// it, apply them to D. This is a bit tricky because PD can have attributes
4982/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004983void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004984 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004985 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004986 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004987
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004988 // Walk the declarator structure, applying decl attributes that were in a type
4989 // position to the decl itself. This handles cases like:
4990 // int *__attr__(x)** D;
4991 // when X is a decl attribute.
4992 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4993 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004994 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004995
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004996 // Finally, apply any attributes on the decl itself.
4997 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004998 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004999}
John McCall28a6aea2009-11-04 02:18:39 +00005000
John McCall31168b02011-06-15 23:02:42 +00005001/// Is the given declaration allowed to use a forbidden type?
5002static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5003 // Private ivars are always okay. Unfortunately, people don't
5004 // always properly make their ivars private, even in system headers.
5005 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005006 // Function declarations in sys headers will be marked unavailable.
5007 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5008 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005009 return false;
5010
5011 // Require it to be declared in a system header.
5012 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5013}
5014
5015/// Handle a delayed forbidden-type diagnostic.
5016static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5017 Decl *decl) {
5018 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00005019 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
5020 "this system declaration uses an unsupported type",
5021 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005022 return;
5023 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005024 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005025 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005026 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005027 // kind of forbidden type messages on unavailable functions.
5028 if (FD->hasAttr<UnavailableAttr>() &&
5029 diag.getForbiddenTypeDiagnostic() ==
5030 diag::err_arc_array_param_no_ownership) {
5031 diag.Triggered = true;
5032 return;
5033 }
5034 }
John McCall31168b02011-06-15 23:02:42 +00005035
5036 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5037 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5038 diag.Triggered = true;
5039}
5040
Aaron Ballmanfb237522014-10-15 15:37:51 +00005041
5042static bool isDeclDeprecated(Decl *D) {
5043 do {
5044 if (D->isDeprecated())
5045 return true;
5046 // A category implicitly has the availability of the interface.
5047 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5048 return CatD->getClassInterface()->isDeprecated();
5049 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5050 return false;
5051}
5052
5053static bool isDeclUnavailable(Decl *D) {
5054 do {
5055 if (D->isUnavailable())
5056 return true;
5057 // A category implicitly has the availability of the interface.
5058 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5059 return CatD->getClassInterface()->isUnavailable();
5060 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5061 return false;
5062}
5063
5064static void DoEmitAvailabilityWarning(Sema &S, DelayedDiagnostic::DDKind K,
5065 Decl *Ctx, const NamedDecl *D,
5066 StringRef Message, SourceLocation Loc,
5067 const ObjCInterfaceDecl *UnknownObjCClass,
5068 const ObjCPropertyDecl *ObjCProperty,
5069 bool ObjCPropertyAccess) {
5070 // Diagnostics for deprecated or unavailable.
5071 unsigned diag, diag_message, diag_fwdclass_message;
5072
5073 // Matches 'diag::note_property_attribute' options.
5074 unsigned property_note_select;
5075
5076 // Matches diag::note_availability_specified_here.
5077 unsigned available_here_select_kind;
5078
5079 // Don't warn if our current context is deprecated or unavailable.
5080 switch (K) {
5081 case DelayedDiagnostic::Deprecation:
5082 if (isDeclDeprecated(Ctx))
5083 return;
5084 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5085 : diag::warn_property_method_deprecated;
5086 diag_message = diag::warn_deprecated_message;
5087 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5088 property_note_select = /* deprecated */ 0;
5089 available_here_select_kind = /* deprecated */ 2;
5090 break;
5091
5092 case DelayedDiagnostic::Unavailable:
5093 if (isDeclUnavailable(Ctx))
5094 return;
5095 diag = !ObjCPropertyAccess ? diag::err_unavailable
5096 : diag::err_property_method_unavailable;
5097 diag_message = diag::err_unavailable_message;
5098 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5099 property_note_select = /* unavailable */ 1;
5100 available_here_select_kind = /* unavailable */ 0;
5101 break;
5102
5103 default:
5104 llvm_unreachable("Neither a deprecation or unavailable kind");
5105 }
5106
Aaron Ballmanfb237522014-10-15 15:37:51 +00005107 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005108 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005109 if (ObjCProperty)
5110 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5111 << ObjCProperty->getDeclName() << property_note_select;
5112 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005113 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005114 if (ObjCProperty)
5115 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5116 << ObjCProperty->getDeclName() << property_note_select;
5117 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005118 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005119 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5120 }
5121
5122 S.Diag(D->getLocation(), diag::note_availability_specified_here)
5123 << D << available_here_select_kind;
5124}
5125
5126static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5127 Decl *Ctx) {
5128 DD.Triggered = true;
5129 DoEmitAvailabilityWarning(S, (DelayedDiagnostic::DDKind)DD.Kind, Ctx,
5130 DD.getDeprecationDecl(), DD.getDeprecationMessage(),
5131 DD.Loc, DD.getUnknownObjCClass(),
5132 DD.getObjCProperty(), false);
5133}
5134
John McCall2ec85372012-05-07 06:16:41 +00005135void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5136 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005137 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005138 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005139
John McCall2ec85372012-05-07 06:16:41 +00005140 // When delaying diagnostics to run in the context of a parsed
5141 // declaration, we only want to actually emit anything if parsing
5142 // succeeds.
5143 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005144
John McCall2ec85372012-05-07 06:16:41 +00005145 // We emit all the active diagnostics in this pool or any of its
5146 // parents. In general, we'll get one pool for the decl spec
5147 // and a child pool for each declarator; in a decl group like:
5148 // deprecated_typedef foo, *bar, baz();
5149 // only the declarator pops will be passed decls. This is correct;
5150 // we really do need to consider delayed diagnostics from the decl spec
5151 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005152 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005153 do {
John McCall6347b682012-05-07 06:16:58 +00005154 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005155 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5156 // This const_cast is a bit lame. Really, Triggered should be mutable.
5157 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005158 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005159 continue;
5160
John McCallc1465822011-02-14 07:13:47 +00005161 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005162 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005163 case DelayedDiagnostic::Unavailable:
5164 // Don't bother giving deprecation/unavailable diagnostics if
5165 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005166 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005167 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005168 break;
5169
5170 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005171 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005172 break;
John McCall31168b02011-06-15 23:02:42 +00005173
5174 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005175 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005176 break;
John McCall86121512010-01-27 03:50:35 +00005177 }
5178 }
John McCall2ec85372012-05-07 06:16:41 +00005179 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005180}
5181
John McCall6347b682012-05-07 06:16:58 +00005182/// Given a set of delayed diagnostics, re-emit them as if they had
5183/// been delayed in the current context instead of in the given pool.
5184/// Essentially, this just moves them to the current pool.
5185void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5186 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5187 assert(curPool && "re-emitting in undelayed context not supported");
5188 curPool->steal(pool);
5189}
5190
Ted Kremenekb79ee572013-12-18 23:30:06 +00005191void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5192 NamedDecl *D, StringRef Message,
5193 SourceLocation Loc,
5194 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005195 const ObjCPropertyDecl *ObjCProperty,
5196 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005197 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005198 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005199 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5200 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5201 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005202 return;
5203 }
5204
Ted Kremenekb79ee572013-12-18 23:30:06 +00005205 Decl *Ctx = cast<Decl>(getCurLexicalContext());
5206 DelayedDiagnostic::DDKind K;
5207 switch (AD) {
5208 case AD_Deprecation:
5209 K = DelayedDiagnostic::Deprecation;
5210 break;
5211 case AD_Unavailable:
5212 K = DelayedDiagnostic::Unavailable;
5213 break;
5214 }
5215
5216 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005217 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005218}