blob: e95e4b70e8dc00b5d41d7c1388914e471701d827 [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"
David Majnemer929025d2016-01-26 19:30:26 +000015#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000023#include "clang/AST/Mangle.h"
Alex Denisovfde64952015-06-26 05:28:36 +000024#include "clang/AST/ASTMutationListener.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000025#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000026#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000028#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000029#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000030#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000031#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000032#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000033#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000034#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000035
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000037using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000038
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000039namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000040 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000041 C,
42 Cpp,
43 ObjC
44 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000045} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000046
Chris Lattner58418ff2008-06-29 00:16:31 +000047//===----------------------------------------------------------------------===//
48// Helper functions
49//===----------------------------------------------------------------------===//
50
Ted Kremenek527042b2009-08-14 20:49:40 +000051/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000052/// type (function or function-typed variable) or an Objective-C
53/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000054static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000055 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000056}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000057
David Majnemer06864812015-04-07 06:01:53 +000058/// \brief Return true if the given decl has function type (function or
59/// function-typed variable) or an Objective-C method or a block.
60static bool isFunctionOrMethodOrBlock(const Decl *D) {
61 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
62}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063
John McCall3882ace2011-01-05 12:14:39 +000064/// Return true if the given decl has a declarator that should have
65/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000067 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000068 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
69 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000070}
71
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000072/// hasFunctionProto - Return true if the given decl has a argument
73/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000074/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000075static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000076 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000077 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000078 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000079}
80
Alp Toker601b22c2014-01-21 23:35:24 +000081/// getFunctionOrMethodNumParams - Return number of function or method
82/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000084static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000085 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000086 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000088 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000089 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000090}
91
Alp Toker601b22c2014-01-21 23:35:24 +000092static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000093 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000094 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000096 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000097
Alp Toker03376dc2014-07-07 09:02:20 +000098 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099}
100
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000101static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
102 if (const auto *FD = dyn_cast<FunctionDecl>(D))
103 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000104 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000105 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000106 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000107 return BD->getParamDecl(Idx)->getSourceRange();
108 return SourceRange();
109}
110
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000111static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000112 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000113 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000114 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000115}
116
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000117static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
118 if (const auto *FD = dyn_cast<FunctionDecl>(D))
119 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000120 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000121 return MD->getReturnTypeSourceRange();
122 return SourceRange();
123}
124
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000126 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000128 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000129 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
131 return BD->isVariadic();
132
133 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000134}
135
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136static bool isInstanceMethod(const Decl *D) {
137 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000138 return MethodDecl->isInstance();
139 return false;
140}
141
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000142static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000143 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000144 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000145 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000146
John McCall96fa4842010-05-17 21:00:27 +0000147 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
148 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000149 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
John McCall96fa4842010-05-17 21:00:27 +0000151 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000152
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000153 // FIXME: Should we walk the chain of classes?
154 return ClsName == &Ctx.Idents.get("NSString") ||
155 ClsName == &Ctx.Idents.get("NSMutableString");
156}
157
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000159 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000160 if (!PT)
161 return false;
162
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000163 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000164 if (!RT)
165 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166
Daniel Dunbar980c6692008-09-26 03:32:58 +0000167 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000168 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000169 return false;
170
171 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
172}
173
Richard Smithb87c4652013-10-31 21:23:20 +0000174static unsigned getNumAttributeArgs(const AttributeList &Attr) {
175 // FIXME: Include the type in the argument list.
176 return Attr.getNumArgs() + Attr.hasParsedType();
177}
178
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000179template <typename Compare>
180static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
181 unsigned Num, unsigned Diag,
182 Compare Comp) {
183 if (Comp(getNumAttributeArgs(Attr), Num)) {
184 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000185 return false;
186 }
187
188 return true;
189}
190
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000191/// \brief Check if the attribute has exactly as many args as Num. May
192/// output an error.
193static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
194 unsigned Num) {
195 return checkAttributeNumArgsImpl(S, Attr, Num,
196 diag::err_attribute_wrong_number_arguments,
197 std::not_equal_to<unsigned>());
198}
199
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000200/// \brief Check if the attribute has at least as many args as Num. May
201/// output an error.
202static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000203 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000204 return checkAttributeNumArgsImpl(S, Attr, Num,
205 diag::err_attribute_too_few_arguments,
206 std::less<unsigned>());
207}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000208
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000209/// \brief Check if the attribute has at most as many args as Num. May
210/// output an error.
211static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
212 unsigned Num) {
213 return checkAttributeNumArgsImpl(S, Attr, Num,
214 diag::err_attribute_too_many_arguments,
215 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216}
217
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000218/// \brief If Expr is a valid integer constant, get the value of the integer
219/// expression and return success or failure. May output an error.
220static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
221 const Expr *Expr, uint32_t &Val,
222 unsigned Idx = UINT_MAX) {
223 llvm::APSInt I(32);
224 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
225 !Expr->isIntegerConstantExpr(I, S.Context)) {
226 if (Idx != UINT_MAX)
227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
228 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
229 << Expr->getSourceRange();
230 else
231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
232 << Attr.getName() << AANT_ArgumentIntegerConstant
233 << Expr->getSourceRange();
234 return false;
235 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000236
237 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000238 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
239 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000240 return false;
241 }
242
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000243 Val = (uint32_t)I.getZExtValue();
244 return true;
245}
246
Aaron Ballmanfb763042013-12-02 18:05:46 +0000247/// \brief Diagnose mutually exclusive attributes when present on a given
248/// declaration. Returns true if diagnosed.
249template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000250static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
251 IdentifierInfo *Ident) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000252 if (AttrTy *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000253 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
254 << A;
255 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000256 return true;
257 }
258 return false;
259}
260
Alp Toker601b22c2014-01-21 23:35:24 +0000261/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000262/// instance method D. May output an error.
263///
264/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000265static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
266 const AttributeList &Attr,
267 unsigned AttrArgNum,
268 const Expr *IdxExpr,
269 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000270 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000271
272 // In C++ the implicit 'this' function parameter also counts.
273 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000274 bool HP = hasFunctionProto(D);
275 bool HasImplicitThisParam = isInstanceMethod(D);
276 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000277 unsigned NumParams =
278 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000279
280 llvm::APSInt IdxInt;
281 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
282 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
284 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
285 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286 return false;
287 }
288
289 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000290 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000291 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
292 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000293 return false;
294 }
295 Idx--; // Convert to zero-based.
296 if (HasImplicitThisParam) {
297 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000298 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000299 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000300 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000301 return false;
302 }
303 --Idx;
304 }
305
306 return true;
307}
308
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000309/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
310/// If not emit an error and return false. If the argument is an identifier it
311/// will emit an error with a fixit hint and treat it as if it was a string
312/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000313bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
314 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000315 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000316 // Look for identifiers. If we have one emit a hint to fix it to a literal.
317 if (Attr.isArgIdent(ArgNum)) {
318 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000319 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000320 << Attr.getName() << AANT_ArgumentString
321 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000322 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000323 Str = Loc->Ident->getName();
324 if (ArgLocation)
325 *ArgLocation = Loc->Loc;
326 return true;
327 }
328
329 // Now check for an actual string literal.
330 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
331 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
332 if (ArgLocation)
333 *ArgLocation = ArgExpr->getLocStart();
334
335 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000336 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000337 << Attr.getName() << AANT_ArgumentString;
338 return false;
339 }
340
341 Str = Literal->getString();
342 return true;
343}
344
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000345/// \brief Applies the given attribute to the Decl without performing any
346/// additional semantic checking.
347template <typename AttrType>
348static void handleSimpleAttribute(Sema &S, Decl *D,
349 const AttributeList &Attr) {
350 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
351 Attr.getAttributeSpellingListIndex()));
352}
353
Justin Lebar3eaaf862016-01-13 01:07:35 +0000354template <typename AttrType>
355static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
356 const AttributeList &Attr) {
357 handleSimpleAttribute<AttrType>(S, D, Attr);
358}
359
360/// \brief Applies the given attribute to the Decl so long as the Decl doesn't
361/// already have one of the given incompatible attributes.
362template <typename AttrType, typename IncompatibleAttrType,
363 typename... IncompatibleAttrTypes>
364static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
365 const AttributeList &Attr) {
366 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(),
367 Attr.getName()))
368 return;
369 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
370 Attr);
371}
372
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000373/// \brief Check if the passed-in expression is of type int or bool.
374static bool isIntOrBool(Expr *Exp) {
375 QualType QT = Exp->getType();
376 return QT->isBooleanType() || QT->isIntegerType();
377}
378
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000379
380// Check to see if the type is a smart pointer of some kind. We assume
381// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000382static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000383 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
384 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000385 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000386 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000387
Richard Smithcf4bdde2015-02-21 02:45:19 +0000388 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
389 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000390 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000391 return false;
392
393 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000394}
395
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396/// \brief Check if passed in Decl is a pointer type.
397/// Note that this function may produce an error message.
398/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000399static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
400 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000401 const ValueDecl *vd = cast<ValueDecl>(D);
402 QualType QT = vd->getType();
403 if (QT->isAnyPointerType())
404 return true;
405
406 if (const RecordType *RT = QT->getAs<RecordType>()) {
407 // If it's an incomplete type, it could be a smart pointer; skip it.
408 // (We don't want to force template instantiation if we can avoid it,
409 // since that would alter the order in which templates are instantiated.)
410 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000411 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000412
Aaron Ballman553e6812013-12-26 14:54:11 +0000413 if (threadSafetyCheckIsSmartPointer(S, RT))
414 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000415 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000416
417 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000418 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000419 return false;
420}
421
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000422/// \brief Checks that the passed in QualType either is of RecordType or points
423/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000424static const RecordType *getRecordType(QualType QT) {
425 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000426 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000427
428 // Now check if we point to record type.
429 if (const PointerType *PT = QT->getAs<PointerType>())
430 return PT->getPointeeType()->getAs<RecordType>();
431
Craig Topperc3ec1492014-05-26 06:22:03 +0000432 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000433}
434
Aaron Ballman76050722014-04-04 15:13:57 +0000435static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000436 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000437
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000438 if (!RT)
439 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000440
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000441 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000442 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000443 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000444
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000445 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000446 // FIXME -- Check the type that the smart pointer points to.
447 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000448 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000449
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000450 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000451 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000452 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000453 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000454
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000455 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000456 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
457 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000458 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
459 const auto *Type = BS->getType()->getAs<RecordType>();
460 return Type->getDecl()->hasAttr<CapabilityAttr>();
461 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000462 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000464 return false;
465}
466
Aaron Ballman76050722014-04-04 15:13:57 +0000467static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000468 const auto *TD = Ty->getAs<TypedefType>();
469 if (!TD)
470 return false;
471
472 TypedefNameDecl *TN = TD->getDecl();
473 if (!TN)
474 return false;
475
476 return TN->hasAttr<CapabilityAttr>();
477}
478
Aaron Ballman76050722014-04-04 15:13:57 +0000479static bool typeHasCapability(Sema &S, QualType Ty) {
480 if (checkTypedefTypeForCapability(Ty))
481 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000482
Aaron Ballman76050722014-04-04 15:13:57 +0000483 if (checkRecordTypeForCapability(S, Ty))
484 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000485
Aaron Ballman76050722014-04-04 15:13:57 +0000486 return false;
487}
488
489static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
490 // Capability expressions are simple expressions involving the boolean logic
491 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
492 // a DeclRefExpr is found, its type should be checked to determine whether it
493 // is a capability or not.
494
495 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
496 return typeHasCapability(S, E->getType());
497 else if (const auto *E = dyn_cast<CastExpr>(Ex))
498 return isCapabilityExpr(S, E->getSubExpr());
499 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
500 return isCapabilityExpr(S, E->getSubExpr());
501 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
502 if (E->getOpcode() == UO_LNot)
503 return isCapabilityExpr(S, E->getSubExpr());
504 return false;
505 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
506 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
507 return isCapabilityExpr(S, E->getLHS()) &&
508 isCapabilityExpr(S, E->getRHS());
509 return false;
510 }
511
512 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000513}
514
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000515/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
516/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000517/// \param Sidx The attribute argument index to start checking with.
518/// \param ParamIdxOk Whether an argument can be indexing into a function
519/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000520static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
521 const AttributeList &Attr,
522 SmallVectorImpl<Expr *> &Args,
523 int Sidx = 0,
524 bool ParamIdxOk = false) {
525 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000526 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000527
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000528 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000529 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000530 Args.push_back(ArgExp);
531 continue;
532 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000533
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000534 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000535 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000536 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000537 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000538 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000539 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000540 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000541 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000542
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000543 // We allow constant strings to be used as a placeholder for expressions
544 // that are not valid C++ syntax, but warn that they are ignored.
545 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
546 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000547 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 continue;
549 }
550
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000551 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000552
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000553 // A pointer to member expression of the form &MyClass::mu is treated
554 // specially -- we need to look at the type of the member.
555 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
556 if (UOp->getOpcode() == UO_AddrOf)
557 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
558 if (DRE->getDecl()->isCXXInstanceMember())
559 ArgTy = DRE->getDecl()->getType();
560
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000561 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000563
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000564 // Now check if we index into a record type function param.
565 if(!RT && ParamIdxOk) {
566 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000567 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
568 if(FD && IL) {
569 unsigned int NumParams = FD->getNumParams();
570 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000571 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
572 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
573 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000574 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
575 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000576 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000577 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000578 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000579 }
580 }
581
Aaron Ballman76050722014-04-04 15:13:57 +0000582 // If the type does not have a capability, see if the components of the
583 // expression have capabilities. This allows for writing C code where the
584 // capability may be on the type, and the expression is a capability
585 // boolean logic expression. Eg) requires_capability(A || B && !C)
586 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
587 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
588 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000590 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592}
593
Chris Lattner58418ff2008-06-29 00:16:31 +0000594//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000595// Attribute Implementations
596//===----------------------------------------------------------------------===//
597
Michael Hana9171bc2012-08-03 17:40:43 +0000598static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000599 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000600 if (!threadSafetyCheckIsPointer(S, D, Attr))
601 return;
602
Michael Han99315932013-01-24 16:46:58 +0000603 D->addAttr(::new (S.Context)
604 PtGuardedVarAttr(Attr.getRange(), S.Context,
605 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000606}
607
Michael Hana9171bc2012-08-03 17:40:43 +0000608static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
609 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000610 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000611 SmallVector<Expr*, 1> Args;
612 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000613 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000614 unsigned Size = Args.size();
615 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000616 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000617
Michael Han3be3b442012-07-23 18:48:41 +0000618 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000619
Michael Han3be3b442012-07-23 18:48:41 +0000620 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000621}
622
Michael Han3be3b442012-07-23 18:48:41 +0000623static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000624 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000625 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
626 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000627
Aaron Ballman36a53502014-01-16 13:03:14 +0000628 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
629 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000630}
631
Michael Hana9171bc2012-08-03 17:40:43 +0000632static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000633 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000634 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000635 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
636 return;
637
638 if (!threadSafetyCheckIsPointer(S, D, Attr))
639 return;
640
641 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000642 S.Context, Arg,
643 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000644}
645
Michael Hana9171bc2012-08-03 17:40:43 +0000646static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
647 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000648 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000649 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000650 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000651
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000652 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000653 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000654 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
655 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
656 << Attr.getName();
657 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000658 }
659
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000660 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000661 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000662 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000663 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000664
Michael Han3be3b442012-07-23 18:48:41 +0000665 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000666}
667
Michael Hana9171bc2012-08-03 17:40:43 +0000668static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000669 const AttributeList &Attr) {
670 SmallVector<Expr*, 1> Args;
671 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
672 return;
673
674 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000675 D->addAttr(::new (S.Context)
676 AcquiredAfterAttr(Attr.getRange(), S.Context,
677 StartArg, Args.size(),
678 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000679}
680
Michael Hana9171bc2012-08-03 17:40:43 +0000681static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000682 const AttributeList &Attr) {
683 SmallVector<Expr*, 1> Args;
684 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
685 return;
686
687 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000688 D->addAttr(::new (S.Context)
689 AcquiredBeforeAttr(Attr.getRange(), S.Context,
690 StartArg, Args.size(),
691 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000692}
693
Michael Hana9171bc2012-08-03 17:40:43 +0000694static bool checkLockFunAttrCommon(Sema &S, Decl *D,
695 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000696 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000697 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000699 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000700
Michael Han3be3b442012-07-23 18:48:41 +0000701 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000702}
703
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000704static void handleAssertSharedLockAttr(Sema &S, Decl *D,
705 const AttributeList &Attr) {
706 SmallVector<Expr*, 1> Args;
707 if (!checkLockFunAttrCommon(S, D, Attr, Args))
708 return;
709
710 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000711 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000712 D->addAttr(::new (S.Context)
713 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
714 Attr.getAttributeSpellingListIndex()));
715}
716
717static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
718 const AttributeList &Attr) {
719 SmallVector<Expr*, 1> Args;
720 if (!checkLockFunAttrCommon(S, D, Attr, Args))
721 return;
722
723 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000724 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000725 D->addAttr(::new (S.Context)
726 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
727 StartArg, Size,
728 Attr.getAttributeSpellingListIndex()));
729}
730
731
Michael Hana9171bc2012-08-03 17:40:43 +0000732static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
733 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000734 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000735 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000736 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000737
Aaron Ballman00e99962013-08-31 01:11:41 +0000738 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000739 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000740 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000741 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000742 }
743
744 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000745 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000746
Michael Han3be3b442012-07-23 18:48:41 +0000747 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000748}
749
Michael Hana9171bc2012-08-03 17:40:43 +0000750static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000751 const AttributeList &Attr) {
752 SmallVector<Expr*, 2> Args;
753 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
754 return;
755
Michael Han99315932013-01-24 16:46:58 +0000756 D->addAttr(::new (S.Context)
757 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000758 Attr.getArgAsExpr(0),
759 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000760 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000761}
762
Michael Hana9171bc2012-08-03 17:40:43 +0000763static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000764 const AttributeList &Attr) {
765 SmallVector<Expr*, 2> Args;
766 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
767 return;
768
Nico Weber462fd1e2015-01-07 23:50:05 +0000769 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
770 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
771 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000772}
773
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000774static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000775 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000776 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000777 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000778 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000779 unsigned Size = Args.size();
780 if (Size == 0)
781 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000782
Michael Han99315932013-01-24 16:46:58 +0000783 D->addAttr(::new (S.Context)
784 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
785 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000786}
787
788static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000789 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000790 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000791 return;
792
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000793 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000794 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000795 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000796 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000797 if (Size == 0)
798 return;
799 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000800
Michael Han99315932013-01-24 16:46:58 +0000801 D->addAttr(::new (S.Context)
802 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
803 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000804}
805
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000806static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
807 Expr *Cond = Attr.getArgAsExpr(0);
808 if (!Cond->isTypeDependent()) {
809 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
810 if (Converted.isInvalid())
811 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000812 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000813 }
814
815 StringRef Msg;
816 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
817 return;
818
819 SmallVector<PartialDiagnosticAt, 8> Diags;
820 if (!Cond->isValueDependent() &&
821 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
822 Diags)) {
823 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
824 for (int I = 0, N = Diags.size(); I != N; ++I)
825 S.Diag(Diags[I].first, Diags[I].second);
826 return;
827 }
828
829 D->addAttr(::new (S.Context)
830 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
831 Attr.getAttributeSpellingListIndex()));
832}
833
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000834static void handlePassObjectSizeAttr(Sema &S, Decl *D,
835 const AttributeList &Attr) {
836 if (D->hasAttr<PassObjectSizeAttr>()) {
837 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
838 << Attr.getName();
839 return;
840 }
841
842 Expr *E = Attr.getArgAsExpr(0);
843 uint32_t Type;
844 if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1))
845 return;
846
847 // pass_object_size's argument is passed in as the second argument of
848 // __builtin_object_size. So, it has the same constraints as that second
849 // argument; namely, it must be in the range [0, 3].
850 if (Type > 3) {
851 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
852 << Attr.getName() << 0 << 3 << E->getSourceRange();
853 return;
854 }
855
856 // pass_object_size is only supported on constant pointer parameters; as a
857 // kindness to users, we allow the parameter to be non-const for declarations.
858 // At this point, we have no clue if `D` belongs to a function declaration or
859 // definition, so we defer the constness check until later.
860 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
861 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
862 << Attr.getName() << 1;
863 return;
864 }
865
866 D->addAttr(::new (S.Context)
867 PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type,
868 Attr.getAttributeSpellingListIndex()));
869}
870
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000871static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000872 ConsumableAttr::ConsumedState DefaultState;
873
874 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000875 IdentifierLoc *IL = Attr.getArgAsIdent(0);
876 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
877 DefaultState)) {
878 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
879 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000880 return;
881 }
David Blaikie16f76d22013-09-06 01:28:43 +0000882 } else {
883 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
884 << Attr.getName() << AANT_ArgumentIdentifier;
885 return;
886 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000887
888 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000889 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000890 Attr.getAttributeSpellingListIndex()));
891}
892
893static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
894 const AttributeList &Attr) {
895 ASTContext &CurrContext = S.getASTContext();
896 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
897
898 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
899 if (!RD->hasAttr<ConsumableAttr>()) {
900 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
901 RD->getNameAsString();
902
903 return false;
904 }
905 }
906
907 return true;
908}
909
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000910static void handleCallableWhenAttr(Sema &S, Decl *D,
911 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000912 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
913 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000914
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000915 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
916 return;
917
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000918 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
919 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
920 CallableWhenAttr::ConsumedState CallableState;
921
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000922 StringRef StateString;
923 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000924 if (Attr.isArgIdent(ArgIndex)) {
925 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
926 StateString = Ident->Ident->getName();
927 Loc = Ident->Loc;
928 } else {
929 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
930 return;
931 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000932
933 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000934 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000935 S.Diag(Loc, diag::warn_attribute_type_not_supported)
936 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000937 return;
938 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000939
940 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000941 }
942
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000943 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000944 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
945 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000946}
947
DeLesley Hutchins69391772013-10-17 23:23:53 +0000948static void handleParamTypestateAttr(Sema &S, Decl *D,
949 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000950 ParamTypestateAttr::ConsumedState ParamState;
951
952 if (Attr.isArgIdent(0)) {
953 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
954 StringRef StateString = Ident->Ident->getName();
955
956 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
957 ParamState)) {
958 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
959 << Attr.getName() << StateString;
960 return;
961 }
962 } else {
963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
964 Attr.getName() << AANT_ArgumentIdentifier;
965 return;
966 }
967
968 // FIXME: This check is currently being done in the analysis. It can be
969 // enabled here only after the parser propagates attributes at
970 // template specialization definition, not declaration.
971 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
972 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
973 //
974 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
975 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
976 // ReturnType.getAsString();
977 // return;
978 //}
979
980 D->addAttr(::new (S.Context)
981 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
982 Attr.getAttributeSpellingListIndex()));
983}
984
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000985static void handleReturnTypestateAttr(Sema &S, Decl *D,
986 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000987 ReturnTypestateAttr::ConsumedState ReturnState;
988
989 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000990 IdentifierLoc *IL = Attr.getArgAsIdent(0);
991 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
992 ReturnState)) {
993 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
994 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000995 return;
996 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000997 } else {
998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
999 Attr.getName() << AANT_ArgumentIdentifier;
1000 return;
1001 }
1002
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001003 // FIXME: This check is currently being done in the analysis. It can be
1004 // enabled here only after the parser propagates attributes at
1005 // template specialization definition, not declaration.
1006 //QualType ReturnType;
1007 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001008 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1009 // ReturnType = Param->getType();
1010 //
1011 //} else if (const CXXConstructorDecl *Constructor =
1012 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001013 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1014 //
1015 //} else {
1016 //
1017 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1018 //}
1019 //
1020 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1021 //
1022 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1023 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1024 // ReturnType.getAsString();
1025 // return;
1026 //}
1027
1028 D->addAttr(::new (S.Context)
1029 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1030 Attr.getAttributeSpellingListIndex()));
1031}
1032
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001033static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001034 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1035 return;
1036
1037 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001038 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001039 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1040 StringRef Param = Ident->Ident->getName();
1041 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1042 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1043 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001044 return;
1045 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001046 } else {
1047 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1048 Attr.getName() << AANT_ArgumentIdentifier;
1049 return;
1050 }
1051
1052 D->addAttr(::new (S.Context)
1053 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1054 Attr.getAttributeSpellingListIndex()));
1055}
1056
Chris Wailes9385f9f2013-10-29 20:28:41 +00001057static void handleTestTypestateAttr(Sema &S, Decl *D,
1058 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001059 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1060 return;
1061
Chris Wailes9385f9f2013-10-29 20:28:41 +00001062 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001063 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001064 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1065 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001066 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001067 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1068 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001069 return;
1070 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001071 } else {
1072 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1073 Attr.getName() << AANT_ArgumentIdentifier;
1074 return;
1075 }
1076
1077 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001078 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001079 Attr.getAttributeSpellingListIndex()));
1080}
1081
Chandler Carruthedc2c642011-07-02 00:01:44 +00001082static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1083 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001084 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001085 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001086}
1087
Chandler Carruthedc2c642011-07-02 00:01:44 +00001088static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001089 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001090 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1091 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001092 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001093 // Report warning about changed offset in the newer compiler versions.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001094 if (!FD->getType()->isDependentType() &&
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001095 !FD->getType()->isIncompleteType() && FD->isBitField() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001096 S.Context.getTypeAlign(FD->getType()) <= 8)
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001097 S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield);
1098
1099 FD->addAttr(::new (S.Context) PackedAttr(
1100 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001101 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001102 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001103}
1104
Ted Kremenek7fd17232011-09-29 07:02:25 +00001105static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1106 // The IBOutlet/IBOutletCollection attributes only apply to instance
1107 // variables or properties of Objective-C classes. The outlet must also
1108 // have an object reference type.
1109 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1110 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001111 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001112 << Attr.getName() << VD->getType() << 0;
1113 return false;
1114 }
1115 }
1116 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1117 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001118 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001119 << Attr.getName() << PD->getType() << 1;
1120 return false;
1121 }
1122 }
1123 else {
1124 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1125 return false;
1126 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001127
Ted Kremenek7fd17232011-09-29 07:02:25 +00001128 return true;
1129}
1130
Chandler Carruthedc2c642011-07-02 00:01:44 +00001131static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001132 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001133 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001134
Michael Han99315932013-01-24 16:46:58 +00001135 D->addAttr(::new (S.Context)
1136 IBOutletAttr(Attr.getRange(), S.Context,
1137 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001138}
1139
Chandler Carruthedc2c642011-07-02 00:01:44 +00001140static void handleIBOutletCollection(Sema &S, Decl *D,
1141 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001142
1143 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001144 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001145 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1146 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001147 return;
1148 }
1149
Ted Kremenek7fd17232011-09-29 07:02:25 +00001150 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001151 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001152
Richard Smithb1f9a282013-10-31 01:56:18 +00001153 ParsedType PT;
1154
1155 if (Attr.hasParsedType())
1156 PT = Attr.getTypeArg();
1157 else {
1158 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1159 S.getScopeForContext(D->getDeclContext()->getParent()));
1160 if (!PT) {
1161 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1162 return;
1163 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001164 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001165
Craig Topperc3ec1492014-05-26 06:22:03 +00001166 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001167 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1168 if (!QTLoc)
1169 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001170
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001171 // Diagnose use of non-object type in iboutletcollection attribute.
1172 // FIXME. Gnu attribute extension ignores use of builtin types in
1173 // attributes. So, __attribute__((iboutletcollection(char))) will be
1174 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001175 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001176 S.Diag(Attr.getLoc(),
1177 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1178 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001179 return;
1180 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001181
Michael Han99315932013-01-24 16:46:58 +00001182 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001183 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001184 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001185}
1186
Hal Finkelee90a222014-09-26 05:04:30 +00001187bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1188 if (RefOkay) {
1189 if (T->isReferenceType())
1190 return true;
1191 } else {
1192 T = T.getNonReferenceType();
1193 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001194
Hal Finkelee90a222014-09-26 05:04:30 +00001195 // The nonnull attribute, and other similar attributes, can be applied to a
1196 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001197 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001198 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1199 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001200 for (const auto *I : UD->fields()) {
1201 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001202 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1203 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001204 }
1205 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001206 }
1207
1208 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001209}
1210
Ted Kremenek9aedc152014-01-17 06:24:56 +00001211static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001212 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001213 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001214 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001215 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001216 if (isReturnValue)
1217 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1218 << Attr.getName() << AttrParmRange << TypeRange;
1219 else
1220 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1221 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001222 return false;
1223 }
1224 return true;
1225}
1226
Chandler Carruthedc2c642011-07-02 00:01:44 +00001227static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001228 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001229 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1230 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001231 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001232 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001233 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001234
1235 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001236 if (Idx < getFunctionOrMethodNumParams(D) &&
1237 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001238 Ex->getSourceRange(),
1239 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001240 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001242 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001243 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
1245 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001246 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1247 // check if the attribute came from a macro expansion or a template
1248 // instantiation.
1249 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1250 S.ActiveTemplateInstantiations.empty()) {
1251 bool AnyPointers = isFunctionOrMethodVariadic(D);
1252 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1253 I != E && !AnyPointers; ++I) {
1254 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001255 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001256 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001257 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001258
Richard Smith588bd9b2014-08-27 04:59:42 +00001259 if (!AnyPointers)
1260 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001261 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001262
Richard Smith588bd9b2014-08-27 04:59:42 +00001263 unsigned *Start = NonNullArgs.data();
1264 unsigned Size = NonNullArgs.size();
1265 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001266 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001267 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001268 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001269}
1270
Jordan Rosec9399072014-02-11 17:27:59 +00001271static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1272 const AttributeList &Attr) {
1273 if (Attr.getNumArgs() > 0) {
1274 if (D->getFunctionType()) {
1275 handleNonNullAttr(S, D, Attr);
1276 } else {
1277 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1278 << D->getSourceRange();
1279 }
1280 return;
1281 }
1282
1283 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001284 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1285 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001286 return;
1287
1288 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001289 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001290 Attr.getAttributeSpellingListIndex()));
1291}
1292
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001293static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1294 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001295 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001296 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1297 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001298 /* isReturnValue */ true))
1299 return;
1300
1301 D->addAttr(::new (S.Context)
1302 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1303 Attr.getAttributeSpellingListIndex()));
1304}
1305
Hal Finkelee90a222014-09-26 05:04:30 +00001306static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1307 const AttributeList &Attr) {
1308 Expr *E = Attr.getArgAsExpr(0),
1309 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1310 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1311 Attr.getAttributeSpellingListIndex());
1312}
1313
1314void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1315 Expr *OE, unsigned SpellingListIndex) {
1316 QualType ResultType = getFunctionOrMethodResultType(D);
1317 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1318
1319 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1320 SourceLocation AttrLoc = AttrRange.getBegin();
1321
1322 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1323 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1324 << &TmpAttr << AttrRange << SR;
1325 return;
1326 }
1327
1328 if (!E->isValueDependent()) {
1329 llvm::APSInt I(64);
1330 if (!E->isIntegerConstantExpr(I, Context)) {
1331 if (OE)
1332 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1333 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1334 << E->getSourceRange();
1335 else
1336 Diag(AttrLoc, diag::err_attribute_argument_type)
1337 << &TmpAttr << AANT_ArgumentIntegerConstant
1338 << E->getSourceRange();
1339 return;
1340 }
1341
1342 if (!I.isPowerOf2()) {
1343 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1344 << E->getSourceRange();
1345 return;
1346 }
1347 }
1348
1349 if (OE) {
1350 if (!OE->isValueDependent()) {
1351 llvm::APSInt I(64);
1352 if (!OE->isIntegerConstantExpr(I, Context)) {
1353 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1354 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1355 << OE->getSourceRange();
1356 return;
1357 }
1358 }
1359 }
1360
1361 D->addAttr(::new (Context)
1362 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1363}
1364
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001365/// Normalize the attribute, __foo__ becomes foo.
1366/// Returns true if normalization was applied.
1367static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001368 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1369 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001370 AttrName = AttrName.drop_front(2).drop_back(2);
1371 return true;
1372 }
1373 return false;
1374}
1375
Chandler Carruthedc2c642011-07-02 00:01:44 +00001376static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001377 // This attribute must be applied to a function declaration. The first
1378 // argument to the attribute must be an identifier, the name of the resource,
1379 // for example: malloc. The following arguments must be argument indexes, the
1380 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001382 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001383 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384
Aaron Ballman00e99962013-08-31 01:11:41 +00001385 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001386 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001387 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001388 return;
1389 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001390
Richard Smith852e9ce2013-11-27 01:46:48 +00001391 // Figure out our Kind.
1392 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001393 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001394 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001395
Richard Smith852e9ce2013-11-27 01:46:48 +00001396 // Check arguments.
1397 switch (K) {
1398 case OwnershipAttr::Takes:
1399 case OwnershipAttr::Holds:
1400 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001401 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1402 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001403 return;
1404 }
1405 break;
1406 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001407 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001408 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1409 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001410 return;
1411 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001412 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413 }
1414
Richard Smith852e9ce2013-11-27 01:46:48 +00001415 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001416
Richard Smith852e9ce2013-11-27 01:46:48 +00001417 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001418 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001419 Module = &S.PP.getIdentifierTable().get(ModuleName);
1420 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001421
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001422 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001423 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1424 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001425 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001426 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001427 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001428
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001429 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001430 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001431 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001432 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001433 case OwnershipAttr::Takes:
1434 case OwnershipAttr::Holds:
1435 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1436 Err = 0;
1437 break;
1438 case OwnershipAttr::Returns:
1439 if (!T->isIntegerType())
1440 Err = 1;
1441 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001442 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001443 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001444 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001445 << Ex->getSourceRange();
1446 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001447 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001448
1449 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001450 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001451 // Cannot have two ownership attributes of different kinds for the same
1452 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001453 if (I->getOwnKind() != K && I->args_end() !=
1454 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001455 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001456 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001457 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001458 } else if (K == OwnershipAttr::Returns &&
1459 I->getOwnKind() == OwnershipAttr::Returns) {
1460 // A returns attribute conflicts with any other returns attribute using
1461 // a different index. Note, diagnostic reporting is 1-based, but stored
1462 // argument indexes are 0-based.
1463 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1464 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1465 << *(I->args_begin()) + 1;
1466 if (I->args_size())
1467 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1468 << (unsigned)Idx + 1 << Ex->getSourceRange();
1469 return;
1470 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001471 }
1472 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001473 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001474 }
1475
1476 unsigned* start = OwnershipArgs.data();
1477 unsigned size = OwnershipArgs.size();
1478 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001479
Michael Han99315932013-01-24 16:46:58 +00001480 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001481 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001482 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001483}
1484
Chandler Carruthedc2c642011-07-02 00:01:44 +00001485static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001486 // Check the attribute arguments.
1487 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1489 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001490 return;
1491 }
1492
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001493 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001494
Rafael Espindolac18086a2010-02-23 22:00:30 +00001495 // gcc rejects
1496 // class c {
1497 // static int a __attribute__((weakref ("v2")));
1498 // static int b() __attribute__((weakref ("f3")));
1499 // };
1500 // and ignores the attributes of
1501 // void f(void) {
1502 // static int a __attribute__((weakref ("v2")));
1503 // }
1504 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001505 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001506 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001507 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1508 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001509 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001510 }
1511
1512 // The GCC manual says
1513 //
1514 // At present, a declaration to which `weakref' is attached can only
1515 // be `static'.
1516 //
1517 // It also says
1518 //
1519 // Without a TARGET,
1520 // given as an argument to `weakref' or to `alias', `weakref' is
1521 // equivalent to `weak'.
1522 //
1523 // gcc 4.4.1 will accept
1524 // int a7 __attribute__((weakref));
1525 // as
1526 // int a7 __attribute__((weak));
1527 // This looks like a bug in gcc. We reject that for now. We should revisit
1528 // it if this behaviour is actually used.
1529
Rafael Espindolac18086a2010-02-23 22:00:30 +00001530 // GCC rejects
1531 // static ((alias ("y"), weakref)).
1532 // Should we? How to check that weakref is before or after alias?
1533
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001534 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1535 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1536 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001537 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001538 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001539 // GCC will accept anything as the argument of weakref. Should we
1540 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001541 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1542 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001543
Michael Han99315932013-01-24 16:46:58 +00001544 D->addAttr(::new (S.Context)
1545 WeakRefAttr(Attr.getRange(), S.Context,
1546 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001547}
1548
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001549static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1550 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001551 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001552 return;
1553
Douglas Gregore8bbc122011-09-02 00:18:52 +00001554 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001555 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1556 return;
1557 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001558 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1559 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx);
1560 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001561
David Majnemer2dc81462015-01-19 09:00:28 +00001562 // Aliases should be on declarations, not definitions.
1563 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1564 if (FD->isThisDeclarationADefinition()) {
1565 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1566 return;
1567 }
1568 } else {
1569 const auto *VD = cast<VarDecl>(D);
1570 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1571 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1572 return;
1573 }
1574 }
1575
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001576 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001577
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001578 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001579 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001580}
1581
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001582static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001583 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001584 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001585
Michael Han99315932013-01-24 16:46:58 +00001586 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1587 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001588}
1589
1590static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001591 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001592 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001593
Michael Han99315932013-01-24 16:46:58 +00001594 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1595 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001596}
1597
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001598static void handleTLSModelAttr(Sema &S, Decl *D,
1599 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001600 StringRef Model;
1601 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001602 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001603 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001604 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001605
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001606 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001607 if (Model != "global-dynamic" && Model != "local-dynamic"
1608 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001609 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001610 return;
1611 }
1612
Michael Han99315932013-01-24 16:46:58 +00001613 D->addAttr(::new (S.Context)
1614 TLSModelAttr(Attr.getRange(), S.Context, Model,
1615 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001616}
1617
David Majnemer631a90b2015-02-04 07:23:21 +00001618static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1619 QualType ResultType = getFunctionOrMethodResultType(D);
1620 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1621 D->addAttr(::new (S.Context) RestrictAttr(
1622 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1623 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001624 }
1625
David Majnemer631a90b2015-02-04 07:23:21 +00001626 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1627 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001628}
1629
Chandler Carruthedc2c642011-07-02 00:01:44 +00001630static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001631 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001632 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001633 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001634 return;
1635 }
1636
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001637 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1638 Attr.getAttributeSpellingListIndex()))
1639 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001640}
1641
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001642static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1643 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1644 Attr.getName()))
1645 return;
1646
1647 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1648 Attr.getAttributeSpellingListIndex()));
1649}
1650
Chandler Carruthedc2c642011-07-02 00:01:44 +00001651static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001652 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001653
1654 if (S.CheckNoReturnAttr(attr)) return;
1655
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001656 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001657 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001658 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001659 return;
1660 }
1661
Michael Han99315932013-01-24 16:46:58 +00001662 D->addAttr(::new (S.Context)
1663 NoReturnAttr(attr.getRange(), S.Context,
1664 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001665}
1666
1667bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001668 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001669 attr.setInvalid();
1670 return true;
1671 }
1672
1673 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001674}
1675
Chandler Carruthedc2c642011-07-02 00:01:44 +00001676static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1677 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001678
1679 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1680 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001681 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001682 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001683 if (!VD || (!VD->getType()->isBlockPointerType() &&
1684 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001685 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001686 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001687 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001688 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001689 return;
1690 }
1691 }
1692
Michael Han99315932013-01-24 16:46:58 +00001693 D->addAttr(::new (S.Context)
1694 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1695 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001696}
1697
John Thompsoncdb847ba2010-08-09 21:53:52 +00001698// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001699static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001700/*
1701 Returning a Vector Class in Registers
1702
Eric Christopherbc638a82010-12-01 22:13:54 +00001703 According to the PPU ABI specifications, a class with a single member of
1704 vector type is returned in memory when used as the return value of a function.
1705 This results in inefficient code when implementing vector classes. To return
1706 the value in a single vector register, add the vecreturn attribute to the
1707 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001708
1709 Example:
1710
1711 struct Vector
1712 {
1713 __vector float xyzw;
1714 } __attribute__((vecreturn));
1715
1716 Vector Add(Vector lhs, Vector rhs)
1717 {
1718 Vector result;
1719 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1720 return result; // This will be returned in a register
1721 }
1722*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001723 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1724 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001725 return;
1726 }
1727
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001728 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001729 int count = 0;
1730
1731 if (!isa<CXXRecordDecl>(record)) {
1732 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1733 return;
1734 }
1735
1736 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1737 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1738 return;
1739 }
1740
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001741 for (const auto *I : record->fields()) {
1742 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001743 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1744 return;
1745 }
1746 count++;
1747 }
1748
Michael Han99315932013-01-24 16:46:58 +00001749 D->addAttr(::new (S.Context)
1750 VecReturnAttr(Attr.getRange(), S.Context,
1751 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001752}
1753
Richard Smithe233fbf2013-01-28 22:42:45 +00001754static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1755 const AttributeList &Attr) {
1756 if (isa<ParmVarDecl>(D)) {
1757 // [[carries_dependency]] can only be applied to a parameter if it is a
1758 // parameter of a function declaration or lambda.
1759 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1760 S.Diag(Attr.getLoc(),
1761 diag::err_carries_dependency_param_not_function_decl);
1762 return;
1763 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001764 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001765
1766 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1767 Attr.getRange(), S.Context,
1768 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001769}
1770
Akira Hatanakac8667622015-11-06 23:56:15 +00001771static void handleNotTailCalledAttr(Sema &S, Decl *D,
1772 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001773 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1774 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001775 return;
1776
1777 D->addAttr(::new (S.Context) NotTailCalledAttr(
1778 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1779}
1780
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001781static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1782 const AttributeList &Attr) {
1783 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1784 Attr.getName()))
1785 return;
1786
1787 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1788 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1789}
1790
Chandler Carruthedc2c642011-07-02 00:01:44 +00001791static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001792 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001793 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001794 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001795 return;
1796 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001797 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001799 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001800 return;
1801 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001802
Michael Han99315932013-01-24 16:46:58 +00001803 D->addAttr(::new (S.Context)
1804 UsedAttr(Attr.getRange(), S.Context,
1805 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001806}
1807
Chandler Carruthedc2c642011-07-02 00:01:44 +00001808static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001809 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001810 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001811 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1812 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001813
Michael Han99315932013-01-24 16:46:58 +00001814 D->addAttr(::new (S.Context)
1815 ConstructorAttr(Attr.getRange(), S.Context, priority,
1816 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001817}
1818
Chandler Carruthedc2c642011-07-02 00:01:44 +00001819static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001820 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001821 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001822 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1823 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001824
Michael Han99315932013-01-24 16:46:58 +00001825 D->addAttr(::new (S.Context)
1826 DestructorAttr(Attr.getRange(), S.Context, priority,
1827 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001828}
1829
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001830template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001831static void handleAttrWithMessage(Sema &S, Decl *D,
1832 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001833 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001834 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001835 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001836 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001837
Michael Han99315932013-01-24 16:46:58 +00001838 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1839 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001840}
1841
Ted Kremenek438f8db2014-02-22 01:06:05 +00001842static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001843 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001844 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001845 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1846 << Attr.getName() << Attr.getRange();
1847 return;
1848 }
1849
Ted Kremenek28eace62013-11-23 01:01:34 +00001850 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001851 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1852 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001853}
1854
Jordy Rose740b0c22012-05-08 03:27:22 +00001855static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1856 IdentifierInfo *Platform,
1857 VersionTuple Introduced,
1858 VersionTuple Deprecated,
1859 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001860 StringRef PlatformName
1861 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1862 if (PlatformName.empty())
1863 PlatformName = Platform->getName();
1864
1865 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1866 // of these steps are needed).
1867 if (!Introduced.empty() && !Deprecated.empty() &&
1868 !(Introduced <= Deprecated)) {
1869 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1870 << 1 << PlatformName << Deprecated.getAsString()
1871 << 0 << Introduced.getAsString();
1872 return true;
1873 }
1874
1875 if (!Introduced.empty() && !Obsoleted.empty() &&
1876 !(Introduced <= Obsoleted)) {
1877 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1878 << 2 << PlatformName << Obsoleted.getAsString()
1879 << 0 << Introduced.getAsString();
1880 return true;
1881 }
1882
1883 if (!Deprecated.empty() && !Obsoleted.empty() &&
1884 !(Deprecated <= Obsoleted)) {
1885 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1886 << 2 << PlatformName << Obsoleted.getAsString()
1887 << 1 << Deprecated.getAsString();
1888 return true;
1889 }
1890
1891 return false;
1892}
1893
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001894/// \brief Check whether the two versions match.
1895///
1896/// If either version tuple is empty, then they are assumed to match. If
1897/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1898static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1899 bool BeforeIsOkay) {
1900 if (X.empty() || Y.empty())
1901 return true;
1902
1903 if (X == Y)
1904 return true;
1905
1906 if (BeforeIsOkay && X < Y)
1907 return true;
1908
1909 return false;
1910}
1911
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001912AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001913 IdentifierInfo *Platform,
1914 VersionTuple Introduced,
1915 VersionTuple Deprecated,
1916 VersionTuple Obsoleted,
1917 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001918 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001919 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001920 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001921 VersionTuple MergedIntroduced = Introduced;
1922 VersionTuple MergedDeprecated = Deprecated;
1923 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001925 bool OverrideOrImpl = false;
1926 switch (AMK) {
1927 case AMK_None:
1928 case AMK_Redeclaration:
1929 OverrideOrImpl = false;
1930 break;
1931
1932 case AMK_Override:
1933 case AMK_ProtocolImplementation:
1934 OverrideOrImpl = true;
1935 break;
1936 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001937
Rafael Espindolac67f2232012-05-10 02:50:16 +00001938 if (D->hasAttrs()) {
1939 AttrVec &Attrs = D->getAttrs();
1940 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1941 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1942 if (!OldAA) {
1943 ++i;
1944 continue;
1945 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001946
Rafael Espindolac67f2232012-05-10 02:50:16 +00001947 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1948 if (OldPlatform != Platform) {
1949 ++i;
1950 continue;
1951 }
1952
Tim Northover7a73cc72015-10-30 16:30:49 +00001953 // If there is an existing availability attribute for this platform that
1954 // is explicit and the new one is implicit use the explicit one and
1955 // discard the new implicit attribute.
1956 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1957 return nullptr;
1958 }
1959
1960 // If there is an existing attribute for this platform that is implicit
1961 // and the new attribute is explicit then erase the old one and
1962 // continue processing the attributes.
1963 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1964 Attrs.erase(Attrs.begin() + i);
1965 --e;
1966 continue;
1967 }
1968
Rafael Espindolac67f2232012-05-10 02:50:16 +00001969 FoundAny = true;
1970 VersionTuple OldIntroduced = OldAA->getIntroduced();
1971 VersionTuple OldDeprecated = OldAA->getDeprecated();
1972 VersionTuple OldObsoleted = OldAA->getObsoleted();
1973 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001974
Douglas Gregord2a713e2015-09-30 21:27:42 +00001975 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1976 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1977 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001978 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001979 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1980 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001981 int Which = -1;
1982 VersionTuple FirstVersion;
1983 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001984 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001985 Which = 0;
1986 FirstVersion = OldIntroduced;
1987 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001988 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001989 Which = 1;
1990 FirstVersion = Deprecated;
1991 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001992 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001993 Which = 2;
1994 FirstVersion = Obsoleted;
1995 SecondVersion = OldObsoleted;
1996 }
1997
1998 if (Which == -1) {
1999 Diag(OldAA->getLocation(),
2000 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002001 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2002 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002003 } else {
2004 Diag(OldAA->getLocation(),
2005 diag::warn_mismatched_availability_override)
2006 << Which
2007 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002008 << FirstVersion.getAsString() << SecondVersion.getAsString()
2009 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002010 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002011 if (AMK == AMK_Override)
2012 Diag(Range.getBegin(), diag::note_overridden_method);
2013 else
2014 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002015 } else {
2016 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2017 Diag(Range.getBegin(), diag::note_previous_attribute);
2018 }
2019
Rafael Espindolac67f2232012-05-10 02:50:16 +00002020 Attrs.erase(Attrs.begin() + i);
2021 --e;
2022 continue;
2023 }
2024
2025 VersionTuple MergedIntroduced2 = MergedIntroduced;
2026 VersionTuple MergedDeprecated2 = MergedDeprecated;
2027 VersionTuple MergedObsoleted2 = MergedObsoleted;
2028
2029 if (MergedIntroduced2.empty())
2030 MergedIntroduced2 = OldIntroduced;
2031 if (MergedDeprecated2.empty())
2032 MergedDeprecated2 = OldDeprecated;
2033 if (MergedObsoleted2.empty())
2034 MergedObsoleted2 = OldObsoleted;
2035
2036 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2037 MergedIntroduced2, MergedDeprecated2,
2038 MergedObsoleted2)) {
2039 Attrs.erase(Attrs.begin() + i);
2040 --e;
2041 continue;
2042 }
2043
2044 MergedIntroduced = MergedIntroduced2;
2045 MergedDeprecated = MergedDeprecated2;
2046 MergedObsoleted = MergedObsoleted2;
2047 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002048 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002049 }
2050
2051 if (FoundAny &&
2052 MergedIntroduced == Introduced &&
2053 MergedDeprecated == Deprecated &&
2054 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002055 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002056
Douglas Gregord2a713e2015-09-30 21:27:42 +00002057 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002058 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002059 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002060 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002061 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002062 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2063 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002064 Obsoleted, IsUnavailable, Message,
2065 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002066 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002067 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002068}
2069
Chandler Carruthedc2c642011-07-02 00:01:44 +00002070static void handleAvailabilityAttr(Sema &S, Decl *D,
2071 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002072 if (!checkAttributeNumArgs(S, Attr, 1))
2073 return;
2074 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002075 unsigned Index = Attr.getAttributeSpellingListIndex();
2076
Aaron Ballman00e99962013-08-31 01:11:41 +00002077 IdentifierInfo *II = Platform->Ident;
2078 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2079 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2080 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002081
Rafael Espindolac231fab2013-01-08 21:30:32 +00002082 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2083 if (!ND) {
2084 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2085 return;
2086 }
2087
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002088 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2089 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2090 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002091 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002092 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002093 if (const StringLiteral *SE =
2094 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002095 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002096
Aaron Ballman00e99962013-08-31 01:11:41 +00002097 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002098 Introduced.Version,
2099 Deprecated.Version,
2100 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002101 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002102 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002103 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002104 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002105 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002106
2107 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2108 // matches before the start of the watchOS platform.
2109 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2110 IdentifierInfo *NewII = nullptr;
2111 if (II->getName() == "ios")
2112 NewII = &S.Context.Idents.get("watchos");
2113 else if (II->getName() == "ios_app_extension")
2114 NewII = &S.Context.Idents.get("watchos_app_extension");
2115
2116 if (NewII) {
2117 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2118 if (Version.empty())
2119 return Version;
2120 auto Major = Version.getMajor();
2121 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2122 if (NewMajor >= 2) {
2123 if (Version.getMinor().hasValue()) {
2124 if (Version.getSubminor().hasValue())
2125 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2126 Version.getSubminor().getValue());
2127 else
2128 return VersionTuple(NewMajor, Version.getMinor().getValue());
2129 }
2130 }
2131
2132 return VersionTuple(2, 0);
2133 };
2134
2135 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2136 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2137 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2138
2139 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2140 SourceRange(),
2141 NewII,
2142 NewIntroduced,
2143 NewDeprecated,
2144 NewObsoleted,
2145 IsUnavailable, Str,
2146 Sema::AMK_None,
2147 Index);
2148 if (NewAttr)
2149 D->addAttr(NewAttr);
2150 }
2151 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2152 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2153 // matches before the start of the tvOS platform.
2154 IdentifierInfo *NewII = nullptr;
2155 if (II->getName() == "ios")
2156 NewII = &S.Context.Idents.get("tvos");
2157 else if (II->getName() == "ios_app_extension")
2158 NewII = &S.Context.Idents.get("tvos_app_extension");
2159
2160 if (NewII) {
2161 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2162 SourceRange(),
2163 NewII,
2164 Introduced.Version,
2165 Deprecated.Version,
2166 Obsoleted.Version,
2167 IsUnavailable, Str,
2168 Sema::AMK_None,
2169 Index);
2170 if (NewAttr)
2171 D->addAttr(NewAttr);
2172 }
2173 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002174}
2175
John McCalld041a9b2013-02-20 01:54:26 +00002176template <class T>
2177static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2178 typename T::VisibilityType value,
2179 unsigned attrSpellingListIndex) {
2180 T *existingAttr = D->getAttr<T>();
2181 if (existingAttr) {
2182 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2183 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002184 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002185 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2186 S.Diag(range.getBegin(), diag::note_previous_attribute);
2187 D->dropAttr<T>();
2188 }
2189 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2190}
2191
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002192VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002193 VisibilityAttr::VisibilityType Vis,
2194 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002195 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2196 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002197}
2198
John McCalld041a9b2013-02-20 01:54:26 +00002199TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2200 TypeVisibilityAttr::VisibilityType Vis,
2201 unsigned AttrSpellingListIndex) {
2202 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2203 AttrSpellingListIndex);
2204}
2205
2206static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2207 bool isTypeVisibility) {
2208 // Visibility attributes don't mean anything on a typedef.
2209 if (isa<TypedefNameDecl>(D)) {
2210 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2211 << Attr.getName();
2212 return;
2213 }
2214
2215 // 'type_visibility' can only go on a type or namespace.
2216 if (isTypeVisibility &&
2217 !(isa<TagDecl>(D) ||
2218 isa<ObjCInterfaceDecl>(D) ||
2219 isa<NamespaceDecl>(D))) {
2220 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2221 << Attr.getName() << ExpectedTypeOrNamespace;
2222 return;
2223 }
2224
Benjamin Kramer70370212013-09-09 15:08:57 +00002225 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002226 StringRef TypeStr;
2227 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002228 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002229 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002230
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002231 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002232 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002233 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002234 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002235 return;
2236 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002237
2238 // Complain about attempts to use protected visibility on targets
2239 // (like Darwin) that don't support it.
2240 if (type == VisibilityAttr::Protected &&
2241 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2242 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2243 type = VisibilityAttr::Default;
2244 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002245
Michael Han99315932013-01-24 16:46:58 +00002246 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002247 clang::Attr *newAttr;
2248 if (isTypeVisibility) {
2249 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2250 (TypeVisibilityAttr::VisibilityType) type,
2251 Index);
2252 } else {
2253 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2254 }
2255 if (newAttr)
2256 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002257}
2258
Chandler Carruthedc2c642011-07-02 00:01:44 +00002259static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2260 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002261 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002262 if (!Attr.isArgIdent(0)) {
2263 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2264 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002265 return;
2266 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002267
Aaron Ballman682ee422013-09-11 19:47:58 +00002268 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2269 ObjCMethodFamilyAttr::FamilyKind F;
2270 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2271 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2272 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002273 return;
2274 }
2275
Alp Toker314cc812014-01-25 16:55:45 +00002276 if (F == ObjCMethodFamilyAttr::OMF_init &&
2277 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002278 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002279 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002280 // Ignore the attribute.
2281 return;
2282 }
2283
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002284 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002285 S.Context, F,
2286 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002287}
2288
Chandler Carruthedc2c642011-07-02 00:01:44 +00002289static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002290 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002291 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002292 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002293 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2294 return;
2295 }
2296 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002297 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2298 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002299 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002300 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2301 return;
2302 }
2303 }
2304 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002305 // It is okay to include this attribute on properties, e.g.:
2306 //
2307 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2308 //
2309 // In this case it follows tradition and suppresses an error in the above
2310 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002311 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002312 }
Michael Han99315932013-01-24 16:46:58 +00002313 D->addAttr(::new (S.Context)
2314 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2315 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002316}
2317
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002318static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2319 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2320 QualType T = TD->getUnderlyingType();
2321 if (!T->isObjCObjectPointerType()) {
2322 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2323 return;
2324 }
2325 } else {
2326 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2327 return;
2328 }
2329 D->addAttr(::new (S.Context)
2330 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2331 Attr.getAttributeSpellingListIndex()));
2332}
2333
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002335 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002337 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002338 return;
2339 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002340
Aaron Ballman00e99962013-08-31 01:11:41 +00002341 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002342 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002343 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2344 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2345 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002346 return;
2347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002348
Michael Han99315932013-01-24 16:46:58 +00002349 D->addAttr(::new (S.Context)
2350 BlocksAttr(Attr.getRange(), S.Context, type,
2351 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002352}
2353
Chandler Carruthedc2c642011-07-02 00:01:44 +00002354static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002355 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002356 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002357 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002358 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002359 if (E->isTypeDependent() || E->isValueDependent() ||
2360 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002361 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002362 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002363 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002364 return;
2365 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002366
John McCallb46f2872011-09-09 07:56:05 +00002367 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002368 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2369 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002370 return;
2371 }
John McCallb46f2872011-09-09 07:56:05 +00002372
2373 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002374 }
2375
Aaron Ballman18a78382013-11-21 00:28:23 +00002376 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002377 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002378 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002379 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002380 if (E->isTypeDependent() || E->isValueDependent() ||
2381 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002382 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002383 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002384 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002385 return;
2386 }
2387 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
John McCallb46f2872011-09-09 07:56:05 +00002389 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002390 // FIXME: This error message could be improved, it would be nice
2391 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002392 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2393 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002394 return;
2395 }
2396 }
2397
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002398 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002399 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002400 if (isa<FunctionNoProtoType>(FT)) {
2401 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2402 return;
2403 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002404
Chris Lattner9363e312009-03-17 23:03:47 +00002405 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002406 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002407 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002408 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002409 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002410 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002411 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002412 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002413 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002414 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2415 if (!BD->isVariadic()) {
2416 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2417 return;
2418 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002419 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002420 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002421 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002422 const FunctionType *FT = Ty->isFunctionPointerType()
2423 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002424 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002425 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002426 int m = Ty->isFunctionPointerType() ? 0 : 1;
2427 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002428 return;
2429 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002430 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002431 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002432 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002433 return;
2434 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002435 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002437 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002438 return;
2439 }
Michael Han99315932013-01-24 16:46:58 +00002440 D->addAttr(::new (S.Context)
2441 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2442 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002443}
2444
Chandler Carruthedc2c642011-07-02 00:01:44 +00002445static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002446 if (D->getFunctionType() &&
2447 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002448 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2449 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002450 return;
2451 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002452 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002453 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002454 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2455 << Attr.getName() << 1;
2456 return;
2457 }
2458
Michael Han99315932013-01-24 16:46:58 +00002459 D->addAttr(::new (S.Context)
2460 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2461 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002462}
2463
Chandler Carruthedc2c642011-07-02 00:01:44 +00002464static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002465 // weak_import only applies to variable & function declarations.
2466 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002467 if (!D->canBeWeakImported(isDef)) {
2468 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002469 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2470 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002471 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002472 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002473 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002474 // Nothing to warn about here.
2475 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002476 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002477 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002478
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002479 return;
2480 }
2481
Michael Han99315932013-01-24 16:46:58 +00002482 D->addAttr(::new (S.Context)
2483 WeakImportAttr(Attr.getRange(), S.Context,
2484 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002485}
2486
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002487// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002488template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002489static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002490 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002491 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002492 for (unsigned i = 0; i < 3; ++i) {
2493 const Expr *E = Attr.getArgAsExpr(i);
2494 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002495 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002496 if (WGSize[i] == 0) {
2497 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2498 << Attr.getName() << E->getSourceRange();
2499 return;
2500 }
2501 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002502
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002503 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2504 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2505 Existing->getYDim() == WGSize[1] &&
2506 Existing->getZDim() == WGSize[2]))
2507 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002508
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002509 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2510 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002511 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002512}
2513
Joey Goulyaba589c2013-03-08 09:42:32 +00002514static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002515 if (!Attr.hasParsedType()) {
2516 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2517 << Attr.getName() << 1;
2518 return;
2519 }
2520
Craig Topperc3ec1492014-05-26 06:22:03 +00002521 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002522 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2523 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002524
2525 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2526 (ParmType->isBooleanType() ||
2527 !ParmType->isIntegralType(S.getASTContext()))) {
2528 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2529 << ParmType;
2530 return;
2531 }
2532
Aaron Ballmana9e05402013-12-02 22:16:55 +00002533 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002534 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002535 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2536 return;
2537 }
2538 }
2539
2540 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002541 ParmTSI,
2542 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002543}
2544
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002545SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002546 StringRef Name,
2547 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002548 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2549 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002550 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002551 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2552 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002553 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002554 }
Michael Han99315932013-01-24 16:46:58 +00002555 return ::new (Context) SectionAttr(Range, Context, Name,
2556 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002557}
2558
Reid Kleckner2a133222015-03-04 23:39:17 +00002559bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2560 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2561 if (!Error.empty()) {
2562 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2563 return false;
2564 }
2565 return true;
2566}
2567
Chandler Carruthedc2c642011-07-02 00:01:44 +00002568static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002569 // Make sure that there is a string literal as the sections's single
2570 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002571 StringRef Str;
2572 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002573 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002574 return;
Mike Stump11289f42009-09-09 15:08:12 +00002575
Reid Kleckner2a133222015-03-04 23:39:17 +00002576 if (!S.checkSectionName(LiteralLoc, Str))
2577 return;
2578
Chris Lattner30ba6742009-08-10 19:03:04 +00002579 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002580 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002581 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002582 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002583 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002584 return;
2585 }
Mike Stump11289f42009-09-09 15:08:12 +00002586
Michael Han99315932013-01-24 16:46:58 +00002587 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002588 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002589 if (NewAttr)
2590 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002591}
2592
Eric Christopher789a7ad2015-06-12 01:36:05 +00002593// Check for things we'd like to warn about, no errors or validation for now.
2594// TODO: Validation should use a backend target library that specifies
2595// the allowable subtarget features and cpus. We could use something like a
2596// TargetCodeGenInfo hook here to do validation.
2597void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2598 for (auto Str : {"tune=", "fpmath="})
2599 if (AttrStr.find(Str) != StringRef::npos)
2600 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2601}
2602
Eric Christopher11acf732015-06-12 01:35:52 +00002603static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002604 StringRef Str;
2605 SourceLocation LiteralLoc;
2606 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2607 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002608 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002609 unsigned Index = Attr.getAttributeSpellingListIndex();
2610 TargetAttr *NewAttr =
2611 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2612 D->addAttr(NewAttr);
2613}
2614
Chandler Carruthedc2c642011-07-02 00:01:44 +00002615static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002616 VarDecl *VD = cast<VarDecl>(D);
2617 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002618 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002619 return;
2620 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002622 Expr *E = Attr.getArgAsExpr(0);
2623 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002624 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002625 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002626
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002627 // gcc only allows for simple identifiers. Since we support more than gcc, we
2628 // will warn the user.
2629 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2630 if (DRE->hasQualifier())
2631 S.Diag(Loc, diag::warn_cleanup_ext);
2632 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2633 NI = DRE->getNameInfo();
2634 if (!FD) {
2635 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2636 << NI.getName();
2637 return;
2638 }
2639 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2640 if (ULE->hasExplicitTemplateArgs())
2641 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002642 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2643 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002644 if (!FD) {
2645 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2646 << NI.getName();
2647 if (ULE->getType() == S.Context.OverloadTy)
2648 S.NoteAllOverloadCandidates(ULE);
2649 return;
2650 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002651 } else {
2652 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002653 return;
2654 }
2655
Anders Carlssond277d792009-01-31 01:16:18 +00002656 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002657 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2658 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002659 return;
2660 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002661
Anders Carlsson723f55d2009-02-07 23:16:50 +00002662 // We're currently more strict than GCC about what function types we accept.
2663 // If this ever proves to be a problem it should be easy to fix.
2664 QualType Ty = S.Context.getPointerType(VD->getType());
2665 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002666 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2667 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002668 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2669 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002670 return;
2671 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002672
Michael Han99315932013-01-24 16:46:58 +00002673 D->addAttr(::new (S.Context)
2674 CleanupAttr(Attr.getRange(), S.Context, FD,
2675 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002676}
2677
Mike Stumpd3bb5572009-07-24 19:02:52 +00002678/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002679/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002680static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002681 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002682 uint64_t Idx;
2683 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002684 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002685
Eric Christopherb64963e2015-08-13 21:34:35 +00002686 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002687 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002688
Eric Christopherb64963e2015-08-13 21:34:35 +00002689 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2690 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002691 !isCFStringType(Ty, S.Context) &&
2692 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002693 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002694 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002695 << "a string type" << IdxExpr->getSourceRange()
2696 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002697 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002698 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002699 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002700 if (!isNSStringType(Ty, S.Context) &&
2701 !isCFStringType(Ty, S.Context) &&
2702 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002703 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002704 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002705 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002706 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002707 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002708 }
2709
Alp Toker601b22c2014-01-21 23:35:24 +00002710 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002711 // because that has corrected for the implicit this parameter, and is zero-
2712 // based. The attribute expects what the user wrote explicitly.
2713 llvm::APSInt Val;
2714 IdxExpr->EvaluateAsInt(Val, S.Context);
2715
Michael Han99315932013-01-24 16:46:58 +00002716 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002717 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002718 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002719}
2720
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002721enum FormatAttrKind {
2722 CFStringFormat,
2723 NSStringFormat,
2724 StrftimeFormat,
2725 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002726 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002727 InvalidFormat
2728};
2729
2730/// getFormatAttrKind - Map from format attribute names to supported format
2731/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002732static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002733 return llvm::StringSwitch<FormatAttrKind>(Format)
2734 // Check for formats that get handled specially.
2735 .Case("NSString", NSStringFormat)
2736 .Case("CFString", CFStringFormat)
2737 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002738
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002739 // Otherwise, check for supported formats.
2740 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2741 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2742 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002743 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002744 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002745
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002746 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2747 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002748}
2749
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002750/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002751/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002752static void handleInitPriorityAttr(Sema &S, Decl *D,
2753 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002754 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002755 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2756 return;
2757 }
2758
Aaron Ballman4a611152013-11-27 16:34:09 +00002759 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002760 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2761 Attr.setInvalid();
2762 return;
2763 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002764 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002765 if (S.Context.getAsArrayType(T))
2766 T = S.Context.getBaseElementType(T);
2767 if (!T->getAs<RecordType>()) {
2768 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2769 Attr.setInvalid();
2770 return;
2771 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002772
2773 Expr *E = Attr.getArgAsExpr(0);
2774 uint32_t prioritynum;
2775 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002776 Attr.setInvalid();
2777 return;
2778 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002779
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002780 if (prioritynum < 101 || prioritynum > 65535) {
2781 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002782 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002783 Attr.setInvalid();
2784 return;
2785 }
Michael Han99315932013-01-24 16:46:58 +00002786 D->addAttr(::new (S.Context)
2787 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2788 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002789}
2790
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002791FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2792 IdentifierInfo *Format, int FormatIdx,
2793 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002794 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002795 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002796 for (auto *F : D->specific_attrs<FormatAttr>()) {
2797 if (F->getType() == Format &&
2798 F->getFormatIdx() == FormatIdx &&
2799 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002800 // If we don't have a valid location for this attribute, adopt the
2801 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002802 if (F->getLocation().isInvalid())
2803 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002804 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002805 }
2806 }
2807
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002808 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2809 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002810}
2811
Mike Stumpd3bb5572009-07-24 19:02:52 +00002812/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002813/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002814static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002815 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002816 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002817 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818 return;
2819 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002820
Chandler Carruth743682b2010-11-16 08:35:43 +00002821 // In C++ the implicit 'this' function parameter also counts, and they are
2822 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002823 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002824 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002825
Aaron Ballman00e99962013-08-31 01:11:41 +00002826 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2827 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002828
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002829 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002830 // If we've modified the string name, we need a new identifier for it.
2831 II = &S.Context.Idents.get(Format);
2832 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002834 // Check for supported formats.
2835 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002836
2837 if (Kind == IgnoredFormat)
2838 return;
2839
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002840 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002841 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002842 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002843 return;
2844 }
2845
2846 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002847 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002848 uint32_t Idx;
2849 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002850 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002851
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002852 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002854 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855 return;
2856 }
2857
2858 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002859 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002860
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002861 if (HasImplicitThisParam) {
2862 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002863 S.Diag(Attr.getLoc(),
2864 diag::err_format_attribute_implicit_this_format_string)
2865 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002866 return;
2867 }
2868 ArgIdx--;
2869 }
Mike Stump11289f42009-09-09 15:08:12 +00002870
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002871 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002872 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002873
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002874 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002875 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002876 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002877 << "a CFString" << IdxExpr->getSourceRange()
2878 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002879 return;
2880 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002881 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002882 // FIXME: do we need to check if the type is NSString*? What are the
2883 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002884 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002885 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002886 << "an NSString" << IdxExpr->getSourceRange()
2887 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002888 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002889 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002890 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002891 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002892 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002893 << "a string type" << IdxExpr->getSourceRange()
2894 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002895 return;
2896 }
2897
2898 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002899 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002900 uint32_t FirstArg;
2901 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002902 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002903
2904 // check if the function is variadic if the 3rd argument non-zero
2905 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002906 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907 ++NumArgs; // +1 for ...
2908 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002909 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002910 return;
2911 }
2912 }
2913
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002914 // strftime requires FirstArg to be 0 because it doesn't read from any
2915 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002916 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002917 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002918 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2919 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002920 return;
2921 }
2922 // if 0 it disables parameter checking (to use with e.g. va_list)
2923 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002924 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002925 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002926 return;
2927 }
2928
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002929 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002930 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002931 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002932 if (NewAttr)
2933 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002934}
2935
Chandler Carruthedc2c642011-07-02 00:01:44 +00002936static void handleTransparentUnionAttr(Sema &S, Decl *D,
2937 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002938 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002939 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002940 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002941 if (TD && TD->getUnderlyingType()->isUnionType())
2942 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2943 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002944 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002945
2946 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002947 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002948 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002949 return;
2950 }
2951
John McCallf937c022011-10-07 06:10:15 +00002952 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002953 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002954 diag::warn_transparent_union_attribute_not_definition);
2955 return;
2956 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002957
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002958 RecordDecl::field_iterator Field = RD->field_begin(),
2959 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002960 if (Field == FieldEnd) {
2961 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2962 return;
2963 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002964
David Blaikie40ed2972012-06-06 20:45:41 +00002965 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002966 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002967 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002968 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002969 diag::warn_transparent_union_attribute_floating)
2970 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002971 return;
2972 }
2973
2974 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2975 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2976 for (; Field != FieldEnd; ++Field) {
2977 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002978 // FIXME: this isn't fully correct; we also need to test whether the
2979 // members of the union would all have the same calling convention as the
2980 // first member of the union. Checking just the size and alignment isn't
2981 // sufficient (consider structs passed on the stack instead of in registers
2982 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002983 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002984 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002985 // Warn if we drop the attribute.
2986 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002987 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002988 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002989 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002990 diag::warn_transparent_union_attribute_field_size_align)
2991 << isSize << Field->getDeclName() << FieldBits;
2992 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002993 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002994 diag::note_transparent_union_first_field_size_align)
2995 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002996 return;
2997 }
2998 }
2999
Michael Han99315932013-01-24 16:46:58 +00003000 RD->addAttr(::new (S.Context)
3001 TransparentUnionAttr(Attr.getRange(), S.Context,
3002 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003003}
3004
Chandler Carruthedc2c642011-07-02 00:01:44 +00003005static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003006 // Make sure that there is a string literal as the annotation's single
3007 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003008 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003009 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003010 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003011
3012 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003013 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3014 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003015 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003016 }
Michael Han99315932013-01-24 16:46:58 +00003017
3018 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003019 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003020 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003021}
3022
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003023static void handleAlignValueAttr(Sema &S, Decl *D,
3024 const AttributeList &Attr) {
3025 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3026 Attr.getAttributeSpellingListIndex());
3027}
3028
3029void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3030 unsigned SpellingListIndex) {
3031 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3032 SourceLocation AttrLoc = AttrRange.getBegin();
3033
3034 QualType T;
3035 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3036 T = TD->getUnderlyingType();
3037 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3038 T = VD->getType();
3039 else
3040 llvm_unreachable("Unknown decl type for align_value");
3041
3042 if (!T->isDependentType() && !T->isAnyPointerType() &&
3043 !T->isReferenceType() && !T->isMemberPointerType()) {
3044 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3045 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3046 return;
3047 }
3048
3049 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003050 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003051 ExprResult ICE
3052 = VerifyIntegerConstantExpression(E, &Alignment,
3053 diag::err_align_value_attribute_argument_not_int,
3054 /*AllowFold*/ false);
3055 if (ICE.isInvalid())
3056 return;
3057
3058 if (!Alignment.isPowerOf2()) {
3059 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3060 << E->getSourceRange();
3061 return;
3062 }
3063
3064 D->addAttr(::new (Context)
3065 AlignValueAttr(AttrRange, Context, ICE.get(),
3066 SpellingListIndex));
3067 return;
3068 }
3069
3070 // Save dependent expressions in the AST to be instantiated.
3071 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003072}
3073
Chandler Carruthedc2c642011-07-02 00:01:44 +00003074static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003075 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003076 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3078 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003079 return;
3080 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003081
Richard Smith848e1f12013-02-01 08:12:08 +00003082 if (Attr.getNumArgs() == 0) {
3083 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003084 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003085 return;
3086 }
3087
Aaron Ballman00e99962013-08-31 01:11:41 +00003088 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003089 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3090 S.Diag(Attr.getEllipsisLoc(),
3091 diag::err_pack_expansion_without_parameter_packs);
3092 return;
3093 }
3094
3095 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3096 return;
3097
David Majnemer26a1e0e2015-04-07 02:37:09 +00003098 if (E->isValueDependent()) {
3099 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3100 if (!TND->getUnderlyingType()->isDependentType()) {
3101 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3102 << E->getSourceRange();
3103 return;
3104 }
3105 }
3106 }
3107
Richard Smith44c247f2013-02-22 08:32:16 +00003108 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3109 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003110}
3111
3112void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003113 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003114 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3115 SourceLocation AttrLoc = AttrRange.getBegin();
3116
Richard Smith1dba27c2013-01-29 09:02:09 +00003117 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003118 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003119 // C++11 [dcl.align]p1:
3120 // An alignment-specifier may be applied to a variable or to a class
3121 // data member, but it shall not be applied to a bit-field, a function
3122 // parameter, the formal parameter of a catch clause, or a variable
3123 // declared with the register storage class specifier. An
3124 // alignment-specifier may also be applied to the declaration of a class
3125 // or enumeration type.
3126 // C11 6.7.5/2:
3127 // An alignment attribute shall not be specified in a declaration of
3128 // a typedef, or a bit-field, or a function, or a parameter, or an
3129 // object declared with the register storage-class specifier.
3130 int DiagKind = -1;
3131 if (isa<ParmVarDecl>(D)) {
3132 DiagKind = 0;
3133 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3134 if (VD->getStorageClass() == SC_Register)
3135 DiagKind = 1;
3136 if (VD->isExceptionVariable())
3137 DiagKind = 2;
3138 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3139 if (FD->isBitField())
3140 DiagKind = 3;
3141 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003142 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003143 << (TmpAttr.isC11() ? ExpectedVariableOrField
3144 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003145 return;
3146 }
3147 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003148 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003149 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003150 return;
3151 }
3152 }
3153
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003154 if (E->isTypeDependent() || E->isValueDependent()) {
3155 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003156 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3157 AA->setPackExpansion(IsPackExpansion);
3158 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003159 return;
3160 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003161
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003162 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003163 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003164 ExprResult ICE
3165 = VerifyIntegerConstantExpression(E, &Alignment,
3166 diag::err_aligned_attribute_argument_not_int,
3167 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003168 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003169 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003170
David Majnemer0be6bd02015-07-26 09:02:21 +00003171 uint64_t AlignVal = Alignment.getZExtValue();
3172
Richard Smith848e1f12013-02-01 08:12:08 +00003173 // C++11 [dcl.align]p2:
3174 // -- if the constant expression evaluates to zero, the alignment
3175 // specifier shall have no effect
3176 // C11 6.7.5p6:
3177 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003178 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003179 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003180 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3181 << E->getSourceRange();
3182 return;
3183 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003184 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003185
David Majnemerabecae72014-02-12 20:36:10 +00003186 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003187 unsigned MaxValidAlignment =
3188 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3189 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003190 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003191 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3192 << E->getSourceRange();
3193 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003194 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003195
David Majnemer0be6bd02015-07-26 09:02:21 +00003196 if (Context.getTargetInfo().isTLSSupported()) {
3197 unsigned MaxTLSAlign =
3198 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3199 .getQuantity();
3200 auto *VD = dyn_cast<VarDecl>(D);
3201 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3202 VD->getTLSKind() != VarDecl::TLS_None) {
3203 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3204 << (unsigned)AlignVal << VD << MaxTLSAlign;
3205 return;
3206 }
3207 }
3208
Richard Smith44c247f2013-02-22 08:32:16 +00003209 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003210 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003211 AA->setPackExpansion(IsPackExpansion);
3212 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003213}
3214
Michael Hanaf02bbe2013-02-01 01:19:17 +00003215void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003216 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003217 // FIXME: Cache the number on the Attr object if non-dependent?
3218 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003219 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3220 SpellingListIndex);
3221 AA->setPackExpansion(IsPackExpansion);
3222 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003223}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003224
Richard Smith848e1f12013-02-01 08:12:08 +00003225void Sema::CheckAlignasUnderalignment(Decl *D) {
3226 assert(D->hasAttrs() && "no attributes on decl");
3227
David Majnemer475b25e2015-01-21 10:54:38 +00003228 QualType UnderlyingTy, DiagTy;
3229 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3230 UnderlyingTy = DiagTy = VD->getType();
3231 } else {
3232 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3233 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3234 UnderlyingTy = ED->getIntegerType();
3235 }
3236 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003237 return;
3238
3239 // C++11 [dcl.align]p5, C11 6.7.5/4:
3240 // The combined effect of all alignment attributes in a declaration shall
3241 // not specify an alignment that is less strict than the alignment that
3242 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003243 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003244 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003245 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003246 if (I->isAlignmentDependent())
3247 return;
3248 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003249 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003250 Align = std::max(Align, I->getAlignment(Context));
3251 }
3252
3253 if (AlignasAttr && Align) {
3254 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003255 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003256 if (NaturalAlign > RequestedAlign)
3257 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003258 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003259 }
3260}
3261
David Majnemer2c4e00a2014-01-29 22:07:36 +00003262bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003263 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003264 MSInheritanceAttr::Spelling SemanticSpelling) {
3265 assert(RD->hasDefinition() && "RD has no definition!");
3266
David Majnemer98c9ee22014-02-07 00:43:07 +00003267 // We may not have seen base specifiers or any virtual methods yet. We will
3268 // have to wait until the record is defined to catch any mismatches.
3269 if (!RD->getDefinition()->isCompleteDefinition())
3270 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003271
David Majnemer98c9ee22014-02-07 00:43:07 +00003272 // The unspecified model never matches what a definition could need.
3273 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3274 return false;
3275
David Majnemer4bb09802014-02-10 19:50:15 +00003276 if (BestCase) {
3277 if (RD->calculateInheritanceModel() == SemanticSpelling)
3278 return false;
3279 } else {
3280 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3281 return false;
3282 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003283
3284 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3285 << 0 /*definition*/;
3286 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3287 << RD->getNameAsString();
3288 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003289}
3290
Alexey Bataevf278eb12015-11-19 10:13:11 +00003291/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3292/// attribute.
3293static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3294 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003295 IntegerMode = true;
3296 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003297 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003298 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003299 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003300 case 'Q':
3301 DestWidth = 8;
3302 break;
3303 case 'H':
3304 DestWidth = 16;
3305 break;
3306 case 'S':
3307 DestWidth = 32;
3308 break;
3309 case 'D':
3310 DestWidth = 64;
3311 break;
3312 case 'X':
3313 DestWidth = 96;
3314 break;
3315 case 'T':
3316 DestWidth = 128;
3317 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003318 }
3319 if (Str[1] == 'F') {
3320 IntegerMode = false;
3321 } else if (Str[1] == 'C') {
3322 IntegerMode = false;
3323 ComplexMode = true;
3324 } else if (Str[1] != 'I') {
3325 DestWidth = 0;
3326 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003327 break;
3328 case 4:
3329 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3330 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003331 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003332 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003333 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003334 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003335 break;
3336 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003337 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003338 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003339 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003340 case 11:
3341 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003342 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003343 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003344 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003345}
3346
3347/// handleModeAttr - This attribute modifies the width of a decl with primitive
3348/// type.
3349///
3350/// Despite what would be logical, the mode attribute is a decl attribute, not a
3351/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3352/// HImode, not an intermediate pointer.
3353static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3354 // This attribute isn't documented, but glibc uses it. It changes
3355 // the width of an int or unsigned int to the specified size.
3356 if (!Attr.isArgIdent(0)) {
3357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3358 << AANT_ArgumentIdentifier;
3359 return;
3360 }
3361
3362 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003363
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003364 S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex());
3365}
3366
3367void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3368 unsigned SpellingListIndex, bool InInstantiation) {
3369 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003370 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003371 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003372
3373 unsigned DestWidth = 0;
3374 bool IntegerMode = true;
3375 bool ComplexMode = false;
3376 llvm::APInt VectorSize(64, 0);
3377 if (Str.size() >= 4 && Str[0] == 'V') {
3378 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3379 size_t StrSize = Str.size();
3380 size_t VectorStringLength = 0;
3381 while ((VectorStringLength + 1) < StrSize &&
3382 isdigit(Str[VectorStringLength + 1]))
3383 ++VectorStringLength;
3384 if (VectorStringLength &&
3385 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3386 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003387 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003388 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003389 // Avoid duplicate warning from template instantiation.
3390 if (!InInstantiation)
3391 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003392 } else {
3393 VectorSize = 0;
3394 }
3395 }
3396
3397 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003398 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3399
3400 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3401 // and friends, at least with glibc.
3402 // FIXME: Make sure floating-point mappings are accurate
3403 // FIXME: Support XF and TF types
3404 if (!DestWidth) {
3405 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3406 return;
3407 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003408
3409 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003410 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003411 OldTy = TD->getUnderlyingType();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003412 else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
3413 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3414 // Try to get type from enum declaration, default to int.
3415 OldTy = ED->getIntegerType();
3416 if (OldTy.isNull())
3417 OldTy = Context.IntTy;
3418 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003419 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003420
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003421 if (OldTy->isDependentType()) {
3422 D->addAttr(::new (Context)
3423 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3424 return;
3425 }
3426
Alexey Bataev326057d2015-06-19 07:46:21 +00003427 // Base type can also be a vector type (see PR17453).
3428 // Distinguish between base type and base element type.
3429 QualType OldElemTy = OldTy;
3430 if (const VectorType *VT = OldTy->getAs<VectorType>())
3431 OldElemTy = VT->getElementType();
3432
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003433 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3434 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3435 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3436 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3437 VectorSize.getBoolValue()) {
3438 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3439 return;
3440 }
3441 bool IntegralOrAnyEnumType =
3442 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3443
3444 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3445 !IntegralOrAnyEnumType)
3446 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003447 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003448 if (!IntegralOrAnyEnumType)
3449 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003450 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003451 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003452 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003453 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003454 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003455 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003456 }
3457
Alexey Bataev326057d2015-06-19 07:46:21 +00003458 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003459
3460 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003461 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3462 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003463 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003464 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003465
Alexey Bataev326057d2015-06-19 07:46:21 +00003466 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003467 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003468 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003469 }
3470
Eli Friedman4735374e2009-03-03 06:41:03 +00003471 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003472 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003473 }
3474
3475 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003476 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003477 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3478 VectorType::GenericVector);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003479 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003480 // Complex machine mode does not support base vector types.
3481 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003482 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003483 return;
3484 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003485 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003486 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003487 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003488 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003489 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003490 }
3491
3492 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003493 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003494 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003495 }
3496
3497 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003498 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3499 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003500 else if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3501 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003502 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003503 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003504
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003505 D->addAttr(::new (Context)
3506 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003507}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003508
Chandler Carruthedc2c642011-07-02 00:01:44 +00003509static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003510 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3511 if (!VD->hasGlobalStorage())
3512 S.Diag(Attr.getLoc(),
3513 diag::warn_attribute_requires_functions_or_static_globals)
3514 << Attr.getName();
3515 } else if (!isFunctionOrMethod(D)) {
3516 S.Diag(Attr.getLoc(),
3517 diag::warn_attribute_requires_functions_or_static_globals)
3518 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003519 return;
3520 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003521
Michael Han99315932013-01-24 16:46:58 +00003522 D->addAttr(::new (S.Context)
3523 NoDebugAttr(Attr.getRange(), S.Context,
3524 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003525}
3526
Paul Robinson30e41fb2014-12-15 18:57:28 +00003527AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003528 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003529 unsigned AttrSpellingListIndex) {
3530 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003531 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003532 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3533 return nullptr;
3534 }
3535
3536 if (D->hasAttr<AlwaysInlineAttr>())
3537 return nullptr;
3538
3539 return ::new (Context) AlwaysInlineAttr(Range, Context,
3540 AttrSpellingListIndex);
3541}
3542
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003543CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3544 IdentifierInfo *Ident,
3545 unsigned AttrSpellingListIndex) {
3546 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3547 return nullptr;
3548
3549 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3550}
3551
3552InternalLinkageAttr *
3553Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3554 IdentifierInfo *Ident,
3555 unsigned AttrSpellingListIndex) {
3556 if (auto VD = dyn_cast<VarDecl>(D)) {
3557 // Attribute applies to Var but not any subclass of it (like ParmVar,
3558 // ImplicitParm or VarTemplateSpecialization).
3559 if (VD->getKind() != Decl::Var) {
3560 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3561 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3562 : ExpectedVariableOrFunction);
3563 return nullptr;
3564 }
3565 // Attribute does not apply to non-static local variables.
3566 if (VD->hasLocalStorage()) {
3567 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3568 return nullptr;
3569 }
3570 }
3571
3572 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3573 return nullptr;
3574
3575 return ::new (Context)
3576 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3577}
3578
Paul Robinson30e41fb2014-12-15 18:57:28 +00003579MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3580 unsigned AttrSpellingListIndex) {
3581 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3582 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3583 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3584 return nullptr;
3585 }
3586
3587 if (D->hasAttr<MinSizeAttr>())
3588 return nullptr;
3589
3590 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3591}
3592
3593OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3594 unsigned AttrSpellingListIndex) {
3595 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3596 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3597 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3598 D->dropAttr<AlwaysInlineAttr>();
3599 }
3600 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3601 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3602 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3603 D->dropAttr<MinSizeAttr>();
3604 }
3605
3606 if (D->hasAttr<OptimizeNoneAttr>())
3607 return nullptr;
3608
3609 return ::new (Context) OptimizeNoneAttr(Range, Context,
3610 AttrSpellingListIndex);
3611}
3612
Paul Robinsonf0674352014-03-31 22:29:15 +00003613static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3614 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003615 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3616 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003617 return;
3618
Paul Robinson080b1f32015-01-13 18:34:56 +00003619 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3620 D, Attr.getRange(), Attr.getName(),
3621 Attr.getAttributeSpellingListIndex()))
3622 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003623}
3624
Paul Robinson080b1f32015-01-13 18:34:56 +00003625static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3626 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3627 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3628 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003629}
3630
Paul Robinsonf0674352014-03-31 22:29:15 +00003631static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3632 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003633 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3634 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3635 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003636}
3637
Chandler Carruthedc2c642011-07-02 00:01:44 +00003638static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00003639 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(),
3640 Attr.getName()) ||
3641 checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(),
3642 Attr.getName())) {
3643 return;
3644 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003645 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003646 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003647 SourceRange RTRange = FD->getReturnTypeSourceRange();
3648 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003649 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003650 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3651 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003652 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003653 }
Justin Lebarc66a1062016-01-20 00:26:57 +00003654 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
3655 if (Method->isInstance()) {
3656 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
3657 << Method;
3658 return;
3659 }
3660 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
3661 }
3662 // Only warn for "inline" when compiling for host, to cut down on noise.
3663 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
3664 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003665
Aaron Ballman3aff6332013-12-02 19:30:36 +00003666 D->addAttr(::new (S.Context)
3667 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003668 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003669}
3670
Chandler Carruthedc2c642011-07-02 00:01:44 +00003671static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003672 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003673 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003674 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003675 return;
3676 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003677
Michael Han99315932013-01-24 16:46:58 +00003678 D->addAttr(::new (S.Context)
3679 GNUInlineAttr(Attr.getRange(), S.Context,
3680 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003681}
3682
Chandler Carruthedc2c642011-07-02 00:01:44 +00003683static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003684 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003685
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003686 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003687 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3688 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003689 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003690 return;
3691
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003692 if (!isa<ObjCMethodDecl>(D)) {
3693 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3694 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003695 return;
3696 }
3697
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003698 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003699 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003700 D->addAttr(::new (S.Context)
3701 FastCallAttr(Attr.getRange(), S.Context,
3702 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003703 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003704 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003705 D->addAttr(::new (S.Context)
3706 StdCallAttr(Attr.getRange(), S.Context,
3707 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003708 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003709 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003710 D->addAttr(::new (S.Context)
3711 ThisCallAttr(Attr.getRange(), S.Context,
3712 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003713 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003714 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003715 D->addAttr(::new (S.Context)
3716 CDeclAttr(Attr.getRange(), S.Context,
3717 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003718 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003719 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003720 D->addAttr(::new (S.Context)
3721 PascalAttr(Attr.getRange(), S.Context,
3722 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003723 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003724 case AttributeList::AT_VectorCall:
3725 D->addAttr(::new (S.Context)
3726 VectorCallAttr(Attr.getRange(), S.Context,
3727 Attr.getAttributeSpellingListIndex()));
3728 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003729 case AttributeList::AT_MSABI:
3730 D->addAttr(::new (S.Context)
3731 MSABIAttr(Attr.getRange(), S.Context,
3732 Attr.getAttributeSpellingListIndex()));
3733 return;
3734 case AttributeList::AT_SysVABI:
3735 D->addAttr(::new (S.Context)
3736 SysVABIAttr(Attr.getRange(), S.Context,
3737 Attr.getAttributeSpellingListIndex()));
3738 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003739 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003740 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003741 switch (CC) {
3742 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003743 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003744 break;
3745 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003746 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003747 break;
3748 default:
3749 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003750 }
3751
Michael Han99315932013-01-24 16:46:58 +00003752 D->addAttr(::new (S.Context)
3753 PcsAttr(Attr.getRange(), S.Context, PCS,
3754 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003755 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003756 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003757 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003758 D->addAttr(::new (S.Context)
3759 IntelOclBiccAttr(Attr.getRange(), S.Context,
3760 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003761 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003762
Abramo Bagnara50099372010-04-30 13:10:51 +00003763 default:
3764 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003765 }
3766}
3767
Aaron Ballman02df2e02012-12-09 17:45:41 +00003768bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3769 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003770 if (attr.isInvalid())
3771 return true;
3772
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003773 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003774 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003775 attr.setInvalid();
3776 return true;
3777 }
3778
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003779 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003780 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003781 case AttributeList::AT_CDecl: CC = CC_C; break;
3782 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3783 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3784 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3785 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003786 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003787 case AttributeList::AT_MSABI:
3788 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3789 CC_X86_64Win64;
3790 break;
3791 case AttributeList::AT_SysVABI:
3792 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3793 CC_C;
3794 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003795 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003796 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003797 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003798 attr.setInvalid();
3799 return true;
3800 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003801 if (StrRef == "aapcs") {
3802 CC = CC_AAPCS;
3803 break;
3804 } else if (StrRef == "aapcs-vfp") {
3805 CC = CC_AAPCS_VFP;
3806 break;
3807 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003808
3809 attr.setInvalid();
3810 Diag(attr.getLoc(), diag::err_invalid_pcs);
3811 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003812 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003813 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003814 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003815 }
3816
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003817 const TargetInfo &TI = Context.getTargetInfo();
3818 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003819 if (A != TargetInfo::CCCR_OK) {
3820 if (A == TargetInfo::CCCR_Warning)
3821 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003822
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003823 // This convention is not valid for the target. Use the default function or
3824 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003825 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3826 if (FD)
3827 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3828 TargetInfo::CCMT_NonMember;
3829 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003830 }
3831
John McCall3882ace2011-01-05 12:14:39 +00003832 return false;
3833}
3834
John McCall3882ace2011-01-05 12:14:39 +00003835/// Checks a regparm attribute, returning true if it is ill-formed and
3836/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003837bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3838 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003839 return true;
3840
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003841 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003842 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003843 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003844 }
Eli Friedman7044b762009-03-27 21:06:47 +00003845
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003846 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003847 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003848 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003849 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003850 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003851 }
3852
Douglas Gregore8bbc122011-09-02 00:18:52 +00003853 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003854 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003855 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003856 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003857 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003858 }
3859
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003860 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003861 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003862 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003863 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003864 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003865 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003866 }
3867
John McCall3882ace2011-01-05 12:14:39 +00003868 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003869}
3870
Artem Belevich7093e402015-04-21 22:55:54 +00003871// Checks whether an argument of launch_bounds attribute is acceptable
3872// May output an error.
3873static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3874 const CUDALaunchBoundsAttr &Attr,
3875 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00003876 if (S.DiagnoseUnexpandedParameterPack(E))
3877 return false;
3878
3879 // Accept template arguments for now as they depend on something else.
3880 // We'll get to check them when they eventually get instantiated.
3881 if (E->isValueDependent())
3882 return true;
3883
3884 llvm::APSInt I(64);
3885 if (!E->isIntegerConstantExpr(I, S.Context)) {
3886 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3887 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3888 return false;
3889 }
3890 // Make sure we can fit it in 32 bits.
3891 if (!I.isIntN(32)) {
3892 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3893 << 32 << /* Unsigned */ 1;
3894 return false;
3895 }
3896 if (I < 0)
3897 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3898 << &Attr << Idx << E->getSourceRange();
3899
3900 return true;
3901}
3902
3903void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3904 Expr *MinBlocks, unsigned SpellingListIndex) {
3905 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3906 SpellingListIndex);
3907
3908 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003909 return;
3910
Artem Belevich7093e402015-04-21 22:55:54 +00003911 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3912 return;
3913
3914 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3915 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3916}
3917
3918static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3919 const AttributeList &Attr) {
3920 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3921 !checkAttributeAtMostNumArgs(S, Attr, 2))
3922 return;
3923
3924 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3925 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3926 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003927}
3928
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003929static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3930 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003931 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003933 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003934 return;
3935 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003936
3937 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003938 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003939
Aaron Ballman00e99962013-08-31 01:11:41 +00003940 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003941
3942 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3943 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3944 << Attr.getName() << ExpectedFunctionOrMethod;
3945 return;
3946 }
3947
3948 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003949 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3950 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003951 return;
3952
3953 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003954 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3955 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003956 return;
3957
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003958 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003959 if (IsPointer) {
3960 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003961 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003962 if (!BufferTy->isPointerType()) {
3963 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003964 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003965 }
3966 }
3967
Michael Han99315932013-01-24 16:46:58 +00003968 D->addAttr(::new (S.Context)
3969 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3970 ArgumentIdx, TypeTagIdx, IsPointer,
3971 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003972}
3973
3974static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3975 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003976 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003977 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003978 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003979 return;
3980 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003981
3982 if (!checkAttributeNumArgs(S, Attr, 1))
3983 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003984
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003985 if (!isa<VarDecl>(D)) {
3986 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3987 << Attr.getName() << ExpectedVariable;
3988 return;
3989 }
3990
Aaron Ballman00e99962013-08-31 01:11:41 +00003991 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003992 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003993 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3994 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003995
Michael Han99315932013-01-24 16:46:58 +00003996 D->addAttr(::new (S.Context)
3997 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003998 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003999 Attr.getLayoutCompatible(),
4000 Attr.getMustBeNull(),
4001 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004002}
4003
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004004//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004005// Checker-specific attribute handlers.
4006//===----------------------------------------------------------------------===//
4007
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004008static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004009 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004010 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004011}
4012
John McCalled433932011-01-25 03:31:58 +00004013static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004014 return type->isDependentType() ||
4015 type->isObjCObjectPointerType() ||
4016 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004017}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004018
John McCalled433932011-01-25 03:31:58 +00004019static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004020 return type->isDependentType() ||
4021 type->isPointerType() ||
4022 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004023}
4024
Chandler Carruthedc2c642011-07-02 00:01:44 +00004025static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004026 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004027 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004028
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004030 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4031 cf = false;
4032 } else {
4033 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4034 cf = true;
4035 }
4036
4037 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004038 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004039 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004040 return;
4041 }
4042
4043 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004044 param->addAttr(::new (S.Context)
4045 CFConsumedAttr(Attr.getRange(), S.Context,
4046 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004047 else
Michael Han99315932013-01-24 16:46:58 +00004048 param->addAttr(::new (S.Context)
4049 NSConsumedAttr(Attr.getRange(), S.Context,
4050 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004051}
4052
Chandler Carruthedc2c642011-07-02 00:01:44 +00004053static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4054 const AttributeList &Attr) {
John McCalled433932011-01-25 03:31:58 +00004055 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004056
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004057 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004058 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004059 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004060 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004061 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004062 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4063 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004064 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004065 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004066 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4067 returnType = Param->getType()->getPointeeType();
4068 if (returnType.isNull()) {
4069 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4070 << Attr.getName() << /*pointer-to-CF*/2
4071 << Attr.getRange();
4072 return;
4073 }
4074 } else {
4075 AttributeDeclKind ExpectedDeclKind;
4076 switch (Attr.getKind()) {
4077 default: llvm_unreachable("invalid ownership attribute");
4078 case AttributeList::AT_NSReturnsRetained:
4079 case AttributeList::AT_NSReturnsAutoreleased:
4080 case AttributeList::AT_NSReturnsNotRetained:
4081 ExpectedDeclKind = ExpectedFunctionOrMethod;
4082 break;
4083
4084 case AttributeList::AT_CFReturnsRetained:
4085 case AttributeList::AT_CFReturnsNotRetained:
4086 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4087 break;
4088 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004089 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004090 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004091 return;
4092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004093
John McCalled433932011-01-25 03:31:58 +00004094 bool typeOK;
4095 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004096 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004097 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004099 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004100 cf = false;
4101 break;
4102
4103 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004105 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4106 cf = false;
4107 break;
4108
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004109 case AttributeList::AT_CFReturnsRetained:
4110 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004111 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4112 cf = true;
4113 break;
4114 }
4115
4116 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004117 if (isa<ParmVarDecl>(D)) {
4118 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4119 << Attr.getName() << /*pointer-to-CF*/2
4120 << Attr.getRange();
4121 } else {
4122 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4123 enum : unsigned {
4124 Function,
4125 Method,
4126 Property
4127 } SubjectKind = Function;
4128 if (isa<ObjCMethodDecl>(D))
4129 SubjectKind = Method;
4130 else if (isa<ObjCPropertyDecl>(D))
4131 SubjectKind = Property;
4132 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4133 << Attr.getName() << SubjectKind << cf
4134 << Attr.getRange();
4135 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004136 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004137 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004138
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004139 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004140 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004141 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004142 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004143 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4144 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004145 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004147 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4148 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004149 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004150 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004151 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4152 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004153 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004154 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004155 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4156 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004157 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004159 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4160 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004161 return;
4162 };
4163}
4164
John McCallcf166702011-07-22 08:53:00 +00004165static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4166 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004167 const int EP_ObjCMethod = 1;
4168 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004169
John McCallcf166702011-07-22 08:53:00 +00004170 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004171 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004172 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004173 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004174 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004175 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004176
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004177 if (!resultType->isReferenceType() &&
4178 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004179 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004180 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004181 << attr.getName()
4182 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004183 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004184
4185 // Drop the attribute.
4186 return;
4187 }
4188
Nico Weber462fd1e2015-01-07 23:50:05 +00004189 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4190 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004191}
4192
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004193static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4194 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004195 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004196
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004197 DeclContext *DC = method->getDeclContext();
4198 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4199 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4200 << attr.getName() << 0;
4201 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4202 return;
4203 }
4204 if (method->getMethodFamily() == OMF_dealloc) {
4205 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4206 << attr.getName() << 1;
4207 return;
4208 }
4209
Michael Han99315932013-01-24 16:46:58 +00004210 method->addAttr(::new (S.Context)
4211 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4212 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004213}
4214
Aaron Ballmanfb763042013-12-02 18:05:46 +00004215static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4216 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004217 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4218 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004219 return;
John McCall32f5fe12011-09-30 05:12:12 +00004220
Aaron Ballmanfb763042013-12-02 18:05:46 +00004221 D->addAttr(::new (S.Context)
4222 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4223 Attr.getAttributeSpellingListIndex()));
4224}
4225
4226static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4227 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004228 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4229 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004230 return;
4231
4232 D->addAttr(::new (S.Context)
4233 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4234 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004235}
4236
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004237static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4238 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004239 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004240
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004241 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004242 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004243 return;
4244 }
John McCall28592582015-02-01 22:34:06 +00004245
4246 // Typedefs only allow objc_bridge(id) and have some additional checking.
4247 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4248 if (!Parm->Ident->isStr("id")) {
4249 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4250 << Attr.getName();
4251 return;
4252 }
4253
4254 // Only allow 'cv void *'.
4255 QualType T = TD->getUnderlyingType();
4256 if (!T->isVoidPointerType()) {
4257 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4258 return;
4259 }
4260 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004261
4262 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004263 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004264 Attr.getAttributeSpellingListIndex()));
4265}
4266
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004267static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4268 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004269 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4270
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004271 if (!Parm) {
4272 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4273 return;
4274 }
4275
4276 D->addAttr(::new (S.Context)
4277 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4278 Attr.getAttributeSpellingListIndex()));
4279}
4280
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004281static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4282 const AttributeList &Attr) {
4283 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004284 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004285 if (!RelatedClass) {
4286 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4287 return;
4288 }
4289 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004290 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004291 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004292 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004293 D->addAttr(::new (S.Context)
4294 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4295 ClassMethod, InstanceMethod,
4296 Attr.getAttributeSpellingListIndex()));
4297}
4298
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004299static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4300 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004301 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004302 if (ObjCCategoryDecl *CatDecl =
4303 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004304 IFace = CatDecl->getClassInterface();
4305 else
4306 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004307
4308 if (!IFace)
4309 return;
4310
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004311 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004312 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004313 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4314 Attr.getAttributeSpellingListIndex()));
4315}
4316
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004317static void handleObjCRuntimeName(Sema &S, Decl *D,
4318 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004319 StringRef MetaDataName;
4320 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4321 return;
4322 D->addAttr(::new (S.Context)
4323 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4324 MetaDataName,
4325 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004326}
4327
Alex Denisovfde64952015-06-26 05:28:36 +00004328// when a user wants to use objc_boxable with a union or struct
4329// but she doesn't have access to the declaration (legacy/third-party code)
4330// then she can 'enable' this feature via trick with a typedef
4331// e.g.:
4332// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4333static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4334 bool notify = false;
4335
4336 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4337 if (RD && RD->getDefinition()) {
4338 RD = RD->getDefinition();
4339 notify = true;
4340 }
4341
4342 if (RD) {
4343 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4344 ObjCBoxableAttr(Attr.getRange(), S.Context,
4345 Attr.getAttributeSpellingListIndex());
4346 RD->addAttr(BoxableAttr);
4347 if (notify) {
4348 // we need to notify ASTReader/ASTWriter about
4349 // modification of existing declaration
4350 if (ASTMutationListener *L = S.getASTMutationListener())
4351 L->AddedAttributeToRecord(BoxableAttr, RD);
4352 }
4353 }
4354}
4355
Chandler Carruthedc2c642011-07-02 00:01:44 +00004356static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4357 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004358 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004359
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004360 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004361 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004362}
4363
Chandler Carruthedc2c642011-07-02 00:01:44 +00004364static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4365 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004366 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004367 QualType type = vd->getType();
4368
4369 if (!type->isDependentType() &&
4370 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004371 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004372 << type;
4373 return;
4374 }
4375
4376 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4377
4378 // If we have no lifetime yet, check the lifetime we're presumably
4379 // going to infer.
4380 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4381 lifetime = type->getObjCARCImplicitLifetime();
4382
4383 switch (lifetime) {
4384 case Qualifiers::OCL_None:
4385 assert(type->isDependentType() &&
4386 "didn't infer lifetime for non-dependent type?");
4387 break;
4388
4389 case Qualifiers::OCL_Weak: // meaningful
4390 case Qualifiers::OCL_Strong: // meaningful
4391 break;
4392
4393 case Qualifiers::OCL_ExplicitNone:
4394 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004395 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004396 << (lifetime == Qualifiers::OCL_Autoreleasing);
4397 break;
4398 }
4399
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004400 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004401 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4402 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004403}
4404
Francois Picheta83957a2010-12-19 06:50:37 +00004405//===----------------------------------------------------------------------===//
4406// Microsoft specific attribute handlers.
4407//===----------------------------------------------------------------------===//
4408
Chandler Carruthedc2c642011-07-02 00:01:44 +00004409static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004410 if (!S.LangOpts.CPlusPlus) {
4411 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4412 << Attr.getName() << AttributeLangSupport::C;
4413 return;
4414 }
4415
Aaron Ballman60e705e2013-11-24 20:58:02 +00004416 if (!isa<CXXRecordDecl>(D)) {
4417 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4418 << Attr.getName() << ExpectedClass;
4419 return;
4420 }
4421
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004422 StringRef StrRef;
4423 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004424 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004425 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004426
David Majnemer89085342013-08-09 08:56:20 +00004427 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4428 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004429 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4430 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004431
Reid Kleckner140c4a72013-05-17 14:04:52 +00004432 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004433 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004434 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004435 return;
4436 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004437
David Majnemer89085342013-08-09 08:56:20 +00004438 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004439 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004440 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004441 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004442 return;
4443 }
David Majnemer89085342013-08-09 08:56:20 +00004444 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004445 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004446 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004447 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004448 }
Francois Picheta83957a2010-12-19 06:50:37 +00004449
David Majnemer89085342013-08-09 08:56:20 +00004450 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4451 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004452}
4453
David Majnemer2c4e00a2014-01-29 22:07:36 +00004454static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4455 if (!S.LangOpts.CPlusPlus) {
4456 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4457 << Attr.getName() << AttributeLangSupport::C;
4458 return;
4459 }
4460 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004461 D, Attr.getRange(), /*BestCase=*/true,
4462 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004463 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00004464 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00004465 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00004466 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
4467 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00004468}
4469
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004470static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4471 const AttributeList &Attr) {
4472 VarDecl *VD = cast<VarDecl>(D);
4473 if (!S.Context.getTargetInfo().isTLSSupported()) {
4474 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4475 return;
4476 }
4477 if (VD->getTSCSpec() != TSCS_unspecified) {
4478 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4479 return;
4480 }
4481 if (VD->hasLocalStorage()) {
4482 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4483 return;
4484 }
4485 VD->addAttr(::new (S.Context) ThreadAttr(
4486 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4487}
4488
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004489static void handleARMInterruptAttr(Sema &S, Decl *D,
4490 const AttributeList &Attr) {
4491 // Check the attribute arguments.
4492 if (Attr.getNumArgs() > 1) {
4493 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4494 << Attr.getName() << 1;
4495 return;
4496 }
4497
4498 StringRef Str;
4499 SourceLocation ArgLoc;
4500
4501 if (Attr.getNumArgs() == 0)
4502 Str = "";
4503 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4504 return;
4505
4506 ARMInterruptAttr::InterruptType Kind;
4507 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4508 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4509 << Attr.getName() << Str << ArgLoc;
4510 return;
4511 }
4512
4513 unsigned Index = Attr.getAttributeSpellingListIndex();
4514 D->addAttr(::new (S.Context)
4515 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4516}
4517
4518static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4519 const AttributeList &Attr) {
4520 if (!checkAttributeNumArgs(S, Attr, 1))
4521 return;
4522
4523 if (!Attr.isArgExpr(0)) {
4524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4525 << AANT_ArgumentIntegerConstant;
4526 return;
4527 }
4528
4529 // FIXME: Check for decl - it should be void ()(void).
4530
4531 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4532 llvm::APSInt NumParams(32);
4533 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4534 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4535 << Attr.getName() << AANT_ArgumentIntegerConstant
4536 << NumParamsExpr->getSourceRange();
4537 return;
4538 }
4539
4540 unsigned Num = NumParams.getLimitedValue(255);
4541 if ((Num & 1) || Num > 30) {
4542 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4543 << Attr.getName() << (int)NumParams.getSExtValue()
4544 << NumParamsExpr->getSourceRange();
4545 return;
4546 }
4547
Aaron Ballman36a53502014-01-16 13:03:14 +00004548 D->addAttr(::new (S.Context)
4549 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4550 Attr.getAttributeSpellingListIndex()));
4551 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004552}
4553
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004554static void handleMipsInterruptAttr(Sema &S, Decl *D,
4555 const AttributeList &Attr) {
4556 // Only one optional argument permitted.
4557 if (Attr.getNumArgs() > 1) {
4558 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4559 << Attr.getName() << 1;
4560 return;
4561 }
4562
4563 StringRef Str;
4564 SourceLocation ArgLoc;
4565
4566 if (Attr.getNumArgs() == 0)
4567 Str = "";
4568 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4569 return;
4570
4571 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4572 // a) Must be a function.
4573 // b) Must have no parameters.
4574 // c) Must have the 'void' return type.
4575 // d) Cannot have the 'mips16' attribute, as that instruction set
4576 // lacks the 'eret' instruction.
4577 // e) The attribute itself must either have no argument or one of the
4578 // valid interrupt types, see [MipsInterruptDocs].
4579
4580 if (!isFunctionOrMethod(D)) {
4581 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4582 << "'interrupt'" << ExpectedFunctionOrMethod;
4583 return;
4584 }
4585
4586 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4587 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4588 << 0;
4589 return;
4590 }
4591
4592 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4593 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4594 << 1;
4595 return;
4596 }
4597
4598 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4599 Attr.getName()))
4600 return;
4601
4602 MipsInterruptAttr::InterruptType Kind;
4603 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4604 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4605 << Attr.getName() << "'" + std::string(Str) + "'";
4606 return;
4607 }
4608
4609 D->addAttr(::new (S.Context) MipsInterruptAttr(
4610 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4611}
4612
Alexey Bataevd51e9932016-01-15 04:06:31 +00004613static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
4614 const AttributeList &Attr) {
4615 // Semantic checks for a function with the 'interrupt' attribute.
4616 // a) Must be a function.
4617 // b) Must have the 'void' return type.
4618 // c) Must take 1 or 2 arguments.
4619 // d) The 1st argument must be a pointer.
4620 // e) The 2nd argument (if any) must be an unsigned integer.
4621 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
4622 CXXMethodDecl::isStaticOverloadedOperator(
4623 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
4624 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4625 << Attr.getName() << ExpectedFunctionWithProtoType;
4626 return;
4627 }
4628 // Interrupt handler must have void return type.
4629 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4630 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
4631 diag::err_anyx86_interrupt_attribute)
4632 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4633 ? 0
4634 : 1)
4635 << 0;
4636 return;
4637 }
4638 // Interrupt handler must have 1 or 2 parameters.
4639 unsigned NumParams = getFunctionOrMethodNumParams(D);
4640 if (NumParams < 1 || NumParams > 2) {
4641 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
4642 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4643 ? 0
4644 : 1)
4645 << 1;
4646 return;
4647 }
4648 // The first argument must be a pointer.
4649 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
4650 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
4651 diag::err_anyx86_interrupt_attribute)
4652 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4653 ? 0
4654 : 1)
4655 << 2;
4656 return;
4657 }
4658 // The second argument, if present, must be an unsigned integer.
4659 unsigned TypeSize =
4660 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
4661 ? 64
4662 : 32;
4663 if (NumParams == 2 &&
4664 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
4665 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
4666 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
4667 diag::err_anyx86_interrupt_attribute)
4668 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4669 ? 0
4670 : 1)
4671 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
4672 return;
4673 }
4674 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
4675 Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex()));
4676 D->addAttr(UsedAttr::CreateImplicit(S.Context));
4677}
4678
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004679static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4680 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00004681 switch (S.Context.getTargetInfo().getTriple().getArch()) {
4682 case llvm::Triple::msp430:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004683 handleMSP430InterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004684 break;
4685 case llvm::Triple::mipsel:
4686 case llvm::Triple::mips:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004687 handleMipsInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004688 break;
4689 case llvm::Triple::x86:
4690 case llvm::Triple::x86_64:
4691 handleAnyX86InterruptAttr(S, D, Attr);
4692 break;
4693 default:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004694 handleARMInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004695 break;
4696 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004697}
4698
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004699static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4700 const AttributeList &Attr) {
4701 uint32_t NumRegs;
4702 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4703 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4704 return;
4705
4706 D->addAttr(::new (S.Context)
4707 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4708 NumRegs,
4709 Attr.getAttributeSpellingListIndex()));
4710}
4711
4712static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4713 const AttributeList &Attr) {
4714 uint32_t NumRegs;
4715 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4716 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4717 return;
4718
4719 D->addAttr(::new (S.Context)
4720 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4721 NumRegs,
4722 Attr.getAttributeSpellingListIndex()));
4723}
4724
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004725static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4726 const AttributeList& Attr) {
4727 // If we try to apply it to a function pointer, don't warn, but don't
4728 // do anything, either. It doesn't matter anyway, because there's nothing
4729 // special about calling a force_align_arg_pointer function.
4730 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4731 if (VD && VD->getType()->isFunctionPointerType())
4732 return;
4733 // Also don't warn on function pointer typedefs.
4734 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4735 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4736 TD->getUnderlyingType()->isFunctionType()))
4737 return;
4738 // Attribute can only be applied to function types.
4739 if (!isa<FunctionDecl>(D)) {
4740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4741 << Attr.getName() << /* function */0;
4742 return;
4743 }
4744
Aaron Ballman36a53502014-01-16 13:03:14 +00004745 D->addAttr(::new (S.Context)
4746 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4747 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004748}
4749
4750DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4751 unsigned AttrSpellingListIndex) {
4752 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004753 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004754 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004755 }
4756
4757 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004758 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004759
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004760 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004761}
4762
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004763DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4764 unsigned AttrSpellingListIndex) {
4765 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004766 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004767 D->dropAttr<DLLImportAttr>();
4768 }
4769
4770 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004771 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004772
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004773 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004774}
4775
Hans Wennborge82f19c2014-06-24 23:57:05 +00004776static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004777 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4778 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4779 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4780 << A.getName();
4781 return;
4782 }
4783
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004784 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4785 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4786 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4787 // MinGW doesn't allow dllimport on inline functions.
4788 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4789 << A.getName();
4790 return;
4791 }
4792 }
4793
Hans Wennborg5869ec42015-09-15 21:05:30 +00004794 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4795 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4796 MD->getParent()->isLambda()) {
4797 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4798 return;
4799 }
4800 }
4801
Hans Wennborge82f19c2014-06-24 23:57:05 +00004802 unsigned Index = A.getAttributeSpellingListIndex();
4803 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4804 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4805 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004806 if (NewAttr)
4807 D->addAttr(NewAttr);
4808}
4809
David Majnemer2c4e00a2014-01-29 22:07:36 +00004810MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004811Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004812 unsigned AttrSpellingListIndex,
4813 MSInheritanceAttr::Spelling SemanticSpelling) {
4814 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4815 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004816 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004817 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4818 << 1 /*previous declaration*/;
4819 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4820 D->dropAttr<MSInheritanceAttr>();
4821 }
4822
4823 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4824 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004825 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4826 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004827 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004828 }
4829 } else {
4830 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4831 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4832 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004833 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004834 }
4835 if (RD->getDescribedClassTemplate()) {
4836 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4837 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004838 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004839 }
4840 }
4841
4842 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004843 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004844}
4845
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004846static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4847 // The capability attributes take a single string parameter for the name of
4848 // the capability they represent. The lockable attribute does not take any
4849 // parameters. However, semantically, both attributes represent the same
4850 // concept, and so they use the same semantic attribute. Eventually, the
4851 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004852 //
Alp Toker958027b2014-07-14 19:42:55 +00004853 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004854 // literal will be considered a "mutex."
4855 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004856 SourceLocation LiteralLoc;
4857 if (Attr.getKind() == AttributeList::AT_Capability &&
4858 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4859 return;
4860
Aaron Ballman6c810072014-03-05 21:47:13 +00004861 // Currently, there are only two names allowed for a capability: role and
4862 // mutex (case insensitive). Diagnose other capability names.
4863 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4864 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4865
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004866 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4867 Attr.getAttributeSpellingListIndex()));
4868}
4869
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004870static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4871 const AttributeList &Attr) {
4872 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4873 Attr.getArgAsExpr(0),
4874 Attr.getAttributeSpellingListIndex()));
4875}
4876
4877static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4878 const AttributeList &Attr) {
4879 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004880 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004881 return;
4882
4883 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4884 S.Context,
4885 Args.data(), Args.size(),
4886 Attr.getAttributeSpellingListIndex()));
4887}
4888
4889static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4890 const AttributeList &Attr) {
4891 SmallVector<Expr*, 2> Args;
4892 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4893 return;
4894
4895 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4896 S.Context,
4897 Attr.getArgAsExpr(0),
4898 Args.data(),
4899 Args.size(),
4900 Attr.getAttributeSpellingListIndex()));
4901}
4902
4903static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4904 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004905 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004906 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004907 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004908
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004909 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4910 Attr.getRange(), S.Context, Args.data(), Args.size(),
4911 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004912}
4913
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004914static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4915 const AttributeList &Attr) {
4916 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4917 return;
4918
4919 // check that all arguments are lockable objects
4920 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004921 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004922 if (Args.empty())
4923 return;
4924
4925 RequiresCapabilityAttr *RCA = ::new (S.Context)
4926 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4927 Args.size(), Attr.getAttributeSpellingListIndex());
4928
4929 D->addAttr(RCA);
4930}
4931
Aaron Ballman43f40102014-11-14 22:34:56 +00004932static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4933 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4934 if (NSD->isAnonymousNamespace()) {
4935 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4936 // Do not want to attach the attribute to the namespace because that will
4937 // cause confusing diagnostic reports for uses of declarations within the
4938 // namespace.
4939 return;
4940 }
4941 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004942
4943 if (!S.getLangOpts().CPlusPlus14)
4944 if (Attr.isCXX11Attribute() &&
4945 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004946 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004947
Aaron Ballman43f40102014-11-14 22:34:56 +00004948 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4949}
4950
Peter Collingbourne915df992015-05-15 18:33:32 +00004951static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4952 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4953 return;
4954
4955 std::vector<std::string> Sanitizers;
4956
4957 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4958 StringRef SanitizerName;
4959 SourceLocation LiteralLoc;
4960
4961 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4962 return;
4963
4964 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4965 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4966
4967 Sanitizers.push_back(SanitizerName);
4968 }
4969
4970 D->addAttr(::new (S.Context) NoSanitizeAttr(
4971 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4972 Attr.getAttributeSpellingListIndex()));
4973}
4974
4975static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4976 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004977 StringRef AttrName = Attr.getName()->getName();
4978 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004979 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004980 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004981 .Case("no_address_safety_analysis", "address")
4982 .Case("no_sanitize_address", "address")
4983 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004984 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004985 D->addAttr(::new (S.Context)
4986 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4987 Attr.getAttributeSpellingListIndex()));
4988}
4989
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004990static void handleInternalLinkageAttr(Sema &S, Decl *D,
4991 const AttributeList &Attr) {
4992 if (InternalLinkageAttr *Internal =
4993 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4994 Attr.getAttributeSpellingListIndex()))
4995 D->addAttr(Internal);
4996}
4997
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004998/// Handles semantic checking for features that are common to all attributes,
4999/// such as checking whether a parameter was properly specified, or the correct
5000/// number of arguments were passed, etc.
5001static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
5002 const AttributeList &Attr) {
5003 // Several attributes carry different semantics than the parsing requires, so
5004 // those are opted out of the common handling.
5005 //
5006 // We also bail on unknown and ignored attributes because those are handled
5007 // as part of the target-specific handling logic.
5008 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005009 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005010 return false;
5011
Aaron Ballman3aff6332013-12-02 19:30:36 +00005012 // Check whether the attribute requires specific language extensions to be
5013 // enabled.
5014 if (!Attr.diagnoseLangOpts(S))
5015 return true;
5016
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005017 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
5018 // If there are no optional arguments, then checking for the argument count
5019 // is trivial.
5020 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
5021 return true;
5022 } else {
5023 // There are optional arguments, so checking is slightly more involved.
5024 if (Attr.getMinArgs() &&
5025 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
5026 return true;
5027 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
5028 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
5029 return true;
5030 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005031
5032 // Check whether the attribute appertains to the given subject.
5033 if (!Attr.diagnoseAppertainsTo(S, D))
5034 return true;
5035
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005036 return false;
5037}
5038
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005039//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005040// Top Level Sema Entry Points
5041//===----------------------------------------------------------------------===//
5042
Richard Smithf8a75c32013-08-29 00:47:48 +00005043/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5044/// the attribute applies to decls. If the attribute is a type attribute, just
5045/// silently ignore it if a GNU attribute.
5046static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5047 const AttributeList &Attr,
5048 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005049 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005050 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005051
Richard Smithf8a75c32013-08-29 00:47:48 +00005052 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5053 // instead.
5054 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5055 return;
5056
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005057 // Unknown attributes are automatically warned on. Target-specific attributes
5058 // which do not apply to the current target architecture are treated as
5059 // though they were unknown attributes.
5060 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00005061 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005062 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
5063 ? diag::warn_unhandled_ms_attribute_ignored
5064 : diag::warn_unknown_attribute_ignored)
5065 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005066 return;
5067 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005068
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005069 if (handleCommonAttributeFeatures(S, scope, D, Attr))
5070 return;
5071
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005072 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005073 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005074 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005075 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005076 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005077 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005078 handleInterruptAttr(S, D, Attr);
5079 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005080 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005081 handleX86ForceAlignArgPointerAttr(S, D, Attr);
5082 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005083 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005084 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00005085 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005086 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005087 case AttributeList::AT_Mips16:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005088 handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D,
5089 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005090 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005091 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005092 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
5093 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005094 case AttributeList::AT_AMDGPUNumVGPR:
5095 handleAMDGPUNumVGPRAttr(S, D, Attr);
5096 break;
5097 case AttributeList::AT_AMDGPUNumSGPR:
5098 handleAMDGPUNumSGPRAttr(S, D, Attr);
5099 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00005100 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005101 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
5102 break;
5103 case AttributeList::AT_IBOutlet:
5104 handleIBOutlet(S, D, Attr);
5105 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005106 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005107 handleIBOutletCollection(S, D, Attr);
5108 break;
5109 case AttributeList::AT_Alias:
5110 handleAliasAttr(S, D, Attr);
5111 break;
5112 case AttributeList::AT_Aligned:
5113 handleAlignedAttr(S, D, Attr);
5114 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005115 case AttributeList::AT_AlignValue:
5116 handleAlignValueAttr(S, D, Attr);
5117 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005118 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00005119 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005120 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005121 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005122 handleAnalyzerNoReturnAttr(S, D, Attr);
5123 break;
5124 case AttributeList::AT_TLSModel:
5125 handleTLSModelAttr(S, D, Attr);
5126 break;
5127 case AttributeList::AT_Annotate:
5128 handleAnnotateAttr(S, D, Attr);
5129 break;
5130 case AttributeList::AT_Availability:
5131 handleAvailabilityAttr(S, D, Attr);
5132 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005133 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005134 handleDependencyAttr(S, scope, D, Attr);
5135 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005136 case AttributeList::AT_Common:
5137 handleCommonAttr(S, D, Attr);
5138 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005139 case AttributeList::AT_CUDAConstant:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005140 handleSimpleAttributeWithExclusions<CUDAConstantAttr, CUDASharedAttr>(S, D,
5141 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005142 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005143 case AttributeList::AT_PassObjectSize:
5144 handlePassObjectSizeAttr(S, D, Attr);
5145 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005146 case AttributeList::AT_Constructor:
5147 handleConstructorAttr(S, D, Attr);
5148 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005149 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005150 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005152 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005153 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005154 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005155 case AttributeList::AT_Destructor:
5156 handleDestructorAttr(S, D, Attr);
5157 break;
5158 case AttributeList::AT_EnableIf:
5159 handleEnableIfAttr(S, D, Attr);
5160 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005161 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005162 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005163 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005164 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005165 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005166 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005167 case AttributeList::AT_OptimizeNone:
5168 handleOptimizeNoneAttr(S, D, Attr);
5169 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005170 case AttributeList::AT_FlagEnum:
5171 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5172 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005173 case AttributeList::AT_Flatten:
5174 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5175 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005176 case AttributeList::AT_Format:
5177 handleFormatAttr(S, D, Attr);
5178 break;
5179 case AttributeList::AT_FormatArg:
5180 handleFormatArgAttr(S, D, Attr);
5181 break;
5182 case AttributeList::AT_CUDAGlobal:
5183 handleGlobalAttr(S, D, Attr);
5184 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005185 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005186 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
5187 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005188 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005189 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005190 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
5191 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005192 break;
5193 case AttributeList::AT_GNUInline:
5194 handleGNUInlineAttr(S, D, Attr);
5195 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005196 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005197 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005198 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005199 case AttributeList::AT_Restrict:
5200 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005201 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005202 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005203 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5204 break;
5205 case AttributeList::AT_Mode:
5206 handleModeAttr(S, D, Attr);
5207 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005208 case AttributeList::AT_NoAlias:
5209 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5210 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005211 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005212 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5213 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005214 case AttributeList::AT_NoSplitStack:
5215 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5216 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005217 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005218 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5219 handleNonNullAttrParameter(S, PVD, Attr);
5220 else
5221 handleNonNullAttr(S, D, Attr);
5222 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005223 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005224 handleReturnsNonNullAttr(S, D, Attr);
5225 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005226 case AttributeList::AT_AssumeAligned:
5227 handleAssumeAlignedAttr(S, D, Attr);
5228 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005229 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005230 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5231 break;
5232 case AttributeList::AT_Ownership:
5233 handleOwnershipAttr(S, D, Attr);
5234 break;
5235 case AttributeList::AT_Cold:
5236 handleColdAttr(S, D, Attr);
5237 break;
5238 case AttributeList::AT_Hot:
5239 handleHotAttr(S, D, Attr);
5240 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005241 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005242 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005243 break;
5244 case AttributeList::AT_NoReturn:
5245 handleNoReturnAttr(S, D, Attr);
5246 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005247 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005248 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5249 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005250 case AttributeList::AT_CUDAShared:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005251 handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D,
5252 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005253 break;
5254 case AttributeList::AT_VecReturn:
5255 handleVecReturnAttr(S, D, Attr);
5256 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005257 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005258 handleObjCOwnershipAttr(S, D, Attr);
5259 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005260 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005261 handleObjCPreciseLifetimeAttr(S, D, Attr);
5262 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005263 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005264 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5265 break;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005266 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005267 handleObjCRequiresSuperAttr(S, D, Attr);
5268 break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005269 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005270 handleObjCBridgeAttr(S, scope, D, Attr);
5271 break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005272 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005273 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5274 break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005275 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005276 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5277 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005278 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005279 handleObjCDesignatedInitializer(S, D, Attr);
5280 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005281 case AttributeList::AT_ObjCRuntimeName:
5282 handleObjCRuntimeName(S, D, Attr);
5283 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005284 case AttributeList::AT_ObjCBoxable:
5285 handleObjCBoxable(S, D, Attr);
5286 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005287 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005288 handleCFAuditedTransferAttr(S, D, Attr);
5289 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005290 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005291 handleCFUnknownTransferAttr(S, D, Attr);
5292 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005293 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005294 case AttributeList::AT_NSConsumed:
5295 handleNSConsumedAttr(S, D, Attr);
5296 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005297 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005298 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5299 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005300 case AttributeList::AT_NSReturnsAutoreleased:
5301 case AttributeList::AT_NSReturnsNotRetained:
5302 case AttributeList::AT_CFReturnsNotRetained:
5303 case AttributeList::AT_NSReturnsRetained:
5304 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005305 handleNSReturnsRetainedAttr(S, D, Attr);
5306 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005307 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005308 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5309 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005310 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005311 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5312 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005313 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005314 handleVecTypeHint(S, D, Attr);
5315 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005316 case AttributeList::AT_InitPriority:
5317 handleInitPriorityAttr(S, D, Attr);
5318 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005319 case AttributeList::AT_Packed:
5320 handlePackedAttr(S, D, Attr);
5321 break;
5322 case AttributeList::AT_Section:
5323 handleSectionAttr(S, D, Attr);
5324 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005325 case AttributeList::AT_Target:
5326 handleTargetAttr(S, D, Attr);
5327 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005328 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005329 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005330 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005331 case AttributeList::AT_ArcWeakrefUnavailable:
5332 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5333 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005334 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005335 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5336 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005337 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005338 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005339 break;
5340 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005341 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5342 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005343 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005344 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5345 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005346 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005347 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5348 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005349 case AttributeList::AT_NotTailCalled:
5350 handleNotTailCalledAttr(S, D, Attr);
5351 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005352 case AttributeList::AT_DisableTailCalls:
5353 handleDisableTailCallsAttr(S, D, Attr);
5354 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005355 case AttributeList::AT_Used:
5356 handleUsedAttr(S, D, Attr);
5357 break;
John McCalld041a9b2013-02-20 01:54:26 +00005358 case AttributeList::AT_Visibility:
5359 handleVisibilityAttr(S, D, Attr, false);
5360 break;
5361 case AttributeList::AT_TypeVisibility:
5362 handleVisibilityAttr(S, D, Attr, true);
5363 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005364 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005365 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5366 break;
5367 case AttributeList::AT_WarnUnusedResult:
5368 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005369 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005370 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005371 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5372 break;
5373 case AttributeList::AT_WeakRef:
5374 handleWeakRefAttr(S, D, Attr);
5375 break;
5376 case AttributeList::AT_WeakImport:
5377 handleWeakImportAttr(S, D, Attr);
5378 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005379 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005380 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005381 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005382 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005383 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5384 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005385 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005386 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005387 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005388 case AttributeList::AT_ObjCNSObject:
5389 handleObjCNSObject(S, D, Attr);
5390 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005391 case AttributeList::AT_ObjCIndependentClass:
5392 handleObjCIndependentClass(S, D, Attr);
5393 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005394 case AttributeList::AT_Blocks:
5395 handleBlocksAttr(S, D, Attr);
5396 break;
5397 case AttributeList::AT_Sentinel:
5398 handleSentinelAttr(S, D, Attr);
5399 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005400 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005401 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5402 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005403 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005404 handleSimpleAttribute<PureAttr>(S, D, Attr);
5405 break;
5406 case AttributeList::AT_Cleanup:
5407 handleCleanupAttr(S, D, Attr);
5408 break;
5409 case AttributeList::AT_NoDebug:
5410 handleNoDebugAttr(S, D, Attr);
5411 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005412 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005413 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5414 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005415 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005416 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5417 break;
5418 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5419 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5420 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005421 case AttributeList::AT_StdCall:
5422 case AttributeList::AT_CDecl:
5423 case AttributeList::AT_FastCall:
5424 case AttributeList::AT_ThisCall:
5425 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005426 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005427 case AttributeList::AT_MSABI:
5428 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005429 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005430 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005431 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005432 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005433 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005434 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5435 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005436 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005437 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5438 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005439 case AttributeList::AT_InternalLinkage:
5440 handleInternalLinkageAttr(S, D, Attr);
5441 break;
John McCall8d32c052012-05-22 21:28:12 +00005442
5443 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005444 case AttributeList::AT_MSNoVTable:
5445 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005446 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005447 case AttributeList::AT_MSStruct:
5448 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005449 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005450 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005451 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005452 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005453 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005454 handleMSInheritanceAttr(S, D, Attr);
5455 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005456 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005457 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5458 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005459 case AttributeList::AT_Thread:
5460 handleDeclspecThreadAttr(S, D, Attr);
5461 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005462
5463 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005464 case AttributeList::AT_AssertExclusiveLock:
5465 handleAssertExclusiveLockAttr(S, D, Attr);
5466 break;
5467 case AttributeList::AT_AssertSharedLock:
5468 handleAssertSharedLockAttr(S, D, Attr);
5469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005470 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005471 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5472 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005473 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005474 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005475 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005476 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005477 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5478 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005479 case AttributeList::AT_NoSanitize:
5480 handleNoSanitizeAttr(S, D, Attr);
5481 break;
5482 case AttributeList::AT_NoSanitizeSpecific:
5483 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005484 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005485 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005486 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005487 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005488 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005489 handleGuardedByAttr(S, D, Attr);
5490 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005491 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005492 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005493 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005494 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005495 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005496 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005497 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005498 handleLockReturnedAttr(S, D, Attr);
5499 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005500 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005501 handleLocksExcludedAttr(S, D, Attr);
5502 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005503 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005504 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005505 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005506 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005507 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005508 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005509 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005510 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005511 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005512
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005513 // Capability analysis attributes.
5514 case AttributeList::AT_Capability:
5515 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005516 handleCapabilityAttr(S, D, Attr);
5517 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005518 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005519 handleRequiresCapabilityAttr(S, D, Attr);
5520 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005521
5522 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005523 handleAssertCapabilityAttr(S, D, Attr);
5524 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005525 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005526 handleAcquireCapabilityAttr(S, D, Attr);
5527 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005528 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005529 handleReleaseCapabilityAttr(S, D, Attr);
5530 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005531 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005532 handleTryAcquireCapabilityAttr(S, D, Attr);
5533 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005534
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005535 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005536 case AttributeList::AT_Consumable:
5537 handleConsumableAttr(S, D, Attr);
5538 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005539 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005540 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5541 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005542 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005543 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5544 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005545 case AttributeList::AT_CallableWhen:
5546 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005547 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005548 case AttributeList::AT_ParamTypestate:
5549 handleParamTypestateAttr(S, D, Attr);
5550 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005551 case AttributeList::AT_ReturnTypestate:
5552 handleReturnTypestateAttr(S, D, Attr);
5553 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005554 case AttributeList::AT_SetTypestate:
5555 handleSetTypestateAttr(S, D, Attr);
5556 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005557 case AttributeList::AT_TestTypestate:
5558 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005559 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005560
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005561 // Type safety attributes.
5562 case AttributeList::AT_ArgumentWithTypeTag:
5563 handleArgumentWithTypeTagAttr(S, D, Attr);
5564 break;
5565 case AttributeList::AT_TypeTagForDatatype:
5566 handleTypeTagForDatatypeAttr(S, D, Attr);
5567 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005568 }
5569}
5570
5571/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5572/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005573void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005574 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005575 bool IncludeCXX11Attributes) {
5576 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005577 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005578
Joey Gouly2cd9db12013-12-13 16:15:28 +00005579 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005580 // GCC accepts
5581 // static int a9 __attribute__((weakref));
5582 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005583 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005584 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5585 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005586 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005587 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005588 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005589
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005590 // FIXME: We should be able to handle this in TableGen as well. It would be
5591 // good to have a way to specify "these attributes must appear as a group",
5592 // for these. Additionally, it would be good to have a way to specify "these
5593 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005594 if (!D->hasAttr<OpenCLKernelAttr>()) {
5595 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005596 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005597 // FIXME: This emits a different error message than
5598 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005599 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005600 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005601 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005602 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005603 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005604 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005605 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005606 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005607 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5608 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5609 << A << ExpectedKernelFunction;
5610 D->setInvalidDecl();
5611 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5612 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5613 << A << ExpectedKernelFunction;
5614 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005615 }
5616 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005617}
5618
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005619// Annotation attributes are the only attributes allowed after an access
5620// specifier.
5621bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5622 const AttributeList *AttrList) {
5623 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005624 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005625 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005626 } else {
5627 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5628 return true;
5629 }
5630 }
5631
5632 return false;
5633}
5634
John McCall42856de2011-10-01 05:17:03 +00005635/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5636/// contains any decl attributes that we should warn about.
5637static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5638 for ( ; A; A = A->getNext()) {
5639 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005640 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005641 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5642
5643 if (A->getKind() == AttributeList::UnknownAttribute) {
5644 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5645 << A->getName() << A->getRange();
5646 } else {
5647 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5648 << A->getName() << A->getRange();
5649 }
5650 }
5651}
5652
5653/// checkUnusedDeclAttributes - Given a declarator which is not being
5654/// used to build a declaration, complain about any decl attributes
5655/// which might be lying around on it.
5656void Sema::checkUnusedDeclAttributes(Declarator &D) {
5657 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5658 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5659 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5660 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5661}
5662
Ryan Flynn7d470f32009-07-30 03:15:39 +00005663/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005664/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005665NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5666 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005667 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005668 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005669 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00005670 FunctionDecl *NewFD;
5671 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00005672 // FIXME: Mangling?
5673 // FIXME: Is the qualifier info correct?
5674 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00005675 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5676 Loc, Loc, DeclarationName(II),
5677 FD->getType(), FD->getTypeSourceInfo(),
5678 SC_None, false/*isInlineSpecified*/,
5679 FD->hasPrototype(),
5680 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005681 NewD = NewFD;
5682
5683 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005684 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005685
5686 // Fake up parameter variables; they are declared as if this were
5687 // a typedef.
5688 QualType FDTy = FD->getType();
5689 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5690 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005691 for (const auto &AI : FT->param_types()) {
5692 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005693 Param->setScopeInfo(0, Params.size());
5694 Params.push_back(Param);
5695 }
David Blaikie9c70e042011-09-21 18:16:56 +00005696 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005697 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005698 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5699 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005700 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005701 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005702 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005703 if (VD->getQualifier()) {
5704 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005705 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005706 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005707 }
5708 return NewD;
5709}
5710
James Dennett634962f2012-06-14 21:40:34 +00005711/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005712/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005713void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005714 if (W.getUsed()) return; // only do this once
5715 W.setUsed(true);
5716 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5717 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005718 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005719 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5720 W.getLocation()));
5721 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005722 WeakTopLevelDecl.push_back(NewD);
5723 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5724 // to insert Decl at TU scope, sorry.
5725 DeclContext *SavedContext = CurContext;
5726 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005727 NewD->setDeclContext(CurContext);
5728 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005729 PushOnScopeChains(NewD, S);
5730 CurContext = SavedContext;
5731 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005732 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005733 }
5734}
5735
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005736void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5737 // It's valid to "forward-declare" #pragma weak, in which case we
5738 // have to do this.
5739 LoadExternalWeakUndeclaredIdentifiers();
5740 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005741 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005742 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5743 if (VD->isExternC())
5744 ND = VD;
5745 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5746 if (FD->isExternC())
5747 ND = FD;
5748 if (ND) {
5749 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005750 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005751 if (I != WeakUndeclaredIdentifiers.end()) {
5752 WeakInfo W = I->second;
5753 DeclApplyPragmaWeak(S, ND, W);
5754 WeakUndeclaredIdentifiers[Id] = W;
5755 }
5756 }
5757 }
5758 }
5759}
5760
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005761/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5762/// it, apply them to D. This is a bit tricky because PD can have attributes
5763/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005764void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005765 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005766 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005767 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005768
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005769 // Walk the declarator structure, applying decl attributes that were in a type
5770 // position to the decl itself. This handles cases like:
5771 // int *__attr__(x)** D;
5772 // when X is a decl attribute.
5773 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5774 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005775 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005776
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005777 // Finally, apply any attributes on the decl itself.
5778 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005779 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005780}
John McCall28a6aea2009-11-04 02:18:39 +00005781
John McCall31168b02011-06-15 23:02:42 +00005782/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005783/// If so, it'll still be annotated with an attribute that makes it
5784/// illegal to actually use.
5785static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5786 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005787 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005788 // Private ivars are always okay. Unfortunately, people don't
5789 // always properly make their ivars private, even in system headers.
5790 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005791 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5792 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005793 return false;
5794
John McCallc6af8c62015-10-28 05:03:19 +00005795 // Silently accept unsupported uses of __weak in both user and system
5796 // declarations when it's been disabled, for ease of integration with
5797 // -fno-objc-arc files. We do have to take some care against attempts
5798 // to define such things; for now, we've only done that for ivars
5799 // and properties.
5800 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5801 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5802 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5803 reason = UnavailableAttr::IR_ForbiddenWeak;
5804 return true;
5805 }
John McCallb61e14e2015-10-27 04:54:50 +00005806 }
5807
John McCallc6af8c62015-10-28 05:03:19 +00005808 // Allow all sorts of things in system headers.
5809 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5810 // Currently, all the failures dealt with this way are due to ARC
5811 // restrictions.
5812 reason = UnavailableAttr::IR_ARCForbiddenType;
5813 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005814 }
5815
5816 return false;
John McCall31168b02011-06-15 23:02:42 +00005817}
5818
5819/// Handle a delayed forbidden-type diagnostic.
5820static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5821 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005822 auto reason = UnavailableAttr::IR_None;
5823 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5824 assert(reason && "didn't set reason?");
5825 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5826 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005827 return;
5828 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005829 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005830 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005831 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005832 // kind of forbidden type messages on unavailable functions.
5833 if (FD->hasAttr<UnavailableAttr>() &&
5834 diag.getForbiddenTypeDiagnostic() ==
5835 diag::err_arc_array_param_no_ownership) {
5836 diag.Triggered = true;
5837 return;
5838 }
5839 }
John McCall31168b02011-06-15 23:02:42 +00005840
5841 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5842 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5843 diag.Triggered = true;
5844}
5845
Aaron Ballmanfb237522014-10-15 15:37:51 +00005846static bool isDeclDeprecated(Decl *D) {
5847 do {
5848 if (D->isDeprecated())
5849 return true;
5850 // A category implicitly has the availability of the interface.
5851 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005852 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5853 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005854 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5855 return false;
5856}
5857
5858static bool isDeclUnavailable(Decl *D) {
5859 do {
5860 if (D->isUnavailable())
5861 return true;
5862 // A category implicitly has the availability of the interface.
5863 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005864 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5865 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005866 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5867 return false;
5868}
5869
Nico Weber0055a192015-03-19 19:18:22 +00005870static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005871 Decl *Ctx, const NamedDecl *D,
5872 StringRef Message, SourceLocation Loc,
5873 const ObjCInterfaceDecl *UnknownObjCClass,
5874 const ObjCPropertyDecl *ObjCProperty,
5875 bool ObjCPropertyAccess) {
5876 // Diagnostics for deprecated or unavailable.
5877 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005878 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005879
5880 // Matches 'diag::note_property_attribute' options.
5881 unsigned property_note_select;
5882
5883 // Matches diag::note_availability_specified_here.
5884 unsigned available_here_select_kind;
5885
5886 // Don't warn if our current context is deprecated or unavailable.
5887 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005888 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005889 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005890 return;
5891 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5892 : diag::warn_property_method_deprecated;
5893 diag_message = diag::warn_deprecated_message;
5894 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5895 property_note_select = /* deprecated */ 0;
5896 available_here_select_kind = /* deprecated */ 2;
5897 break;
5898
Nico Weber0055a192015-03-19 19:18:22 +00005899 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005900 if (isDeclUnavailable(Ctx))
5901 return;
5902 diag = !ObjCPropertyAccess ? diag::err_unavailable
5903 : diag::err_property_method_unavailable;
5904 diag_message = diag::err_unavailable_message;
5905 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5906 property_note_select = /* unavailable */ 1;
5907 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005908
John McCallc6af8c62015-10-28 05:03:19 +00005909 if (auto attr = D->getAttr<UnavailableAttr>()) {
5910 if (attr->isImplicit() && attr->getImplicitReason()) {
5911 // Most of these failures are due to extra restrictions in ARC;
5912 // reflect that in the primary diagnostic when applicable.
5913 auto flagARCError = [&] {
5914 if (S.getLangOpts().ObjCAutoRefCount &&
5915 S.getSourceManager().isInSystemHeader(D->getLocation()))
5916 diag = diag::err_unavailable_in_arc;
5917 };
5918
5919 switch (attr->getImplicitReason()) {
5920 case UnavailableAttr::IR_None: break;
5921
5922 case UnavailableAttr::IR_ARCForbiddenType:
5923 flagARCError();
5924 diag_available_here = diag::note_arc_forbidden_type;
5925 break;
5926
5927 case UnavailableAttr::IR_ForbiddenWeak:
5928 if (S.getLangOpts().ObjCWeakRuntime)
5929 diag_available_here = diag::note_arc_weak_disabled;
5930 else
5931 diag_available_here = diag::note_arc_weak_no_runtime;
5932 break;
5933
5934 case UnavailableAttr::IR_ARCForbiddenConversion:
5935 flagARCError();
5936 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5937 break;
5938
5939 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5940 flagARCError();
5941 diag_available_here = diag::note_arc_init_returns_unrelated;
5942 break;
5943
5944 case UnavailableAttr::IR_ARCFieldWithOwnership:
5945 flagARCError();
5946 diag_available_here = diag::note_arc_field_with_ownership;
5947 break;
5948 }
5949 }
John McCallb61e14e2015-10-27 04:54:50 +00005950 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00005951 break;
5952
Nico Weber0055a192015-03-19 19:18:22 +00005953 case Sema::AD_Partial:
5954 diag = diag::warn_partial_availability;
5955 diag_message = diag::warn_partial_message;
5956 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5957 property_note_select = /* partial */ 2;
5958 available_here_select_kind = /* partial */ 3;
5959 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005960 }
5961
Aaron Ballmanfb237522014-10-15 15:37:51 +00005962 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005963 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005964 if (ObjCProperty)
5965 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5966 << ObjCProperty->getDeclName() << property_note_select;
5967 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005968 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005969 if (ObjCProperty)
5970 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5971 << ObjCProperty->getDeclName() << property_note_select;
5972 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005973 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005974 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5975 }
5976
John McCallb61e14e2015-10-27 04:54:50 +00005977 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005978 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005979 if (K == Sema::AD_Partial)
5980 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005981}
5982
5983static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5984 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005985 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5986 DD.Kind == DelayedDiagnostic::Unavailable);
5987 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5988 ? Sema::AD_Deprecation
5989 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005990 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005991 DoEmitAvailabilityWarning(
5992 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5993 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005994}
5995
John McCall2ec85372012-05-07 06:16:41 +00005996void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5997 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005998 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005999 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00006000
John McCall2ec85372012-05-07 06:16:41 +00006001 // When delaying diagnostics to run in the context of a parsed
6002 // declaration, we only want to actually emit anything if parsing
6003 // succeeds.
6004 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00006005
John McCall2ec85372012-05-07 06:16:41 +00006006 // We emit all the active diagnostics in this pool or any of its
6007 // parents. In general, we'll get one pool for the decl spec
6008 // and a child pool for each declarator; in a decl group like:
6009 // deprecated_typedef foo, *bar, baz();
6010 // only the declarator pops will be passed decls. This is correct;
6011 // we really do need to consider delayed diagnostics from the decl spec
6012 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00006013 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00006014 do {
John McCall6347b682012-05-07 06:16:58 +00006015 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00006016 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
6017 // This const_cast is a bit lame. Really, Triggered should be mutable.
6018 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00006019 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00006020 continue;
6021
John McCallc1465822011-02-14 07:13:47 +00006022 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00006023 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00006024 case DelayedDiagnostic::Unavailable:
6025 // Don't bother giving deprecation/unavailable diagnostics if
6026 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00006027 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00006028 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00006029 break;
6030
6031 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00006032 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00006033 break;
John McCall31168b02011-06-15 23:02:42 +00006034
6035 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00006036 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00006037 break;
John McCall86121512010-01-27 03:50:35 +00006038 }
6039 }
John McCall2ec85372012-05-07 06:16:41 +00006040 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00006041}
6042
John McCall6347b682012-05-07 06:16:58 +00006043/// Given a set of delayed diagnostics, re-emit them as if they had
6044/// been delayed in the current context instead of in the given pool.
6045/// Essentially, this just moves them to the current pool.
6046void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
6047 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
6048 assert(curPool && "re-emitting in undelayed context not supported");
6049 curPool->steal(pool);
6050}
6051
Ted Kremenekb79ee572013-12-18 23:30:06 +00006052void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
6053 NamedDecl *D, StringRef Message,
6054 SourceLocation Loc,
6055 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00006056 const ObjCPropertyDecl *ObjCProperty,
6057 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00006058 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00006059 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00006060 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
6061 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
6062 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00006063 return;
6064 }
6065
Ted Kremenekb79ee572013-12-18 23:30:06 +00006066 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00006067 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
6068 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00006069}